build.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env python
  2. try:
  3. import argparse
  4. ap = 1
  5. except ImportError:
  6. import optparse
  7. ap = 0
  8. import os
  9. import tempfile
  10. import sys
  11. import json
  12. def merge(files):
  13. buffer = []
  14. for filename in files:
  15. with open(os.path.join('..', 'src', filename), 'r') as f:
  16. buffer.append(f.read())
  17. return "".join(buffer)
  18. def output(text, filename):
  19. with open(os.path.join('..', 'build', filename), 'w') as f:
  20. f.write(text)
  21. def compress(text, fname_externs):
  22. externs = ""
  23. if fname_externs:
  24. externs = "--externs %s.js" % fname_externs
  25. in_tuple = tempfile.mkstemp()
  26. with os.fdopen(in_tuple[0], 'w') as handle:
  27. handle.write(text)
  28. out_tuple = tempfile.mkstemp()
  29. os.system("java -jar compiler/compiler.jar --warning_level=VERBOSE --jscomp_off=globalThis --jscomp_off=checkTypes --externs externs_common.js %s --language_in=ECMASCRIPT5_STRICT --js %s --js_output_file %s" % (externs, in_tuple[1], out_tuple[1]))
  30. with os.fdopen(out_tuple[0], 'r') as handle:
  31. compressed = handle.read()
  32. os.unlink(in_tuple[1])
  33. os.unlink(out_tuple[1])
  34. return compressed
  35. def addHeader(text, endFilename):
  36. return ("// %s - http://github.com/mrdoob/three.js\n" % endFilename) + text
  37. def makeDebug(text):
  38. position = 0
  39. while True:
  40. position = text.find("/* DEBUG", position)
  41. if position == -1:
  42. break
  43. text = text[0:position] + text[position+8:]
  44. position = text.find("*/", position)
  45. text = text[0:position] + text[position+2:]
  46. return text
  47. def buildLib(files, debug, minified, filename, fname_externs):
  48. text = merge(files)
  49. if debug:
  50. text = makeDebug(text)
  51. filename = filename + 'Debug'
  52. if filename == "Three":
  53. folder = ''
  54. else:
  55. folder = 'custom/'
  56. filename = filename + '.js'
  57. print("=" * 40)
  58. print("Compiling " + filename)
  59. print("=" * 40)
  60. if minified:
  61. text = compress(text, fname_externs)
  62. output(addHeader(text, filename), folder + filename)
  63. def buildIncludes(files, filename):
  64. template = '\t\t<script src="../src/%s"></script>'
  65. text = "\n".join(template % f for f in files)
  66. output(text, filename + '.js')
  67. def getFileNames():
  68. file = open(os.path.join('.', 'files.json'), 'r')
  69. data = json.load(file)
  70. file.close()
  71. return data
  72. def parse_args():
  73. if ap:
  74. parser = argparse.ArgumentParser(description='Build and compress Three.js')
  75. parser.add_argument('--includes', help='Build includes.js', action='store_true')
  76. parser.add_argument('--common', help='Build Three.js', action='store_const', const=True)
  77. parser.add_argument('--extras', help='Build ThreeExtras.js', action='store_const', const=True)
  78. parser.add_argument('--canvas', help='Build ThreeCanvas.js', action='store_true')
  79. parser.add_argument('--webgl', help='Build ThreeWebGL.js', action='store_true')
  80. parser.add_argument('--debug', help='Generate debug versions', action='store_const', const=True, default=False)
  81. parser.add_argument('--minified', help='Generate minified versions', action='store_const', const=True, default=False)
  82. parser.add_argument('--all', help='Build all Three.js versions', action='store_true')
  83. args = parser.parse_args()
  84. else:
  85. parser = optparse.OptionParser(description='Build and compress Three.js')
  86. parser.add_option('--includes', dest='includes', help='Build includes.js', action='store_true')
  87. parser.add_option('--common', dest='common', help='Build Three.js', action='store_const', const=True)
  88. parser.add_option('--extras', dest='extras', help='Build ThreeExtras.js', action='store_const', const=True)
  89. parser.add_option('--canvas', dest='canvas', help='Build ThreeCanvas.js', action='store_true')
  90. parser.add_option('--webgl', dest='webgl', help='Build ThreeWebGL.js', action='store_true')
  91. parser.add_option('--debug', dest='debug', help='Generate debug versions', action='store_const', const=True, default=False)
  92. parser.add_option('--minified', help='Generate minified versions', action='store_const', const=True, default=False)
  93. parser.add_option('--all', dest='all', help='Build all Three.js versions', action='store_true')
  94. args, remainder = parser.parse_args()
  95. # If no arguments have been passed, show the help message and exit
  96. if len(sys.argv) == 1:
  97. parser.print_help()
  98. sys.exit(1)
  99. return args
  100. def main(argv=None):
  101. args = parse_args()
  102. debug = args.debug
  103. minified = args.minified
  104. files = getFileNames()
  105. config = [
  106. ['Three', 'includes', '', files["COMMON"] + files["EXTRAS"], args.common],
  107. ['ThreeCanvas', 'includes_canvas', '', files["CANVAS"], args.canvas],
  108. ['ThreeWebGL', 'includes_webgl', '', files["WEBGL"], args.webgl],
  109. ['ThreeExtras', 'includes_extras', 'externs_extras', files["EXTRAS"], args.extras]
  110. ]
  111. for fname_lib, fname_inc, fname_externs, files, enabled in config:
  112. if enabled or args.all:
  113. buildLib(files, debug, minified, fname_lib, fname_externs)
  114. if args.includes:
  115. buildIncludes(files, fname_inc)
  116. if __name__ == "__main__":
  117. main()