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.
 
 
 
 
 
 

210 lines
5.0 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. from webnotes.utils.minify import JavascriptMinify
  5. """
  6. Build the `public` folders and setup languages
  7. """
  8. import os, sys, webnotes
  9. def bundle(no_compress, cms_make=True):
  10. """concat / minify js files"""
  11. # build js files
  12. check_public()
  13. check_lang()
  14. bundle = Bundle()
  15. bundle.no_compress = no_compress
  16. bundle.make()
  17. webnotes.cache().delete_value("website_routes")
  18. if cms_make:
  19. try:
  20. from startup.event_handlers import on_build
  21. on_build()
  22. except ImportError, e:
  23. pass
  24. def watch(no_compress):
  25. """watch and rebuild if necessary"""
  26. import time
  27. bundle = Bundle()
  28. bundle.no_compress = no_compress
  29. while True:
  30. if bundle.dirty():
  31. bundle.make()
  32. time.sleep(3)
  33. def check_public():
  34. from webnotes.install_lib.setup_public_folder import make
  35. make()
  36. def check_lang():
  37. from webnotes.translate import update_translations
  38. update_translations()
  39. class Bundle:
  40. """
  41. Concatenate, compress and mix (if required) js+css files from build.json
  42. """
  43. no_compress = False
  44. timestamps = {}
  45. path = '.'
  46. def concat(self, filelist, outfile=None):
  47. """
  48. Concat css and js files into a bundle
  49. """
  50. from cStringIO import StringIO
  51. out_type = outfile and outfile.split('.')[-1] or 'js'
  52. outtxt = ''
  53. for f in filelist:
  54. suffix = None
  55. if ':' in f:
  56. f, suffix = f.split(':')
  57. if not os.path.exists(f) or os.path.isdir(f):
  58. continue
  59. self.timestamps[f] = os.path.getmtime(f)
  60. # get datas
  61. try:
  62. with open(f, 'r') as infile:
  63. # get file type
  64. ftype = f.split('.')[-1]
  65. data = unicode(infile.read(), 'utf-8', errors='ignore')
  66. outtxt += ('\n/*\n *\t%s\n */' % f)
  67. # append
  68. if suffix=='concat' or out_type != 'js' or self.no_compress or ('.min.' in f):
  69. outtxt += '\n' + data + '\n'
  70. else:
  71. jsm = JavascriptMinify()
  72. tmpin = StringIO(data.encode('utf-8'))
  73. tmpout = StringIO()
  74. jsm.minify(tmpin, tmpout)
  75. tmpmin = unicode(tmpout.getvalue() or '', 'utf-8')
  76. tmpmin.strip('\n')
  77. outtxt += tmpmin
  78. except Exception, e:
  79. print "--Error in:" + f + "--"
  80. print webnotes.getTraceback()
  81. with open(outfile, 'w') as f:
  82. f.write(outtxt.encode("utf-8"))
  83. print "Wrote %s - %sk" % (outfile, str(int(os.path.getsize(outfile)/1024)))
  84. def dirty(self):
  85. """check if build files are dirty"""
  86. self.make_build_data()
  87. for builddict in self.bdata:
  88. for f in self.get_infiles(builddict):
  89. if ':' in f:
  90. f, suffix = f.split(':')
  91. if not os.path.exists(f) or os.path.isdir(f):
  92. continue
  93. if os.path.getmtime(f) != self.timestamps.get(f):
  94. print f + ' dirty'
  95. return True
  96. else:
  97. return False
  98. def make(self):
  99. """Build (stitch + compress) the file defined in build.json"""
  100. print "Building js and css files..."
  101. self.make_build_data()
  102. for builddict in self.bdata:
  103. outfile = builddict.keys()[0]
  104. infiles = self.get_infiles(builddict)
  105. self.concat(infiles, os.path.relpath(os.path.join(self.path, outfile), os.curdir))
  106. self.reset_app_html()
  107. def reset_app_html(self):
  108. import webnotes
  109. if os.path.exists("public/app.html"):
  110. os.remove("public/app.html")
  111. splash = ""
  112. if os.path.exists("public/app/images/splash.svg"):
  113. with open("public/app/images/splash.svg") as splash_file:
  114. splash = splash_file.read()
  115. with open('lib/public/html/app.html', 'r') as app_html:
  116. data = app_html.read()
  117. data = data % {
  118. "_version_number": webnotes.generate_hash(),
  119. "splash": splash
  120. }
  121. with open('public/app.html', 'w') as new_app_html:
  122. new_app_html.write(data)
  123. def get_infiles(self, builddict):
  124. """make list of files to merge"""
  125. outfile = builddict.keys()[0]
  126. infiles = builddict[outfile]
  127. # add app js and css to the list
  128. if outfile in self.appfiles:
  129. for f in self.appfiles[outfile]:
  130. if f not in infiles:
  131. infiles.append(f)
  132. fl = []
  133. for f in infiles:
  134. ## load files from directory
  135. if f.endswith('/'):
  136. # add init js first
  137. fl += [os.path.relpath(os.path.join(f, 'init.js'), os.curdir)]
  138. # files other than init.js and beginning with "_"
  139. fl += [os.path.relpath(os.path.join(f, tmp), os.curdir) \
  140. for tmp in os.listdir(f) if (tmp != 'init.js' and not tmp.startswith('_'))]
  141. else:
  142. fl.append(os.path.relpath(os.path.join(self.path, f), os.curdir))
  143. return fl
  144. def make_build_data(self):
  145. """merge build.json and lib/build.json"""
  146. # framework js and css files
  147. with open('lib/public/build.json', 'r') as bfile:
  148. bdata = eval(bfile.read())
  149. # app js and css files
  150. if os.path.exists('app/public/build.json'):
  151. with open('app/public/build.json', 'r') as bfile:
  152. appfiles = eval(bfile.read())
  153. else:
  154. appfiles = {}
  155. # add additional app files in bdata
  156. buildfile_list = [builddict.keys()[0] for builddict in bdata]
  157. for f in appfiles:
  158. if f not in buildfile_list:
  159. bdata.append({f: appfiles[f]})
  160. self.appfiles = appfiles
  161. self.bdata = bdata