run method
Runs this command.
The return value is wrapped in a Future
if necessary and returned by
CommandRunner.runCommand.
Implementation
@override
void run() async {
final argFlavor = argResults.getOptionFlavor(defaultTo: Constants.dev);
final argMorphemeYaml = argResults.getOptionMorphemeYaml();
final argOverwrite = argResults?['overwrite'] as bool? ?? false;
YamlHelper.validateMorphemeYaml(argMorphemeYaml);
final flavor = FlavorHelper.byFlavor(argFlavor, argMorphemeYaml);
final firebase = FirebaseHelper.byFlavor(argFlavor, argMorphemeYaml);
if (firebase.isEmpty) {
StatusHelper.warning(
'Cannot setup flavor firebase, You don\'t have config "firebase" with flavor "$argFlavor" in morpheme.yaml');
} else if (which('flutterfire').found) {
final project = firebase['project_id'];
final token = firebase['token'];
final platform = firebase['platform'];
final output = firebase['output'];
final androidPackageName =
firebase['android_package_name'] ?? flavor['ANDROID_APPLICATION_ID'];
final iosBundleId =
firebase['ios_bundle_id'] ?? flavor['IOS_APPLICATION_ID'];
final webAppId = firebase['web_app_id'];
final serviceAccount = firebase['service_account']?.toString();
final enableCiUseServiceAccount =
firebase['enable_ci_use_service_account'] is bool
? firebase['enable_ci_use_service_account'] as bool
: false;
final argToken =
token is String && token.isNotEmpty ? ' -t "$token"' : '';
final argPlatform = platform is String && platform.isNotEmpty
? ' --platforms="$platform"'
: '';
final argWebAppId =
webAppId is String && webAppId.isNotEmpty ? ' -w "$webAppId"' : '';
final argOutput =
output is String && output.isNotEmpty ? ' -o "$output"' : '';
bool regenerate = true;
final pathFirebaseOptions = output != null
? join(current, output)
: join(current, 'lib', 'firebase_options.dart');
if (exists(pathFirebaseOptions)) {
final firebaseOptions = readFile(pathFirebaseOptions);
if (RegExp('''projectId:(\\s+)?('|")$project('|")''')
.hasMatch(firebaseOptions)) {
regenerate = false;
StatusHelper.generated('you already have lib/firebase_options.dart');
}
}
final isCiCdEnvironment = Platform.environment.containsKey('CI') &&
Platform.environment['CI'] == 'true';
final isExistServiceAccount = serviceAccount != null &&
serviceAccount.isNotEmpty &&
exists(serviceAccount);
final commandFlutterFire =
'flutterfire configure $argToken$argPlatform$argWebAppId$argOutput -p "$project" -a "$androidPackageName" -i "$iosBundleId" -m "$iosBundleId" -w "$androidPackageName" -x "$androidPackageName" -y';
if ((isCiCdEnvironment && enableCiUseServiceAccount ||
!isCiCdEnvironment) &&
isExistServiceAccount &&
(regenerate || argOverwrite)) {
final filename = join(current, 'firebase_command.sh');
filename.write('''#!/bin/bash
export GOOGLE_APPLICATION_CREDENTIALS="$serviceAccount"
$commandFlutterFire
''');
await 'chmod +x $filename'.run;
await filename.run;
delete(filename);
}
if ((!isExistServiceAccount) && (regenerate || argOverwrite)) {
await commandFlutterFire.run;
}
} else {
StatusHelper.failed(
'flutterfire not installed, You can install with \'dart pub global activate flutterfire_cli\'');
}
}