1#!/usr/bin/ruby
2
3require 'fileutils'
4require 'tmpdir'
5
6if ARGV.size != 0
7  puts "usage: #{File.basename $0}"
8  exit 1
9end
10
11WEB_INSPECTOR_PATH = File.expand_path File.join(File.dirname(__FILE__), "..")
12JAVASCRIPTCORE_PATH = File.expand_path File.join(File.dirname(__FILE__), "..", "..", "JavaScriptCore")
13
14$code_generator_path = File.join JAVASCRIPTCORE_PATH, "inspector", "scripts", "CodeGeneratorInspector.py"
15$versions_directory_path = File.join WEB_INSPECTOR_PATH, "Versions"
16$web_inspector_protocol_legacy_path = File.join WEB_INSPECTOR_PATH, "UserInterface", "Protocol", "Legacy"
17
18class Task
19  def initialize(input_json_path, dependency_json_path, type, output_directory_path)
20    @type = type
21    @input_json_path = input_json_path
22    @dependency_json_path = dependency_json_path
23    @output_directory_path = output_directory_path
24  end
25
26  def run
27    output_filename_prefix = {"JavaScript" => "JS", "Web" => "Web"}[@type]
28    output_filename = "Inspector#{output_filename_prefix}BackendCommands.js"
29    display_input = File.basename @input_json_path
30    display_output = File.join @output_directory_path.gsub(/^.*?\/UserInterface/, "UserInterface"), output_filename
31    puts "#{display_input} -> #{display_output}"
32
33    Dir.mktmpdir do |tmpdir|
34      dependency = @dependency_json_path ? "'#{@dependency_json_path}'" : ""
35      cmd = "#{$code_generator_path} '#{@input_json_path}' #{dependency} --no_verification --output_h_dir '#{tmpdir}' --output_cpp_dir '#{tmpdir}' --output_js_dir '#{tmpdir}' --write_always --output_type '#{@type}'"
36      %x{ #{cmd} }
37      if $?.exitstatus != 0
38        puts "ERROR: Error Code (#{$?.exitstatus}) Evaluating: #{cmd}"
39        exit 1
40      end
41
42      generated_path = File.join tmpdir, output_filename
43      if !File.exists?(generated_path)
44        puts "ERROR: Generated file does not exist at expected path."
45        exit 1
46      end
47
48      FileUtils.mkdir_p @output_directory_path
49      FileUtils.cp generated_path, @output_directory_path
50    end
51  end
52end
53
54def all_tasks
55  tasks = []
56
57  had_error = false
58  Dir.glob(File.join($versions_directory_path, "*.json")).each do |version_path|
59    match = File.basename(version_path).match(/^Inspector(.*?)\-([^-]+?)\.json$/)
60    if match
61      output_path = File.join $web_inspector_protocol_legacy_path, match[2]
62      tasks << Task.new(version_path, nil, "Web", output_path)
63    else
64      puts "ERROR: Version file (#{version_path}) did not match the template Inspector<ANYTHING>-<VERSION>.js"
65      had_error = true
66    end
67  end
68  exit 1 if had_error
69
70  tasks
71end
72
73def main
74  all_tasks.each { |task| task.run }
75end
76
77main
78