diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 0b3ff26f..f24df958 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -13,7 +13,14 @@ jobs: - uses: dart-lang/setup-dart@v1 with: sdk: ${{ matrix.sdk }} + - name: Cache prisma engines + uses: actions/cache@v3 + with: + path: .dart_tool/prisma + key: ${{ runner.os }}-${{ runner.arch }}-prisma-engines - name: Install dependencies run: dart pub get + - name: Pre-download engines + run: dart run bin/orm.dart precache - name: Run tests run: dart test -r github diff --git a/bin/orm.dart b/bin/orm.dart index a12134f7..afbf7509 100644 --- a/bin/orm.dart +++ b/bin/orm.dart @@ -9,6 +9,7 @@ import 'src/commands/db/db_command.dart'; import 'src/commands/format_command.dart'; import 'src/commands/generate_command.dart'; import 'src/commands/init_command.dart'; +import 'src/commands/precache_command.dart'; /// The Prisma CLI executable name. const String _executableName = r'dart run orm'; @@ -32,6 +33,7 @@ void main(List args) async { runner.addCommand(FormatCommand()); runner.addCommand(DbCommand()); runner.addCommand(GenerateCommand()); + runner.addCommand(PrecacheCommand()); // Get command result. final ArgResults results = runner.parse(args); diff --git a/bin/src/commands/precache_command.dart b/bin/src/commands/precache_command.dart new file mode 100644 index 00000000..4d2d2cc4 --- /dev/null +++ b/bin/src/commands/precache_command.dart @@ -0,0 +1,55 @@ +import 'package:args/command_runner.dart'; +import 'package:orm/version.dart'; + +import '../binary_engine/binary_engine.dart'; +import '../binary_engine/binary_engine_platform.dart'; +import '../binary_engine/binray_engine_type.dart'; +import '../utils/ansi_progress.dart'; + +class PrecacheCommand extends Command { + @override + String get description => + 'Populate the Prisma engines cache of binary artifacts.'; + + @override + String get name => 'precache'; + + PrecacheCommand() { + argParser.addMultiOption( + 'type', + abbr: 't', + help: 'The engine type to precache.', + valueHelp: 'engine', + allowed: BinaryEngineType.values.map((e) => e.name), + defaultsTo: BinaryEngineType.values.map((e) => e.name), + ); + } + + Iterable get types { + final List types = argResults?['type'] as List; + if (types.isEmpty) return BinaryEngineType.values; + + return BinaryEngineType.values + .where((element) => types.contains(element.name)); + } + + @override + void run() async { + for (final BinaryEngineType type in types) { + final BinaryEngine binaryEngine = BinaryEngine( + platform: BinaryEnginePlatform.current, + type: type, + version: binaryVersion, + ); + + if (!await binaryEngine.hasDownloaded) { + await binaryEngine.download(AnsiProgress.createFutureHandler( + 'Download Prisma ${type.name} engine')); + await Future.delayed(Duration(microseconds: 100)); + continue; + } + + print('Prisma ${type.name} engine is already downloaded.'); + } + } +}