flutter-dev/asset_generator

根据你的案例我修改了下

Opened this issue · 0 comments

`import 'dart:io';

var previewServerPort = 2227;

bool isHiddenFile(FileSystemEntity file) =>
file.path[file.parent.path.length + 1] == '.';

void main() async {
bool working = false;
var pubSpec = File('pubspec.yaml');
var pubLines = pubSpec.readAsLinesSync();
var newLines = [];
var varNames = [];
var resource = [];
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 =
        Directory(line.replaceAll('#', '').replaceAll('*', '').trim());
    if (directory.existsSync()) {
      var list = directory.listSync(recursive: true)
        ..sort((a, b) => a.path.compareTo(b.path));
      for (var file in list) {
        if (file.statSync().type == FileSystemEntityType.file &&
            !isHiddenFile(file)) {
          var path = file.path.replaceAll('\\', '/');
          var data = path.split('.');
          if (data.isNotEmpty) {
            var varName = data[0]
                .replaceAll('images/', '')
                .replaceAll('assets/', '')
                .toLowerCase()
                .trim();
            varName = varName.replaceAll('/', '_').replaceAll('.', '_');
            var pos = 0;
            String char;
            while (pos < varName.lastIndexOf('_')) {
              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:$previewServerPort/$path)");
            resource.add("static const String $varName = '$path';");
            varNames.add("    $varName,");
            String newLinesData =
                '    - ${path.substring(0, path.lastIndexOf('/')+1)}';
            if (!newLines.contains(newLinesData)) {
              newLines.add(newLinesData);
            }
          }
        }
      }
    } else {
      throw FileSystemException('Directory wrong');
    }
  }
} else {
  newLines.add(line);
}

}

var r = File('lib/r.dart');
if (r.existsSync()) {
r.deleteSync();
}
r.createSync();
var content = 'class R {\n';
for (var line in resource) {
content = '$content $line\n';
}
content = '$content\n static const values = [\n';
for (var line in varNames) {
content = '$content $line\n';
}
content = '$content ];\n}\n';
r.writeAsStringSync(content);

var spec = '';
for (var line in newLines) {
spec = '$spec$line\n';
}
pubSpec.writeAsStringSync(spec);

var ser;
try {
ser = await HttpServer.bind('127.0.0.1', previewServerPort);
print('成功启动图片预览服务器于本机<$previewServerPort>端口');
ser.listen(
(req) {
var index = req.uri.path.lastIndexOf('.');
var subType = req.uri.path.substring(index + 1);
print(subType);
req.response
..headers.contentType = ContentType('image', subType)
..add(File('.${req.uri.path}').readAsBytesSync())
..close();
},
);
} catch (e) {
print('图片预览服务器已启动或端口被占用');
}
}`
改完的效果如下

微信截图_20210901170325
微信截图_20210901170342