-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
148 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module AppProfiler | ||
module Exec # :nodoc: | ||
protected | ||
|
||
def valid_commands | ||
raise NotImplementedError | ||
end | ||
|
||
def ensure_command_valid(command) | ||
unless valid_command?(command) | ||
raise YarnError, "Illegal command: #{command.join(" ")}." | ||
end | ||
end | ||
|
||
def valid_command?(command) | ||
valid_commands.any? do |valid_command| | ||
next unless valid_command.size == command.size | ||
|
||
valid_command.zip(command).all? do |valid_part, part| | ||
part.match?(valid_part) | ||
end | ||
end | ||
end | ||
|
||
def exec(*command, silent: false, environment: {}) | ||
ensure_command_valid(command) | ||
|
||
if silent | ||
system(environment, *command, out: File::NULL).tap { |return_code| yield unless return_code } | ||
else | ||
system(environment, *command).tap { |return_code| yield unless return_code } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
require "app_profiler/exec" | ||
|
||
module AppProfiler | ||
module Viewer | ||
class FirefoxViewer < BaseViewer | ||
include Exec | ||
|
||
class ProfileViewerError < StandardError; end | ||
|
||
VALID_COMMANDS = [ | ||
["which", "profile-viewer"], | ||
["gem", "install", "profile-viewer"], | ||
["profile-viewer", /.*\.json/], | ||
] | ||
private_constant(:VALID_COMMANDS) | ||
|
||
class << self | ||
def view(profile, params = {}) | ||
new(profile).view(**params) | ||
end | ||
end | ||
|
||
def valid_commands | ||
VALID_COMMANDS | ||
end | ||
|
||
def initialize(profile) | ||
super() | ||
@profile = profile | ||
end | ||
|
||
def view(_params = {}) | ||
profile_viewer(@profile.file.to_s) | ||
end | ||
|
||
private | ||
|
||
def setup_profile_viewer | ||
exec("which", "profile-viewer", silent: true) do | ||
gem_install("profile_viewer") | ||
end | ||
@profile_viewer_initialized = true | ||
end | ||
|
||
def profile_viewer_setup | ||
@profile_viewer_initialized || false | ||
end | ||
|
||
def gem_install(gem) | ||
exec("gem", "install", gem) do | ||
raise ProfileViewerError, "Failed to run gem install #{gem}." | ||
end | ||
end | ||
|
||
def profile_viewer(path) | ||
setup_profile_viewer unless profile_viewer_setup | ||
|
||
pid = fork do | ||
Bundler.with_clean_env do | ||
exec("profile-viewer", path) do | ||
raise ProfileViewerError, "Failed to run profile-viewer #{path}." | ||
end | ||
end | ||
end | ||
|
||
sleep(1) | ||
|
||
Process.kill(pid) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters