-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathgenerate_versions_spm.dart
More file actions
179 lines (152 loc) · 5.94 KB
/
generate_versions_spm.dart
File metadata and controls
179 lines (152 loc) · 5.94 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
170
171
172
173
174
175
176
177
178
179
// Copyright 2024 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// ignore_for_file: avoid_print
import 'package:melos/melos.dart' as melos;
import 'package:glob/glob.dart';
import 'dart:io';
import 'package:cli_util/cli_logging.dart' as logging;
import 'package:yaml/yaml.dart';
// Used to generate a simple txt file for Package.swift file to parse in order to use correct firebase-ios-sdk version
void main(List<String> args) async {
final workspace = await getMelosWorkspace();
// get version from core
final firebaseCorePackage = workspace.filteredPackages.values
.firstWhere((package) => package.name == 'firebase_core');
final firebaseCoreIosVersionFile = File(
'${firebaseCorePackage.path}/ios/firebase_sdk_version.rb',
);
final firebaseiOSVersion = getFirebaseiOSVersion(firebaseCoreIosVersionFile);
// Update hard-coded versions in all plugin Package.swift files
final firebaseCoreVersion = loadYaml(
File('${firebaseCorePackage.path}/pubspec.yaml').readAsStringSync(),
)['version']
.toString();
updatePluginPackageSwiftVersions(
workspace,
firebaseiOSVersion,
firebaseCoreVersion,
);
// Update plugin version in Constants.swift for pure Swift plugins. Unable to pass macros in pure Swift implementations
updateLibraryVersionPureSwiftPlugins();
}
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;
}
String getFirebaseiOSVersion(File firebaseCoreIosSdkVersion) {
if (firebaseCoreIosSdkVersion.existsSync()) {
final content = firebaseCoreIosSdkVersion.readAsStringSync();
final versionMatch = RegExp(r"'(\d+\.\d+\.\d+)'").firstMatch(content);
if (versionMatch != null && versionMatch.group(1) != null) {
return versionMatch.group(1)!;
} else {
throw Exception(
'Firebase iOS SDK version not found in firebase_sdk_version.rb.',
);
}
} else {
throw Exception('firebase_sdk_version.rb file does not exist.');
}
}
void updatePluginPackageSwiftVersions(
melos.MelosWorkspace workspace,
String firebaseiOSVersion,
String firebaseCoreVersion,
) {
for (final package in workspace.filteredPackages.values) {
for (final platform in ['ios', 'macos']) {
final packageSwiftFile =
File('${package.path}/$platform/${package.name}/Package.swift');
if (!packageSwiftFile.existsSync()) continue;
var content = packageSwiftFile.readAsStringSync();
// Update firebase_sdk_version
content = content.replaceAll(
RegExp('let firebase_sdk_version: Version = "[^"]+"'),
'let firebase_sdk_version: Version = "$firebaseiOSVersion"',
);
// Update shared_spm_version
content = content.replaceAll(
RegExp('let shared_spm_version: Version = "[^"]+"'),
'let shared_spm_version: Version = "$firebaseCoreVersion-firebase-core-swift"',
);
// Update library_version or library_version_string from pubspec version
final pubspecFile = File('${package.path}/pubspec.yaml');
if (pubspecFile.existsSync()) {
final pubspecYaml = loadYaml(pubspecFile.readAsStringSync());
final version = pubspecYaml['version']?.toString();
if (version != null) {
final spmVersion = version.replaceAll('+', '-');
content = content.replaceAll(
RegExp('let library_version_string = "[^"]+"'),
'let library_version_string = "$spmVersion"',
);
content = content.replaceAll(
RegExp('let library_version = "[^"]+"'),
'let library_version = "$spmVersion"',
);
}
}
packageSwiftFile.writeAsStringSync(content);
print('Updated ${package.name}/$platform/Package.swift');
}
}
}
void updateLibraryVersionPureSwiftPlugins() {
// Packages that require updating library versions
const packages = [
'firebase_ml_model_downloader',
'firebase_app_installations',
'cloud_functions',
'firebase_remote_config',
'firebase_app_check',
];
for (final package in packages) {
final pubspecPath = 'packages/$package/$package/pubspec.yaml';
final pubspecFile = File(pubspecPath);
if (!pubspecFile.existsSync()) {
print('Error: pubspec.yaml file not found at $pubspecFile');
return;
}
// Read the pubspec.yaml file
final pubspecContent = pubspecFile.readAsStringSync();
final pubspecYaml = loadYaml(pubspecContent);
// Extract the version
final version = pubspecYaml['version'];
if (version == null) {
print('Error: Version not found in pubspec.yaml');
return;
}
// Define the path to the Constants.swift file
final packageSwiftPath =
'packages/$package/$package/ios/$package/Sources/$package/Constants.swift';
// Read the Constants.swift file
final constantsSwiftFile = File(packageSwiftPath);
if (!constantsSwiftFile.existsSync()) {
print('Error: Constants.swift file not found at $packageSwiftPath');
return;
}
// Read the content of Constants.swift
final constantsFileContent = constantsSwiftFile.readAsStringSync();
// Update the versionNumber with the new version
final updatedConstantsFileContent = constantsFileContent.replaceAll(
RegExp('public let versionNumber = "[^"]+"'),
'public let versionNumber = "$version"',
);
// Write the updated content back to Constants.swift
constantsSwiftFile.writeAsStringSync(updatedConstantsFileContent);
print('Updated Constants.swift with $package version: $version');
}
}