Skip to content

Commit

Permalink
chore(cli): Add precache prisma engines command
Browse files Browse the repository at this point in the history
  • Loading branch information
Seven Du committed Sep 20, 2022
1 parent 35728d6 commit d7c2bd8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions bin/orm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -32,6 +33,7 @@ void main(List<String> args) async {
runner.addCommand(FormatCommand());
runner.addCommand(DbCommand());
runner.addCommand(GenerateCommand());
runner.addCommand(PrecacheCommand());

// Get command result.
final ArgResults results = runner.parse(args);
Expand Down
55 changes: 55 additions & 0 deletions bin/src/commands/precache_command.dart
Original file line number Diff line number Diff line change
@@ -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<BinaryEngineType> get types {
final List<String> types = argResults?['type'] as List<String>;
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.');
}
}
}

0 comments on commit d7c2bd8

Please sign in to comment.