Skip to content

Commit

Permalink
feat: add messaging sender id to output
Browse files Browse the repository at this point in the history
  • Loading branch information
Salakar committed Mar 30, 2022
1 parent ea0f6b7 commit 3ba34ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
9 changes: 3 additions & 6 deletions packages/flutterfire_cli/lib/src/commands/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@ class ConfigCommand extends FlutterFireCommand {
if (iosOptions != null) {
final appIDFile = FirebaseAppIDFile(
iosAppIDOutputFilePrefix,
appId: iosOptions.appId,
firebaseProjectId: iosOptions.projectId,
options: iosOptions,
force: isCI || yes,
);
futures.add(appIDFile.write());
Expand All @@ -322,8 +321,7 @@ class ConfigCommand extends FlutterFireCommand {
if (macosOptions != null) {
final appIDFile = FirebaseAppIDFile(
macosAppIDOutputFilePrefix,
appId: macosOptions.appId,
firebaseProjectId: macosOptions.projectId,
options: macosOptions,
force: isCI || yes,
);
futures.add(appIDFile.write());
Expand All @@ -332,8 +330,7 @@ class ConfigCommand extends FlutterFireCommand {
if (androidOptions != null) {
final appIDFile = FirebaseAppIDFile(
androidAppIDOutputFilePrefix,
appId: androidOptions.appId,
firebaseProjectId: androidOptions.projectId,
options: androidOptions,
force: isCI || yes,
);
futures.add(appIDFile.write());
Expand Down
30 changes: 13 additions & 17 deletions packages/flutterfire_cli/lib/src/firebase/firebase_app_id_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ import 'package:path/path.dart';

import '../common/exception.dart';
import '../common/utils.dart';
import 'firebase_options.dart';

const _defaultAppIdFileName = 'firebase_app_id_file.json';
const _keyGoogleAppId = 'GOOGLE_APP_ID';
const _keyFirebaseProjectId = 'FIREBASE_PROJECT_ID';
const _keyGcmSenderId = 'GCM_SENDER_ID';

class FirebaseAppIDFile {
FirebaseAppIDFile(
this.outputDirectoryPath, {
required this.appId,
required this.firebaseProjectId,
required this.options,
this.fileName = _defaultAppIdFileName,
this.force = false,
});
Expand All @@ -46,8 +47,7 @@ class FirebaseAppIDFile {

final String fileName;

final String appId;
final String firebaseProjectId;
final FirebaseOptions options;

Future<void> write() async {
if (!Directory(outputDirectoryPath).existsSync()) {
Expand All @@ -57,36 +57,32 @@ class FirebaseAppIDFile {
final appIDFilePath = joinAll([outputDirectoryPath, fileName]);
final outputFile = File(joinAll([Directory.current.path, appIDFilePath]));

_writeHeaderAndAppID(outputFile.path);
final newFileContents = _stringBuffer.toString();

if (outputFile.existsSync() && !force) {
final existingFileContents = await outputFile.readAsString();
final existingFileContentsAsJson =
json.decode(existingFileContents) as Map;
final existingAppId =
existingFileContentsAsJson[_keyGoogleAppId] as String;
final existingFirebaseProjectId =
existingFileContentsAsJson[_keyFirebaseProjectId] as String;
// Only prompt overwrite if values are different.
if (existingAppId != appId ||
existingFirebaseProjectId != firebaseProjectId) {
if (newFileContents != existingFileContents) {
final shouldOverwrite = promptBool(
'Generated FirebaseAppID file ${AnsiStyles.cyan(appIDFilePath)} already exists (for app id "$existingAppId" on Firebase Project "$existingFirebaseProjectId"), do you want to override it?',
'Generated FirebaseAppID file ${AnsiStyles.cyan(appIDFilePath)} already exists, do you want to override it?',
);
if (!shouldOverwrite) {
throw FirebaseAppIDAlreadyExistsException(appIDFilePath);
}
}
}
_writeHeaderAndAppID(outputFile.path);
outputFile.writeAsStringSync(_stringBuffer.toString());
outputFile.writeAsStringSync(newFileContents);
}

void _writeHeaderAndAppID(String outputFile) {
final fileData = {
'file_generated_by': 'FlutterFire CLI',
'purpose':
'FirebaseAppID & ProjectID for this Firebase app in this directory',
_keyGoogleAppId: appId,
_keyFirebaseProjectId: firebaseProjectId,
_keyGoogleAppId: options.appId,
_keyFirebaseProjectId: options.projectId,
_keyGcmSenderId: options.messagingSenderId,
};
_stringBuffer.write(const JsonEncoder.withIndent(' ').convert(fileData));
}
Expand Down

0 comments on commit 3ba34ae

Please sign in to comment.