Compile all Closure CSS files into a generated file to cut down on HTTP requests.
[armadillo.git] / build.py
1 #!/usr/bin/env python2.5
2 #
3 # Armadillo File Manager
4 # Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
5 #
6 # This program is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free Software
8 # Foundation, either version 3 of the License, or any later version.
9 #
10 import optparse
11 import os
12 import re
13 import shutil
14 import string
15 import subprocess
16 import sys
17 import time
18
19 ROOT = os.path.dirname(os.path.realpath(__file__))
20 SRC_PATH = os.path.join(ROOT, 'src')
21 PROD_PATH = os.path.join(ROOT, 'out')
22 FE_PATH = os.path.join(ROOT, 'web_frontend')
23
24 CLOSURE_SVN = 'http://closure-library.googlecode.com/svn/trunk/'
25 CLOSURE_REV = '235'
26 CLOSURE_DEST = os.path.join(ROOT, 'closure')
27 CLOSURE_COMPILER = os.path.join(ROOT, 'closure-compiler.jar')
28 CLOSURE_CALCDEPS = os.path.join(CLOSURE_DEST, 'closure', 'bin', 'calcdeps.py')
29
30 VERSION_FILE = os.path.join(FE_PATH, 'version.js.proto')
31
32 SOURCES = [
33 'paths.go',
34 'server.go',
35 'main.go'
36 ]
37 SOURCES_FE = [
38 'version.js',
39 'path_control.js',
40 'actor.js',
41 'file.js',
42 'main.js',
43 ]
44 RESOURCES_FE = [
45 'index.html',
46 'screen.css',
47 'reset.css'
48 ]
49 RESOURCES_CLOSURE = [
50 'common.css',
51 'dialog.css',
52 'menu.css',
53 'menuitem.css',
54 'menubutton.css',
55 ]
56 PRODUCT_NAME = 'armadillo'
57
58 COMPILER = '8g'
59 LINKER = '8l'
60 O_EXTENSION = '8'
61
62 def _ObjFileName(gofile):
63 gofile = os.path.basename(gofile)
64 return os.path.join(PROD_PATH, os.path.splitext(gofile)[0] + '.' + O_EXTENSION)
65
66 def _PullDeps():
67 print '=== Pulling Dependencies ==='
68 if os.path.exists(CLOSURE_DEST):
69 handle = subprocess.Popen([ 'svn', 'info', CLOSURE_DEST ], stdout = subprocess.PIPE)
70 handle.wait()
71 for line in handle.stdout:
72 if line.startswith('Revision'):
73 if not line.startswith('Revision: ' + CLOSURE_REV):
74 subprocess.Popen([ 'svn', 'update', '-r', CLOSURE_REV, CLOSURE_DEST ]).wait()
75 else:
76 print ' Closure @ ' + CLOSURE_REV
77 else:
78 subprocess.Popen([ 'svn', 'checkout', '-r', CLOSURE_REV, CLOSURE_SVN, CLOSURE_DEST ]).wait()
79
80 def Main():
81 parser = optparse.OptionParser()
82 parser.add_option('-c', '--closure_fe', action="store_true", dest="compile_fe",
83 help="Run the Front End inputs through the Closure Compiler")
84 (options, args) = parser.parse_args()
85
86 print '=== Starting Build ==='
87 os.chdir(PROD_PATH)
88
89 # Compile.
90 for gofile in SOURCES:
91 gofile = os.path.join(SRC_PATH, gofile)
92 args = [ COMPILER, gofile ]
93 print ' ' + ' '.join(args)
94 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
95 handle.wait()
96
97 # Link
98 objects = map(_ObjFileName, SOURCES)
99 args = [ LINKER, '-o', os.path.join(PROD_PATH, PRODUCT_NAME), 'main.8' ]
100 print ' ' + ' ' .join(args)
101 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
102 handle.wait()
103
104 _PullDeps()
105
106 # Copy
107 print '=== Copying Resources ==='
108 fe_resources = os.path.join(PROD_PATH, 'fe')
109 subprocess.Popen([ 'rm', '-rf', fe_resources ]).wait()
110 os.mkdir(fe_resources)
111 for resource in RESOURCES_FE:
112 print ' COPY ' + resource
113 shutil.copy(os.path.join(FE_PATH, resource), fe_resources)
114 fd = open(os.path.join(fe_resources, 'closure.css'), 'w+')
115 fd.write('/*=== Generated Resources for Closure Library ===*/')
116 for resource in RESOURCES_CLOSURE:
117 print ' COPY closure/' + resource
118 respath = os.path.join(CLOSURE_DEST, 'closure', 'goog', 'css', resource)
119 ofd = open(respath, 'r')
120 fd.write('\n\n/*=== File: ' + respath.replace(ROOT, '/') + ' ===*/\n')
121 fd.writelines(ofd.readlines())
122 ofd.close()
123 fd.close()
124
125 # Version
126 print '=== Version Stamp ==='
127 if os.path.exists(VERSION_FILE):
128 gitcrement = subprocess.Popen([ 'gitcrement', 'next' ], stdout = subprocess.PIPE, cwd = ROOT)
129 gitcrement.wait()
130 build_stamp = gitcrement.stdout.read().strip()
131 time_stamp = str(int(time.time()))
132
133 fd = open(VERSION_FILE, 'a+')
134 fd.seek(0)
135 lines = fd.readlines()
136 fd.seek(0)
137 fd.truncate()
138 for line in lines:
139 line = re.sub(r'(BUILD =) ([0-9]+)', r'\1 ' + build_stamp, line)
140 line = re.sub(r'(STAMP =) ([0-9]+)', r'\1 ' + time_stamp, line)
141 fd.write(line)
142 fd.close()
143 print ' BUILD ' + build_stamp + ' @ ' + time_stamp
144 if options.compile_fe:
145 mfiles = subprocess.Popen([ 'git', 'ls-files', '-m' ], stdout = subprocess.PIPE, cwd = ROOT)
146 mfiles.wait()
147 versioned_stamp_file = string.replace(VERSION_FILE, '.proto', '')
148 shutil.copy(VERSION_FILE, versioned_stamp_file)
149 print ' COPY version.js.proto -> version.js'
150 if not len(mfiles.stdout.readlines()):
151 subprocess.Popen([ 'git', 'commit', '--author=Armadillo Build Script <armadillo@bluestatic.org>',
152 '-m', 'Stamp version.js @ ' + build_stamp + '.', versioned_stamp_file ], stdout = sys.stdout,
153 stderr = sys.stderr).wait()
154
155 # Compile JS.
156 print '=== Compiling Front End ==='
157 outfile = os.path.join(PROD_PATH, 'fe', PRODUCT_NAME + '.js')
158 fe_sources = map(lambda f: '-i' + os.path.join(FE_PATH, f), SOURCES_FE)
159 closure_sources = os.path.join(CLOSURE_DEST, 'closure', 'goog')
160 args = [ CLOSURE_CALCDEPS ]
161 args.extend(fe_sources)
162 output = "script"
163 if options.compile_fe:
164 output = "compiled"
165 args.extend([ '-p', closure_sources, '-o', output, '-c', CLOSURE_COMPILER,
166 '--output_file', outfile ])
167 print ' ' + ' '.join(args)
168 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
169 handle.wait()
170
171 if __name__ == '__main__':
172 Main()