Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 15 additions & 6 deletions packages/flutter_tools/gradle/src/main/groovy/flutter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -1336,12 +1336,8 @@ class FlutterPlugin implements Plugin<Project> {
def outputDir = copyFlutterAssetsTask.destinationDir
def shorebirdYamlFile = new File("${outputDir}/flutter_assets/shorebird.yaml")

def shorebirdPublicKeyEnvVar = System.getenv('SHOREBIRD_PUBLIC_KEY')
if (shorebirdPublicKeyEnvVar != null && !shorebirdPublicKeyEnvVar.isEmpty()) {
content += 'patch_public_key: ' + shorebirdPublicKeyEnvVar + '\n';
}

if (variant.flavorName != null && !variant.flavorName.isEmpty()) {
def usedFlavors = variant.flavorName != null && !variant.flavorName.isEmpty();
if (usedFlavors) {
def flavor = variant.flavorName
def shorebirdYaml = new Yaml().load(shorebirdYamlFile.text)
def flavorAppId = shorebirdYaml['flavors'][flavor]
Expand All @@ -1356,6 +1352,19 @@ class FlutterPlugin implements Plugin<Project> {
content += 'auto_update: ' + shorebirdYaml['auto_update'] + '\n';
}
}

def shorebirdPublicKeyEnvVar = System.getenv('SHOREBIRD_PUBLIC_KEY')
if (shorebirdPublicKeyEnvVar != null && !shorebirdPublicKeyEnvVar.isEmpty()) {
// When a flavor were used, the content variable will already include
// the app_id and other attributes, since the code above makes sure of that
//
// But when no flavor was used, it will be empty, so we make sure that include
// the original file in the beginning
if (!usedFlavors) {
content += shorebirdYamlFile.text;
}
content += 'patch_public_key: ' + shorebirdPublicKeyEnvVar + '\n';
}
if (!content.isEmpty()) {
shorebirdYamlFile.write(content)
}
Expand Down
83 changes: 49 additions & 34 deletions packages/shorebird_tests/test/android_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,32 @@ void main() {
expect(projectDirectory.shorebirdFile.existsSync(), isTrue);
});

testWithShorebirdProject(
'adds the public key when passed through the environment variable',
(projectDirectory) async {
const base64PublicKey = 'public_123';
await projectDirectory.runFlutterBuildApk(
environment: {
'SHOREBIRD_PUBLIC_KEY': base64PublicKey,
},
);
group('when passing the public key through the environment variable', () {
testWithShorebirdProject(
'adds the public key on top of the original file',
(projectDirectory) async {
final originalContent =
await projectDirectory.shorebirdFile.readAsString();

const base64PublicKey = 'public_123';
await projectDirectory.runFlutterBuildApk(
environment: {
'SHOREBIRD_PUBLIC_KEY': base64PublicKey,
},
);

final generatedYaml =
await projectDirectory.getGeneratedShorebirdYaml();
final generatedYaml =
await projectDirectory.getGeneratedShorebirdYaml();

expect(generatedYaml, contains('patch_public_key: $base64PublicKey'));
},
);
expect(
generatedYaml,
equals(
'${originalContent}patch_public_key: $base64PublicKey\n',
),
);
},
);
});

group('when building with a flavor', () {
testWithShorebirdProject(
Expand All @@ -46,29 +56,34 @@ void main() {
},
);

testWithShorebirdProject(
'correctly changes the app id and adds the public key when passed through the environment variable',
(projectDirectory) async {
const base64PublicKey = 'public_123';
projectDirectory.addAndroidFlavors();
projectDirectory.addShorebirdFlavors();
group('when passed through the environment variable', () {
testWithShorebirdProject(
'correctly changes the app id and adds the public key',
(projectDirectory) async {
const base64PublicKey = 'public_123';
projectDirectory.addAndroidFlavors();
projectDirectory.addShorebirdFlavors();

await projectDirectory.runFlutterBuildApk(
flavor: 'internal',
environment: {
'SHOREBIRD_PUBLIC_KEY': base64PublicKey,
},
);
await projectDirectory.runFlutterBuildApk(
flavor: 'internal',
environment: {
'SHOREBIRD_PUBLIC_KEY': base64PublicKey,
},
);

final generatedYaml =
await projectDirectory.getGeneratedShorebirdYaml(
flavor: 'internal',
);
final generatedYaml =
await projectDirectory.getGeneratedShorebirdYaml(
flavor: 'internal',
);

expect(generatedYaml, contains('app_id: internal_123'));
expect(generatedYaml, contains('patch_public_key: $base64PublicKey'));
},
);
expect(generatedYaml, contains('app_id: internal_123'));
expect(
generatedYaml,
contains('patch_public_key: $base64PublicKey'),
);
},
);
});
});
});
}