Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

106 rindas
3.2 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from minify import JavascriptMinify
  23. class Bundle:
  24. """
  25. Concatenate, compress and mix (if required) js+css files from build.json
  26. """
  27. def concat(self, filelist, outfile=None):
  28. """
  29. Concat css and js files into a bundle
  30. """
  31. import os
  32. from cStringIO import StringIO
  33. from build import verbose
  34. out_type = outfile and outfile.split('.')[-1] or 'js'
  35. outtxt = ''
  36. for f in filelist:
  37. suffix = None
  38. if ':' in f:
  39. f, suffix = f.split(':')
  40. # print f + ' | ' + str(int(os.path.getsize(f)/1024)) + 'k'
  41. # get data
  42. with open(f, 'r') as infile:
  43. # get file type
  44. ftype = f.split('.')[-1]
  45. data = infile.read()
  46. # css -> js
  47. if out_type=='js' and ftype =='css':
  48. data = "\nwn.assets.handler.css('%s');\n" %\
  49. data.replace("'", "\\'").replace('\n', '\\\n')
  50. outtxt += ('\n/*\n *\t%s\n */' % f)
  51. # append
  52. if suffix=='concat' or out_type != 'js':
  53. outtxt += '\n' + data + '\n'
  54. else:
  55. jsm = JavascriptMinify()
  56. tmpin = StringIO(data)
  57. tmpout = StringIO()
  58. jsm.minify(tmpin, tmpout)
  59. tmpmin = tmpout.getvalue() or ''
  60. tmpmin.strip('\n')
  61. outtxt += tmpmin
  62. with open(outfile, 'w') as f:
  63. f.write(outtxt)
  64. print "Wrote %s - %sk" % (outfile, str(int(os.path.getsize(outfile)/1024)))
  65. def make(self, bpath):
  66. """
  67. Build (stitch + compress) the file defined in build.json
  68. """
  69. import os, sys
  70. from build import no_minify
  71. # open the build.json file and read
  72. # the dict
  73. print "making %s ..." % bpath
  74. with open(bpath, 'r') as bfile:
  75. bdata = eval(bfile.read())
  76. path = os.path.dirname(bpath)
  77. for buildfile in bdata:
  78. # build the file list relative to the main folder
  79. outfile = buildfile.keys()[0]
  80. infiles = buildfile[outfile]
  81. fl = [os.path.relpath(os.path.join(path, f), os.curdir) for f in infiles]
  82. # js files are minified by default unless explicitly
  83. # mentioned in the prefix.
  84. # some files may not work if minified (known jsmin bug)
  85. self.concat(fl, os.path.relpath(os.path.join(path, outfile), os.curdir))