-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbuild.py
150 lines (111 loc) · 4.65 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
150
import json
import os
import plistlib
import re
import sys
from dataclasses import dataclass
from typing import Optional
@dataclass
class Product:
keyword: str
uid: str
folder_name: str
bundle_id: str
display_name: Optional[str] = None
preferences_path: Optional[str] = None
def name(self) -> str:
return self.display_name if self.display_name else self.folder_name
def create_connection(destination_uid: str) -> list[dict]:
return [{'destinationuid': destination_uid,
'modifiers': 0,
'modifiersubtext': '',
'vitoclose': False}]
def create_script_filter(product: Product) -> dict:
return {
'config': {'alfredfiltersresults': False,
'alfredfiltersresultsmatchmode': 0,
'argumenttreatemptyqueryasnil': False,
'argumenttrimmode': 0,
'argumenttype': 1,
'escaping': 102,
'keyword': f'{{var:{product.keyword}}}',
'queuedelaycustom': 3,
'queuedelayimmediatelyinitially': True,
'queuedelaymode': 0,
'queuemode': 1,
'runningsubtext': '',
'script': f'/usr/bin/python3 recent_projects.py "{product.keyword}" "${{1}}"',
'scriptargtype': 1,
'scriptfile': '',
'subtext': '',
'skipuniversalaction': True,
'title': f'Search through your recent {product.name()} projects',
'type': 0,
'withspace': True},
'type': 'alfred.workflow.input.scriptfilter',
'uid': product.uid,
'version': 3}
def create_userconfigurationconfig(product: Product) -> dict:
return {'config': {'default': '',
'placeholder': product.keyword,
'required': False,
'trim': True},
'description': f'Assign a keyword to enable {product.name()}',
'label': f'{product.name()} keyword',
'type': 'textfield',
'variable': product.keyword}
def create_coordinates(xpos: int, ypos: int) -> dict[str, int]:
return {'xpos': xpos, 'ypos': ypos}
def get_run_script_uid(plist) -> str:
for obj in plist["objects"]:
if obj["config"]["script"] == 'open -nb "${bundle_id}" --args "${1}"' and obj["uid"] is not None:
return obj["uid"]
raise ValueError(
f'Could not find the script object with \'open -nb "${{bundle_id}}" --args "${{1}}"\' as the script in the template')
def create_coordinate_ruler(size: int) -> list[int]:
start = 40
step = 120
return list(range(start, start + (step * size), step))
def build():
# Collect info
products = get_products()
with open('alfred/template.plist', 'rb') as fp:
plist = plistlib.load(fp)
version = sys.argv[1] if len(sys.argv) > 1 else "unknown"
version = re.sub(r'v(.*)', r'\1', version)
# Modify plist
# Get the UID of the runscript action in the template
run_script_uid = get_run_script_uid(plist)
run_script_connection = create_connection(run_script_uid)
plist["connections"].update({product.uid: run_script_connection for product in products})
y_coordinate_ruler = create_coordinate_ruler(len(products))
plist["uidata"].update(
{product.uid: create_coordinates(30, coord) for product, coord in zip(products, y_coordinate_ruler)})
plist["uidata"][run_script_uid]["ypos"] = sum(y_coordinate_ruler) / len(y_coordinate_ruler)
plist["objects"].extend([create_script_filter(product) for product in products])
plist["userconfigurationconfig"].extend([create_userconfigurationconfig(product) for product in products])
plist["version"] = version
with open(".readme/embedded-readme.md", 'r', encoding='utf-8') as file:
plist["readme"] = file.read()
# Output
print(f"Building {[product.name() for product in products]}")
os.system(f'mkdir -p out')
with open('out/info.plist', 'wb') as fp:
plistlib.dump(plist, fp)
for product in products:
os.system(f'cp icons/{product.keyword}.png ./out/{product.uid}.png')
os.system(
f'zip -j -r alfred-jetbrains-projects.alfredworkflow out/* recent_projects.py products.json icon.png')
def get_products() -> list[Product]:
with open('products.json', 'r') as outfile:
js = json.load(outfile)
products = [Product(k, **v) for k, v in js.items()]
return products
def clean():
os.system("rm out/*")
os.system("rm *.alfredworkflow")
def main():
clean()
build()
if __name__ == '__main__':
main()