This repository has been archived by the owner on Sep 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.py
149 lines (130 loc) · 5.33 KB
/
build.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
# CodeMirror, copyright (c) by Marijn Haverbeke and others
# Distributed under an MIT license: https://codemirror.net/LICENSE
# Author: Gk0Wk ([email protected])
# Licence: MIT
"""
Build TiddlyWiki Plugin to JSON & NodeJS format with build.json
"""
import re
import os
import json
import shutil
SRC_DIR = 'src'
DIST_DIR = 'dist'
BUILD_DIR = 'dist/build'
PLUGIN_INFO_KEY = 'plugin.info'
def gen_plugin_name(build_info):
"""Generate plugin output name
:param build_info: JSON object of build.json
:type build_info: object
:return: Output name of plugin
:rtype: str
"""
return re.sub('[^A-Za-z0-9_-]+', '-',
build_info[PLUGIN_INFO_KEY]['title'].split(
'/')[-1]) + '-' + build_info[PLUGIN_INFO_KEY]['version']
def minify_source_code(build_info):
"""Minify CSS and JS file and update build_info
:param build_info: JSON object of build.json
:type build_info: object
:return: str
:rtype: string of updated build.json
"""
for tiddler in build_info['tiddlers']:
if 'src' not in tiddler:
continue
snamepath, type_ = os.path.splitext(tiddler['src'])
spath, sname = os.path.split(snamepath)
type_ = type_.lower()
if type_ == '.js':
new_spath = os.path.join('..', BUILD_DIR, spath)
new_src = os.path.join(new_spath, sname + '.min.js')
src = os.path.join(SRC_DIR, tiddler['src'])
dist = os.path.join(SRC_DIR, new_src)
os.makedirs(os.path.join(SRC_DIR, new_spath), exist_ok=True)
os.system(
f'uglifyjs {src} -c -m --v8 --webkit --ie --output {dist}')
tiddler['src'] = new_src
elif type_ == '.css':
new_spath = os.path.join('..', BUILD_DIR, spath)
new_src = os.path.join(new_spath, sname + '.min.css')
src = os.path.join(SRC_DIR, tiddler['src'])
dist = os.path.join(SRC_DIR, new_src)
os.makedirs(os.path.join(SRC_DIR, new_spath), exist_ok=True)
os.system(f'cleancss {src} -o {dist}')
tiddler['src'] = new_src
else:
continue
return json.dumps(build_info)
def build_json_plugin(build_info):
"""Build JSON format TiddlyWiki5 plugin
:param build_info: JSON object of build.json
:type build_info: object
"""
tiddlers = {}
for tiddler_ in build_info['tiddlers']:
tiddler = tiddler_['fields']
if 'src' in tiddler_:
file_path = os.path.join(SRC_DIR, tiddler_['src'])
if os.path.exists(file_path) and os.path.isfile(file_path):
with open(file_path, 'r') as ffp:
tiddler['text'] = ffp.read()
else:
tiddler['text'] = ''
else:
tiddler['text'] = tiddler_['text'] if 'text' in tiddler_ else ''
tiddlers[tiddler['title']] = tiddler
plugin = build_info[PLUGIN_INFO_KEY]
plugin['text'] = json.dumps({'tiddlers': tiddlers})
os.makedirs(DIST_DIR, exist_ok=True)
with open(os.path.join(DIST_DIR,
gen_plugin_name(build_info) + '.json'), 'w') as ffp:
ffp.write(json.dumps([plugin]))
def build_node_plugin(build_info):
"""Build NodeJS format TiddlyWiki5 plugin (compressed in zip file)
:param build_info: JSON object of build.json
:type build_info: object
"""
NODE_DIST = os.path.join(
DIST_DIR, 'node',
re.sub(
'[^A-Za-z0-9_-]+',
'-',
build_info[PLUGIN_INFO_KEY]['title'].split('/')[-2],
), build_info[PLUGIN_INFO_KEY]['title'].split('/')[-1])
os.makedirs(NODE_DIST, exist_ok=True)
with open(os.path.join(NODE_DIST, 'plugin.info'), 'w') as ffp:
ffp.write(json.dumps(build_info[PLUGIN_INFO_KEY], indent=4))
for tiddler in build_info['tiddlers']:
sub_dist_path = tiddler['dist'] if 'dist' in tiddler else tiddler[
'fields']['title'].split('/', 4)[-1]
dist_path = os.path.join(NODE_DIST, sub_dist_path)
os.makedirs(os.path.split(dist_path)[0], exist_ok=True)
if 'src' in tiddler:
src_path = os.path.join(SRC_DIR, tiddler['src'])
if not os.path.exists(src_path) or not os.path.isfile(src_path):
with open(dist_path, 'w') as ffp:
ffp.write('')
shutil.copyfile(src_path, dist_path)
del tiddler['src']
else:
with open(dist_path, 'w') as ffp:
ffp.write(tiddler['text'] if 'text' in tiddler else '')
if 'text' in tiddler:
del tiddler['text']
if 'dist' in tiddler:
del tiddler['dist']
tiddler['file'] = sub_dist_path
with open(os.path.join(NODE_DIST, 'tiddlywiki.files'), 'w') as ffp:
ffp.write(json.dumps({'tiddlers': build_info['tiddlers']}, indent=4))
shutil.make_archive(os.path.join(DIST_DIR, gen_plugin_name(build_info)),
format="zip",
root_dir=os.path.join(DIST_DIR, 'node'))
shutil.rmtree(os.path.join(DIST_DIR, 'node'))
if __name__ == '__main__':
with open('build.json', 'r') as fp:
build_info_str = fp.read()
build_info_str = minify_source_code(json.loads(build_info_str))
build_json_plugin(json.loads(build_info_str))
build_node_plugin(json.loads(build_info_str))
shutil.rmtree(BUILD_DIR)