build.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 shutil
  11. import tempfile
  12. from io import open
  13. def main(argv=None):
  14. parser = argparse.ArgumentParser()
  15. parser.add_argument('--include', action='append', required=True)
  16. parser.add_argument('--externs', action='append', default=['externs/common.js'])
  17. parser.add_argument('--amd', action='store_true', default=False)
  18. parser.add_argument('--minify', action='store_true', default=False)
  19. parser.add_argument('--output', default='../../build/three.js')
  20. parser.add_argument('--sourcemaps', action='store_true', default=False)
  21. args = parser.parse_args()
  22. output = args.output
  23. # merge
  24. print(' * Building ' + output)
  25. # enable sourcemaps support
  26. if args.sourcemaps:
  27. sourcemap = output + '.map'
  28. sourcemapping = '\n//@ sourceMappingURL=' + sourcemap
  29. sourcemapargs = ' --create_source_map ' + sourcemap + ' --source_map_format=V3'
  30. else:
  31. sourcemap = sourcemapping = sourcemapargs = ''
  32. fd, path = tempfile.mkstemp()
  33. tmp = open(path, 'w', encoding='utf-8')
  34. sources = []
  35. if args.amd:
  36. tmp.write('( 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')
  37. for include in args.include:
  38. with open('includes/' + include + '.json','r', encoding='utf-8') as f:
  39. files = json.load(f)
  40. for filename in files:
  41. tmp.write('// File:' + filename)
  42. tmp.write(u'\n\n')
  43. filename = '../../' + filename
  44. sources.append(filename)
  45. with open(filename, 'r', encoding='utf-8') as f:
  46. if filename.endswith(".glsl"):
  47. tmp.write('THREE.ShaderChunk[ \'' + os.path.splitext(os.path.basename(filename))[0] + '\'] = "')
  48. tmp.write(f.read().replace('\n','\\n'))
  49. tmp.write(u'";\n\n')
  50. else:
  51. tmp.write(f.read())
  52. tmp.write(u'\n')
  53. if args.amd:
  54. tmp.write('exports.THREE = THREE;\n\n} ) );')
  55. tmp.close()
  56. # save
  57. if args.minify is False:
  58. shutil.copy(path, output)
  59. os.chmod(output, 0o664) # temp files would usually get 0600
  60. else:
  61. backup = ''
  62. if os.path.exists(output):
  63. with open(output, 'r', encoding='utf-8') as f: backup = f.read()
  64. os.remove(output)
  65. externs = ' --externs '.join(args.externs)
  66. source = ' '.join(sources)
  67. cmd = 'java -jar compiler/compiler.jar --warning_level=VERBOSE --jscomp_off=globalThis --externs %s --jscomp_off=checkTypes --language_in=ECMASCRIPT5_STRICT --js %s --js_output_file %s %s' % (externs, path, output, sourcemapargs)
  68. os.system(cmd)
  69. # header
  70. if os.path.exists(output):
  71. with open(output, 'r', encoding='utf-8') as f: text = f.read()
  72. with open(output, 'w', encoding='utf-8') as f: f.write('// threejs.org/license\n' + text + sourcemapping)
  73. else:
  74. print("Minification with Closure compiler failed. Check your Java runtime version.")
  75. with open(output, 'w', encoding='utf-8') as f: f.write(backup)
  76. os.close(fd)
  77. os.remove(path)
  78. if __name__ == "__main__":
  79. script_dir = os.path.dirname(os.path.abspath(__file__))
  80. os.chdir(script_dir)
  81. main()