-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwscript
147 lines (120 loc) · 4.63 KB
/
wscript
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
import configparser
from collections import OrderedDict
from waflib import Task
from waflib.TaskGen import feature, after_method
top = '.'
out = 'build'
required_configs = [
'name',
'description',
'language',
'layout',
'fallback_imes',
'options_page',
]
def configure(ctx):
ctx.find_program('jscodeshift')
def build(ctx):
config = _read_config()
for name in config:
_build_ime(ctx, name, config[name])
def _read_config():
parser = configparser.ConfigParser()
parser.read('config.ini')
config = OrderedDict()
for name in parser.sections():
spec = {}
for prop in required_configs:
# TODO: more human-friendly error
spec[prop] = parser[name][prop]
spec['fallback_imes'] = [ime_name.strip()
for ime_name in spec['fallback_imes'].split(',')
if len(ime_name.strip()) > 0]
config[name] = spec
return config
def _build_ime(ctx, name, spec):
out = ctx.bldnode.find_or_declare(name)
# copy / transform sources
imes_root = ctx.path.find_dir('imes')
transformer = ctx.path.find_node('hijack.js')
# remapper must be the first in stack in order for hijacking to work.
ime_stack = ['remapper'] + spec['fallback_imes']
for ime_name in ime_stack:
extension_root = imes_root.find_dir(ime_name)
for extension_file in extension_root.ant_glob('**/*'):
is_javascript = extension_file.suffix() == '.js'
source = extension_file
target = out.find_or_declare(extension_file.path_from(imes_root))
if is_javascript:
jscodeshift_env = ctx.env.derive()
jscodeshift_env.TRANSFORMER = transformer.abspath()
jscodeshift_task = jscodeshift(env=jscodeshift_env)
jscodeshift_task.set_inputs(source)
jscodeshift_task.set_outputs(target)
ctx.add_to_group(jscodeshift_task)
ctx.add_manual_dependency(source, transformer)
else:
ctx(features='subst',
is_copy=True,
source=source,
target=target)
# build manifest
manifests = [out.find_or_declare(ime_name).find_or_declare('manifest.json')
for ime_name in ime_stack]
manifest_env = ctx.env.derive()
manifest_env.identifier = name
manifest_env.name = spec['name']
manifest_env.description = spec['description']
manifest_env.language = spec['language']
manifest_env.layout = spec['layout']
manifest_env.options_page = spec['options_page']
manifest_task = manifest(env=manifest_env)
manifest_task.set_inputs(manifests)
manifest_task.set_outputs(out.find_or_declare('manifest.json'))
ctx.add_to_group(manifest_task)
class jscodeshift(Task.Task):
run_str = 'node ${TRANSFORMER} ${SRC} ${TGT}'
class manifest(Task.Task):
'''
Builds a manifest JSON by collecting background scripts and permissions
from input files and reading the name, description, language, and layout
from `self.env`.
'''
def run(self):
target = self.outputs[0]
out = target.parent
submanifests = OrderedDict([(path, path.read_json()) for path in self.inputs])
scripts = [path.parent.find_or_declare(script).path_from(out)
for path, submanifest in submanifests.items()
for script in submanifest['background']['scripts']]
permissions = [permission
for path, submanifest in submanifests.items()
for permission in submanifest['permissions']]
input_component = {
"name": self.env.name,
"type": "ime",
"id": "io.github.ento.cros_key_remapper." + self.env.identifier,
"description": self.env.description,
"language": self.env.language,
"layouts": [self.env.layout],
}
manifest = {
"name": self.env.name,
"version": "1.0",
"manifest_version": 2,
"description": self.env.description,
"background": {
"scripts": scripts,
},
"permissions": list(set(permissions)),
"input_components": [
input_component
]
}
if self.env.options_page:
manifest['options_page'] = self.env.options_page
target.write_json(manifest)
def imes(ctx):
imes_root = ctx.path.find_dir('imes')
for extension_root in imes.ant_glob('*', src=False, dir=True):
print(extension_root.path_from(imes_root))