|
1 | | -import 'dart:io'; |
2 | | -import 'package:yaml/yaml.dart'; |
3 | | -import 'package:path/path.dart' as path; |
| 1 | +part of flutter_automation; |
4 | 2 |
|
5 | | -String scriptRoot = 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"; |
6 | 14 |
|
7 | | -const String pubspecPath = './pubspec.yaml'; |
8 | | -const String stringsPath = "./android/app/src/main/res/values/strings.xml"; |
9 | | -const String manifestPath = "./android/app/src/main/AndroidManifest.xml"; |
10 | | -const String appBuildPath = "./android/app/build.gradle"; |
11 | | -const String projectBuildPath = "./android/build.gradle"; |
12 | | -/// Default plugin versions |
13 | | -Map<String, dynamic> defaultConfig = { |
14 | | - "plugins": { |
15 | | - "firebase_auth": "^0.14.0+5", |
16 | | - "google_sign_in": "^4.0.7", |
17 | | - "provider": "^3.1.0", |
18 | | - "google_maps": "^0.5.21+2", |
19 | | - "firestore": "^0.12.9+4" |
20 | | - }, |
21 | | - "google_services": "4.3.3" |
22 | | -}; |
| 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 | + }; |
23 | 26 |
|
24 | | -/// Loads config either from the flutter_automation.yaml config file or default config |
25 | | -Map<String, dynamic> loadConfig() { |
26 | | - if (!File("./flutter_automation.yaml").existsSync()) return defaultConfig; |
27 | | - String configcontent = File("./flutter_automation.yaml").readAsStringSync(); |
28 | | - var configFile = loadYaml(configcontent); |
29 | | - return Map<String, dynamic>.from(configFile); |
30 | | -} |
| 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 | + } |
31 | 34 |
|
32 | | -/// Adds provided dependencies to pubspec.yaml file |
33 | | -void addDependencise(String dependencies) { |
34 | | - replaceFirstStringInfile( |
35 | | - pubspecPath, "dev_dependencies:", "$dependencies\ndev_dependencies:"); |
36 | | -} |
| 35 | + static bool fileContainsString(String path, String pattern) { |
| 36 | + String file = getFileAsString(path); |
| 37 | + return file.contains(pattern); |
| 38 | + } |
37 | 39 |
|
38 | | -/// replace string in a file at [path] from [from] to [to] |
39 | | -void replaceFirstStringInfile(String path, Pattern from, String to) { |
40 | | - String contents = getFileAsString(path); |
41 | | - contents = contents.replaceFirst(from, to); |
42 | | - writeStringToFile(path, contents); |
43 | | -} |
| 40 | + static bool pluginExists(String plugin) { |
| 41 | + return fileContainsString(pubspecPath, plugin); |
| 42 | + } |
44 | 43 |
|
45 | | -/// Reads a file at [path] as string |
46 | | -String getFileAsString(String path) { |
47 | | - return File(path).readAsStringSync(); |
48 | | -} |
| 44 | + /// Adds provided dependencies to pubspec.yaml file |
| 45 | + static void addDependencise(String dependencies) { |
| 46 | + replaceFirstStringInfile( |
| 47 | + pubspecPath, "dev_dependencies:", "$dependencies\ndev_dependencies:"); |
| 48 | + } |
49 | 49 |
|
50 | | -/// writes a string [contents] to a file at [path] |
51 | | -void writeStringToFile(String path, String contents) { |
52 | | - File(path).writeAsStringSync(contents); |
53 | | -} |
| 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 | + } |
54 | 56 |
|
55 | | -/// Reads a file at [path] as a list of lines |
56 | | -List<String> getFileAsLines(String path) { |
57 | | - return File(path).readAsLinesSync(); |
58 | | -} |
| 57 | + /// Reads a file at [path] as string |
| 58 | + static String getFileAsString(String path) { |
| 59 | + return File(path).readAsStringSync(); |
| 60 | + } |
| 61 | + |
| 62 | + /// writes a string [contents] to a file at [path] |
| 63 | + static void writeStringToFile(String path, String contents) { |
| 64 | + File(path).writeAsStringSync(contents); |
| 65 | + } |
59 | 66 |
|
60 | | -/// Copies files recursively from [from] directory to [to] directory |
61 | | -void copyFilesRecursive(String from, String to, {String renameBaseDir}) { |
62 | | - Process.run( |
63 | | - "cp", |
64 | | - ["-r", from, to], |
65 | | - ).then((res) { |
66 | | - stdout.write(res.stdout); |
67 | | - if (res.stderr.toString().isNotEmpty) { |
68 | | - stderr.write(res.stderr); |
69 | | - } else { |
70 | | - if (renameBaseDir != null) { |
71 | | - renameStockFiles(renameBaseDir); |
| 67 | + /// Reads a file at [path] as a list of lines |
| 68 | + static List<String> getFileAsLines(String path) { |
| 69 | + return File(path).readAsLinesSync(); |
| 70 | + } |
| 71 | + |
| 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"); |
72 | 87 | } |
73 | | - stdout.writeln("copied stock files"); |
74 | | - } |
75 | | - }); |
76 | | -} |
| 88 | + }); |
| 89 | + } |
77 | 90 |
|
78 | | -/// Renames all stock files in [basedir] |
79 | | -void renameStockFiles(String basedir) { |
80 | | - Directory dir = Directory(basedir); |
81 | | - List<FileSystemEntity> files = dir.listSync(recursive: true); |
82 | | - files.forEach((file) { |
83 | | - if (file is File) { |
84 | | - String filename = path.basename(file.path); |
85 | | - if (filename.substring(filename.length - 5, filename.length) == ".temp") { |
86 | | - String newFilename = filename.substring(0, filename.length - 5); |
87 | | - 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 | + } |
88 | 104 | } |
89 | | - } |
90 | | - }); |
91 | | - stdout.writeln("renamed stock files"); |
| 105 | + }); |
| 106 | + stdout.writeln("renamed stock files"); |
| 107 | + } |
92 | 108 | } |
0 commit comments