forked from rohanb10/snoozz-tab-snoozing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
140 lines (121 loc) · 4.95 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
# usage
# python3 build.py
# local dependencies (npm) - uglifyjs, csso
import json
import shutil
from re import search
import os
from shell import ex
FOLDER = 'build_temp'
with open('manifest.json') as m:
data = json.load(m)
VERSION = data['version']
print('\n\nBuilding Snoozz \x1b[1;31;34mv' + VERSION + '\x1b[0m\n');
#
# delete old files if they exist
#
old = [FOLDER, 'snoozz-chrome-' + VERSION, 'snoozz-ff-' + VERSION, 'snoozz-' + VERSION, 'snoozz-safari-' + VERSION]
for file in old:
if os.path.exists(file):
shutil.rmtree(file)
oldzip = ['snoozz-chrome-' + VERSION + '.zip', 'snoozz-ff-' + VERSION + '.zip', 'snoozz-' + VERSION + '.zip', 'snoozz-safari-' + VERSION + '.zip']
for zippy in oldzip:
if (os.path.exists(zippy)) :
os.remove(zippy)
#
# Copy essential files only
#
shitfiles = shutil.ignore_patterns('.DS_Store', '.git', '.Trashes', '.Spotlight-V100', '.github')
shutil.copytree('html', FOLDER + '/html', ignore = shitfiles)
# shutil.copytree('scripts', FOLDER + '/scripts', ignore = shitfiles)
shutil.copytree('icons', FOLDER + '/icons', dirs_exist_ok=True, ignore = shitfiles)
shutil.copytree('sounds', FOLDER + '/sounds', dirs_exist_ok=True, ignore = shitfiles)
#
# Minify Files (JS + CSS)
#
def minifyFilesInDirectory(directory, ext, url):
sameSize = '{:<16}'
os.mkdir(FOLDER + '/' + directory)
for root, dirs, files in os.walk(directory):
for name in files:
print('\n⧖ Minifying ' + '\x1b[1;32;33m' + sameSize.format(name) + '\x1b[0m ...', end='')
chars = len(ext)
if name.endswith('.min' + ext):
shutil.copyfile(os.path.join(root, name), FOLDER + '/' + directory + '/' + name)
print('\r✓ Copied ' + '\x1b[1;32;33m' + sameSize.format(name) + '\x1b[0m -> \x1b[1;32;32m' + name[:-chars] + '.min' + ext + '\x1b[0m', end='', flush=True)
elif ext == '.js' and not name.endswith('.min' + ext):
ex('uglifyjs ' + directory + '/' + name + ' -c -m -o ' + FOLDER + '/' + directory + '/' + name[:-chars] + '.min' + ext).stdout()
replaceInHTMLFiles(name, name[:-chars] + '.min' + ext)
replaceInManfest(name, name[:-chars] + '.min' + ext)
print('\r✓ Minified ' + '\x1b[1;32;33m' + sameSize.format(name) + '\x1b[0m -> \x1b[1;32;32m' + name[:-chars] + '.min' + ext + '\x1b[0m', end='', flush=True)
elif ext == '.css' and not name.endswith('.min' + ext):
ex('csso ' + directory + '/' + name + ' -o ' + FOLDER + '/' + directory + '/' + name[:-chars] + '.min' + ext).stdout()
replaceInHTMLFiles(name, name[:-chars] + '.min' + ext)
print('\r✓ Minified ' + '\x1b[1;32;33m' + sameSize.format(name) + '\x1b[0m -> \x1b[1;32;32m' + name[:-chars] + '.min' + ext + '\x1b[0m', end='', flush=True)
def replaceInHTMLFiles(original, replacement):
for root, dirs, files in os.walk(FOLDER + '/html'):
for name in files:
file = open(os.path.join(root, name), 'rt')
h_data = file.read()
h_data = h_data.replace(original, replacement)
file.close()
file = open(os.path.join(root, name), 'wt')
file.write(h_data)
file.close()
def replaceInManfest(original, replacement):
global data
data = json.loads(json.dumps(data).replace(original, replacement))
minifyFilesInDirectory('scripts', '.js', 'https://www.toptal.com/developers/javascript-minifier/raw')
minifyFilesInDirectory('styles', '.css', 'https://cssminifier.com/raw')
#
# Update manifest file
#
with open(FOLDER + '/manifest.json', 'w+') as m:
m.write(json.dumps(data, indent=4))
#
# Build release for chrome
#
name = 'snoozz-chrome-' + VERSION
shutil.make_archive(name, 'zip', FOLDER)
print('\n\nCreated Chrome Release: ' + '\x1b[35m ' + name + '.zip' + '\x1b[0m')
#
# Add Open popup shortcut to start of manifest.commands
#
del data['offline_enabled']
mod_commands = {'_execute_browser_action' : {'description': 'Open the Snoozz popup'}}
for key, value in data['commands'].items(): mod_commands[key] = value
data['commands'] = mod_commands
with open(FOLDER + '/manifest.json', 'w+') as m:
m.write(json.dumps(data, indent=4))
#
# Build release for firefox
#
name = 'snoozz-ff-' + VERSION
shutil.make_archive(name, 'zip', FOLDER)
print('Created Firefox Release: ' + '\x1b[35m ' + name + '.zip' + '\x1b[0m')
#
# Build release for github
#
shutil.copy('LICENSE', FOLDER)
name = 'snoozz-' + VERSION
shutil.make_archive(name, 'zip', FOLDER)
print('Created GH Release: ' + '\x1b[35m ' + name + '.zip' + '\x1b[0m')
#
# Modify manifest file for safari and build
#
shutil.copy('docs/safari.md', FOLDER)
shutil.copy('instructions_safari.txt', FOLDER)
shutil.copy('safari.sh', FOLDER)
if 'idle' in data['permissions']: data['permissions'].remove('idle')
if 'notifications' in data['permissions']: data['permissions'].remove('notifications')
data['permissions'] = [p.replace('tabs','activeTab') for p in data['permissions']]
del data['commands']
with open(FOLDER + '/manifest.json', 'w+') as m:
m.write(json.dumps(data, indent=4))
name = 'snoozz-safari-' + VERSION
shutil.make_archive(name, 'zip', FOLDER)
print('Created Safari Release: ' + '\x1b[35m ' + name + '.zip' + '\x1b[0m')
#
# Cleanup
#
shutil.rmtree(FOLDER)