Skip to content

Commit

Permalink
fix(shorebird_cli): shorebird should work within child directories (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Nov 14, 2023
1 parent a73a4f2 commit 8df2e3c
Show file tree
Hide file tree
Showing 27 changed files with 1,077 additions and 1,344 deletions.
3 changes: 2 additions & 1 deletion packages/shorebird_cli/lib/src/code_push_client_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,15 @@ Please create a release using "shorebird release" and try again.
required String appId,
required int releaseId,
required ReleasePlatform platform,
required String projectRoot,
required String aabPath,
required Map<Arch, ArchMetadata> architectures,
String? flavor,
}) async {
final createArtifactProgress = logger.progress('Creating artifacts');
for (final archMetadata in architectures.values) {
final artifactPath = p.join(
Directory.current.path,
projectRoot,
'build',
'app',
'intermediates',
Expand Down
32 changes: 21 additions & 11 deletions packages/shorebird_cli/lib/src/commands/init_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class InitCommand extends ShorebirdCommand {
if (!shorebirdEnv.hasPubspecYaml) {
logger.err('''
Could not find a "pubspec.yaml".
Please make sure you are running "shorebird init" from the root of your Flutter project.
Please make sure you are running "shorebird init" from within your Flutter project.
''');
return ExitCode.noInput.code;
}
Expand All @@ -66,11 +66,12 @@ Please make sure you are running "shorebird init" from the root of your Flutter
Set<String>? androidFlavors;
Set<String>? iosFlavors;
var productFlavors = <String>{};
final projectRoot = shorebirdEnv.getFlutterProjectRoot()!;
final detectFlavorsProgress = logger.progress('Detecting product flavors');
try {
final flavors = await Future.wait([
_maybeGetAndroidFlavors(Directory.current.path),
_maybeGetiOSFlavors(Directory.current.path),
_maybeGetAndroidFlavors(projectRoot.path),
_maybeGetiOSFlavors(projectRoot.path),
]);
androidFlavors = flavors[0];
iosFlavors = flavors[1];
Expand Down Expand Up @@ -133,7 +134,8 @@ Please make sure you are running "shorebird init" from the root of your Flutter
flavorsToAppIds[flavor] = app.id;
}
_addShorebirdYamlToProject(
shorebirdYaml.appId,
projectRoot: projectRoot,
appId: shorebirdYaml.appId,
flavors: flavorsToAppIds,
);
updateShorebirdYamlProgress.complete('Flavors added to shorebird.yaml');
Expand Down Expand Up @@ -201,10 +203,16 @@ Please make sure you are running "shorebird init" from the root of your Flutter
return ExitCode.software.code;
}

_addShorebirdYamlToProject(appId, flavors: flavors);
_addShorebirdYamlToProject(
projectRoot: projectRoot,
appId: appId,
flavors: flavors,
);

if (!shorebirdEnv.pubspecContainsShorebirdYaml) {
_addShorebirdYamlToPubspecAssets();
_addShorebirdYamlToPubspecAssets(
shorebirdEnv.getPubspecYamlFile(cwd: projectRoot),
);
}

logger.info(
Expand Down Expand Up @@ -274,8 +282,9 @@ For more information about Shorebird, visit ${link(uri: Uri.parse('https://shore
}
}

ShorebirdYaml _addShorebirdYamlToProject(
String appId, {
ShorebirdYaml _addShorebirdYamlToProject({
required String appId,
required Directory projectRoot,
Map<String, String>? flavors,
}) {
const content = '''
Expand All @@ -299,13 +308,14 @@ app_id:

if (flavors != null) editor.update(['flavors'], flavors);

shorebirdEnv.getShorebirdYamlFile().writeAsStringSync(editor.toString());
shorebirdEnv
.getShorebirdYamlFile(cwd: projectRoot)
.writeAsStringSync(editor.toString());

return ShorebirdYaml(appId: appId);
}

void _addShorebirdYamlToPubspecAssets() {
final pubspecFile = shorebirdEnv.getPubspecYamlFile();
void _addShorebirdYamlToPubspecAssets(File pubspecFile) {
final pubspecContents = pubspecFile.readAsStringSync();
final yaml = loadYaml(pubspecContents, sourceUrl: pubspecFile.uri) as Map;
final editor = YamlEditor(pubspecContents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,18 @@ If this option is not provided, the version number will be determined from the p
return ExitCode.software.code;
}

final projectRoot = shorebirdEnv.getShorebirdProjectRoot()!;

final bundleDirPath = p.join(
projectRoot.path,
'build',
'app',
'outputs',
'bundle',
);
final bundlePath = flavor != null
? './build/app/outputs/bundle/${flavor}Release/app-$flavor-release.aab'
: './build/app/outputs/bundle/release/app-release.aab';
? p.join(bundleDirPath, '${flavor}Release', 'app-$flavor-release.aab')
: p.join(bundleDirPath, 'release', 'app-release.aab');

final String releaseVersion;
final argReleaseVersion = results['release-version'] as String?;
Expand Down Expand Up @@ -251,7 +260,7 @@ Current Flutter Revision: $originalFlutterRevision
for (final releaseArtifactPath in releaseArtifactPaths.entries) {
final archMetadata = architectures[releaseArtifactPath.key]!;
final patchArtifactPath = p.join(
Directory.current.path,
projectRoot.path,
'build',
'app',
'intermediates',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,11 @@ ${summary.join('\n')}
return ExitCode.success.code;
}

String get _aotOutputPath =>
p.join(Directory.current.path, 'build', 'out.aot');
String get _aotOutputPath => p.join(
shorebirdEnv.getShorebirdProjectRoot()!.path,
'build',
'out.aot',
);

Future<void> _buildPatch() async {
final target = results['target'] as String?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ Please re-run the release command for this version or create a new release.''');
final newestDillFile = newestAppDill();
aotFile = await buildElfAotSnapshot(
appDillPath: newestDillFile.path,
outFilePath: p.join(Directory.current.path, 'build', 'out.aot'),
outFilePath: p.join(
shorebirdEnv.getShorebirdProjectRoot()!.path,
'build',
'out.aot',
),
);
} catch (error) {
buildProgress.fail('$error');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,10 @@ ${summary.join('\n')}

// Copy release AAR to a new directory to avoid overwriting with subsequent
// patch builds.
final sourceLibraryDirectory = Directory(
aarLibraryPath(
packageName: shorebirdEnv.androidPackageName!,
),
final sourceLibraryDirectory = Directory(aarLibraryPath);
final targetLibraryDirectory = Directory(
p.join(shorebirdEnv.getShorebirdProjectRoot()!.path, 'release'),
);
final targetLibraryDirectory =
Directory(p.join(Directory.current.path, 'release'));
await copyPath(sourceLibraryDirectory.path, targetLibraryDirectory.path);

final extractAarProgress = logger.progress('Creating artifacts');
Expand Down Expand Up @@ -209,7 +206,7 @@ dependencyResolutionManagement {
google()
mavenCentral()
+ maven {
+ url '../${p.basename(Directory.current.path)}/${p.relative(targetLibraryDirectory.path)}'
+ url '../${p.basename(shorebirdEnv.getShorebirdProjectRoot()!.path)}/${p.relative(targetLibraryDirectory.path)}'
+ }
+ maven {
- url 'https://storage.googleapis.com/download.flutter.io'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,28 @@ Please comment and upvote ${link(uri: Uri.parse('https://github.com/shorebirdtec
return ExitCode.software.code;
}

final projectRoot = shorebirdEnv.getShorebirdProjectRoot()!;
final shorebirdYaml = shorebirdEnv.getShorebirdYaml()!;
final appId = shorebirdYaml.getAppId(flavor: flavor);
final app = await codePushClientWrapper.getApp(appId: appId);

final bundleDirPath = p.join('build', 'app', 'outputs', 'bundle');
final bundleDirPath = p.join(
projectRoot.path,
'build',
'app',
'outputs',
'bundle',
);
final apkDirPath = p.join(
projectRoot.path,
'build',
'app',
'outputs',
'apk',
);
final bundlePath = flavor != null
? p.join(bundleDirPath, '${flavor}Release', 'app-$flavor-release.aab')
: p.join(bundleDirPath, 'release', 'app-release.aab');
final apkDirPath = p.join('build', 'app', 'outputs', 'apk');
final apkPath = flavor != null
? p.join(apkDirPath, flavor, 'release', 'app-$flavor-release.apk')
: p.join(apkDirPath, 'release', 'app-release.apk');
Expand Down Expand Up @@ -194,6 +207,7 @@ ${summary.join('\n')}
await codePushClientWrapper.createAndroidReleaseArtifacts(
appId: app.appId,
releaseId: release.id,
projectRoot: projectRoot.path,
aabPath: bundlePath,
platform: platform,
architectures: architectures,
Expand Down
54 changes: 31 additions & 23 deletions packages/shorebird_cli/lib/src/shorebird_artifact_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ import 'package:collection/collection.dart';
import 'package:path/path.dart' as p;
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';

mixin ShorebirdArtifactMixin on ShorebirdCommand {
String aarLibraryPath({required String packageName}) => p.joinAll([
Directory.current.path,
'build',
'host',
'outputs',
'repo',
]);
String get aarLibraryPath {
final projectRoot = shorebirdEnv.getShorebirdProjectRoot()!;
return p.joinAll([
projectRoot.path,
'build',
'host',
'outputs',
'repo',
]);
}

String aarArtifactDirectory({
required String packageName,
required String buildNumber,
}) =>
p.joinAll([
aarLibraryPath(packageName: packageName),
aarLibraryPath,
...packageName.split('.'),
'flutter_release',
buildNumber,
Expand Down Expand Up @@ -69,18 +73,17 @@ mixin ShorebirdArtifactMixin on ShorebirdCommand {
/// Returns the .xcarchive directory generated by `flutter build ipa`. This
/// was traditionally named `Runner.xcarchive`, but can now be renamed.
Directory? getXcarchiveDirectory() {
final projectRoot = shorebirdEnv.getShorebirdProjectRoot()!;
final archiveDirectory = Directory(
p.join(
Directory.current.path,
projectRoot.path,
'build',
'ios',
'archive',
),
);

if (!archiveDirectory.existsSync()) {
return null;
}
if (!archiveDirectory.existsSync()) return null;

return archiveDirectory
.listSync()
Expand Down Expand Up @@ -118,9 +121,10 @@ mixin ShorebirdArtifactMixin on ShorebirdCommand {
/// an exception if there is not exactly one .ipa file in the build directory,
/// or if there is no build directory.
String getIpaPath() {
final projectRoot = shorebirdEnv.getShorebirdProjectRoot()!;
final ipaBuildDirectory = Directory(
p.join(
Directory.current.path,
projectRoot.path,
'build',
'ios',
'ipa',
Expand Down Expand Up @@ -161,23 +165,27 @@ mixin ShorebirdArtifactMixin on ShorebirdCommand {
/// Returns the [Directory] containing the App.xcframework generated by
/// `shorebird release ios-framework-alpha` or
/// `shorebird patch ios-framework-alpha`.
Directory getAppXcframeworkDirectory() => Directory(
p.join(
Directory.current.path,
'build',
'ios',
'framework',
'Release',
),
);
Directory getAppXcframeworkDirectory() {
final projectRoot = shorebirdEnv.getShorebirdProjectRoot()!;
return Directory(
p.join(
projectRoot.path,
'build',
'ios',
'framework',
'Release',
),
);
}

/// Finds the most recently-edited app.dill file in the .dart_tool directory.
// TODO(bryanoltman): This is an enormous hack – we don't know that this is
// the correct file.
File newestAppDill() {
final projectRoot = shorebirdEnv.getShorebirdProjectRoot()!;
final dartToolBuildDir = Directory(
p.join(
Directory.current.path,
projectRoot.path,
'.dart_tool',
'flutter_build',
),
Expand Down
Loading

0 comments on commit 8df2e3c

Please sign in to comment.