-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun_create.py
More file actions
executable file
·254 lines (209 loc) · 6.23 KB
/
run_create.py
File metadata and controls
executable file
·254 lines (209 loc) · 6.23 KB
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
#!/usr/bin/env python3
import os
import shutil
print(
"""
Flutter 项目创建脚本!
_ _ _ _
| | | | | | | |
| |_ ___ _ __ ___ _ __ | | __ _ | |_ ___ __ _ ___ | |_ __ __
| __|/ _ \| '_ ` _ \ | '_ \ | | / _` || __|/ _ \ / _` | / _ \| __|\ \/ /
| |_| __/| | | | | || |_) || || (_| || |_| __/ | (_| || __/| |_ > <
\__|\___||_| |_| |_|| .__/ |_| \__,_| \__|\___| \__, | \___| \__|/_/\_\
| | ______ __/ |
|_| |______||___/
"""
)
# 用户输入项目名称
def get_project_name(prompt):
while True:
project_name = input(prompt)
if project_name.strip() != "":
break
else:
print("项目名称不能为空!")
return project_name
# 用户输入项目组织名称
def get_project_org(prompt):
project_org = input(prompt)
if project_org.strip() == "":
project_org = "com.example"
return project_org
# 选择项目类型
def get_project_type(prompt):
while True:
project_type = input(prompt)
if project_type.isdigit():
break
else:
print("项目类型错误!")
return project_type
# 复制文件
def copy_folder(source_folder, destination_folder):
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
for item in os.listdir(source_folder):
source = os.path.join(source_folder, item)
destination = os.path.join(destination_folder, item)
if os.path.isdir(source):
copy_folder(source, destination)
else:
shutil.copy2(source, destination)
# 文件修改
def modfy_file(file, old_str, new_str):
"""
替换文件中的字符串
:param file:文件名
:param old_str:就字符串
:param new_str:新字符串
:return:
"""
file_data = ""
with open(file, "r", encoding="utf-8") as f:
for line in f:
if old_str in line:
# remove_line(file, line)
line = new_str
# line = line.replace(old_str, new_str)
file_data += line
with open(file, "w", encoding="utf-8") as f:
f.write(file_data)
# def remove_line(file_name, line_to_skip):
# """
# 删除指定行
# """
# with open(file_name, "r", encoding="utf-8") as read_file:
# lines = read_file.readlines()
# with open(file_name, "w", encoding="utf-8") as write_file:
# for current_line, line in enumerate(lines, start=1):
# if current_line != line_to_skip:
# write_file.write(line)
# 当前脚本文件路径
# /Users/xxx/Desktop/template_batch
scriptPath = os.path.dirname(__file__)
# 创建项目配置
project_name = get_project_name("请输入项目名称:")
project_org = get_project_org("请输入项目BundleID(默认:com.example):")
# 获取用户输入的目标路径
project_path = input(f"请输入项目目录(默认:{scriptPath}):")
if project_path == "":
project_path = scriptPath
if not os.path.exists(project_path):
os.makedirs(project_path)
# 项目类型
project_type = get_project_type(
f"""请选择项目目录结构:
1. 单页面模版
2. tabs结构模版
"""
)
# 切换到目标工程目录
os.chdir(project_path)
# print("*** 项目输出目录:", project_path)
# print("*** 名称:", project_name)
# print("*** 组织:", project_org)
print("*** 正在创建项目...")
os.system(f"flutter create --platforms ios,android {project_name} --org {project_org}")
print("*** 复制模版文件...")
# assets
copy_folder(
f"{scriptPath}/tabs_template/assets", f"{project_path}/{project_name}/assets"
)
# lib
source_lib = f"{scriptPath}/{'simple_template' if project_type == '1' else 'tabs_template' }/lib"
print(source_lib)
copy_folder(source_lib, f"{project_path}/{project_name}/lib")
print("*** 修改插件文件...")
pubspec = f"{project_path}/{project_name}/pubspec.yaml"
# dependencies
modfy_file(
pubspec,
"cupertino_icons:",
"""
cupertino_icons: ^1.0.8
flutter_localizations:
sdk: flutter
# get: ^5.0.0-release-candidate-6
get: ^5.0.0-release-candidate-9.2
animated_bottom_navigation_bar: ^1.3.3
json_annotation: ^4.9.0
freezed_annotation: ^2.4.1
get_storage: ^2.1.1
bot_toast: ^4.1.3
easy_refresh: ^3.4.0
infinite_scroll_pagination: ^4.0.0
dio: ^5.4.3+1
retrofit: ^4.1.0
flutter_screenutil: ^5.9.3
flutter_spinkit: ^5.2.1
simple_animations: ^5.0.2
event_bus: ^2.0.0
photo_view: ^0.15.0
wechat_assets_picker: ^9.0.4
wechat_camera_picker: ^4.2.2
flutter_native_splash: ^2.4.0
path_provider: ^2.1.3
# cached_network_image: ^3.2.2
extended_image: ^8.2.1
image_gallery_saver: ^2.0.3
permission_handler: ^11.3.1
package_info_plus: ^8.0.0
device_info_plus: ^10.1.0
url_launcher: ^6.3.0
internet_connection_checker_plus: ^2.5.2
""",
)
# dev_dependencies
modfy_file(
pubspec,
"flutter_lints:",
"""
flutter_lints: ^4.0.0
build_runner: ^2.4.12
flutter_gen_runner: ^5.7.0
json_serializable: ^6.8.0
freezed: ^2.5.7
retrofit_generator: ^9.1.2
# 一键生成启动图标: dart pub run flutter_launcher_icons
flutter_launcher_icons: ^0.14.0
pretty_dio_logger: ^1.4.0
""",
)
# 资源引用路径
modfy_file(
pubspec,
"uses-material-design: true",
"""
uses-material-design: true
assets:
- assets/images/
""",
)
# 添加build生成文件路径
with open(pubspec, "a") as file:
file.write(
"""
flutter_gen:
output: lib/support_files/
line_length: 80
"""
)
# 添加analysis_options配置
with open(f"{project_path}/{project_name}/analysis_options.yaml", "a") as file:
file.write(
"""
analyzer:
exclude:
- "**/*.g.dart"
- /**/generated/**/*.dart
errors:
invalid_annotation_target: ignore
use_build_context_synchronously: ignore
unused_element: ignore
plugins:
- custom_lint
"""
)
print("*** 执行 dart run build_runner build ...")
os.chdir(os.path.join(project_path, project_name))
os.system(f"dart run build_runner build")