Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'[Fair][2.0]compiler:增加编译后产物自动copy到bundle目录功能' #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion compiler/lib/src/post_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class ArchiveBuilder extends PostProcessBuilder with FlatCompiler {
var zipDesPath =
path.join(Directory.current.path, 'build', 'fair', 'fair_patch.zip');
_zip(Directory(zipSrcPath), File(zipDesPath));

//copy bundle file to asset bundle dir
var assetPath = path.join(Directory.current.path, 'assets', 'bundle');
_copy2Asset(Directory(assetPath), Directory(zipSrcPath));
}

@override
Expand Down Expand Up @@ -94,11 +98,40 @@ class ArchiveBuilder extends PostProcessBuilder with FlatCompiler {
}
if (entity.path.endsWith('.js') || entity.path.endsWith('.json')) {
final file = entity as File;
var filename = file.path.split('/').last;
var filename = file.path.split(path.separator).last;
final List<int> bytes = file.readAsBytesSync();
archive.addFile(ArchiveFile(filename, bytes.length, bytes));
}
}
zipFile.writeAsBytesSync(ZipEncoder().encode(archive), flush: false);
}

/// copy file to asset bundle dir
void _copy2Asset(Directory assetDir, Directory srcDir) {
print('assetDir:' + assetDir.path + ' srcDir: ' + srcDir.path);
if (!srcDir.existsSync() || srcDir.listSync().isEmpty) {
print('[Fair] _copy2Asset srcDir is not exits');
}

if (assetDir.existsSync()) {
assetDir.deleteSync(recursive: true);
}
assetDir.create(recursive: true);

for (var entity in srcDir.listSync(recursive: false)) {
if (entity is! File) {
continue;
}
if (entity.path.endsWith('.js') || entity.path.endsWith('.json')
|| entity.path.endsWith('.bin')) {
final file = entity as File;
var fileName = file.path.split(path.separator).last;
var decodeFile = path.join(assetDir.path, fileName);
print('decodeFile:' + decodeFile + ' assetDir.path:' + assetDir.path + ' fileName:' + fileName);
final List<int> bytes = file.readAsBytesSync();
File(decodeFile)..createSync(recursive: true)..writeAsBytesSync(bytes);
}
}

}
}