build.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python
  2. import sys
  3. if sys.version_info < (2, 7):
  4. print("This script requires at least Python 2.7.")
  5. print("Please, update to a newer version: http://www.python.org/download/releases/")
  6. # exit()
  7. import argparse
  8. import json
  9. import os
  10. import re
  11. import shutil
  12. import tempfile
  13. from io import open
  14. def main(argv=None):
  15. parser = argparse.ArgumentParser()
  16. parser.add_argument('--include', action='append', required=True)
  17. parser.add_argument('--externs', action='append', default=['externs/common.js'])
  18. parser.add_argument('--amd', action='store_true', default=False)
  19. parser.add_argument('--minify', action='store_true', default=False)
  20. parser.add_argument('--output', default='../../build/three.js')
  21. parser.add_argument('--sourcemaps', action='store_true', default=False)
  22. args = parser.parse_args()
  23. output = args.output
  24. # merge
  25. print(' * Building ' + output)
  26. # enable sourcemaps support
  27. if args.sourcemaps:
  28. sourcemap = output + '.map'
  29. sourcemapping = '\n//@ sourceMappingURL=' + sourcemap
  30. sourcemapargs = ' --create_source_map ' + sourcemap + ' --source_map_format=V3'
  31. else:
  32. sourcemap = sourcemapping = sourcemapargs = ''
  33. fd, path = tempfile.mkstemp()
  34. tmp = open(path, 'w', encoding='utf-8')
  35. sources = []
  36. if args.amd:
  37. tmp.write(u'( function ( root, factory ) {\n\n\tif ( typeof define === \'function\' && define.amd ) {\n\n\t\tdefine( [ \'exports\' ], factory );\n\n\t} else if ( typeof exports === \'object\' ) {\n\n\t\tfactory( exports );\n\n\t} else {\n\n\t\tfactory( root );\n\n\t}\n\n}( this, function ( exports ) {\n\n')
  38. for include in args.include:
  39. with open('includes/' + include + '.json','r', encoding='utf-8') as f:
  40. files = json.load(f)
  41. for filename in files:
  42. tmp.write(u'// File:' + filename)
  43. tmp.write(u'\n\n')
  44. filename = '../../' + filename
  45. sources.append(filename)
  46. with open(filename, 'r', encoding='utf-8') as f:
  47. if filename.endswith(".glsl"):
  48. tmp.write(u'THREE.ShaderChunk[ \'' + os.path.splitext(os.path.basename(filename))[0] + u'\' ] = "')
  49. text = f.read()
  50. text = re.sub(r"[ \t]*//.*\n", "", text) # remove //
  51. text = re.sub(r"[ \t]*/\*[\s\S]*?\*/", "", text) # remove /* */
  52. text = re.sub(r"\n+", '\\\\n', text) # \n+ to \n
  53. tmp.write(text)
  54. tmp.write(u'";\n\n')
  55. else:
  56. tmp.write(f.read())
  57. tmp.write(u'\n')
  58. if args.amd:
  59. tmp.write(u'exports.THREE = THREE;\n\n} ) );')
  60. tmp.close()
  61. # save
  62. if args.minify is False:
  63. shutil.copy(path, output)
  64. os.chmod(output, 0o664) # temp files would usually get 0600
  65. else:
  66. backup = ''
  67. if os.path.exists(output):
  68. with open(output, 'r', encoding='utf-8') as f: backup = f.read()
  69. os.remove(output)
  70. externs = ' --externs '.join(args.externs)
  71. source = ' '.join(sources)
  72. cmd = 'java -jar compiler/compiler.jar --warning_level=VERBOSE --jscomp_off=globalThis --jscomp_off=checkTypes --externs %s --language_in=ECMASCRIPT5_STRICT --js %s --js_output_file %s %s' % (externs, path, output, sourcemapargs)
  73. os.system(cmd)
  74. # header
  75. if os.path.exists(output):
  76. with open(output, 'r', encoding='utf-8') as f: text = f.read()
  77. with open(output, 'w', encoding='utf-8') as f: f.write('// threejs.org/license\n' + text + sourcemapping)
  78. else:
  79. print("Minification with Closure compiler failed. Check your Java runtime version.")
  80. with open(output, 'w', encoding='utf-8') as f: f.write(backup)
  81. os.close(fd)
  82. os.remove(path)
  83. if __name__ == "__main__":
  84. script_dir = os.path.dirname(os.path.abspath(__file__))
  85. os.chdir(script_dir)
  86. main()