|
| 1 | +// ignore_for_file: dead_code |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:logging/logging.dart'; |
| 5 | +import 'package:native_assets_cli/code_assets_builder.dart'; |
| 6 | +import 'package:native_assets_cli/native_assets_cli.dart'; |
| 7 | +import 'package:native_toolchain_cmake/native_toolchain_cmake.dart'; |
| 8 | + |
| 9 | +void main(List<String> args) async { |
| 10 | + await build(args, (input, output) async { |
| 11 | + final sourceDir = Directory(await getPackagePath('add')).uri.resolve('src'); |
| 12 | + const exampleGit = false; |
| 13 | + if (!exampleGit) { |
| 14 | + await runBuild(input, output, sourceDir); |
| 15 | + } else { |
| 16 | + await runBuildGit(input, output, sourceDir); |
| 17 | + } |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +const name = 'add'; |
| 22 | + |
| 23 | +Future<void> runBuild( |
| 24 | + BuildInput input, |
| 25 | + BuildOutputBuilder output, |
| 26 | + Uri sourceDir, |
| 27 | +) async { |
| 28 | + final _logger = Logger('') |
| 29 | + ..level = Level.ALL |
| 30 | + // temp fwd to stderr until process logs pass to stdout |
| 31 | + ..onRecord.listen((record) => stderr.writeln(record)); |
| 32 | + final builder = CMakeBuilder.create( |
| 33 | + name: name, |
| 34 | + sourceDir: sourceDir, |
| 35 | + defines: { |
| 36 | + 'CMAKE_BUILD_TYPE': 'Release', |
| 37 | + 'CMAKE_INSTALL_PREFIX': '${input.outputDirectory.toFilePath()}/install', |
| 38 | + }, |
| 39 | + targets: [ |
| 40 | + 'install', |
| 41 | + ], |
| 42 | + buildLocal: true, |
| 43 | + logger: _logger, |
| 44 | + ); |
| 45 | + |
| 46 | + await builder.run(input: input, output: output, logger: _logger); |
| 47 | + |
| 48 | + final buildjson = input.config.json; |
| 49 | + _logger.info('Build output: $buildjson'); |
| 50 | + |
| 51 | + // automatically search and add libraries |
| 52 | + final outLibs = await output.findAndAddCodeAssets( |
| 53 | + input, |
| 54 | + names: {r'(lib)?add\.(dll|so|dylib)': 'add.dart'}, |
| 55 | + outDir: input.outputDirectory.resolve('install'), |
| 56 | + logger: _logger, |
| 57 | + regExp: true, |
| 58 | + ); |
| 59 | + |
| 60 | + // Do something else with outLibs uris |
| 61 | + _logger.info('Found libs: $outLibs'); |
| 62 | +} |
| 63 | + |
| 64 | +Future<void> runBuildGit( |
| 65 | + BuildInput input, |
| 66 | + BuildOutputBuilder output, |
| 67 | + Uri sourceDir, |
| 68 | +) async { |
| 69 | + final logger = Logger('') |
| 70 | + ..level = Level.ALL |
| 71 | + // temp fwd to stderr until process logs pass to stdout |
| 72 | + ..onRecord.listen((record) => stderr.writeln(record)); |
| 73 | + // From git url |
| 74 | + final builder = CMakeBuilder.fromGit( |
| 75 | + gitUrl: "https://github.com/rainyl/native_toolchain_cmake.git", |
| 76 | + sourceDir: sourceDir, |
| 77 | + name: name, |
| 78 | + gitSubDir: "example/add/src", |
| 79 | + defines: { |
| 80 | + 'CMAKE_BUILD_TYPE': 'Release', |
| 81 | + 'CMAKE_INSTALL_PREFIX': '${input.outputDirectory.toFilePath()}/install', |
| 82 | + }, |
| 83 | + targets: ['install'], |
| 84 | + buildLocal: true, |
| 85 | + logger: logger, |
| 86 | + ); |
| 87 | + |
| 88 | + await builder.run( |
| 89 | + input: input, |
| 90 | + output: output, |
| 91 | + logger: logger, |
| 92 | + ); |
| 93 | + |
| 94 | + // manually add assets |
| 95 | + final libPath = switch (input.config.code.targetOS) { |
| 96 | + OS.linux => "install/lib/libadd.so", |
| 97 | + OS.macOS => "install/lib/libadd.dylib", |
| 98 | + OS.windows => "install/lib/add.dll", |
| 99 | + OS.android => "install/lib/libadd.so", |
| 100 | + OS.iOS => "install/lib/libadd.dylib", |
| 101 | + _ => throw UnsupportedError("Unsupported OS") |
| 102 | + }; |
| 103 | + output.assets.code.add( |
| 104 | + CodeAsset( |
| 105 | + package: name, |
| 106 | + name: '$name.dart', |
| 107 | + linkMode: DynamicLoadingBundled(), |
| 108 | + os: input.config.code.targetOS, |
| 109 | + file: input.outputDirectory.resolve(libPath), |
| 110 | + architecture: input.config.code.targetArchitecture, |
| 111 | + ), |
| 112 | + ); |
| 113 | +} |
0 commit comments