Skip to content

Commit

Permalink
feat: updates configure to also write out iOS app ID files (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
elenadoty committed Mar 30, 2022
1 parent 911de96 commit e7d5a8f
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
38 changes: 37 additions & 1 deletion packages/flutterfire_cli/lib/src/commands/config.dart
Expand Up @@ -22,6 +22,7 @@ import '../common/platform.dart';
import '../common/utils.dart';
import '../firebase.dart' as firebase;
import '../firebase/firebase_android_options.dart';
import '../firebase/firebase_app_id_file.dart';
import '../firebase/firebase_apple_options.dart';
import '../firebase/firebase_configuration_file.dart';
import '../firebase/firebase_options.dart';
Expand Down Expand Up @@ -107,6 +108,18 @@ class ConfigCommand extends FlutterFireCommand {
return argResults!['out'] as String;
}

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

String get iosAppIDOutputFilePrefix {
return 'ios';
}

String get macosAppIDOutputFilePrefix {
return 'macos';
}

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 @@ -143,6 +156,7 @@ class ConfigCommand extends FlutterFireCommand {
}

List<FirebaseProject>? firebaseProjects;

final fetchingProjectsSpinner = spinner(
(done) {
if (!done) {
Expand All @@ -157,6 +171,7 @@ class ConfigCommand extends FlutterFireCommand {
},
);
firebaseProjects = await firebase.getProjects(account: accountEmail);

fetchingProjectsSpinner.done();
if (selectedProjectId != null) {
return firebaseProjects.firstWhere(
Expand Down Expand Up @@ -271,15 +286,36 @@ class ConfigCommand extends FlutterFireCommand {
);
}

final futures = <Future>[];

final configFile = FirebaseConfigurationFile(
outputFilePath,
androidOptions: androidOptions,
iosOptions: iosOptions,
macosOptions: macosOptions,
webOptions: webOptions,
);
futures.add(configFile.write());

if (iosOptions != null) {
final appIDFile = FirebaseAppIDFile(
iosAppIDOutputFilePrefix,
appIDFilePath,
iosOptions.appId,
);
futures.add(appIDFile.write());
}

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

await configFile.write();
await Future.wait<void>(futures);

logger.stdout('');
logger.stdout(
Expand Down
22 changes: 22 additions & 0 deletions packages/flutterfire_cli/lib/src/common/exception.dart
Expand Up @@ -63,6 +63,28 @@ class FirebaseOptionsAlreadyExistsException implements FlutterFireException {
}
}

class FirebaseAppIDAlreadyExistsException implements FlutterFireException {
FirebaseAppIDAlreadyExistsException(this.filePath) : super();

final String filePath;

@override
String toString() {
return 'FirebaseAppIDAlreadyExistsException: Firebase app ID file ${AnsiStyles.cyan(filePath)} already exists.';
}
}

class PlatformDirectoryDoesNotExistException implements FlutterFireException {
PlatformDirectoryDoesNotExistException(this.filePath) : super();

final String filePath;

@override
String toString() {
return 'PlatformDirectoryDoesNotExistException: platform directory ${AnsiStyles.cyan(filePath)} does not exist. Please re-run after initializing this directory with Flutter.';
}
}

class FlutterPlatformNotSupportedException implements FlutterFireException {
FlutterPlatformNotSupportedException(this.platform) : super();

Expand Down
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import 'dart:convert';
import 'dart:io';

import 'package:ansi_styles/ansi_styles.dart';
import 'package:interact/interact.dart' as interact;
import 'package:path/path.dart';

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

class FirebaseAppIDFile {
FirebaseAppIDFile(this.outputDirectoryPath, this.fileName, this.appId);

final StringBuffer _stringBuffer = StringBuffer();

final String outputDirectoryPath;

final String fileName;

final String appId;

Future<void> write() async {
// ignore: avoid_slow_async_io
final directoryExists = await Directory(outputDirectoryPath).exists();
if (!directoryExists) {
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);
}
}
_writeHeaderAndAppID(outputFile.path);
outputFile.writeAsStringSync(_stringBuffer.toString());
}

void _writeHeaderAndAppID(String outputFile) {
final fileData = {
'file_generated_by': 'FlutterFire CLI',
'purpose': 'FirebaseAppID for this Firebase app in this directory',
'GOOGLE_APP_ID': appId
};
_stringBuffer.write(json.encode(fileData));
}
}

0 comments on commit e7d5a8f

Please sign in to comment.