forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 10
refactor: Move logic for compiling shorebird.yaml into a new file #61
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7e04938
refactor: Move logic for compiling shorebird.yaml into a new file
eseidel 3131a27
fix test name
eseidel d555cd9
Add more tests
eseidel 44bf7ca
Attempt to fix CI rules too
eseidel feb1383
Add running Flutter Tools tests
eseidel 8fb3074
rename shorebird_tests to shorebird_ci
eseidel e0c785c
run shorebird_ci on all PRs
eseidel db24fa3
Merge branch 'shorebird/dev' into shorebird_yaml
eseidel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/flutter_tools/lib/src/shorebird/shorebird_yaml.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2024 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:yaml/yaml.dart'; | ||
import 'package:yaml_edit/yaml_edit.dart'; | ||
|
||
import '../base/file_system.dart'; | ||
import '../build_info.dart'; | ||
import '../globals.dart' as globals; | ||
|
||
void updateShorebirdYaml(BuildInfo buildInfo, String shorebirdYamlPath) { | ||
final File shorebirdYaml = globals.fs.file(shorebirdYamlPath); | ||
if (!shorebirdYaml.existsSync()) { | ||
throw Exception('shorebird.yaml not found at $shorebirdYamlPath'); | ||
} | ||
final YamlDocument input = loadYamlDocument(shorebirdYaml.readAsStringSync()); | ||
final YamlMap yamlMap = input.contents as YamlMap; | ||
final Map<String, dynamic> compiled = compileShorebirdYaml(yamlMap, flavor: buildInfo.flavor, environment: globals.platform.environment); | ||
// Currently we write out over the same yaml file, we should fix this to | ||
// write to a new .json file instead and avoid naming confusion between the | ||
// input and compiled files. | ||
final YamlEditor yamlEditor = YamlEditor(''); | ||
yamlEditor.update(<Object?>[], compiled); | ||
shorebirdYaml.writeAsStringSync(yamlEditor.toString(), flush: true); | ||
} | ||
|
||
String appIdForFlavor(YamlMap yamlMap, {required String? flavor}) { | ||
if (flavor == null) { | ||
final String? defaultAppId = yamlMap['app_id'] as String?; | ||
if (defaultAppId == null || defaultAppId.isEmpty) { | ||
throw Exception('Cannot find "app_id" in shorebird.yaml'); | ||
} | ||
return defaultAppId; | ||
} | ||
|
||
final YamlMap? yamlFlavors = yamlMap['flavors'] as YamlMap?; | ||
if (yamlFlavors == null) { | ||
throw Exception('Cannot find "flavors" in shorebird.yaml.'); | ||
} | ||
final String? flavorAppId = yamlFlavors[flavor] as String?; | ||
if (flavorAppId == null || flavorAppId.isEmpty) { | ||
throw Exception('Cannot find "app_id" for $flavor in shorebird.yaml'); | ||
} | ||
return flavorAppId; | ||
} | ||
|
||
Map<String, dynamic> compileShorebirdYaml(YamlMap yamlMap, {required String? flavor, required Map<String, String> environment}) { | ||
final String appId = appIdForFlavor(yamlMap, flavor: flavor); | ||
final Map<String, dynamic> compiled = <String, dynamic>{ | ||
'app_id': appId, | ||
}; | ||
void copyIfSet(String key) { | ||
if (yamlMap[key] != null) { | ||
compiled[key] = yamlMap[key]; | ||
} | ||
} | ||
copyIfSet('base_url'); | ||
copyIfSet('auto_update'); | ||
final String? shorebirdPublicKeyEnvVar = environment['SHOREBIRD_PUBLIC_KEY']; | ||
if (shorebirdPublicKeyEnvVar != null) { | ||
compiled['patch_public_key'] = shorebirdPublicKeyEnvVar; | ||
} | ||
return compiled; | ||
} |
73 changes: 73 additions & 0 deletions
73
packages/flutter_tools/test/general.shard/shorebird/shorebird_yaml_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2024 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter_tools/src/shorebird/shorebird_yaml.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:yaml/yaml.dart'; | ||
|
||
void main() { | ||
group('ShorebirdYaml', () { | ||
test('yaml ignores comments', () { | ||
const String yamlContents = ''' | ||
# This file is used to configure the Shorebird updater used by your app. | ||
app_id: 6160a7d8-cc18-4928-1233-05b51c0bb02c | ||
|
||
# auto_update controls if Shorebird should automatically update in the background on launch. | ||
auto_update: false | ||
'''; | ||
final YamlDocument input = loadYamlDocument(yamlContents); | ||
final YamlMap yamlMap = input.contents as YamlMap; | ||
final Map<String, dynamic> compiled = | ||
compileShorebirdYaml(yamlMap, flavor: null, environment: <String, String>{}); | ||
expect(compiled, <String, dynamic>{ | ||
'app_id': '6160a7d8-cc18-4928-1233-05b51c0bb02c', | ||
'auto_update': false, | ||
}); | ||
}); | ||
test('flavors', () { | ||
// These are invalid app_ids but make for easy testing. | ||
const String yamlContents = ''' | ||
app_id: 1-a | ||
auto_update: false | ||
flavors: | ||
foo: 2-a | ||
bar: 3-a | ||
'''; | ||
final YamlDocument input = loadYamlDocument(yamlContents); | ||
final YamlMap yamlMap = input.contents as YamlMap; | ||
expect(appIdForFlavor(yamlMap, flavor: null), '1-a'); | ||
expect(appIdForFlavor(yamlMap, flavor: 'foo'), '2-a'); | ||
expect(appIdForFlavor(yamlMap, flavor: 'bar'), '3-a'); | ||
expect(() => appIdForFlavor(yamlMap, flavor: 'unknown'), throwsException); | ||
}); | ||
test('all values', () { | ||
// These are invalid app_ids but make for easy testing. | ||
const String yamlContents = ''' | ||
app_id: 1-a | ||
auto_update: false | ||
flavors: | ||
foo: 2-a | ||
bar: 3-a | ||
base_url: https://example.com | ||
'''; | ||
final YamlDocument input = loadYamlDocument(yamlContents); | ||
final YamlMap yamlMap = input.contents as YamlMap; | ||
final Map<String, dynamic> compiled1 = | ||
compileShorebirdYaml(yamlMap, flavor: null, environment: <String, String>{}); | ||
expect(compiled1, <String, dynamic>{ | ||
'app_id': '1-a', | ||
'auto_update': false, | ||
'base_url': 'https://example.com', | ||
}); | ||
final Map<String, dynamic> compiled2 = | ||
compileShorebirdYaml(yamlMap, flavor: 'foo', environment: <String, String>{'SHOREBIRD_PUBLIC_KEY': '4-a'}); | ||
expect(compiled2, <String, dynamic>{ | ||
'app_id': '2-a', | ||
'auto_update': false, | ||
'base_url': 'https://example.com', | ||
'patch_public_key': '4-a', | ||
}); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't it use itself?