-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack_dist_electron.py
executable file
·285 lines (243 loc) · 9.39 KB
/
pack_dist_electron.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import os
import platform
import shlex
import shutil
import stat
import sys
from subprocess import Popen, PIPE, STDOUT
# Log to stdout
def log(message):
print('%s' % (message))
# Log to stderr
def log_err(message):
print(message, file=sys.stderr)
def run_cmd(cmd):
log('Run: \n%s' % (cmd))
os.system(cmd)
def run_cmd_with_stdio(cmd, input_data):
log('Run: \n%s' % (cmd))
p = Popen(shlex.split(cmd), stdout=PIPE, stdin=PIPE, stderr=STDOUT)
return p.communicate(input = input_data)[0]
def run_cmd_with_stderr(cmd):
log('Run: \n%s' % (cmd))
p = Popen(shlex.split(cmd), stdout=PIPE, stdin=PIPE, stderr=PIPE)
stdout_text, stderr_text = p.communicate()
return stderr_text
def is_windows_sys():
return (platform.system() == 'Windows')
def is_macos_sys():
return (platform.system() == 'Darwin')
def is_linux_sys():
return (platform.system() == 'Linux')
def fix_win_path(path):
if is_windows_sys():
return path.replace('/', '\\')
else:
return path
def find_strings_in_string(strings, in_string):
for str_itr in strings:
if in_string.find(str_itr) >= 0:
return True
return False
def copy_file(source, dest):
follow_symlinks_os = False
if is_windows_sys():
follow_symlinks_os = True
shutil.copyfile(source, dest, follow_symlinks=follow_symlinks_os)
def copy_dir(source, dest):
for filename in os.listdir(source):
src_file = os.path.join(source, filename)
dst_file = os.path.join(dest, filename)
if os.path.isdir(src_file):
if not os.path.exists(dst_file):
os.mkdir(dst_file)
copy_dir(src_file, dst_file)
if os.path.isfile(src_file):
copy_file(src_file, dst_file)
def remove_file(path):
if os.path.exists(path):
os.remove(path)
def remove_dir(path):
if os.path.exists(path):
shutil.rmtree(path)
def read_file_content(file_path):
if not os.path.exists(file_path):
return ''
file_content = open(file_path, 'rb').read()
return file_content
def write_file_content(file_path, file_content):
file_obj = open(file_path, 'wb')
file_obj.write(file_content)
file_obj.close()
def log_stage(stage_message):
log('\n%s\n' % (stage_message))
EXE_7Z_WIN = '7z'
USING_XZ_MACOS = True
SUPPORT_LINUX = True
APP_TITLE = 'electron-adb-file'
PACKAGE_NAME = 'electron-adb-file'
APP_BUNDLE_ID_MACOS = 'org.sunjw.ElectronAdbFile'
DIST_DIR = 'dist'
APP_DIRS = ['node_modules', 'assets', 'css', 'js']
APP_FILES = ['index.html', 'main.js',
'package.json', 'package-lock.json',
'README.md', 'LICENSE']
EXEC_FIX_PATHS = ['./node_modules/.bin/electron-rebuild',
'./node_modules/.bin/rimraf']
REBUILD_CMD = ''
REBUILD_CLEAN_PATHS_WIN = []
REBUILD_CLEAN_PATHS_MACOS = []
REBUILD_CLEAN_PATHS_LINUX = []
def main():
exe_7z_sys = EXE_7Z_WIN # 7z is not supported on macOS
tar_ext = 'gz'
tar_param = '-czvf'
if USING_XZ_MACOS:
tar_ext = 'xz'
tar_param = '-cJvf'
if is_linux_sys() and (not SUPPORT_LINUX):
log_stage('Not supported Linux.')
exit()
system_name = 'win'
if is_macos_sys():
system_name = 'macos'
elif is_linux_sys():
system_name = 'linux'
machine_name = platform.machine()
machine_name = machine_name.lower()
app_name = PACKAGE_NAME
app_dir_path_relative = 'resources/app'
if is_macos_sys():
app_name = PACKAGE_NAME + '.app'
app_dir_path_relative = 'Contents/Resources/app'
app_path_relative = os.path.join(app_name, app_dir_path_relative)
cwd = os.getcwd()
# Update modules.
log_stage('Update modules...')
run_cmd('npm install')
# Extract old package and remove old app.
if not os.path.exists(DIST_DIR):
os.mkdir(DIST_DIR)
log_stage('No extract old package and copy from electron dist...')
electron_node_module_dir = 'node_modules/electron'
electron_dist_dir = 'dist'
os.chdir(electron_node_module_dir)
if is_windows_sys():
electron_dist_package = 'dist.7z'
run_cmd('%s -t7z a %s %s' % (exe_7z_sys, electron_dist_package, electron_dist_dir))
os.chdir(cwd)
electron_dist_package_path = os.path.join(electron_node_module_dir, electron_dist_package)
copy_file(electron_dist_package_path, os.path.join(DIST_DIR, electron_dist_package))
remove_file(electron_dist_package_path)
os.chdir(DIST_DIR)
run_cmd('%s -t7z x %s' % (exe_7z_sys, electron_dist_package))
remove_file(electron_dist_package)
os.rename(electron_dist_dir, app_name)
elif is_macos_sys():
os.chdir(electron_dist_dir)
electron_dist_app_name = 'Electron.app'
electron_dist_package = '%s.tar.%s' % (electron_dist_app_name, tar_ext)
run_cmd('tar %s %s %s' % (tar_param, electron_dist_package, electron_dist_app_name))
os.chdir(cwd)
electron_dist_package_path = os.path.join(electron_node_module_dir, electron_dist_dir, electron_dist_package)
copy_file(electron_dist_package_path, os.path.join(DIST_DIR, electron_dist_package))
remove_file(electron_dist_package_path)
os.chdir(DIST_DIR)
run_cmd('tar -xvf %s' % (electron_dist_package))
remove_file(electron_dist_package)
os.rename(electron_dist_app_name, app_name)
elif is_linux_sys():
electron_dist_package = 'dist.tar.%s' % (tar_ext)
run_cmd('tar %s %s %s' % (tar_param, electron_dist_package, electron_dist_dir))
os.chdir(cwd)
electron_dist_package_path = os.path.join(electron_node_module_dir, electron_dist_package)
copy_file(electron_dist_package_path, os.path.join(DIST_DIR, electron_dist_package))
remove_file(electron_dist_package_path)
os.chdir(DIST_DIR)
run_cmd('tar -xvf %s' % (electron_dist_package))
remove_file(electron_dist_package)
os.rename(electron_dist_dir, app_name)
else:
log_stage('Not supported system.')
exit()
os.chdir(cwd)
os.chdir(DIST_DIR)
os.mkdir(app_path_relative)
os.chdir(cwd)
# Copy new app.
log_stage('Copy new app...')
for app_dir in APP_DIRS:
dest_app_dir = os.path.join(DIST_DIR, app_path_relative)
dest_app_dir = os.path.join(dest_app_dir, app_dir)
os.mkdir(dest_app_dir)
copy_dir(app_dir, dest_app_dir)
for app_file in APP_FILES:
dest_app_file = os.path.join(DIST_DIR, app_path_relative)
dest_app_file = os.path.join(dest_app_file, app_file)
copy_file(app_file, dest_app_file)
release_file = os.path.join(DIST_DIR, app_path_relative, 'assets', 'RELEASED')
open(release_file, 'a').close()
# Rebuild and clean.
log_stage('Rebuild and clean...')
os.chdir(os.path.join(DIST_DIR, app_path_relative))
if is_macos_sys() or is_linux_sys():
for exec_file in EXEC_FIX_PATHS:
st = os.stat(exec_file)
os.chmod(exec_file, st.st_mode | stat.S_IEXEC)
run_cmd('npm rebuild')
if REBUILD_CMD:
run_cmd('npm run %s' % (REBUILD_CMD))
remove_dir('./node_modules/electron/dist')
rebuild_clean_paths = REBUILD_CLEAN_PATHS_WIN
if is_macos_sys():
rebuild_clean_paths = REBUILD_CLEAN_PATHS_MACOS
if is_linux_sys():
rebuild_clean_paths = REBUILD_CLEAN_PATHS_LINUX
for rebuild_clean_path in rebuild_clean_paths:
remove_dir(rebuild_clean_path)
os.chdir(cwd)
# Rename electron files.
log_stage('Rename electron files...')
os.chdir(os.path.join(DIST_DIR, app_name))
electron_exe_dir = ''
electron_exe_name = ''
electron_exe_app_name = ''
if is_windows_sys():
electron_exe_dir = './'
electron_exe_name = 'electron.exe'
electron_exe_app_name = '%s%s.exe' % (electron_exe_dir, APP_TITLE)
elif is_macos_sys():
electron_exe_dir = './Contents/MacOS/'
electron_exe_name = 'Electron'
electron_exe_app_name = '%s%s' % (electron_exe_dir, APP_TITLE)
elif is_linux_sys():
electron_exe_dir = './'
electron_exe_name = 'electron'
electron_exe_app_name = '%s%s' % (electron_exe_dir, APP_TITLE)
electron_exe_path = '%s%s' % (electron_exe_dir, electron_exe_name)
if os.path.exists(electron_exe_path):
os.rename(electron_exe_path, electron_exe_app_name)
if is_macos_sys():
info_plist_path = './Contents/Info.plist'
info_plist_content = read_file_content(info_plist_path)
info_plist_content = info_plist_content.replace(b'>Electron<',
bytes('>%s<' % (APP_TITLE), encoding='utf8'))
info_plist_content = info_plist_content.replace(b'>com.github.Electron<',
bytes('>%s<' % (APP_BUNDLE_ID_MACOS), encoding='utf8'))
write_file_content(info_plist_path, info_plist_content)
os.chdir(cwd)
# Package and clean up.
log_stage('Package and clean up...')
os.chdir(DIST_DIR)
app_package_file = '%s.%s.%s' % (app_name, system_name, machine_name)
if is_windows_sys():
app_package_file = '%s.7z' % (app_package_file)
remove_file(app_package_file)
run_cmd('%s -t7z -mx9 a %s %s' % (exe_7z_sys, app_package_file, app_name))
else:
app_package_file = '%s.tar.%s' % (app_package_file, tar_ext)
remove_file(app_package_file)
run_cmd('tar %s %s %s' % (tar_param, app_package_file, app_name))
remove_dir(app_name)
if __name__ == '__main__':
main()