-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets_mapper
More file actions
executable file
·142 lines (131 loc) · 4.84 KB
/
Copy pathassets_mapper
File metadata and controls
executable file
·142 lines (131 loc) · 4.84 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
#!/usr/local/bin/python3.9
# -*- coding:utf8 -*-
# 资源映射器
# 自动生成/刷新flutter全局资源类文件;资源文件请放在根目录中使用assets
# 目录名称请使用符合类名规范的名称
# 文件名请使用符合字段名规范的名称
# 文件需放在Flutter 项目的根目录执行(与pubspec.yaml同级目录);执行后会在./lib下生成assets.dart文件,
# 此时访问资源将不再需要拷贝冗长的路径了,直接使用类访问与assets目录结构相同的访问即可;
# 同时执行该脚本也会自动更新pubspec.yaml文件中的资源定义
# 【警告】:
# 1. 执行命令前确保./lib/assets.dart文件为自动生成或不存在!因为接下来该文件将被覆盖!
import os;
import json;
import re;
import yaml;
import datetime as time;
ignores=[];
# 资产文件代码
def logE (msg):
'''
打印错误日志
'''
print('\033[31m' + msg + '\033[00m');
def mapping (path):
'''
返回目录的树形字典结构
path: 路径
'''
# 当前的映射器位置
c_Mapper = {};
fList = os.listdir(os.path.join(path));
for fEach in fList:
if os.path.isdir(os.path.join(path, fEach)):
c_Mapper[fEach] = mapping(os.path.join(path, fEach));
elif os.path.isfile(os.path.join(path, fEach)):
c_Mapper[fEach] = os.path.join(path, fEach);
return c_Mapper;
def assets_cls_gen (name, mapper, file, depth = 1):
'''
根据json生成同等结构的类文件
name: 根类的名称, 若名称以$开头表示根类名
mapper: json结构的类模版
file: 文件对象,将内容写入到该对象
depth: 不需要传入, 由函数内部维护;表示递归层数
'''
cls_doc = '';
if type(mapper) is dict:
mapK = dict(mapper).keys();
if depth == 1:
cls_doc += 'class %s {\n' % (covertClassName(name));
else:
cls_doc += 'class _%s {\n' % (covertClassName(name));
for k in mapK:
if type(mapper[k]) is dict:
assets_cls_gen(k, mapper[k], file, depth + 1);
if depth == 1:
cls_doc += ' static final _%s %s = _%s();\n' %(covertClassName(k), covertName(k), covertClassName(k));
else:
cls_doc += ' final _%s %s = _%s();\n' %(covertClassName(k), covertName(k), covertClassName(k));
else:
cls_doc += ' %s final String %s = \'%s\';\n' % ('static' if depth == 1 else '', covertName(k).upper(), mapper[k]);
cls_doc += '}\n\n';
if file.writable():
file.write(cls_doc);
else:
logE("./lib/assets.dart 文件不可写入!");
exit(1);
else:
pass;
def covertClassName (name):
'''
将名称转化为标准类名
'''
name = name[:1].upper() + name[1:];
name = re.sub('[^a-z$A-Z0-9]', '_', name);
names = name.split('_');
name = ''
for sec in names:
name += sec[:1].upper() + sec[1:].lower();
return name;
def covertName (name):
'''
将名称转换为合法的字段名
'''
surfixI=name.rfind('.');
if surfixI > -1:
name = name if name[0:surfixI] == '' else name[0:surfixI];
name = re.sub('[^a-z$A-Z0-9]', '_', name);
name = re.sub('\..*$', '', name);
return name;
def updateAssetDefinition (assets):
'''
更新资源定义
assets: 资产列表
'''
with open ('./pubspec.yaml', 'r') as pubspec:
tempYaml = yaml.safe_load(pubspec);
tempYaml['flutter']["assets"] = assets;
with open ('./pubspec.yaml', 'w', 128, 'utf-8') as te:
te.write(yaml.dump(tempYaml));
def getAssetsList (mapper):
'''
递归目录映射器获取所有文件并返回
'''
list = [];
mK = mapper;
for k in mK:
if type(mK[k]) is dict:
children = getAssetsList(mK[k]);
list.extend(children);
else:
list.append(mK[k]);
return list;
if os.path.exists('./assets') and os.path.isdir('./assets'):
mapper = mapping('assets');
before_doc = 'import \'dart:core\';\n';
before_doc += '/// <<FLUTTER 资产映射文件>>\n';
before_doc += '/// 该类由Assets Mapper生成,请不要编辑该文件\n';
before_doc += '/// Last update time: %s\n' % (time.datetime.strftime(time.datetime.now(), '%Y-%m-%d %H:%M:%S'));
before_doc += '/// \n';
with open('./lib/assets.dart', 'w') as ast_file:
if ast_file.writable():
ast_file.write(before_doc);
else:
logE("./lib/assets.dart 文件不可写入!");
exit(1);
assets_cls_gen('Ast', mapper, ast_file);
updateAssetDefinition(getAssetsList(mapper));
else:
logE('未找到资源文件夹./assets');
exit(1);