Skip to content

Commit 81af62c

Browse files
authored
clean up shorebird.yaml update in flutter.groovy (#52)
1 parent 153f83c commit 81af62c

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

packages/flutter_tools/gradle/src/main/groovy/flutter.groovy

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,42 +1332,32 @@ class FlutterPlugin implements Plugin<Project> {
13321332
}
13331333
Task copyFlutterAssetsTask = addFlutterDeps(variant)
13341334
copyFlutterAssetsTask.doLast {
1335-
def content = "";
1335+
def yaml = new Yaml()
13361336
def outputDir = copyFlutterAssetsTask.destinationDir
1337+
1338+
// Read the shorebird.yaml file into a map.
13371339
def shorebirdYamlFile = new File("${outputDir}/flutter_assets/shorebird.yaml")
1340+
def shorebirdYamlData = yaml.load(shorebirdYamlFile.text)
13381341

1339-
def usedFlavors = variant.flavorName != null && !variant.flavorName.isEmpty();
1340-
if (usedFlavors) {
1341-
def flavor = variant.flavorName
1342-
def shorebirdYaml = new Yaml().load(shorebirdYamlFile.text)
1343-
def flavorAppId = shorebirdYaml['flavors'][flavor]
1344-
if (flavorAppId == null) {
1345-
throw new GradleException("Cannot find app_id for ${flavor} in shorebird.yaml")
1346-
}
1347-
content += 'app_id: ' + flavorAppId + '\n';
1348-
if (shorebirdYaml.containsKey('base_url')) {
1349-
content += 'base_url: ' + shorebirdYaml['base_url'] + '\n';
1350-
}
1351-
if (shorebirdYaml.containsKey('auto_update')) {
1352-
content += 'auto_update: ' + shorebirdYaml['auto_update'] + '\n';
1353-
}
1342+
// Update the app_id to the correct flavor if one was provided.
1343+
if (variant.flavorName != null && !variant.flavorName.isEmpty()) {
1344+
shorebirdYamlData['app_id'] = shorebirdYamlData.flavors[variant.flavorName]
13541345
}
13551346

1347+
// Remove any flavors. This is a no-op if the flavors key doesn't exist.
1348+
shorebirdYamlData.remove('flavors')
1349+
1350+
// Add a public key if one was provided via an env var.
1351+
// Ideally we'd have a way to pass the key as a parameter, but
1352+
// an env var was the easiest way to get this working.
13561353
def shorebirdPublicKeyEnvVar = System.getenv('SHOREBIRD_PUBLIC_KEY')
13571354
if (shorebirdPublicKeyEnvVar != null && !shorebirdPublicKeyEnvVar.isEmpty()) {
1358-
// When a flavor were used, the content variable will already include
1359-
// the app_id and other attributes, since the code above makes sure of that
1360-
//
1361-
// But when no flavor was used, it will be empty, so we make sure that include
1362-
// the original file in the beginning
1363-
if (!usedFlavors) {
1364-
content += shorebirdYamlFile.text;
1365-
}
1366-
content += 'patch_public_key: ' + shorebirdPublicKeyEnvVar + '\n';
1367-
}
1368-
if (!content.isEmpty()) {
1369-
shorebirdYamlFile.write(content)
1355+
shorebirdYamlData['patch_public_key'] = shorebirdPublicKeyEnvVar
13701356
}
1357+
1358+
// Write the updated map back to the shorebird.yaml file.
1359+
def updatedYamlString = yaml.dumpAsMap(shorebirdYamlData)
1360+
shorebirdYamlFile.write(updatedYamlString)
13711361
}
13721362
def variantOutput = variant.outputs.first()
13731363
def processResources = variantOutput.hasProperty(propProcessResourcesProvider) ?

0 commit comments

Comments
 (0)