build.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. #!/usr/bin/python2.7
  2. # Compresses the core Blockly files into a single JavaScript file.
  3. #
  4. # Copyright 2012 Google Inc.
  5. # https://developers.google.com/blockly/
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. # Usage: build.py <0 or more of accessible, core, generators, langfiles>
  19. # build.py with no parameters builds all files.
  20. # core builds blockly_compressed, blockly_uncompressed, and blocks_compressed.
  21. # accessible builds blockly_accessible_compressed,
  22. # blockly_accessible_uncompressed, and blocks_compressed.
  23. # generators builds every <language>_compressed.js.
  24. # langfiles builds every msg/js/<LANG>.js file.
  25. # This script generates four versions of Blockly's core files. The first pair
  26. # are:
  27. # blockly_compressed.js
  28. # blockly_uncompressed.js
  29. # The compressed file is a concatenation of all of Blockly's core files which
  30. # have been run through Google's Closure Compiler. This is done using the
  31. # online API (which takes a few seconds and requires an Internet connection).
  32. # The uncompressed file is a script that loads in each of Blockly's core files
  33. # one by one. This takes much longer for a browser to load, but is useful
  34. # when debugging code since line numbers are meaningful and variables haven't
  35. # been renamed. The uncompressed file also allows for a faster development
  36. # cycle since there is no need to rebuild or recompile, just reload.
  37. #
  38. # The second pair are:
  39. # blockly_accessible_compressed.js
  40. # blockly_accessible_uncompressed.js
  41. # These files are analogous to blockly_compressed and blockly_uncompressed,
  42. # but also include the visually-impaired module for Blockly.
  43. #
  44. # This script also generates:
  45. # blocks_compressed.js: The compressed Blockly language blocks.
  46. # javascript_compressed.js: The compressed JavaScript generator.
  47. # python_compressed.js: The compressed Python generator.
  48. # php_compressed.js: The compressed PHP generator.
  49. # lua_compressed.js: The compressed Lua generator.
  50. # dart_compressed.js: The compressed Dart generator.
  51. # msg/js/<LANG>.js for every language <LANG> defined in msg/js/<LANG>.json.
  52. import sys
  53. if sys.version_info[0] != 2:
  54. raise Exception("Blockly build only compatible with Python 2.x.\n"
  55. "You are using: " + sys.version)
  56. import errno, glob, fnmatch, httplib, json, os, re, subprocess, threading, urllib
  57. REMOTE_COMPILER = "remote"
  58. CLOSURE_DIR = os.path.pardir
  59. CLOSURE_ROOT = os.path.pardir
  60. CLOSURE_LIBRARY = "closure-library"
  61. CLOSURE_COMPILER = REMOTE_COMPILER
  62. CLOSURE_DIR_NPM = "node_modules"
  63. CLOSURE_ROOT_NPM = os.path.join("node_modules")
  64. CLOSURE_LIBRARY_NPM = "google-closure-library"
  65. CLOSURE_COMPILER_NPM = "google-closure-compiler"
  66. def import_path(fullpath):
  67. """Import a file with full path specification.
  68. Allows one to import from any directory, something __import__ does not do.
  69. Args:
  70. fullpath: Path and filename of import.
  71. Returns:
  72. An imported module.
  73. """
  74. path, filename = os.path.split(fullpath)
  75. filename, ext = os.path.splitext(filename)
  76. sys.path.append(path)
  77. module = __import__(filename)
  78. reload(module) # Might be out of date.
  79. del sys.path[-1]
  80. return module
  81. def read(filename):
  82. f = open(filename)
  83. content = "".join(f.readlines())
  84. f.close()
  85. return content
  86. HEADER = ("// Do not edit this file; automatically generated by build.py.\n"
  87. "'use strict';\n")
  88. class Gen_uncompressed(threading.Thread):
  89. """Generate a JavaScript file that loads Blockly's raw files.
  90. Runs in a separate thread.
  91. """
  92. def __init__(self, search_paths, target_filename, closure_env):
  93. threading.Thread.__init__(self)
  94. self.search_paths = search_paths
  95. self.target_filename = target_filename
  96. self.closure_env = closure_env
  97. def run(self):
  98. f = open(self.target_filename, 'w')
  99. f.write(HEADER)
  100. f.write(self.format_js("""
  101. var isNodeJS = !!(typeof module !== 'undefined' && module.exports &&
  102. typeof window === 'undefined');
  103. if (isNodeJS) {
  104. var window = {};
  105. require('{closure_library}');
  106. }
  107. window.BLOCKLY_DIR = (function() {
  108. if (!isNodeJS) {
  109. // Find name of current directory.
  110. var scripts = document.getElementsByTagName('script');
  111. var re = new RegExp('(.+)[\/]blockly_uncompressed(_vertical|_horizontal|)\.js$');
  112. for (var i = 0, script; script = scripts[i]; i++) {
  113. var match = re.exec(script.src);
  114. if (match) {
  115. return match[1];
  116. }
  117. }
  118. alert('Could not detect Blockly\\'s directory name.');
  119. }
  120. return '';
  121. })();
  122. window.BLOCKLY_BOOT = function() {
  123. var dir = '';
  124. if (isNodeJS) {
  125. require('{closure_library}');
  126. dir = 'blockly';
  127. } else {
  128. // Execute after Closure has loaded.
  129. if (!window.goog) {
  130. alert('Error: Closure not found. Read this:\\n' +
  131. 'developers.google.com/blockly/guides/modify/web/closure');
  132. }
  133. if (window.BLOCKLY_DIR.search(/node_modules/)) {
  134. dir = '..';
  135. } else {
  136. dir = window.BLOCKLY_DIR.match(/[^\\/]+$/)[0];
  137. }
  138. }
  139. """))
  140. add_dependency = []
  141. base_path = calcdeps.FindClosureBasePath(self.search_paths)
  142. for dep in calcdeps.BuildDependenciesFromFiles(self.search_paths):
  143. add_dependency.append(calcdeps.GetDepsLine(dep, base_path))
  144. add_dependency.sort() # Deterministic build.
  145. add_dependency = '\n'.join(add_dependency)
  146. # Find the Blockly directory name and replace it with a JS variable.
  147. # This allows blockly_uncompressed.js to be compiled on one computer and be
  148. # used on another, even if the directory name differs.
  149. m = re.search('[\\/]([^\\/]+)[\\/]core[\\/]blockly.js', add_dependency)
  150. add_dependency = re.sub('([\\/])' + re.escape(m.group(1)) +
  151. '([\\/](core)[\\/])', '\\1" + dir + "\\2', add_dependency)
  152. f.write(add_dependency + '\n')
  153. provides = []
  154. for dep in calcdeps.BuildDependenciesFromFiles(self.search_paths):
  155. if not dep.filename.startswith(self.closure_env["closure_root"] + os.sep): # '../'
  156. provides.extend(dep.provides)
  157. provides.sort() # Deterministic build.
  158. f.write('\n')
  159. f.write('// Load Blockly.\n')
  160. for provide in provides:
  161. f.write("goog.require('%s');\n" % provide)
  162. f.write(self.format_js("""
  163. delete this.BLOCKLY_DIR;
  164. delete this.BLOCKLY_BOOT;
  165. };
  166. if (isNodeJS) {
  167. window.BLOCKLY_BOOT();
  168. module.exports = Blockly;
  169. } else {
  170. // Delete any existing Closure (e.g. Soy's nogoog_shim).
  171. document.write('<script>var goog = undefined;</script>');
  172. // Load fresh Closure Library.
  173. document.write('<script src="' + window.BLOCKLY_DIR +
  174. '/{closure_dir}/{closure_library}/closure/goog/base.js"></script>');
  175. document.write('<script>window.BLOCKLY_BOOT();</script>');
  176. }
  177. """))
  178. f.close()
  179. print("SUCCESS: " + self.target_filename)
  180. def format_js(self, code):
  181. """Format JS in a way that python's format method can work with to not
  182. consider brace-wrapped sections to be format replacements while still
  183. replacing known keys.
  184. """
  185. key_whitelist = self.closure_env.keys()
  186. keys_pipe_separated = reduce(lambda accum, key: accum + "|" + key, key_whitelist)
  187. begin_brace = re.compile(r"\{(?!%s)" % (keys_pipe_separated,))
  188. end_brace = re.compile(r"\}")
  189. def end_replacement(match):
  190. try:
  191. maybe_key = match.string[match.string[:match.start()].rindex("{") + 1:match.start()]
  192. except ValueError:
  193. return "}}"
  194. if maybe_key and maybe_key in key_whitelist:
  195. return "}"
  196. else:
  197. return "}}"
  198. return begin_brace.sub("{{", end_brace.sub(end_replacement, code)).format(**self.closure_env)
  199. class Gen_compressed(threading.Thread):
  200. """Generate a JavaScript file that contains all of Blockly's core and all
  201. required parts of Closure, compiled together.
  202. Uses the Closure Compiler's online API.
  203. Runs in a separate thread.
  204. """
  205. def __init__(self, search_paths, closure_env):
  206. threading.Thread.__init__(self)
  207. self.search_paths = search_paths
  208. self.closure_env = closure_env
  209. def run(self):
  210. self.gen_core()
  211. self.gen_blocks()
  212. self.gen_generator("arduino")
  213. self.gen_generator("python")
  214. def gen_core(self):
  215. target_filename = "blockly_compressed.js"
  216. if self.closure_env["closure_compiler"] == REMOTE_COMPILER:
  217. # Define the parameters for the POST request.
  218. params = [
  219. ("compilation_level", "SIMPLE_OPTIMIZATIONS"),
  220. ("use_closure_library", "true"),
  221. ("output_format", "json"),
  222. ("output_info", "compiled_code"),
  223. ("output_info", "warnings"),
  224. ("output_info", "errors"),
  225. ("output_info", "statistics"),
  226. ("warning_level", "DEFAULT"),
  227. ]
  228. # Read in all the source files.
  229. filenames = calcdeps.CalculateDependencies(self.search_paths,
  230. [os.path.join("core", "blockly.js")])
  231. filenames.sort() # Deterministic build.
  232. for filename in filenames:
  233. # Filter out the Closure files (the compiler will add them).
  234. if filename.startswith(os.pardir + os.sep): # '../'
  235. continue
  236. f = open(filename)
  237. params.append(("js_code", "".join(f.readlines())))
  238. f.close()
  239. else:
  240. # Define the parameters for the POST request.
  241. params = [
  242. ("compilation_level", "SIMPLE"),
  243. ("language_in", "ECMASCRIPT_2017"),
  244. ("language_out", "ECMASCRIPT5"),
  245. ("rewrite_polyfills", "false"),
  246. ("define", "goog.DEBUG=false"),
  247. ]
  248. # Read in all the source files.
  249. filenames = calcdeps.CalculateDependencies(search_paths,
  250. [os.path.join("core", "blockly.js")])
  251. filenames.sort() # Deterministic build.
  252. for filename in filenames:
  253. # Append filenames as false arguments the step before compiling will
  254. # either transform them into arguments for local or remote compilation
  255. params.append(("js_file", filename))
  256. self.do_compile(params, target_filename, filenames, [])
  257. def gen_blocks(self):
  258. target_filename = "blocks_compressed.js"
  259. if self.closure_env["closure_compiler"] == REMOTE_COMPILER:
  260. # Define the parameters for the POST request.
  261. params = [
  262. ("compilation_level", "SIMPLE_OPTIMIZATIONS"),
  263. ("output_format", "json"),
  264. ("output_info", "compiled_code"),
  265. ("output_info", "warnings"),
  266. ("output_info", "errors"),
  267. ("output_info", "statistics"),
  268. ("warning_level", "DEFAULT"),
  269. ]
  270. # Read in all the source files.
  271. # Add Blockly.Blocks to be compatible with the compiler.
  272. params.append(("js_code", "goog.provide('Blockly');goog.provide('Blockly.Blocks');"))
  273. params.append(("js_code", "goog.provide('Blockly.Types');"))
  274. filenames = []
  275. for root, folders, files in os.walk("blocks"):
  276. for filename in fnmatch.filter(files, "*.js"):
  277. filenames.append(os.path.join(root, filename))
  278. for filename in filenames:
  279. f = open(filename)
  280. params.append(("js_code", "".join(f.readlines())))
  281. f.close()
  282. else:
  283. # Define the parameters for the POST request.
  284. params = [
  285. ("compilation_level", "SIMPLE"),
  286. ]
  287. # Read in all the source files.
  288. # Add Blockly.Blocks to be compatible with the compiler.
  289. params.append(("js_file", os.path.join("build", "gen_blocks.js")))
  290. # Add Blockly.Colours for use of centralized colour bank
  291. filenames = []
  292. for root, folders, files in os.walk("blocks"):
  293. for filename in fnmatch.filter(files, "*.js"):
  294. filenames.append(os.path.join(root, filename))
  295. for filename in filenames:
  296. # Append filenames as false arguments the step before compiling will
  297. # either transform them into arguments for local or remote compilation
  298. params.append(("js_file", filename))
  299. # Remove Blockly.Blocks to be compatible with Blockly.
  300. remove = ["var Blockly={Blocks:{}};", "Blockly.Types={};", "var Blockly={Blocks:{},Types:{}};"]
  301. self.do_compile(params, target_filename, filenames, remove)
  302. def gen_generator(self, language):
  303. target_filename = language + "_compressed.js"
  304. if self.closure_env["closure_compiler"] == REMOTE_COMPILER:
  305. # Define the parameters for the POST request.
  306. params = [
  307. ("compilation_level", "SIMPLE_OPTIMIZATIONS"),
  308. ("output_format", "json"),
  309. ("output_info", "compiled_code"),
  310. ("output_info", "warnings"),
  311. ("output_info", "errors"),
  312. ("output_info", "statistics"),
  313. ]
  314. # Read in all the source files.
  315. # Add Blockly.Generator to be compatible with the compiler.
  316. params.append(("js_code", "goog.provide('Blockly.Generator');"))
  317. params.append(("js_code", "goog.provide('Blockly.StaticTyping');"))
  318. filenames = glob.glob(
  319. os.path.join("generators", language, "*.js"))
  320. filenames.insert(0, os.path.join("generators", language + ".js"))
  321. for filename in filenames:
  322. f = open(filename)
  323. params.append(("js_code", "".join(f.readlines())))
  324. f.close()
  325. filenames.insert(0, "[goog.provide]")
  326. else:
  327. # Define the parameters for the POST request.
  328. params = [
  329. ("compilation_level", "SIMPLE"),
  330. ]
  331. # Read in all the source files.
  332. # Add Blockly.Generator to be compatible with the compiler.
  333. params.append(("js_file", os.path.join("build", "gen_generator.js")))
  334. filenames = glob.glob(
  335. os.path.join("generators", language, "*.js"))
  336. filenames.insert(0, os.path.join("generators", language + ".js"))
  337. for filename in filenames:
  338. # Append filenames as false arguments the step before compiling will
  339. # either transform them into arguments for local or remote compilation
  340. params.append(("js_file", filename))
  341. filenames.insert(0, "[goog.provide]")
  342. # Remove Blockly.Generator to be compatible with Blockly.
  343. remove = ["var Blockly={Generator:{}};", "Blockly.StaticTyping={};", "var Blockly={Generator:{},StaticTyping:{}};"]
  344. self.do_compile(params, target_filename, filenames, remove)
  345. def do_compile(self, params, target_filename, filenames, remove):
  346. if self.closure_env["closure_compiler"] == REMOTE_COMPILER:
  347. do_compile = self.do_compile_remote
  348. else:
  349. do_compile = self.do_compile_local
  350. json_data = do_compile(params, target_filename)
  351. if self.report_errors(target_filename, filenames, json_data):
  352. self.write_output(target_filename, remove, json_data)
  353. self.report_stats(target_filename, json_data)
  354. def do_compile_local(self, params, target_filename):
  355. filter_keys = ["use_closure_library"]
  356. # Drop arg if arg is js_file else add dashes
  357. dash_params = []
  358. for (arg, value) in params:
  359. dash_params.append((value,) if arg == "js_file" else ("--" + arg, value))
  360. # Flatten dash_params into dash_args if their keys are not in filter_keys
  361. dash_args = []
  362. for pair in dash_params:
  363. if pair[0][2:] not in filter_keys:
  364. dash_args.extend(pair)
  365. # Build the final args array by prepending google-closure-compiler to
  366. # dash_args and dropping any falsy members
  367. args = []
  368. for group in [["google-closure-compiler"], dash_args]:
  369. args.extend(filter(lambda item: item, group))
  370. proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  371. (stdout, stderr) = proc.communicate()
  372. # Build the JSON response.
  373. filesizes = [os.path.getsize(value) for (arg, value) in params if arg == "js_file"]
  374. return dict(
  375. compiledCode=stdout,
  376. statistics=dict(
  377. originalSize=reduce(lambda v, size: v + size, filesizes, 0),
  378. compressedSize=len(stdout),
  379. )
  380. )
  381. def do_compile_remote(self, params, target_filename):
  382. # Send the request to Google.
  383. headers = {"Content-type": "application/x-www-form-urlencoded"}
  384. conn = httplib.HTTPSConnection("closure-compiler.appspot.com")
  385. conn.request("POST", "/compile", urllib.urlencode(params), headers)
  386. response = conn.getresponse()
  387. json_str = response.read()
  388. conn.close()
  389. # Parse the JSON response.
  390. return json.loads(json_str)
  391. def report_errors(self, target_filename, filenames, json_data):
  392. def file_lookup(name):
  393. if not name.startswith("Input_"):
  394. return "???"
  395. n = int(name[6:]) - 1
  396. return filenames[n]
  397. if json_data.has_key("serverErrors"):
  398. errors = json_data["serverErrors"]
  399. for error in errors:
  400. print("SERVER ERROR: %s" % target_filename)
  401. print(error["error"])
  402. elif json_data.has_key("errors"):
  403. errors = json_data["errors"]
  404. for error in errors:
  405. print("FATAL ERROR")
  406. print(error["error"])
  407. if error["file"]:
  408. print("%s at line %d:" % (
  409. file_lookup(error["file"]), error["lineno"]))
  410. print(error["line"])
  411. print((" " * error["charno"]) + "^")
  412. sys.exit(1)
  413. else:
  414. if json_data.has_key("warnings"):
  415. warnings = json_data["warnings"]
  416. for warning in warnings:
  417. print("WARNING")
  418. print(warning["warning"])
  419. if warning["file"]:
  420. print("%s at line %d:" % (
  421. file_lookup(warning["file"]), warning["lineno"]))
  422. print(warning["line"])
  423. print((" " * warning["charno"]) + "^")
  424. print()
  425. return True
  426. return False
  427. def write_output(self, target_filename, remove, json_data):
  428. if not json_data.has_key("compiledCode"):
  429. print("FATAL ERROR: Compiler did not return compiledCode.")
  430. sys.exit(1)
  431. code = HEADER + "\n" + json_data["compiledCode"]
  432. for code_statement in remove:
  433. code = code.replace(code_statement, "")
  434. # Trim down Google's (and only Google's) Apache licences.
  435. # The Closure Compiler preserves these.
  436. LICENSE = re.compile("""/\\*
  437. [\w ]+
  438. Copyright \\d+ Google Inc.
  439. https://developers.google.com/blockly/
  440. Licensed under the Apache License, Version 2.0 \(the "License"\);
  441. you may not use this file except in compliance with the License.
  442. You may obtain a copy of the License at
  443. http://www.apache.org/licenses/LICENSE-2.0
  444. Unless required by applicable law or agreed to in writing, software
  445. distributed under the License is distributed on an "AS IS" BASIS,
  446. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  447. See the License for the specific language governing permissions and
  448. limitations under the License.
  449. \\*/""")
  450. code = re.sub(LICENSE, "", code)
  451. stats = json_data["statistics"]
  452. original_b = stats["originalSize"]
  453. compressed_b = stats["compressedSize"]
  454. if original_b > 0 and compressed_b > 0:
  455. f = open(target_filename, "w")
  456. f.write(code)
  457. f.close()
  458. def report_stats(self, target_filename, json_data):
  459. stats = json_data["statistics"]
  460. original_b = stats["originalSize"]
  461. compressed_b = stats["compressedSize"]
  462. if original_b > 0 and compressed_b > 0:
  463. original_kb = int(original_b / 1024 + 0.5)
  464. compressed_kb = int(compressed_b / 1024 + 0.5)
  465. ratio = int(float(compressed_b) / float(original_b) * 100 + 0.5)
  466. print("SUCCESS: " + target_filename)
  467. print("Size changed from %d KB to %d KB (%d%%)." % (
  468. original_kb, compressed_kb, ratio))
  469. else:
  470. print("UNKNOWN ERROR")
  471. class Gen_langfiles(threading.Thread):
  472. """Generate JavaScript file for each natural language supported.
  473. Runs in a separate thread.
  474. """
  475. def __init__(self):
  476. threading.Thread.__init__(self)
  477. def _rebuild(self, srcs, dests):
  478. # Determine whether any of the files in srcs is newer than any in dests.
  479. try:
  480. return (max(os.path.getmtime(src) for src in srcs) >
  481. min(os.path.getmtime(dest) for dest in dests))
  482. except OSError as e:
  483. # Was a file not found?
  484. if e.errno == errno.ENOENT:
  485. # If it was a source file, we can't proceed.
  486. if e.filename in srcs:
  487. print("Source file missing: " + e.filename)
  488. sys.exit(1)
  489. else:
  490. # If a destination file was missing, rebuild.
  491. return True
  492. else:
  493. print("Error checking file creation times: " + e)
  494. def run(self):
  495. # The files msg/json/{en,qqq,synonyms}.json depend on msg/messages.js.
  496. if self._rebuild([os.path.join("msg", "messages.js")],
  497. [os.path.join("msg", "json", f) for f in
  498. ["en.json", "qqq.json", "synonyms.json"]]):
  499. try:
  500. subprocess.check_call([
  501. "python",
  502. os.path.join("i18n", "js_to_json.py"),
  503. "--input_file", "msg/messages.js",
  504. "--output_dir", "msg/json/",
  505. "--quiet"])
  506. except (subprocess.CalledProcessError, OSError) as e:
  507. # Documentation for subprocess.check_call says that CalledProcessError
  508. # will be raised on failure, but I found that OSError is also possible.
  509. print("Error running i18n/js_to_json.py: ", e)
  510. sys.exit(1)
  511. # Checking whether it is necessary to rebuild the js files would be a lot of
  512. # work since we would have to compare each <lang>.json file with each
  513. # <lang>.js file. Rebuilding is easy and cheap, so just go ahead and do it.
  514. try:
  515. # Use create_messages.py to create .js files from .json files.
  516. cmd = [
  517. "python",
  518. os.path.join("i18n", "create_messages.py"),
  519. "--source_lang_file", os.path.join("msg", "json", "en.json"),
  520. "--source_synonym_file", os.path.join("msg", "json", "synonyms.json"),
  521. "--key_file", os.path.join("msg", "json", "keys.json"),
  522. "--output_dir", os.path.join("msg", "js"),
  523. "--quiet"]
  524. json_files = glob.glob(os.path.join("msg", "json", "*.json"))
  525. json_files = [file for file in json_files if not
  526. (file.endswith(("keys.json", "synonyms.json", "qqq.json")))]
  527. cmd.extend(json_files)
  528. subprocess.check_call(cmd)
  529. except (subprocess.CalledProcessError, OSError) as e:
  530. print("Error running i18n/create_messages.py: ", e)
  531. sys.exit(1)
  532. # Output list of .js files created.
  533. for f in json_files:
  534. # This assumes the path to the current directory does not contain "json".
  535. f = f.replace("json", "js")
  536. if os.path.isfile(f):
  537. print("SUCCESS: " + f)
  538. else:
  539. print("FAILED to create " + f)
  540. if __name__ == "__main__":
  541. try:
  542. closure_dir = CLOSURE_DIR_NPM
  543. closure_root = CLOSURE_ROOT_NPM
  544. closure_library = CLOSURE_LIBRARY_NPM
  545. closure_compiler = CLOSURE_COMPILER_NPM
  546. # Load calcdeps from the local library
  547. calcdeps = import_path(os.path.join(
  548. closure_root, closure_library, "closure", "bin", "calcdeps.py"))
  549. # Sanity check the local compiler
  550. test_args = [closure_compiler, os.path.join("build", "test_input.js")]
  551. test_proc = subprocess.Popen(test_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  552. (stdout, _) = test_proc.communicate()
  553. assert stdout == read(os.path.join("build", "test_expect.js"))
  554. print("Using local compiler: google-closure-compiler ...\n")
  555. except (ImportError, AssertionError):
  556. print("Using remote compiler: closure-compiler.appspot.com ...\n")
  557. try:
  558. closure_dir = CLOSURE_DIR
  559. closure_root = CLOSURE_ROOT
  560. closure_library = CLOSURE_LIBRARY
  561. closure_compiler = CLOSURE_COMPILER
  562. calcdeps = import_path(os.path.join(
  563. closure_root, closure_library, "closure", "bin", "calcdeps.py"))
  564. except ImportError:
  565. if os.path.isdir(os.path.join(os.path.pardir, "closure-library-read-only")):
  566. # Dir got renamed when Closure moved from Google Code to GitHub in 2014.
  567. print("Error: Closure directory needs to be renamed from"
  568. "'closure-library-read-only' to 'closure-library'.\n"
  569. "Please rename this directory.")
  570. elif os.path.isdir(os.path.join(os.path.pardir, "google-closure-library")):
  571. # When Closure is installed by npm, it is named "google-closure-library".
  572. #calcdeps = import_path(os.path.join(
  573. # os.path.pardir, "google-closure-library", "closure", "bin", "calcdeps.py"))
  574. print("Error: Closure directory needs to be renamed from"
  575. "'google-closure-library' to 'closure-library'.\n"
  576. "Please rename this directory.")
  577. else:
  578. print("""Error: Closure not found. Read this:
  579. developers.google.com/blockly/guides/modify/web/closure""")
  580. sys.exit(1)
  581. search_paths = calcdeps.ExpandDirectories(
  582. ["core", os.path.join(os.path.pardir, "closure-library")])
  583. search_paths.sort() # Deterministic build.
  584. closure_env = {
  585. "closure_dir": closure_dir,
  586. "closure_root": closure_root,
  587. "closure_library": closure_library,
  588. "closure_compiler": closure_compiler,
  589. }
  590. # Uncompressed and compressed are run in parallel threads.
  591. # Uncompressed is limited by processor speed.
  592. Gen_uncompressed(search_paths, 'blockly_uncompressed.js', closure_env).start()
  593. # Compressed is limited by network and server speed.
  594. Gen_compressed(search_paths, closure_env).start()
  595. # This is run locally in a separate thread
  596. # defaultlangfiles checks for changes in the msg files, while manually asking
  597. # to build langfiles will force the messages to be rebuilt.
  598. Gen_langfiles().start()