修改了一下 asset_generator
方便阅读,缩减了R文件的静态常量名称
import 'dart:io';
var preview_server_port = 2227;
void main() async {
bool working = false;
var pubSpec = new File('pubspec.yaml');
var pubLines = pubSpec.readAsLinesSync();
var newLines = <String>[];
var varNames = <String>[];
var resource = <String>[];
for (var line in pubLines) {
if (line.contains('begin') &&
line.contains('#') &&
line.contains('assets')) {
working = true;
newLines.add(line);
}
if (line.contains('end') && line.contains('#') && line.contains('assets'))
working = false;
if (working) {
if (line.trim().startsWith('#') && line.trim().endsWith('*')) {
newLines.add(line);
var directory =
new Directory(line.replaceAll('#', '').replaceAll('*', '').trim());
if (directory.existsSync()) {
var list = directory.listSync(recursive: true);
for (var file in list) {
if (new File(file.path).statSync().type ==
FileSystemEntityType.file) {
var path = file.path.replaceAll('\\', '/');
var varName = path.substring(
path.lastIndexOf('/') + 1, path.lastIndexOf('.'));
// path.replaceAll('/', '_').replaceAll('.', '_').toLowerCase();
var pos = 0;
String char;
while (true) {
pos = varName.indexOf('_', pos);
if (pos == -1) break;
char = varName.substring(pos + 1, pos + 2);
//varName = varName.replaceFirst('_$char', '_${char.toUpperCase()}');
pos++;
}
// varName = varName.replaceAll('_', '');
resource
.add("/// ![](http://127.0.0.1:$preview_server_port/$path)");
resource.add("static final String $varName = '$path';");
varNames.add(" $varName,");
newLines.add(' - $path');
}
}
} else {
throw new FileSystemException('Directory wrong');
}
}
} else {
newLines.add(line);
}
}
var r = new File('lib/r.dart');
if (r.existsSync()) {
r.deleteSync();
}
r.createSync();
var content = 'class R {\n';
for (var line in resource) {
if (line.contains("2.0x") || line.contains("3.0x")) {
continue;
} else if (line.contains("1.0x")) {
line = line.replaceAll("1.0x/", '');
}
content = '$content $line\n';
}
List<String> newList = [];
for (var line in varNames) {
if (!newList.contains(line)) {
newList.add(line);
}
}
content = '$content\n static final values = [\n';
for (var line in newList) {
content = '$content $line\n';
}
content = '$content ];\n}\n';
r.writeAsStringSync(content);
var spec = '';
for (var line in newLines) {
if (line.contains('2.0x/') || line.contains('3.0x/')) {
continue;
} else if (line.contains('1.0x/')) {
line = line.replaceAll('1.0x/', '');
}
spec = '$spec$line\n';
}
// print("spec + $spec");
pubSpec.writeAsStringSync(spec);
var ser;
try {
ser = await HttpServer.bind('127.0.0.1', preview_server_port);
print('成功启动图片预览服务器于本机<$preview_server_port>端口');
ser.listen(
(req) {
var index = req.uri.path.lastIndexOf('.');
var subType = req.uri.path.substring(index + 1);
print(subType);
req.response
..headers.contentType = new ContentType('image', subType)
..add(new File('.${req.uri.path}').readAsBytesSync())
..close();
},
);
} catch (e) {
print('图片预览服务器已启动或端口被占用');
}
}
使用方法:
创建一个dart文件 将代码复制进去
1、
- Android Studio (4.0)/ idea:
Setting >>Tools >> External Tools >> 点击添加按钮 ---->- Program:填写 fluutersdk 安装目录 + fluuter/bin/cache/dart-sdk/bin/dart
- Arguments: 本文代码的地址
- Working directory:
$ProjectFileDir$
- Name 和 Description:随便写
- 其他默认
2.编辑 pubspec.yaml 文件,在需要脚本处理的位置加入如下格式的注释:
# assets begin
# images/*
# assets end
3.右键 pubspec.yaml,
- VSCode:
终端/任务 >> 配置任务 >> 使用task模板创建 >> Others 运行任意外部命令的示例 >>
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "命令名称",
"type": "shell",
"command": "dart脚本的实际路径"
}
]
}