Skip to content

Commit

Permalink
feat(shorebird_cli): support local engine artifacts (#1524)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Nov 21, 2023
1 parent 1e34cca commit ef38380
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 82 deletions.
2 changes: 2 additions & 0 deletions packages/shorebird_cli/bin/shorebird.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:shorebird_cli/src/code_push_client_wrapper.dart';
import 'package:shorebird_cli/src/command_runner.dart';
import 'package:shorebird_cli/src/doctor.dart';
import 'package:shorebird_cli/src/executables/executables.dart';
import 'package:shorebird_cli/src/flutter_artifacts.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/os/os.dart';
import 'package:shorebird_cli/src/patch_diff_checker.dart';
Expand Down Expand Up @@ -37,6 +38,7 @@ Future<void> main(List<String> args) async {
devicectlRef,
doctorRef,
engineConfigRef,
flutterArtifactsRef,
gitRef,
gradlewRef,
idevicesyslogRef,
Expand Down
5 changes: 5 additions & 0 deletions packages/shorebird_cli/lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:cli_completion/cli_completion.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/commands/commands.dart';
import 'package:shorebird_cli/src/flutter_artifacts.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/platform.dart';
import 'package:shorebird_cli/src/process.dart';
Expand Down Expand Up @@ -86,12 +87,16 @@ class ShorebirdCliCommandRunner extends CompletionCommandRunner<int> {
localEngine: topLevelResults['local-engine'] as String?,
);
final process = ShorebirdProcess(engineConfig: engineConfig);
final flutterArtifacts = engineConfig.localEngineSrcPath != null
? const FlutterLocalEngineArtifacts()
: const FlutterCachedArtifacts();

return await runScoped<Future<int?>>(
() => runCommand(topLevelResults),
values: {
engineConfigRef.overrideWith(() => engineConfig),
processRef.overrideWith(() => process),
flutterArtifactsRef.overrideWith(() => flutterArtifacts),
},
) ??
ExitCode.success.code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:shorebird_cli/src/config/config.dart';
import 'package:shorebird_cli/src/deployment_track.dart';
import 'package:shorebird_cli/src/doctor.dart';
import 'package:shorebird_cli/src/executables/executables.dart';
import 'package:shorebird_cli/src/flutter_artifacts.dart';
import 'package:shorebird_cli/src/formatters/file_size_formatter.dart';
import 'package:shorebird_cli/src/ios.dart';
import 'package:shorebird_cli/src/logger.dart';
Expand Down Expand Up @@ -371,7 +372,11 @@ ${summary.join('\n')}
return ExitCode.software.code;
}

final analyzeSnapshot = shorebirdEnv.analyzeSnapshotFile;
final analyzeSnapshot = File(
flutterArtifacts.getArtifactPath(
artifact: FlutterArtifact.analyzeSnapshot,
),
);

if (!analyzeSnapshot.existsSync()) {
logger.err('Unable to find analyze_snapshot at ${analyzeSnapshot.path}');
Expand Down
123 changes: 123 additions & 0 deletions packages/shorebird_cli/lib/src/flutter_artifacts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// ignore_for_file: one_member_abstracts

import 'dart:io';

import 'package:path/path.dart' as p;
import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/process.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';

/// All Flutter artifacts used explicitly by Shorebird.
enum FlutterArtifact {
/// The gen_snapshot executable.
genSnapshot,

/// The analyze_snapshot executable.
analyzeSnapshot,
}

/// A reference to a [FlutterArtifacts] instance.
final flutterArtifactsRef = create<FlutterArtifacts>(
FlutterCachedArtifacts.new,
);

/// The [FlutterArtifacts] instance available in the current zone.
FlutterArtifacts get flutterArtifacts => read(flutterArtifactsRef);

/// {@template flutter_artifacts}
/// A class that provides access to Flutter artifacts.
/// {@endtemplate}
abstract class FlutterArtifacts {
/// Returns the path to the given [artifact].
String getArtifactPath({required FlutterArtifact artifact});
}

/// {@template flutter_cached_artifacts}
/// A class that provides access to cached Flutter artifacts.
/// {@endtemplate}
class FlutterCachedArtifacts implements FlutterArtifacts {
/// {@macro flutter_cached_artifacts}
const FlutterCachedArtifacts();

@override
String getArtifactPath({
required FlutterArtifact artifact,
}) {
switch (artifact) {
case FlutterArtifact.genSnapshot:
return _genSnapshotFile.path;
case FlutterArtifact.analyzeSnapshot:
return _analyzeSnapshotFile.path;
}
}

File get _genSnapshotFile {
return File(
p.join(
shorebirdEnv.flutterDirectory.path,
'bin',
'cache',
'artifacts',
'engine',
'ios-release',
'gen_snapshot_arm64',
),
);
}

File get _analyzeSnapshotFile {
return File(
p.join(
shorebirdEnv.flutterDirectory.path,
'bin',
'cache',
'artifacts',
'engine',
'ios-release',
'analyze_snapshot_arm64',
),
);
}
}

/// {@template flutter_local_engine_artifacts}
/// A class that provides access to locally built Flutter artifacts.
/// {@endtemplate}
class FlutterLocalEngineArtifacts implements FlutterArtifacts {
/// {@macro flutter_local_engine_artifacts}
const FlutterLocalEngineArtifacts();

@override
String getArtifactPath({required FlutterArtifact artifact}) {
switch (artifact) {
case FlutterArtifact.genSnapshot:
return _genSnapshotFile.path;
case FlutterArtifact.analyzeSnapshot:
return _analyzeSnapshotFile.path;
}
}

File get _genSnapshotFile {
return File(
p.join(
engineConfig.localEngineSrcPath!,
'out',
'ios_release',
'clang_x64',
'gen_snapshot_arm64',
),
);
}

File get _analyzeSnapshotFile {
return File(
p.join(
engineConfig.localEngineSrcPath!,
'out',
'ios_release',
'clang_x64',
'analyze_snapshot_arm64',
),
);
}
}
4 changes: 2 additions & 2 deletions packages/shorebird_cli/lib/src/shorebird_build_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import 'dart:io';
import 'package:collection/collection.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/flutter_artifacts.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/os/operating_system_interface.dart';
import 'package:shorebird_cli/src/process.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';

enum Arch {
arm64,
Expand Down Expand Up @@ -348,7 +348,7 @@ Either run `flutter pub get` manually, or follow the steps in ${link(uri: Uri.pa
];

final result = await process.run(
shorebirdEnv.genSnapshotFile.path,
flutterArtifacts.getArtifactPath(artifact: FlutterArtifact.genSnapshot),
arguments,
);

Expand Down
28 changes: 0 additions & 28 deletions packages/shorebird_cli/lib/src/shorebird_env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,6 @@ class ShorebirdEnv {
return File(p.join(flutterDirectory.path, 'bin', 'flutter'));
}

File get genSnapshotFile {
return File(
p.join(
flutterDirectory.path,
'bin',
'cache',
'artifacts',
'engine',
'ios-release',
'gen_snapshot_arm64',
),
);
}

File get analyzeSnapshotFile {
return File(
p.join(
flutterDirectory.path,
'bin',
'cache',
'artifacts',
'engine',
'ios-release',
'analyze_snapshot_arm64',
),
);
}

/// The `shorebird.yaml` file for this project.
File getShorebirdYamlFile({required Directory cwd}) {
return File(p.join(cwd.path, 'shorebird.yaml'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ flutter:
),
),
),
() => doctor.runValidators(any(), applyFixes: true)
() => doctor.runValidators(any(), applyFixes: true),
]);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:shorebird_cli/src/config/config.dart';
import 'package:shorebird_cli/src/deployment_track.dart';
import 'package:shorebird_cli/src/doctor.dart';
import 'package:shorebird_cli/src/executables/aot_tools.dart';
import 'package:shorebird_cli/src/flutter_artifacts.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/os/operating_system_interface.dart';
import 'package:shorebird_cli/src/patch_diff_checker.dart';
Expand Down Expand Up @@ -132,6 +133,7 @@ flutter:
late Directory projectRoot;
late File genSnapshotFile;
late File analyzeSnapshotFile;
late FlutterArtifacts flutterArtifacts;
late Doctor doctor;
late IosArchiveDiffer archiveDiffer;
late Progress progress;
Expand All @@ -158,6 +160,7 @@ flutter:
authRef.overrideWith(() => auth),
codePushClientWrapperRef.overrideWith(() => codePushClientWrapper),
doctorRef.overrideWith(() => doctor),
flutterArtifactsRef.overrideWith(() => flutterArtifacts),
loggerRef.overrideWith(() => logger),
osInterfaceRef.overrideWith(() => operatingSystemInterface),
patchDiffCheckerRef.overrideWith(() => patchDiffChecker),
Expand Down Expand Up @@ -246,6 +249,7 @@ flutter:
auth = MockAuth();
codePushClientWrapper = MockCodePushClientWrapper();
doctor = MockDoctor();
flutterArtifacts = MockFlutterArtifacts();
shorebirdRoot = Directory.systemTemp.createTempSync();
projectRoot = Directory.systemTemp.createTempSync();
flutterDirectory = Directory(
Expand Down Expand Up @@ -349,10 +353,16 @@ flutter:
() => shorebirdEnv.getShorebirdProjectRoot(),
).thenReturn(projectRoot);
when(() => shorebirdEnv.flutterDirectory).thenReturn(flutterDirectory);
when(() => shorebirdEnv.genSnapshotFile).thenReturn(genSnapshotFile);
when(
() => shorebirdEnv.analyzeSnapshotFile,
).thenReturn(analyzeSnapshotFile);
() => flutterArtifacts.getArtifactPath(
artifact: FlutterArtifact.genSnapshot,
),
).thenReturn(genSnapshotFile.path);
when(
() => flutterArtifacts.getArtifactPath(
artifact: FlutterArtifact.analyzeSnapshot,
),
).thenReturn(analyzeSnapshotFile.path);
when(() => shorebirdEnv.flutterRevision).thenReturn(flutterRevision);
when(() => shorebirdEnv.isRunningOnCI).thenReturn(false);
when(() => shorebirdFlutter.useRevision(revision: any(named: 'revision')))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:shorebird_cli/src/commands/patch/patch.dart';
import 'package:shorebird_cli/src/config/config.dart';
import 'package:shorebird_cli/src/deployment_track.dart';
import 'package:shorebird_cli/src/doctor.dart';
import 'package:shorebird_cli/src/flutter_artifacts.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/os/operating_system_interface.dart';
import 'package:shorebird_cli/src/patch_diff_checker.dart';
Expand Down Expand Up @@ -80,6 +81,7 @@ flutter:
late Directory projectRoot;
late Directory flutterDirectory;
late File genSnapshotFile;
late FlutterArtifacts flutterArtifacts;
late Doctor doctor;
late IosArchiveDiffer archiveDiffer;
late PatchDiffChecker patchDiffChecker;
Expand All @@ -105,6 +107,7 @@ flutter:
authRef.overrideWith(() => auth),
codePushClientWrapperRef.overrideWith(() => codePushClientWrapper),
doctorRef.overrideWith(() => doctor),
flutterArtifactsRef.overrideWith(() => flutterArtifacts),
loggerRef.overrideWith(() => logger),
osInterfaceRef.overrideWith(() => operatingSystemInterface),
patchDiffCheckerRef.overrideWith(() => patchDiffChecker),
Expand Down Expand Up @@ -171,6 +174,7 @@ flutter:
archiveDiffer = MockIosArchiveDiffer();
codePushClientWrapper = MockCodePushClientWrapper();
doctor = MockDoctor();
flutterArtifacts = MockFlutterArtifacts();
patchDiffChecker = MockPatchDiffChecker();
platform = MockPlatform();
shorebirdRoot = Directory.systemTemp.createTempSync();
Expand Down Expand Up @@ -243,7 +247,11 @@ flutter:
() => shorebirdEnv.getShorebirdProjectRoot(),
).thenReturn(projectRoot);
when(() => shorebirdEnv.flutterDirectory).thenReturn(flutterDirectory);
when(() => shorebirdEnv.genSnapshotFile).thenReturn(genSnapshotFile);
when(
() => flutterArtifacts.getArtifactPath(
artifact: FlutterArtifact.genSnapshot,
),
).thenReturn(genSnapshotFile.path);
when(() => shorebirdEnv.flutterRevision).thenReturn(flutterRevision);
when(() => shorebirdEnv.isRunningOnCI).thenReturn(false);
when(
Expand Down
Loading

0 comments on commit ef38380

Please sign in to comment.