Skip to content

Commit

Permalink
feat: updates configure to also write out Android app ID files (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Salakar committed Mar 30, 2022
1 parent e1602b0 commit 991d5a4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 31 deletions.
25 changes: 17 additions & 8 deletions packages/flutterfire_cli/lib/src/commands/config.dart
Expand Up @@ -108,10 +108,6 @@ class ConfigCommand extends FlutterFireCommand {
return argResults!['out'] as String;
}

String get appIDFilePath {
return 'firebase_app_id_file.json';
}

String get iosAppIDOutputFilePrefix {
return 'ios';
}
Expand All @@ -120,6 +116,10 @@ class ConfigCommand extends FlutterFireCommand {
return 'macos';
}

String get androidAppIDOutputFilePrefix {
return 'android';
}

Future<FirebaseProject> _promptCreateFirebaseProject() async {
final newProjectId = promptInput(
'Enter a project id for your new Firebase project (e.g. ${AnsiStyles.cyan('my-cool-project')})',
Expand Down Expand Up @@ -300,17 +300,26 @@ class ConfigCommand extends FlutterFireCommand {
if (iosOptions != null) {
final appIDFile = FirebaseAppIDFile(
iosAppIDOutputFilePrefix,
appIDFilePath,
iosOptions.appId,
appId: iosOptions.appId,
firebaseProjectId: iosOptions.projectId,
);
futures.add(appIDFile.write());
}

if (macosOptions != null) {
final appIDFile = FirebaseAppIDFile(
macosAppIDOutputFilePrefix,
appIDFilePath,
macosOptions.appId,
appId: macosOptions.appId,
firebaseProjectId: macosOptions.projectId,
);
futures.add(appIDFile.write());
}

if (androidOptions != null) {
final appIDFile = FirebaseAppIDFile(
androidAppIDOutputFilePrefix,
appId: androidOptions.appId,
firebaseProjectId: androidOptions.projectId,
);
futures.add(appIDFile.write());
}
Expand Down
51 changes: 37 additions & 14 deletions packages/flutterfire_cli/lib/src/firebase/firebase_app_id_file.dart
Expand Up @@ -25,8 +25,17 @@ import 'package:path/path.dart';
import '../common/exception.dart';
import '../common/utils.dart';

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

class FirebaseAppIDFile {
FirebaseAppIDFile(this.outputDirectoryPath, this.fileName, this.appId);
FirebaseAppIDFile(
this.outputDirectoryPath, {
required this.appId,
required this.firebaseProjectId,
this.fileName = _defaultAppIdFileName,
});

final StringBuffer _stringBuffer = StringBuffer();

Expand All @@ -35,23 +44,35 @@ class FirebaseAppIDFile {
final String fileName;

final String appId;
final String firebaseProjectId;

Future<void> write() async {
// ignore: avoid_slow_async_io
final directoryExists = await Directory(outputDirectoryPath).exists();
if (!directoryExists) {
if (!Directory(outputDirectoryPath).existsSync()) {
throw PlatformDirectoryDoesNotExistException(outputDirectoryPath);
}

final appIDFilePath = joinAll([outputDirectoryPath, fileName]);
final outputFile = File(joinAll([Directory.current.path, appIDFilePath]));

if (outputFile.existsSync() && !isCI) {
final shouldOverwrite = interact.Confirm(
prompt:
'Generated FirebaseAppID file ${AnsiStyles.cyan(appIDFilePath)} already exists, do you want to override it?',
defaultValue: true,
).interact();
if (!shouldOverwrite) {
throw FirebaseAppIDAlreadyExistsException(appIDFilePath);
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) {
final shouldOverwrite = interact.Confirm(
prompt:
'Generated FirebaseAppID file ${AnsiStyles.cyan(appIDFilePath)} already exists (for app id "$existingAppId" on Firebase Project "$existingFirebaseProjectId"), do you want to override it?',
defaultValue: true,
).interact();
if (!shouldOverwrite) {
throw FirebaseAppIDAlreadyExistsException(appIDFilePath);
}
}
}
_writeHeaderAndAppID(outputFile.path);
Expand All @@ -61,9 +82,11 @@ class FirebaseAppIDFile {
void _writeHeaderAndAppID(String outputFile) {
final fileData = {
'file_generated_by': 'FlutterFire CLI',
'purpose': 'FirebaseAppID for this Firebase app in this directory',
'GOOGLE_APP_ID': appId
'purpose':
'FirebaseAppID & ProjectID for this Firebase app in this directory',
_keyGoogleAppId: appId,
_keyFirebaseProjectId: firebaseProjectId,
};
_stringBuffer.write(json.encode(fileData));
_stringBuffer.write(const JsonEncoder.withIndent(' ').convert(fileData));
}
}
Expand Up @@ -48,18 +48,26 @@ class FirebaseConfigurationFile {

Future<void> write() async {
final outputFile = File(joinAll([Directory.current.path, outputFilePath]));

// Write buffer early so we can string compare contents if file exists already.
_writeHeader();
_writeClass();
final newFileContents = _stringBuffer.toString();

if (outputFile.existsSync() && !isCI) {
final shouldOverwrite = interact.Confirm(
prompt:
'Generated FirebaseOptions file ${AnsiStyles.cyan(outputFilePath)} already exists, do you want to override it?',
defaultValue: true,
).interact();
if (!shouldOverwrite) {
throw FirebaseOptionsAlreadyExistsException(outputFilePath);
final existingFileContents = await outputFile.readAsString();
// Only prompt overwrite if contents have changed.
if (existingFileContents != newFileContents) {
final shouldOverwrite = interact.Confirm(
prompt:
'Generated FirebaseOptions file ${AnsiStyles.cyan(outputFilePath)} already exists, do you want to override it?',
defaultValue: true,
).interact();
if (!shouldOverwrite) {
throw FirebaseOptionsAlreadyExistsException(outputFilePath);
}
}
}
_writeHeader();
_writeClass();
outputFile.writeAsStringSync(_stringBuffer.toString());
}

Expand Down

0 comments on commit 991d5a4

Please sign in to comment.