Web Inspector: vended backend commands file should be generated as part of the build
[WebKit-https.git] / Source / WebInspectorUI / Scripts / update-InspectorBackendCommands.rb
1 #!/usr/bin/ruby
2
3 require 'fileutils'
4 require 'tempfile'
5 require 'tmpdir'
6
7 if ARGV.size != 0
8   puts "usage: #{File.basename $0}"
9   exit 1
10 end
11
12 WEB_INSPECTOR_PATH = File.expand_path File.join(File.dirname(__FILE__), "..")
13 JAVASCRIPTCORE_PATH = File.expand_path File.join(File.dirname(__FILE__), "..", "..", "JavaScriptCore")
14 WEBCORE_PATH = File.expand_path File.join(File.dirname(__FILE__), "..", "..", "WebCore")
15
16 JAVASCRIPT_INSPECTOR_PROTOCOLS_PATH = File.join JAVASCRIPTCORE_PATH, "inspector", "protocol"
17 WEB_INSPECTOR_PROTOCOLS_PATH = File.join WEBCORE_PATH, "inspector", "protocol"
18
19 $code_generator_path = File.join JAVASCRIPTCORE_PATH, "inspector", "scripts", "CodeGeneratorInspector.py"
20 $inspector_json_generator_path = File.join JAVASCRIPTCORE_PATH, "inspector", "scripts", "generate-combined-inspector-json.py"
21 $inspector_js_json_path = Tempfile.new("generated-InspectorJS.json").path
22 $inspector_web_json_path = Tempfile.new("generated-InspectorWeb.json").path
23 $versions_directory_path = File.join WEB_INSPECTOR_PATH, "Versions"
24 $web_inspector_user_interface_path = File.join WEB_INSPECTOR_PATH, "UserInterface"
25 $web_inspector_protocol_path = File.join WEB_INSPECTOR_PATH, "UserInterface", "Protocol"
26
27 class Task
28   def initialize(input_json_path, dependency_json_path, type, output_directory_path, verification)
29     @type = type
30     @input_json_path = input_json_path
31     @dependency_json_path = dependency_json_path
32     @output_directory_path = output_directory_path
33     @verification = verification
34   end
35
36   def run
37     output_filename_prefix = {"JavaScript" => "JS", "Web" => "Web"}[@type]
38     output_filename = "Inspector#{output_filename_prefix}BackendCommands.js"
39     display_input = File.basename @input_json_path
40     display_output = File.join @output_directory_path.gsub(/^.*?\/UserInterface/, "UserInterface"), output_filename
41     puts "#{display_input} -> #{display_output}"
42
43     Dir.mktmpdir do |tmpdir|
44       dependency = @dependency_json_path ? "'#{@dependency_json_path}'" : ""
45       cmd = "#{$code_generator_path} '#{@input_json_path}' #{dependency} --output_h_dir '#{tmpdir}' --output_cpp_dir '#{tmpdir}' --output_js_dir '#{tmpdir}' --write_always --output_type '#{@type}'"
46       cmd += " --no_verification" if !@verification
47       %x{ #{cmd} }
48       if $?.exitstatus != 0
49         puts "ERROR: Error Code (#{$?.exitstatus}) Evaluating: #{cmd}"
50         exit 1
51       end
52
53       generated_path = File.join tmpdir, output_filename
54       if !File.exists?(generated_path)
55         puts "ERROR: Generated file does not exist at expected path."
56         exit 1
57       end
58
59       FileUtils.mkdir_p @output_directory_path
60       FileUtils.cp generated_path, @output_directory_path
61     end
62   end
63 end
64
65 def generate_combined_inspector_json
66   # InspectorJS.json
67   output = %x{ #{$inspector_json_generator_path} "#{JAVASCRIPT_INSPECTOR_PROTOCOLS_PATH}" > "#{$inspector_js_json_path}" }
68   if $?.exitstatus != 0
69     puts "ERROR: Could not generate combined InspectorJS.json file"
70     exit 1
71   end
72
73   # InspectorWeb.json
74   output = %x{ #{$inspector_json_generator_path} "#{WEB_INSPECTOR_PROTOCOLS_PATH}" > "#{$inspector_web_json_path}" }
75   if $?.exitstatus != 0
76     puts "ERROR: Could not generate combined InspectorWeb.json file"
77     exit 1
78   end
79 end
80
81 def all_tasks
82   tasks = []
83
84   # ToT Inspector json files.
85   generate_combined_inspector_json
86   tasks << Task.new($inspector_js_json_path, nil, "JavaScript", $web_inspector_protocol_path, true)
87   tasks << Task.new($inspector_web_json_path, $inspector_js_json_path, "Web", $web_inspector_protocol_path, true)
88
89   had_error = false
90   Dir.glob(File.join($versions_directory_path, "*.json")).each do |version_path|
91     match = File.basename(version_path).match(/^Inspector(.*?)\-([^-]+?)\.json$/)
92     if match
93       output_path = File.join $web_inspector_protocol_path, "Legacy", match[2]
94       tasks << Task.new(version_path, nil, "Web", output_path, false)
95     else
96       puts "ERROR: Version file (#{version_path}) did not match the template Inspector<ANYTHING>-<VERSION>.js"
97       had_error = true
98     end
99   end
100   exit 1 if had_error
101
102   tasks
103 end
104
105 def main
106   all_tasks.each { |task| task.run }
107 end
108
109 main