Skip to content

Commit

Permalink
feat(shorebird_cli): Add support for updater_tools (#2124)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanoltman authored May 20, 2024
1 parent ac17716 commit 1d64dcb
Show file tree
Hide file tree
Showing 24 changed files with 562 additions and 141 deletions.
2 changes: 1 addition & 1 deletion packages/artifact_proxy/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -578,4 +578,4 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.4.0-256.0.dev <4.0.0"
dart: ">=3.4.0 <4.0.0"
2 changes: 1 addition & 1 deletion packages/discord_gcp_alerts/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -570,4 +570,4 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.4.0-256.0.dev <4.0.0"
dart: ">=3.4.0 <4.0.0"
1 change: 1 addition & 0 deletions packages/shorebird_cli/bin/shorebird.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Future<void> main(List<String> args) async {
shorebirdToolsRef,
shorebirdValidatorRef,
shorebirdVersionRef,
updaterToolsRef,
xcodeBuildRef,
},
),
Expand Down
36 changes: 22 additions & 14 deletions packages/shorebird_cli/lib/src/artifact_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,41 @@ class ArtifactManager {
/// Generates a binary diff between two files and returns the path to the
/// output diff file.
Future<String> createDiff({
required String releaseArtifactPath,
required String patchArtifactPath,
required File releaseArtifact,
required File patchArtifact,
}) async {
if (!File(releaseArtifactPath).existsSync()) {
if (!releaseArtifact.existsSync()) {
throw FileSystemException(
'Release artifact does not exist',
releaseArtifactPath,
releaseArtifact.path,
);
}

if (!File(patchArtifactPath).existsSync()) {
if (!patchArtifact.existsSync()) {
throw FileSystemException(
'Patch artifact does not exist',
patchArtifactPath,
patchArtifact.path,
);
}

final tempDir = await Directory.systemTemp.createTemp();
final diffPath = p.join(tempDir.path, 'diff.patch');

await patchExecutable.run(
releaseArtifactPath: releaseArtifactPath,
patchArtifactPath: patchArtifactPath,
diffPath: diffPath,
);
final outputFile = File(p.join(tempDir.path, 'diff.patch'));
final updaterToolsFile = File(updaterTools.path);
if (updaterToolsFile.existsSync()) {
await updaterTools.createDiff(
releaseArtifact: releaseArtifact,
patchArtifact: patchArtifact,
outputFile: outputFile,
);
} else {
await patchExecutable.run(
releaseArtifactPath: releaseArtifact.path,
patchArtifactPath: patchArtifact.path,
diffPath: outputFile.path,
);
}

return diffPath;
return outputFile.path;
}

/// Downloads the file at the given [uri] to a temporary directory and returns
Expand Down
32 changes: 32 additions & 0 deletions packages/shorebird_cli/lib/src/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Cache {
registerArtifact(BundleToolArtifact(cache: this, platform: platform));
registerArtifact(AotToolsDillArtifact(cache: this, platform: platform));
registerArtifact(AotToolsExeArtifact(cache: this, platform: platform));
registerArtifact(UpdaterToolsArtifact(cache: this, platform: platform));
}

void registerArtifact(CachedArtifact artifact) => _artifacts.add(artifact);
Expand Down Expand Up @@ -297,3 +298,34 @@ class BundleToolArtifact extends CachedArtifact {
return 'https://github.com/google/bundletool/releases/download/1.15.6/bundletool-all-1.15.6.jar';
}
}

/// {@template updater_tools_artifact}
/// Tools used to package patch artifacts for use by the Updater.
/// {@endtemplate}
class UpdaterToolsArtifact extends CachedArtifact {
/// {@macro updater_tools_artifact}
UpdaterToolsArtifact({required super.cache, required super.platform});

@override
String get name => 'updater-tools.dill';

@override
bool get isExecutable => false;

/// Updater tools was introduced in release 1.1.7.
// TODO(bryanoltman): add engine rev and flutter version once this is nailed down
@override
bool get required => false;

@override
Directory get location => Directory(
p.join(
cache.getArtifactDirectory(name).path,
shorebirdEnv.shorebirdEngineRevision,
),
);

@override
String get storageUrl =>
'${cache.storageBaseUrl}/${cache.storageBucket}/shorebird/${shorebirdEnv.shorebirdEngineRevision}/$name';
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ class AarPatcher extends Patcher {
final hash = sha256.convert(await patchArtifact.readAsBytes()).toString();
try {
final diffPath = await artifactManager.createDiff(
releaseArtifactPath: releaseArtifactPath.value,
patchArtifactPath: artifactPath,
releaseArtifact: File(releaseArtifactPath.value),
patchArtifact: patchArtifact,
);
patchArtifactBundles[arch] = PatchArtifactBundle(
arch: arch.arch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ Looked in:
final hash = sha256.convert(await patchArtifact.readAsBytes()).toString();
try {
final diffPath = await artifactManager.createDiff(
releaseArtifactPath: releaseArtifactPath.value,
patchArtifactPath: patchArtifactPath,
releaseArtifact: File(releaseArtifactPath.value),
patchArtifact: patchArtifact,
);
patchArtifactBundles[releaseArtifactPath.key] = PatchArtifactBundle(
arch: arch.arch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ class IosFrameworkPatcher extends Patcher {

patchFile = File(
await artifactManager.createDiff(
releaseArtifactPath: patchBaseFile.path,
patchArtifactPath: patchBuildFile.path,
releaseArtifact: patchBaseFile,
patchArtifact: patchBuildFile,
),
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ class IosPatcher extends Patcher {

patchFile = File(
await artifactManager.createDiff(
releaseArtifactPath: patchBaseFile.path,
patchArtifactPath: patchBuildFile.path,
releaseArtifact: patchBaseFile,
patchArtifact: patchBuildFile,
),
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/shorebird_cli/lib/src/executables/aot_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class AotTools {
workingDirectory: workingDirectory,
);

if (result.exitCode != 0) {
if (result.exitCode != ExitCode.success.code) {
throw Exception('Failed to link: ${result.stderr}');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export 'ios_deploy.dart';
export 'java.dart';
export 'patch_executable.dart';
export 'shorebird_tools.dart';
export 'updater_tools.dart';
export 'xcodebuild.dart';
12 changes: 7 additions & 5 deletions packages/shorebird_cli/lib/src/executables/patch_executable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@ class PatchFailedException implements Exception {
///
/// Throws [PatchFailedException] if the patch command exits with non-zero code.
class PatchExecutable {
/// The path to the `patch` executable.
String get path => p.join(
cache.getArtifactDirectory('patch').path,
'patch',
);

Future<void> run({
required String releaseArtifactPath,
required String patchArtifactPath,
required String diffPath,
}) async {
final diffExecutable = p.join(
cache.getArtifactDirectory('patch').path,
'patch',
);
final diffArguments = [
releaseArtifactPath,
patchArtifactPath,
diffPath,
];

final result = await process.run(diffExecutable, diffArguments);
final result = await process.run(path, diffArguments);

if (result.exitCode != ExitCode.success.code) {
throw PatchFailedException(
Expand Down
65 changes: 65 additions & 0 deletions packages/shorebird_cli/lib/src/executables/updater_tools.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:mason_logger/mason_logger.dart';
import 'package:scoped_deps/scoped_deps.dart';
import 'package:shorebird_cli/src/executables/patch_executable.dart';
import 'package:shorebird_cli/src/shorebird_artifacts.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';
import 'package:shorebird_cli/src/shorebird_process.dart';
import 'package:shorebird_cli/src/third_party/flutter_tools/lib/flutter_tools.dart';

/// A reference to a [UpdaterTools] instance.
final updaterToolsRef = create(UpdaterTools.new);

/// The [UpdaterTools] instance available in the current zone.
UpdaterTools get updaterTools => read(updaterToolsRef);

class UpdaterTools {
/// Path to the updater tools .dill file.
String get path => shorebirdArtifacts.getArtifactPath(
artifact: ShorebirdArtifact.updaterTools,
);

Future<ShorebirdProcessResult> _exec(
List<String> command, {
String? workingDirectory,
}) async {
// local engine versions use .dart and we distribute aot-tools as a .dill
return process.run(
shorebirdEnv.dartBinaryFile.path,
['run', path, ...command],
workingDirectory: workingDirectory,
);
}

/// Create a binary diff between a release artifact and a patch artifact.
Future<void> createDiff({
required File releaseArtifact,
required File patchArtifact,
required File outputFile,
}) async {
if (!releaseArtifact.existsSync()) {
throw FileSystemException(
'Release artifact does not exist',
releaseArtifact.path,
);
}

if (!patchArtifact.existsSync()) {
throw FileSystemException(
'Patch artifact does not exist',
patchArtifact.path,
);
}

final result = await _exec([
'diff',
'--release=${releaseArtifact.path}',
'--patch=${patchArtifact.path}',
'--patch-executable=${patchExecutable.path}',
'--output=${outputFile.path}',
]);

if (result.exitCode != ExitCode.success.code) {
throw Exception('Failed to create diff: ${result.stderr}');
}
}
}
30 changes: 30 additions & 0 deletions packages/shorebird_cli/lib/src/shorebird_artifacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ enum ShorebirdArtifact {

/// The gen_snapshot executable.
genSnapshot,

/// The updater_tools kernel file.
updaterTools,
}

/// A reference to a [ShorebirdArtifacts] instance.
Expand Down Expand Up @@ -54,6 +57,8 @@ class ShorebirdCachedArtifacts implements ShorebirdArtifacts {
return _aotToolsFile.path;
case ShorebirdArtifact.genSnapshot:
return _genSnapshotFile.path;
case ShorebirdArtifact.updaterTools:
return _updaterToolsFile.path;
}
}

Expand Down Expand Up @@ -108,6 +113,16 @@ class ShorebirdCachedArtifacts implements ShorebirdArtifacts {
),
);
}

File get _updaterToolsFile {
return File(
p.join(
cache.getArtifactDirectory('updater-tools').path,
shorebirdEnv.shorebirdEngineRevision,
'updater-tools.dill',
),
);
}
}

/// {@template shorebird_local_engine_artifacts}
Expand All @@ -126,6 +141,8 @@ class ShorebirdLocalEngineArtifacts implements ShorebirdArtifacts {
return _aotToolsFile.path;
case ShorebirdArtifact.genSnapshot:
return _genSnapshotFile.path;
case ShorebirdArtifact.updaterTools:
return _updaterToolsFile.path;
}
}

Expand Down Expand Up @@ -166,4 +183,17 @@ class ShorebirdLocalEngineArtifacts implements ShorebirdArtifacts {
),
);
}

File get _updaterToolsFile {
return File(
p.join(
engineConfig.localEngineSrcPath!,
'third_party',
'updater',
'updater_tools',
'bin',
'updater_tools.dart',
),
);
}
}
4 changes: 2 additions & 2 deletions packages/shorebird_cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,10 @@ packages:
dependency: "direct main"
description:
name: scoped_deps
sha256: "7416a9026f3b457dda634a8e83c8e47d7ece33bef360ffba114c2d89255166ae"
sha256: bc54cece4fed785157dc53b7554d31107f574897f4b2d1196db905a38c084e31
url: "https://pub.dev"
source: hosted
version: "0.1.0+1"
version: "0.1.0+2"
shelf:
dependency: transitive
description:
Expand Down
Loading

0 comments on commit 1d64dcb

Please sign in to comment.