forked from photofroggy/wsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fudge
executable file
·120 lines (93 loc) · 3.14 KB
/
fudge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python
''' fudge packer
Created by photofroggy.
This script is designed to package javascript projects into their
distributable form using information from a configuration file.
Also capable of using sass to compile CSS files.
Uses YUI Compressor to minify scripts, so can minify Javascript and CSS.
May add the ability to create executables for Python applications, but that
may be largely dependent on platform.
'''
import os
import sys
import json
import os.path
import subprocess
from nomming import nom
def notice(msg):
sys.stdout.write('>> {0}\n'.format(msg))
sys.stdout.flush()
def nnotice(name):
notice(' {0}...'.format(os.path.basename(name)))
def package():
'''
Create a package based on the configuration data in packaging.json.
All this really does is create a load of files in the dist folder.
'''
conf = json.loads(fetch('packaging.json'))
notice('Cleaning {0}...'.format(conf['clean']))
subprocess.call(['rm', '-f', os.path.join(conf['clean'], '*.js')])
subprocess.call(['rm', '-f', os.path.join(conf['clean'], '*.css')])
yui = ['java'] + conf['yuic']
notice('Packaging items...')
for package in conf['packs']:
nnotice(package['dest'])
bundle(package)
nom(package['dest'])
subprocess.call(yui + ['-o', '{0}'.format(package['mini']), package['dest']])
notice('Running post scripts...')
if 'post' in conf:
if isinstance(conf['post'], list):
for scr in conf['post']:
try:
subprocess.call([scr])
except Exception as e:
notice(' Failed running {0}...'.format(scr))
notice( e )
else:
try:
subprocess.call([conf['post'] + ''])
except Exception as e:
notice(' Failed running {0}...'.format(scr))
notice( e )
notice('Done!')
def bundle(package):
'''
Combine files in a given package into a single file.
'''
if package.get('sass', False):
subprocess.call(['sass'] + package.get('options', []) + ['{0}:{1}'.format(package['files'], package['dest'])])
else:
put_raw(package['dest'], fetch_bundle(package['files']))
def fetch_bundle(files):
'''
Aggregate the contents of several files into one string.
'''
if isinstance(files, list):
out = ''
for file in files:
try:
out+= fetch(file)
except IOError:
notice(' failed for {0}...'.format(file))
return out
else:
try:
return fetch_bundle([os.path.join(files, f) for f in sorted(os.listdir(files))])
except TypeError:
notice(' failed for {0}...'.format(files))
return ''
def fetch(file):
'''
Fetch the contents of a single file.
'''
with open(file, 'r') as f:
return f.read()
def put_raw(file, contents):
'''
Save a file, wooo!
'''
with open(file, 'w') as f:
f.write(contents)
if __name__ == '__main__':
package()