Skip to content

Commit f94493a

Browse files
committed
Merge branch 'feature/refactor' into develop
2 parents 47e4cac + e2aac9f commit f94493a

File tree

8 files changed

+234
-217
lines changed

8 files changed

+234
-217
lines changed

lib/android_signing.dart

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import 'dart:io';
2-
import './commons.dart' as commons;
1+
part of flutter_automation;
32

43
String alias;
54
String keystorePath = "keys/keystore.jks";
@@ -8,14 +7,14 @@ String keystorePass;
87
const String keyPropertiesPath = "./android/key.properties";
98

109
/// Main function that uses other helper functions to setup android signing
11-
void androidSign() {
12-
generateKeystore();
13-
createKeyProperties();
14-
configureBuildConfig();
10+
void _androidSign() {
11+
_generateKeystore();
12+
_createKeyProperties();
13+
_configureBuildConfig();
1514
}
1615

1716
/// Generates the keystore with the given settings
18-
void generateKeystore() {
17+
void _generateKeystore() {
1918
String defDname =
2019
"CN=popupbits.com, OU=DD, O=Popup Bits Ltd., L=Kathmandu, S=Bagmati, C=NP";
2120

@@ -69,8 +68,8 @@ void generateKeystore() {
6968
}
7069

7170
/// Creates key.properties file required by signing config in build.gradle file
72-
void createKeyProperties() {
73-
commons.writeStringToFile(keyPropertiesPath, """storePassword=$keystorePass
71+
void _createKeyProperties() {
72+
_Commons.writeStringToFile(keyPropertiesPath, """storePassword=$keystorePass
7473
keyPassword=$keyPass
7574
keyAlias=$alias
7675
storeFile=../../$keystorePath
@@ -79,9 +78,9 @@ storeFile=../../$keystorePath
7978
}
8079

8180
/// configures build.gradle with release config with the generated key details
82-
void configureBuildConfig() {
83-
String bfString = commons.getFileAsString(commons.appBuildPath);
84-
List<String> buildfile = commons.getFileAsLines(commons.appBuildPath);
81+
void _configureBuildConfig() {
82+
String bfString = _Commons.getFileAsString(_Commons.appBuildPath);
83+
List<String> buildfile = _Commons.getFileAsLines(_Commons.appBuildPath);
8584
if (!bfString.contains("deft keystoreProperties") &&
8685
!bfString.contains("keystoreProperties['keyAlias']")) {
8786
buildfile = buildfile.map((line) {
@@ -114,7 +113,7 @@ void configureBuildConfig() {
114113
}
115114
}).toList();
116115

117-
commons.writeStringToFile(commons.appBuildPath, buildfile.join("\n"));
116+
_Commons.writeStringToFile(_Commons.appBuildPath, buildfile.join("\n"));
118117
stdout.writeln("configured release configs");
119118
} else {
120119
stdout.writeln("release configs already configured");

lib/commons.dart

Lines changed: 93 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,108 @@
1-
import 'dart:io';
2-
import 'package:yaml/yaml.dart';
3-
import 'package:path/path.dart' as path;
1+
part of flutter_automation;
42

5-
String scriptRoot =
6-
path.dirname(Platform.script.toFilePath()) + "${path.separator}..";
3+
class _Commons {
4+
static final String scriptRoot =
5+
path.dirname(Platform.script.toFilePath()) + "${path.separator}..";
6+
static final String basePath = "./lib";
7+
static final String pubspecPath = './pubspec.yaml';
8+
static final String stringsPath =
9+
"./android/app/src/main/res/values/strings.xml";
10+
static final String manifestPath =
11+
"./android/app/src/main/AndroidManifest.xml";
12+
static final String appBuildPath = "./android/app/build.gradle";
13+
static final String projectBuildPath = "./android/build.gradle";
714

8-
const String basePath = "./lib";
15+
/// Default plugin versions
16+
static Map<String, dynamic> defaultConfig = {
17+
"plugins": {
18+
"firebase_auth": "^0.14.0+5",
19+
"google_sign_in": "^4.0.7",
20+
"provider": "^3.1.0",
21+
"google_maps": "^0.5.21+2",
22+
"firestore": "^0.12.9+4"
23+
},
24+
"google_services": "4.3.3"
25+
};
926

10-
const String pubspecPath = './pubspec.yaml';
11-
const String stringsPath = "./android/app/src/main/res/values/strings.xml";
12-
const String manifestPath = "./android/app/src/main/AndroidManifest.xml";
13-
const String appBuildPath = "./android/app/build.gradle";
14-
const String projectBuildPath = "./android/build.gradle";
27+
/// Loads config either from the flutter_automation.yaml config file or default config
28+
static Map<String, dynamic> loadConfig() {
29+
if (!File("./flutter_automation.yaml").existsSync()) return defaultConfig;
30+
String configcontent = File("./flutter_automation.yaml").readAsStringSync();
31+
var configFile = loadYaml(configcontent);
32+
return Map<String, dynamic>.from(configFile);
33+
}
1534

16-
/// Default plugin versions
17-
Map<String, dynamic> defaultConfig = {
18-
"plugins": {
19-
"firebase_auth": "^0.14.0+5",
20-
"google_sign_in": "^4.0.7",
21-
"provider": "^3.1.0",
22-
"google_maps": "^0.5.21+2",
23-
"firestore": "^0.12.9+4"
24-
},
25-
"google_services": "4.3.3"
26-
};
35+
static bool fileContainsString(String path, String pattern) {
36+
String file = getFileAsString(path);
37+
return file.contains(pattern);
38+
}
2739

28-
/// Loads config either from the flutter_automation.yaml config file or default config
29-
Map<String, dynamic> loadConfig() {
30-
if (!File("./flutter_automation.yaml").existsSync()) return defaultConfig;
31-
String configcontent = File("./flutter_automation.yaml").readAsStringSync();
32-
var configFile = loadYaml(configcontent);
33-
return Map<String, dynamic>.from(configFile);
34-
}
40+
static bool pluginExists(String plugin) {
41+
return fileContainsString(pubspecPath, plugin);
42+
}
3543

36-
/// Adds provided dependencies to pubspec.yaml file
37-
void addDependencise(String dependencies) {
38-
replaceFirstStringInfile(
39-
pubspecPath, "dev_dependencies:", "$dependencies\ndev_dependencies:");
40-
}
44+
/// Adds provided dependencies to pubspec.yaml file
45+
static void addDependencise(String dependencies) {
46+
replaceFirstStringInfile(
47+
pubspecPath, "dev_dependencies:", "$dependencies\ndev_dependencies:");
48+
}
4149

42-
/// replace string in a file at [path] from [from] to [to]
43-
void replaceFirstStringInfile(String path, Pattern from, String to) {
44-
String contents = getFileAsString(path);
45-
contents = contents.replaceFirst(from, to);
46-
writeStringToFile(path, contents);
47-
}
50+
/// replace string in a file at [path] from [from] to [to]
51+
static void replaceFirstStringInfile(String path, Pattern from, String to) {
52+
String contents = getFileAsString(path);
53+
contents = contents.replaceFirst(from, to);
54+
writeStringToFile(path, contents);
55+
}
4856

49-
/// Reads a file at [path] as string
50-
String getFileAsString(String path) {
51-
return File(path).readAsStringSync();
52-
}
57+
/// Reads a file at [path] as string
58+
static String getFileAsString(String path) {
59+
return File(path).readAsStringSync();
60+
}
5361

54-
/// writes a string [contents] to a file at [path]
55-
void writeStringToFile(String path, String contents) {
56-
File(path).writeAsStringSync(contents);
57-
}
62+
/// writes a string [contents] to a file at [path]
63+
static void writeStringToFile(String path, String contents) {
64+
File(path).writeAsStringSync(contents);
65+
}
5866

59-
/// Reads a file at [path] as a list of lines
60-
List<String> getFileAsLines(String path) {
61-
return File(path).readAsLinesSync();
62-
}
67+
/// Reads a file at [path] as a list of lines
68+
static List<String> getFileAsLines(String path) {
69+
return File(path).readAsLinesSync();
70+
}
6371

64-
/// Copies files recursively from [from] directory to [to] directory
65-
void copyFilesRecursive(String from, String to, {String renameBaseDir}) {
66-
Process.run(
67-
"cp",
68-
["-r", from, to],
69-
).then((res) {
70-
stdout.write(res.stdout);
71-
if (res.stderr.toString().isNotEmpty) {
72-
stderr.write(res.stderr);
73-
} else {
74-
if (renameBaseDir != null) {
75-
renameStockFiles(renameBaseDir);
72+
/// Copies files recursively from [from] directory to [to] directory
73+
static void copyFilesRecursive(String from, String to,
74+
{String renameBaseDir}) {
75+
Process.run(
76+
"cp",
77+
["-r", from, to],
78+
).then((res) {
79+
stdout.write(res.stdout);
80+
if (res.stderr.toString().isNotEmpty) {
81+
stderr.write(res.stderr);
82+
} else {
83+
if (renameBaseDir != null) {
84+
renameStockFiles(renameBaseDir);
85+
}
86+
stdout.writeln("copied stock files");
7687
}
77-
stdout.writeln("copied stock files");
78-
}
79-
});
80-
}
88+
});
89+
}
8190

82-
/// Renames all stock files in [basedir]
83-
void renameStockFiles(String basedir) {
84-
Directory dir = Directory(basedir);
85-
List<FileSystemEntity> files = dir.listSync(recursive: true);
86-
files.forEach((file) {
87-
if (file is File) {
88-
String filename = path.basename(file.path);
89-
if (filename.substring(filename.length - 5, filename.length) == ".temp") {
90-
String newFilename = filename.substring(0, filename.length - 5);
91-
file.renameSync(path.dirname(file.path) + path.separator + newFilename);
91+
/// Renames all stock files in [basedir]
92+
static void renameStockFiles(String basedir) {
93+
Directory dir = Directory(basedir);
94+
List<FileSystemEntity> files = dir.listSync(recursive: true);
95+
files.forEach((file) {
96+
if (file is File) {
97+
String filename = path.basename(file.path);
98+
if (filename.substring(filename.length - 5, filename.length) ==
99+
".temp") {
100+
String newFilename = filename.substring(0, filename.length - 5);
101+
file.renameSync(
102+
path.dirname(file.path) + path.separator + newFilename);
103+
}
92104
}
93-
}
94-
});
95-
stdout.writeln("renamed stock files");
105+
});
106+
stdout.writeln("renamed stock files");
107+
}
96108
}

lib/firebase_auth.dart

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,57 @@
1-
import 'dart:io';
2-
import 'package:path/path.dart' as path;
3-
import './pubspec_api.dart';
4-
5-
import './commons.dart' as commons;
1+
part of flutter_automation;
62

73
List<String> plugins = ["firebase_auth", "provider", "google_sign_in"];
8-
Map<String, dynamic> config = commons.loadConfig();
4+
Map<String, dynamic> config = _Commons.loadConfig();
95
const String gradlePropertiesPath = "./android/gradle.properties";
106

117
/// Main firebase auth script function that setups firebase authentication with the help of other functions
12-
void firebaseAuth() async {
13-
upgradeToAndroidX();
14-
await addDependencise();
15-
addGoogleService();
16-
copyStockFiles();
8+
Future<void> _firebaseAuth() async {
9+
_upgradeToAndroidX();
10+
await _addDependencise();
11+
_addGoogleService();
12+
_copyStockFiles();
1713
stdout.write("firebase auth implemented\n");
1814
}
1915

2016
/// Adds firebase auth related dependencies to the pubspec.yaml file
21-
Future<void> addDependencise() async {
22-
String pubspec = commons.getFileAsString(commons.pubspecPath);
17+
Future<void> _addDependencise() async {
2318
plugins = plugins.where((plugin) {
24-
return !pubspec.contains(plugin);
19+
return !_Commons.pluginExists(plugin);
2520
}).toList();
2621
List<String> latest = [];
27-
for(var i = 0; i<plugins.length; i++) {
22+
for (var i = 0; i < plugins.length; i++) {
2823
String plugin = plugins[i];
29-
String plug = await PubspecAPI().getPackage(plugin);
30-
latest.add(plug != null ? " $plug" : " $plugin: ${config['plugins'][plugin]}");
24+
String plug = await _PubspecAPI().getPackage(plugin);
25+
latest.add(
26+
plug != null ? " $plug" : " $plugin: ${config['plugins'][plugin]}");
3127
}
3228
String content = latest.join("\n");
33-
commons.addDependencise(content);
29+
_Commons.addDependencise(content);
3430
stdout.writeln("added dependencies");
3531
}
3632

3733
/// adds google services configuration to build.gradle files
38-
void addGoogleService() {
39-
String pbuild = commons.getFileAsString(commons.projectBuildPath);
34+
void _addGoogleService() {
35+
String pbuild = _Commons.getFileAsString(_Commons.projectBuildPath);
4036
if (!pbuild.contains("com.google.gms:google-services")) {
41-
commons.replaceFirstStringInfile(
42-
commons.projectBuildPath,
37+
_Commons.replaceFirstStringInfile(
38+
_Commons.projectBuildPath,
4339
RegExp("dependencies.*{"),
4440
"dependencies {\n classpath 'com.google.gms:google-services:${config['google_services']}'\n");
4541
stdout.writeln("added google services");
4642
}
4743
}
4844

4945
/// upgrades project to androidx if not already
50-
void upgradeToAndroidX() {
51-
String properties = commons.getFileAsString(gradlePropertiesPath);
46+
void _upgradeToAndroidX() {
47+
String properties = _Commons.getFileAsString(gradlePropertiesPath);
5248
if (!properties.contains("android.useAndroidX")) {
5349
properties =
5450
"$properties\n\nandroid.enableJetifier=true\nandroid.useAndroidX=true\n";
55-
commons.writeStringToFile(gradlePropertiesPath, properties);
51+
_Commons.writeStringToFile(gradlePropertiesPath, properties);
5652
}
5753

58-
List<String> contents = commons.getFileAsLines(commons.appBuildPath);
54+
List<String> contents = _Commons.getFileAsLines(_Commons.appBuildPath);
5955
contents = contents.map((line) {
6056
if (line.contains("testInstrumentationRunner"))
6157
return " testInstrumentationRunner \"androidx.test.runner.AndroidJUnitRunner\"\n";
@@ -69,13 +65,22 @@ void upgradeToAndroidX() {
6965
return line;
7066
}).toList();
7167
String content = contents.join("\n");
72-
content = "$content\napply plugin: 'com.google.gms.google-services'\n";
73-
commons.writeStringToFile(commons.appBuildPath, content);
68+
69+
if (!_Commons.fileContainsString(
70+
_Commons.appBuildPath,
71+
"com.google.gms.google-services",
72+
)) {
73+
content = "$content\napply plugin: 'com.google.gms.google-services'\n";
74+
}
75+
76+
_Commons.writeStringToFile(_Commons.appBuildPath, content);
7477
stdout.write("upgraded to androidx\n");
7578
}
7679

7780
/// copies stock files related to firebase authentication
78-
void copyStockFiles() {
79-
String stockPath = "${commons.scriptRoot}${path.separator}lib${path.separator}auth_stock${path.separator}lib";
80-
commons.copyFilesRecursive(stockPath, '.${path.separator}', renameBaseDir: '.${path.separator}lib');
81+
void _copyStockFiles() {
82+
String stockPath =
83+
"${_Commons.scriptRoot}${path.separator}lib${path.separator}auth_stock${path.separator}lib";
84+
_Commons.copyFilesRecursive(stockPath, '.${path.separator}',
85+
renameBaseDir: '.${path.separator}lib');
8186
}

0 commit comments

Comments
 (0)