forked from mr-pennyworth/alfred-better-dictionaries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkworkflow.py
113 lines (88 loc) · 2.43 KB
/
mkworkflow.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
# -*- coding: utf-8 -*-
import os
import plistlib
import shutil
import subprocess
from contextlib import contextmanager
BUILD_DIR = 'wfbuild'
WF_FILES = [
'AlfredExtraPane.app',
'BetterDict.app',
'alfred-dict-server',
'icon.png',
'info.plist',
'jq',
'README.md',
'sayipa.sh',
'search.sh',
'setup.sh',
]
def copy(filenames, dest_folder):
if os.path.exists(dest_folder):
shutil.rmtree(dest_folder)
os.makedirs(dest_folder)
for filename in filenames:
if os.path.isdir(filename):
shutil.copytree(filename, f'{dest_folder}/{filename}')
else:
shutil.copy(filename, f'{dest_folder}/{filename}')
def plistRead(path):
with open(path, 'rb') as f:
return plistlib.load(f)
def plistWrite(obj, path):
with open(path, 'wb') as f:
return plistlib.dump(obj, f)
@contextmanager
def cwd(dir):
old_wd = os.path.abspath(os.curdir)
os.chdir(dir)
yield
os.chdir(old_wd)
def make_export_ready(plist_path):
wf = plistRead(plist_path)
# remove noexport vars
wf['variablesdontexport'] = []
# remove noexport objects
noexport_uids = [
uid
for uid, data
in wf['uidata'].items()
if 'noexport' in data
]
# direct forward references
for noexport_uid in noexport_uids:
del wf['connections'][noexport_uid]
del wf['uidata'][noexport_uid]
# actual objects
new_objs = []
for obj in wf['objects']:
if obj['uid'] not in noexport_uids:
new_objs.append(obj)
wf['objects'] = new_objs
# backward references
new_connections = {}
for src_uid, conn_infos in wf['connections'].items():
new_conn_infos = []
for conn_info in conn_infos:
if not conn_info['destinationuid'] in noexport_uids:
new_conn_infos.append(conn_info)
new_connections[src_uid] = new_conn_infos
wf['connections'] = new_connections
#remove noexport router outputs
for uid, data in wf['uidata'].items():
if data.get('note') == 'router':
for obj in wf['objects']:
if obj['uid'] == uid:
router = obj
router['config']['conditions'] = router['config']['conditions'][:1]
# add readme
with open('README.md') as f:
wf['readme'] = f.read()
plistWrite(wf, plist_path)
return wf['name']
if __name__ == '__main__':
subprocess.call(['./mkapp.sh'])
copy(WF_FILES, BUILD_DIR)
wf_name = make_export_ready(f'{BUILD_DIR}/info.plist')
with cwd(BUILD_DIR):
subprocess.call(['zip', '-r', f'../{wf_name}.alfredworkflow'] + WF_FILES)