Class: Pf2::CLI
- Inherits:
-
Object
- Object
- Pf2::CLI
- Defined in:
- lib/pf2/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
- #run(argv) ⇒ Object
- #subcommand_annotate(argv) ⇒ Object
- #subcommand_report(argv) ⇒ Object
- #subcommand_serve(argv) ⇒ Object
Class Method Details
.run ⇒ Object
10 11 12 |
# File 'lib/pf2/cli.rb', line 10 def self.run(...) new.run(...) end |
Instance Method Details
#run(argv) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pf2/cli.rb', line 14 def run(argv) argv = argv.dup program_name = File.basename($PROGRAM_NAME) subcommand = argv.shift case subcommand when 'report' subcommand_report(argv) when 'annotate' subcommand_annotate(argv) when 'serve' subcommand_serve(argv) when 'version' puts VERSION return 0 when '--help' STDERR.puts " Usage: \#{program_name} COMMAND [options]\n\n Commands:\n report Generate a report from a profile\n serve Start an HTTP server alongside a target process\n version Show version information\n __EOS__\n\n return 1\n else\n STDERR.puts \"\#{program_name}: Unknown subcommand '\#{subcommand}'.\"\n STDERR.puts \"See '\#{program_name} --help'\"\n return 1\n end\nend\n" |
#subcommand_annotate(argv) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/pf2/cli.rb', line 82 def subcommand_annotate(argv) = {} option_parser = OptionParser.new do |opts| opts. = "Usage: pf2 report [options] COMMAND" opts.on('-h', '--help', 'Prints this help') do puts opts return 0 end opts.on('-d', '--source-directory DIR', 'Path to the source directory') do |dir| [:source_directory] = dir end end option_parser.parse!(argv) profile = Marshal.load(File.binread(argv[0])) Pf2::Reporter::Annotate.new(profile, [:source_directory] || '.').annotate return 0 end |
#subcommand_report(argv) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/pf2/cli.rb', line 47 def subcommand_report(argv) = {} option_parser = OptionParser.new do |opts| opts. = "Usage: pf2 report [options] COMMAND" opts.on('-h', '--help', 'Prints this help') do puts opts return 0 end opts.on('-o', '--output FILE', 'Output file') do |path| [:output_file] = path end opts.on('--experimental-serializer', 'Enable the experimental serializer mode') do [:experimental_serializer] = true end end option_parser.parse!(argv) if [:experimental_serializer] profile = Marshal.load(File.read(argv[0])) report = Pf2::Reporter::FirefoxProfilerSer2.new(profile).emit report = JSON.generate(report) else profile = JSON.parse(File.read(argv[0]), symbolize_names: true, max_nesting: false) report = JSON.generate(Pf2::Reporter::FirefoxProfiler.new(profile).emit) end if [:output_file] File.write([:output_file], report) else puts report end return 0 end |
#subcommand_serve(argv) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/pf2/cli.rb', line 102 def subcommand_serve(argv) = {} option_parser = OptionParser.new do |opts| opts. = "Usage: pf2 serve [options] COMMAND" opts.on('-h', '--help', 'Prints this help') do puts opts return 0 end opts.on('-b', '--bind ADDRESS', 'Address to bind') do |host| [:serve_host] = host end opts.on('-p', '--port PORT', '') do |port| [:serve_port] = port end end option_parser.parse!(argv) if argv.size == 0 # No subcommand was specified STDERR.puts option_parser.help return 1 end # Inject the profiler (pf2/serve) into the target process via RUBYOPT (-r). # This will have no effect if the target process is not Ruby. env = { 'RUBYOPT' => '-rpf2/serve' } env['PF2_SERVE_HOST'] = [:serve_host] if [:serve_host] env['PF2_SERVE_PORT'] = [:serve_port] if [:serve_port] exec(env, *argv) # never returns if succesful return 1 end |