-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathgenerate_versions_gradle.dart
More file actions
169 lines (146 loc) · 5.3 KB
/
generate_versions_gradle.dart
File metadata and controls
169 lines (146 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file 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.
// ignore_for_file: avoid_print
import 'dart:io';
import 'package:cli_util/cli_logging.dart' as logging;
import 'package:glob/glob.dart';
import 'package:melos/melos.dart' as melos;
import 'package:path/path.dart' show joinAll;
// Used to generate config files from ../gradle/local-config.gradle in order to use correct java and compilation versions.
// Tested against every example app in the packages.
// NOTICE: This script does not update auth or vertexai packages as they are manually updated.
// Furthermore, this script does not update the test app.
void main() async {
final workspace = await getMelosWorkspace();
// To edit versions for all packages, edit the global-config.gradle file in ./scripts/global-config.gradle
final globalConfigPath = joinAll(
[
Directory.current.path,
'scripts',
'global-config.gradle',
],
);
// Define files using paths
final globalConfig = File(globalConfigPath);
// Check if the files exist
if (!globalConfig.existsSync()) {
throw Exception(
'global_config.gradle file not found in the expected location.',
);
}
for (final package in workspace.filteredPackages.values) {
switch (package.name) {
case 'cloud_firestore':
case 'cloud_functions':
case 'firebase_analytics':
case 'firebase_app_check':
case 'firebase_app_installations':
case 'firebase_core':
case 'firebase_crashlytics':
case 'firebase_database':
case 'firebase_in_app_messaging':
case 'firebase_messaging':
case 'firebase_ml_model_downloader':
case 'firebase_performance':
case 'firebase_remote_config':
case 'firebase_storage':
final localConfigGradleFilePath =
'${package.path}/android/local-config.gradle';
final copiedConfig = await globalConfig.copy(
localConfigGradleFilePath,
);
print('File copied to: ${copiedConfig.path}');
final gradlePropertiesFilePath =
'${package.path}/example/android/gradle.properties';
extractAndWriteProperty(
globalConfig: globalConfig,
gradlePropertiesFile: File(gradlePropertiesFilePath),
);
print('successfully wrote property to $gradlePropertiesFilePath');
break;
case 'firebase_data_connect':
// Only has gradle in the example application.
final localConfigGradleFilePath =
'${package.path}/example/android/app/local-config.gradle';
final copiedConfig = await globalConfig.copy(
localConfigGradleFilePath,
);
print('File copied to: ${copiedConfig.path}');
final gradlePropertiesFilePath =
'${package.path}/example/android/gradle.properties';
extractAndWriteProperty(
globalConfig: globalConfig,
gradlePropertiesFile: File(gradlePropertiesFilePath),
);
print('successfully wrote property to $gradlePropertiesFilePath');
break;
case 'firebase_vertexai':
case 'firebase_ai':
case 'firebase_auth':
// skip these packages, manually update.
break;
}
}
}
Future<melos.MelosWorkspace> getMelosWorkspace() async {
final packageFilters = melos.PackageFilters(
includePrivatePackages: false,
ignore: [
Glob('*web*'),
Glob('*platform*'),
Glob('*internals*'),
],
);
final workspace = await melos.MelosWorkspace.fromConfig(
await melos.MelosWorkspaceConfig.fromWorkspaceRoot(Directory.current),
logger: melos.MelosLogger(logging.Logger.standard()),
packageFilters: packageFilters,
);
return workspace;
}
void extractAndWriteProperty({
required File globalConfig,
required File gradlePropertiesFile,
}) {
const String propertyName = 'androidGradlePluginVersion';
if (!globalConfig.existsSync()) {
print('Global config file not found: ${globalConfig.path}');
return;
}
final globalContent = globalConfig.readAsStringSync();
// Extract the property from the ext block
final regex = RegExp('$propertyName\\s*=\\s*[\'"]?([^\\n\'"]+)[\'"]?');
final match = regex.firstMatch(globalContent);
if (match == null) {
print('Property $propertyName not found in global config.');
return;
}
final value = match.group(1);
final lines = gradlePropertiesFile.existsSync()
? gradlePropertiesFile.readAsLinesSync()
: <String>[];
bool updated = false;
final updatedLines = lines.map((line) {
if (line.startsWith('$propertyName=')) {
updated = true;
return '$propertyName=$value';
}
return line;
}).toList();
if (!updated) {
updatedLines.add('$propertyName=$value');
}
gradlePropertiesFile.writeAsStringSync(updatedLines.join('\n'));
print('Wrote $propertyName=$value to ${gradlePropertiesFile.path}');
}