forked from billdawson/ti_coffee_plugin
/
plugin.py
162 lines (136 loc) · 4.85 KB
/
plugin.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
"""
Copyright 2011 William Dawson
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
---------------------------------------------------------------------------
ti_coffee_plugin/plugin.py
A simple Titanium project build plugin that will scan your Resources folder
for any .coffee files and invoke "coffee -c" on them, producing .js files with
the same base name.
See README.md for a longer description.
"""
import os, sys, subprocess, hashlib
try:
import json
except:
import simplejson as json
# The Titanium build scripts contain their own json library (Patrick Logan's),
# so we have to figure out which json functions to use.
json_read = None
json_write = None
if hasattr(json, 'loads'):
json_read = json.loads
else:
json_read = json.read
if hasattr(json, 'dumps'):
json_write = json.dumps
else:
json_write = json.write
HASHES_FILE = 'coffee_file_hashes.json'
ERROR_LOG_PREFIX = '[ERROR]'
INFO_LOG_PREFIX = '[INFO]'
DEBUG_LOG_PREIX = '[DEBUG]'
def log(prefix, msg, stream=None):
if not stream is None:
print >> stream, "%s %s" % (prefix, msg)
else:
print "%s %s" % (prefix, msg)
def err(msg, stream=None):
# Matches the [ERROR]... messages of the Titanium builder.py, so the
# message can be recognized as an error for console purposes
log(ERROR_LOG_PREFIX, msg, stream)
def info(msg):
# Matches the [INFO]... messages of the Titanium builder.py, so the
# message can be recognized as an info msgs for console purposes
log(INFO_LOG_PREFIX, msg)
def debug(msg):
# Matches the [DEBUG]... messages of the Titanium builder.py, so the
# message can be recognized as an debug msgs for console purposes
log(DEBUG_LOG_PREIX, msg)
def read_file_hashes(path):
hashes_file = os.path.join(path, HASHES_FILE)
hashes = {}
if os.path.exists(hashes_file):
f = open(hashes_file, 'r')
text = f.read()
f.close()
if len(text):
hashes = json_read(text)
return hashes
def write_file_hashes(path, hashes):
hashes_file = os.path.join(path, HASHES_FILE)
text = json_write(hashes)
f = open(hashes_file, 'w')
f.write(text)
f.close()
def get_md5_digest(path):
f = open(path, 'r')
contents = f.read()
f.close()
return hashlib.md5(contents).hexdigest()
def build_coffee(path, targetpath):
debug('Compiling %s' % path)
command_args = ['coffee', '-b', '-c', '-o', targetpath, path]
if not os.path.exists(targetpath):
os.makedirs(targetpath)
process = subprocess.Popen(command_args, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
result = process.wait()
if result != 0:
msg = process.stderr.read()
if msg:
err("%s (%s)" % (msg, path))
else:
err("CoffeeScript compiler call for %s failed but no error message was generated" % path)
return False
return True
def build_all_coffee(basepath, coffeepath, resoursepath, file_hash_folder):
info_msg_shown = False
file_hashes = read_file_hashes(file_hash_folder)
for root, dirs, files in os.walk(os.path.join(basepath, coffeepath)):
for name in files:
if name.endswith('.coffee'):
if not info_msg_shown:
info("Compiling CoffeeScript files")
info("\t%s" % (name,))
info_msg_shown = True
else:
info("\t%s" % (name,))
file_path = os.path.join(root, name)
target_path = root.replace(coffeepath, resoursepath, 1)
digest = get_md5_digest(file_path)
if (not os.path.exists(os.path.join(target_path, name)) or
not file_path in file_hashes) or (
file_hashes[file_path] != digest):
if build_coffee(file_path, target_path):
file_hashes[file_path] = digest
write_file_hashes(file_hash_folder, file_hashes)
def compile(config, file_hash_folder=None):
if file_hash_folder is None:
file_hash_folder = os.path.abspath(os.path.join(config['build_dir'], '..'))
build_all_coffee(config['project_dir'], 'CoffeeSources', 'Resources', file_hash_folder)
if __name__ == "__main__":
proj_dir = None
if len(sys.argv) < 2:
proj_dir = os.getcwd()
else:
proj_dir = sys.argv[1]
resource_dir = os.path.join(proj_dir, 'Resources')
coffeesource_dir = os.path.join(proj_dir, 'CoffeeSources')
if os.path.exists(coffeesource_dir):
if not os.path.exists(resource_dir):
err("%s does not look like Titanium project folder. Resources/ folder not found." % proj_dir, sys.stderr)
config = {'project_dir': proj_dir}
if os.path.exists(os.path.join(proj_dir, 'build')):
compile(config, os.path.join(proj_dir, 'build'))
else:
compile(config, proj_dir)
else:
print "%s does not exist. Skipping compile."