Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
9 lars 1
#! /usr/bin/python
2
 
3
def default(context):
4
    minifyfiles(context)
5
 
6
def minifyfiles(context):
7
 
8
    src = context.Node('jspdf.js')
9
 
10
    dst = src.parent + 'dist/' + src.name - '.js' + '.source.js'
11
 
12
    dst.text = src.text.replace(
13
            "${buildDate}", timeUTC()
14
        ).replace(
15
            "${commitID}", getCommitIDstring()
16
        ) + \
17
        (src - '.js' + '.plugin.addimage.js').text + \
18
        (src - '.js' + '.plugin.from_html.js').text + \
19
        (src - '.js' + '.plugin.sillysvgrenderer.js').text + \
20
        (src - '.js' + '.plugin.split_text_to_size.js').text + \
21
        (src - '.js' + '.plugin.standard_fonts_metrics.js').text + \
22
        (src - 'jspdf.js' + 'libs/Blob.js/BlobBuilder.js').text + \
23
        (src - 'jspdf.js' + 'libs/FileSaver.js/FileSaver.js').text + \
24
        (src - 'jspdf.js' + 'libs/Deflate/deflate.js').text + \
25
        (src - 'jspdf.js' + 'libs/Deflate/adler32cs.js').text
26
        # (src - '.js' + '.plugin.from_html.js').text + \
27
        #
28
 
29
 
30
    minified = dst - '.source.js' + '.min.js'
31
 
32
    print("=== Compressing jsPDF and select plugins into " + minified.name)
33
    minified.text = compress_with_closure_compiler( dst.text )
34
 
35
    # AMD-compatible version:
36
    (minified - '.min.js' + '.amd.min.js').text = """;(function(){
37
%s
38
;define(function(){return jsPDF})})();
39
""" % minified.text
40
 
41
    # jQuery "NoConflict" version:
42
    # only needed if some of the modules compiled into jsPDF need $
43
    # one such module is fromHTML
44
#     (minified - '.min.js' + '.noconflict.min.js').text = """;(function($){
45
# %s
46
# })(jQuery);
47
# """ % minified.text
48
 
49
def docs(context):
50
	'''
51
	java -jar %jsdocbindir%\jsrun.jar %jsdocbindir%\app\run.js -v %rootdir%\jspdf.js -d=%rootdir%\doc -t=%rootdir%\tools\jsdoc_template
52
	'''
53
 
54
	jsdocBinDir = context.Node('~/bin/jsdoc-toolkit/')
55
	codefile = context.Node('jspdf.js')
56
	destinationFolder = context.Node('doc/')
57
	templateFolder = context.Node('tools/jsdoc_template/')
58
 
59
	import subprocess
60
	subprocess.call(
61
		[
62
			'java'
63
			, '-jar'
64
			, (jsdocBinDir + 'jsrun.jar').absolutepath
65
			, (jsdocBinDir + 'app/run.js').absolutepath
66
			, '-v'
67
			, codefile.absolutepath
68
			, '-d='+destinationFolder.absolutepath
69
			, '-t='+templateFolder.absolutepath
70
		]
71
	)
72
 
73
def timeUTC():
74
    import datetime
75
    return datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M")
76
 
77
def getCommitIDstring():
78
    import subprocess
79
 
80
    if not hasattr( subprocess, "check_output"):
81
        # let's not bother emulating it. Not important
82
        return ""
83
    else:
84
        return "commit ID " + subprocess.check_output(
85
            [
86
                'git'
87
                , 'rev-parse'
88
                , 'HEAD'
89
            ]
90
        ).strip()
91
 
92
def compress_with_closure_compiler(code, compression_level = None):
93
    '''Sends text of JavaScript code to Google's Closure Compiler API
94
    Returns text of compressed code.
95
    '''
96
    # script (with some modifications) from
97
    # https://developers.google.com/closure/compiler/docs/api-tutorial1
98
 
99
    import httplib, urllib, sys
100
 
101
    compression_levels = [
102
        'WHITESPACE_ONLY'
103
        , 'SIMPLE_OPTIMIZATIONS'
104
        , 'ADVANCED_OPTIMIZATIONS'
105
    ]
106
 
107
    if compression_level not in compression_levels:
108
        compression_level = compression_levels[1] # simple optimizations
109
 
110
    # Define the parameters for the POST request and encode them in
111
    # a URL-safe format.
112
    params = urllib.urlencode([
113
        ('js_code', code)
114
        , ('compilation_level', compression_level)
115
        , ('output_format', 'json')
116
        , ('output_info', 'compiled_code')
117
        , ('output_info', 'warnings')
118
        , ('output_info', 'errors')
119
        , ('output_info', 'statistics')
120
        # , ('output_file_name', 'default.js')
121
        # , ('js_externs', 'javascript with externs') # only used on Advanced.
122
      ])
123
 
124
    # Always use the following value for the Content-type header.
125
    headers = { "Content-type": "application/x-www-form-urlencoded" }
126
    conn = httplib.HTTPConnection('closure-compiler.appspot.com')
127
    conn.request('POST', '/compile', params, headers)
128
    response = conn.getresponse()
129
 
130
    if response.status != 200:
131
        raise Exception("Compilation server responded with non-OK status of " + str(response.status))
132
 
133
    compressedcode = response.read()
134
    conn.close()
135
 
136
    import json # needs python 2.6+ or simplejson module for earlier
137
    parts = json.loads(compressedcode)
138
 
139
    if 'errors' in parts:
140
        prettyerrors = ['\nCompilation Error:']
141
        for error in parts['errors']:
142
            prettyerrors.append(
143
                "\nln %s, ch %s, '%s' - %s" % (
144
                    error['lineno']
145
                    , error['charno']
146
                    , error['line']
147
                    , error['error']
148
                )
149
            )
150
        raise Exception(''.join(prettyerrors))
151
 
152
    return parts['compiledCode']
153
 
154
if __name__ == '__main__':
155
    print("This is a Wak build automation tool script. Please, get Wak on GitHub and run it against the folder containing this automation script.")