Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(shorebird_cli): adjust shorebird.yaml flavors format #440

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Defaults to the app_id in "shorebird.yaml".''',
if (appIdArg == null) {
String? defaultAppId;
try {
defaultAppId = getShorebirdYaml()?.appId.value;
defaultAppId = getShorebirdYaml()?.appId;
} catch (_) {}

appId = logger.prompt(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class CreateChannelsCommand extends ShorebirdCommand
hostedUri: hostedUri,
);

final appId =
results[_appIdOption] as String? ?? getShorebirdYaml()?.appId.value;
final appId = results[_appIdOption] as String? ?? getShorebirdYaml()?.appId;
if (appId == null) {
logger.err(
'''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class ListChannelsCommand extends ShorebirdCommand
hostedUri: hostedUri,
);

final appId =
results[_appIdOption] as String? ?? getShorebirdYaml()?.appId.value;
final appId = results[_appIdOption] as String? ?? getShorebirdYaml()?.appId;
if (appId == null) {
logger.err(
'''
Expand Down
11 changes: 6 additions & 5 deletions packages/shorebird_cli/lib/src/commands/init_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:io';
import 'package:mason_logger/mason_logger.dart';
import 'package:shorebird_cli/src/auth_logger_mixin.dart';
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/config/config.dart';
import 'package:shorebird_cli/src/shorebird_config_mixin.dart';
import 'package:shorebird_cli/src/shorebird_create_app_mixin.dart';
import 'package:shorebird_cli/src/shorebird_flavor_mixin.dart';
Expand Down Expand Up @@ -75,7 +74,8 @@ If you want to reinitialize Shorebird, please run "shorebird init --force".''');
return ExitCode.software.code;
}

