You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

216 line
6.0 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals, print_function
  4. from frappe.utils.minify import JavascriptMinify
  5. import subprocess
  6. """
  7. Build the `public` folders and setup languages
  8. """
  9. import os, frappe, json, shutil, re
  10. # from cssmin import cssmin
  11. app_paths = None
  12. def setup():
  13. global app_paths
  14. pymodules = []
  15. for app in frappe.get_all_apps(True):
  16. try:
  17. pymodules.append(frappe.get_module(app))
  18. except ImportError: pass
  19. app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules]
  20. def bundle(no_compress, make_copy=False, verbose=False):
  21. """concat / minify js files"""
  22. # build js files
  23. setup()
  24. make_asset_dirs(make_copy=make_copy)
  25. # new nodejs build system
  26. command = 'node --use_strict ../apps/frappe/frappe/build.js --build'
  27. if not no_compress:
  28. command += ' --minify'
  29. subprocess.call(command.split(' '))
  30. # build(no_compress, verbose)
  31. def watch(no_compress):
  32. """watch and rebuild if necessary"""
  33. # new nodejs file watcher
  34. command = 'node --use_strict ../apps/frappe/frappe/build.js --watch'
  35. subprocess.call(command.split(' '))
  36. # setup()
  37. # import time
  38. # compile_less()
  39. # build(no_compress=True)
  40. # while True:
  41. # compile_less()
  42. # if files_dirty():
  43. # build(no_compress=True)
  44. # time.sleep(3)
  45. def make_asset_dirs(make_copy=False):
  46. assets_path = os.path.join(frappe.local.sites_path, "assets")
  47. for dir_path in [
  48. os.path.join(assets_path, 'js'),
  49. os.path.join(assets_path, 'css')]:
  50. if not os.path.exists(dir_path):
  51. os.makedirs(dir_path)
  52. # symlink app/public > assets/app
  53. for app_name in frappe.get_all_apps(True):
  54. pymodule = frappe.get_module(app_name)
  55. app_base_path = os.path.abspath(os.path.dirname(pymodule.__file__))
  56. symlinks = []
  57. symlinks.append([os.path.join(app_base_path, 'public'), os.path.join(assets_path, app_name)])
  58. symlinks.append([os.path.join(app_base_path, 'docs'), os.path.join(assets_path, app_name + '_docs')])
  59. for source, target in symlinks:
  60. source = os.path.abspath(source)
  61. if not os.path.exists(target) and os.path.exists(source):
  62. if make_copy:
  63. shutil.copytree(source, target)
  64. else:
  65. os.symlink(source, target)
  66. def build(no_compress=False, verbose=False):
  67. assets_path = os.path.join(frappe.local.sites_path, "assets")
  68. for target, sources in get_build_maps().iteritems():
  69. pack(os.path.join(assets_path, target), sources, no_compress, verbose)
  70. def get_build_maps():
  71. """get all build.jsons with absolute paths"""
  72. # framework js and css files
  73. build_maps = {}
  74. for app_path in app_paths:
  75. path = os.path.join(app_path, 'public', 'build.json')
  76. if os.path.exists(path):
  77. with open(path) as f:
  78. try:
  79. for target, sources in json.loads(f.read()).iteritems():
  80. # update app path
  81. source_paths = []
  82. for source in sources:
  83. if isinstance(source, list):
  84. s = frappe.get_pymodule_path(source[0], *source[1].split("/"))
  85. else:
  86. s = os.path.join(app_path, source)
  87. source_paths.append(s)
  88. build_maps[target] = source_paths
  89. except ValueError as e:
  90. print(path)
  91. print('JSON syntax error {0}'.format(str(e)))
  92. return build_maps
  93. timestamps = {}
  94. def pack(target, sources, no_compress, verbose):
  95. from cStringIO import StringIO
  96. outtype, outtxt = target.split(".")[-1], ''
  97. jsm = JavascriptMinify()
  98. for f in sources:
  99. suffix = None
  100. if ':' in f: f, suffix = f.split(':')
  101. if not os.path.exists(f) or os.path.isdir(f):
  102. print("did not find " + f)
  103. continue
  104. timestamps[f] = os.path.getmtime(f)
  105. try:
  106. with open(f, 'r') as sourcefile:
  107. data = unicode(sourcefile.read(), 'utf-8', errors='ignore')
  108. extn = f.rsplit(".", 1)[1]
  109. if outtype=="js" and extn=="js" and (not no_compress) and suffix!="concat" and (".min." not in f):
  110. tmpin, tmpout = StringIO(data.encode('utf-8')), StringIO()
  111. jsm.minify(tmpin, tmpout)
  112. minified = tmpout.getvalue()
  113. if minified:
  114. outtxt += unicode(minified or '', 'utf-8').strip('\n') + ';'
  115. if verbose:
  116. print("{0}: {1}k".format(f, int(len(minified) / 1024)))
  117. elif outtype=="js" and extn=="html":
  118. # add to frappe.templates
  119. outtxt += html_to_js_template(f, data)
  120. else:
  121. outtxt += ('\n/*\n *\t%s\n */' % f)
  122. outtxt += '\n' + data + '\n'
  123. except Exception:
  124. print("--Error in:" + f + "--")
  125. print(frappe.get_traceback())
  126. if not no_compress and outtype == 'css':
  127. pass
  128. #outtxt = cssmin(outtxt)
  129. with open(target, 'w') as f:
  130. f.write(outtxt.encode("utf-8"))
  131. print("Wrote %s - %sk" % (target, str(int(os.path.getsize(target)/1024))))
  132. def html_to_js_template(path, content):
  133. '''returns HTML template content as Javascript code, adding it to `frappe.templates`'''
  134. return """frappe.templates["{key}"] = '{content}';\n""".format(\
  135. key=path.rsplit("/", 1)[-1][:-5], content=scrub_html_template(content))
  136. def scrub_html_template(content):
  137. '''Returns HTML content with removed whitespace and comments'''
  138. # remove whitespace to a single space
  139. content = re.sub("\s+", " ", content)
  140. # strip comments
  141. content = re.sub("(<!--.*?-->)", "", content)
  142. return content.replace("'", "\'")
  143. def files_dirty():
  144. for target, sources in get_build_maps().iteritems():
  145. for f in sources:
  146. if ':' in f: f, suffix = f.split(':')
  147. if not os.path.exists(f) or os.path.isdir(f): continue
  148. if os.path.getmtime(f) != timestamps.get(f):
  149. print(f + ' dirty')
  150. return True
  151. else:
  152. return False
  153. def compile_less():
  154. from distutils.spawn import find_executable
  155. if not find_executable("lessc"):
  156. return
  157. for path in app_paths:
  158. less_path = os.path.join(path, "public", "less")
  159. if os.path.exists(less_path):
  160. for fname in os.listdir(less_path):
  161. if fname.endswith(".less") and fname != "variables.less":
  162. fpath = os.path.join(less_path, fname)
  163. mtime = os.path.getmtime(fpath)
  164. if fpath in timestamps and mtime == timestamps[fpath]:
  165. continue
  166. timestamps[fpath] = mtime
  167. print("compiling {0}".format(fpath))
  168. css_path = os.path.join(path, "public", "css", fname.rsplit(".", 1)[0] + ".css")
  169. os.system("lessc {0} > {1}".format(fpath, css_path))