final AppId appId;
final String appId;
Map<String, String>? flavors;
try {
final displayName = logger.prompt(
'${lightGreen.wrap('?')} How should we refer to this app?',
Expand All @@ -88,16 +88,17 @@ If you want to reinitialize Shorebird, please run "shorebird init --force".''');
values[flavor] =
(await createApp(appName: '$displayName ($flavor)')).id;
}
appId = AppId(values: values);
flavors = values;
appId = flavors.values.first;
} else {
appId = AppId(value: (await createApp(appName: displayName)).id);
appId = (await createApp(appName: displayName)).id;
}
} catch (error) {
logger.err('$error');
return ExitCode.software.code;
}

addShorebirdYamlToProject(appId);
addShorebirdYamlToProject(appId, flavors: flavors);

if (!pubspecContainsShorebirdYaml) addShorebirdYamlToPubspecAssets();

Expand Down
2 changes: 1 addition & 1 deletion packages/shorebird_cli/lib/src/commands/patch_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class PatchCommand extends ShorebirdCommand
return ExitCode.software.code;
}

final appId = shorebirdYaml.appId.value;
final appId = shorebirdYaml.appId;
final app = apps.firstWhereOrNull((a) => a.id == appId);
if (app == null) {
logger.err(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ make smaller updates to your app.
return ExitCode.software.code;
}

final appId = shorebirdYaml.appId.value;
final appId = shorebirdYaml.appId;
final app = apps.firstWhereOrNull((a) => a.id == appId);
if (app == null) {
logger.err(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DeleteReleasesCommand extends ShorebirdCommand
return ExitCode.config.code;
}

final appId = getShorebirdYaml()!.appId.value!;
final appId = getShorebirdYaml()!.appId;

final codePushClient = buildCodePushClient(
httpClient: auth.client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ListReleasesCommand extends ShorebirdCommand
return ExitCode.config.code;
}

final appId = getShorebirdYaml()!.appId.value!;
final appId = getShorebirdYaml()!.appId;

final codePushClient = buildCodePushClient(
httpClient: auth.client,
Expand Down
33 changes: 8 additions & 25 deletions packages/shorebird_cli/lib/src/config/shorebird_yaml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,20 @@ part 'shorebird_yaml.g.dart';
)
class ShorebirdYaml {
/// {@macro shorebird_yaml}
const ShorebirdYaml({required this.appId, this.baseUrl});
const ShorebirdYaml({required this.appId, this.flavors, this.baseUrl});

factory ShorebirdYaml.fromJson(Map<dynamic, dynamic> json) =>
_$ShorebirdYamlFromJson(json);

@JsonKey(fromJson: AppId.fromJson)
final AppId appId;
final String? baseUrl;
}

/// {@template app_id}
/// The unique identifier for the app. Can be a single string or a map of
/// flavor names to ids for multi-flavor apps.
/// {@endtemplate}
class AppId {
/// {@macro app_id}
const AppId({this.value, this.values});

factory AppId.fromJson(dynamic json) {
if (json is String) return AppId(value: json);
return AppId(values: (json as Map).cast<String, String>());
}

/// A single app id.
///
/// Will be `null` for multi-flavor apps (if [values] is not `null`).
/// The base app id.
///
/// Example:
/// `"8d3155a8-a048-4820-acca-824d26c29b71"`
final String? value;
final String appId;

/// A map of flavor names to app ids.
///
/// Will be `null` for apps with no flavors (if [value] is not `null`).
/// Will be `null` for apps with no flavors.
///
/// Example:
/// ```json
Expand All @@ -54,5 +34,8 @@ class AppId {
/// "production": "d458e87a-7362-4386-9eeb-629db2af413a"
/// }
/// ```
final Map<String, String>? values;
final Map<String, String>? flavors;

/// The base url used to check for updates.
final String? baseUrl;
}
9 changes: 7 additions & 2 deletions packages/shorebird_cli/lib/src/config/shorebird_yaml.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions packages/shorebird_cli/lib/src/shorebird_config_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ mixin ShorebirdConfigMixin on ShorebirdCommand {
return Pubspec.parse(yaml);
}

ShorebirdYaml addShorebirdYamlToProject(AppId appId) {
ShorebirdYaml addShorebirdYamlToProject(
String appId, {
Map<String, String>? flavors,
}) {
const content = '''
# This file is used to configure the Shorebird updater used by your application.
# Learn more at https://shorebird.dev
Expand All @@ -65,8 +68,9 @@ mixin ShorebirdConfigMixin on ShorebirdCommand {
app_id:
''';

final editor = YamlEditor(content)
..update(['app_id'], appId.value ?? appId.values);
final editor = YamlEditor(content)..update(['app_id'], appId);

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

getShorebirdYamlFile().writeAsStringSync(editor.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ If you want to reinitialize Shorebird, please run "shorebird init --force".''',
expect(
File(p.join(tempDir.path, 'shorebird.yaml')).readAsStringSync(),
contains('''
app_id:
app_id: ${appIds[0]}
flavors:
development: ${appIds[0]}
production: ${appIds[1]}
staging: ${appIds[2]}'''),
Expand Down
15 changes: 8 additions & 7 deletions packages/shorebird_cli/test/src/config/shorebird_yaml_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:test/test.dart';

void main() {
group('ShorebirdYaml', () {
test('can be deserialized with single app_id', () {
test('can be deserialized without flavors', () {
const yaml = '''
app_id: test_app_id
base_url: https://example.com
Expand All @@ -13,14 +13,15 @@ base_url: https://example.com
yaml,
(m) => ShorebirdYaml.fromJson(m!),
);
expect(shorebirdYaml.appId.value, 'test_app_id');
expect(shorebirdYaml.appId.values, isNull);
expect(shorebirdYaml.appId, 'test_app_id');
expect(shorebirdYaml.flavors, isNull);
expect(shorebirdYaml.baseUrl, 'https://example.com');
});

test('can be deserialized with multiple app_id', () {
test('can be deserialized with flavors', () {
const yaml = '''
app_id:
app_id: test_app_id1
flavors:
development: test_app_id1
production: test_app_id2
base_url: https://example.com
Expand All @@ -29,8 +30,8 @@ base_url: https://example.com
yaml,
(m) => ShorebirdYaml.fromJson(m!),
);
expect(shorebirdYaml.appId.value, isNull);
expect(shorebirdYaml.appId.values, {
expect(shorebirdYaml.appId, equals('test_app_id1'));
expect(shorebirdYaml.flavors, {
'development': 'test_app_id1',
'production': 'test_app_id2',
});
Expand Down