|
| 1 | +#!/usr/bin/env dart |
| 2 | +/* |
| 3 | + * Copyright 2025 LiveKit |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
1 | 18 | import 'dart:io'; |
2 | 19 |
|
3 | 20 | import 'package:yaml/yaml.dart'; |
4 | 21 |
|
5 | | -void main() { |
6 | | - final File pubspec = File('pubspec.yaml'); |
7 | | - final doc = loadYaml(pubspec.readAsStringSync()); |
8 | | - final versionStr = doc['version']; |
| 22 | +class _Path { |
| 23 | + static const pubspec = 'pubspec.yaml'; |
| 24 | + static const iosPodspec = 'ios/livekit_client.podspec'; |
| 25 | + static const macosPodspec = 'macos/livekit_client.podspec'; |
| 26 | + static const livekitVersion = 'lib/src/livekit.dart'; |
| 27 | +} |
9 | 28 |
|
10 | | - final RegExpMatch? match = RegExp(r'(\d+\.\d+\.\d+)').firstMatch(versionStr); |
| 29 | +class _Pattern { |
| 30 | + static final semanticVersion = RegExp(r'(\d+\.\d+\.\d+)'); |
| 31 | + static final webrtcSdkVersion = RegExp(r"s\.dependency\s+'WebRTC-SDK',\s+'([^']+)'"); |
| 32 | +} |
11 | 33 |
|
12 | | - final version = match?[0]; |
| 34 | +class _Color { |
| 35 | + static const reset = '\x1B[0m'; |
| 36 | + static const green = '\x1B[32m'; |
| 37 | + static const bold = '\x1B[1m'; |
| 38 | +} |
13 | 39 |
|
14 | | - if (version == null) { |
15 | | - // ignore: avoid_print |
16 | | - print('Could not find version in pubspec.yaml'); |
17 | | - exit(1); |
| 40 | +String readFile(String path) { |
| 41 | + try { |
| 42 | + return File(path).readAsStringSync(); |
| 43 | + } catch (e) { |
| 44 | + throw Exception('Failed to read $path: $e'); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +String extractVersionFromPubspec() { |
| 49 | + final content = readFile(_Path.pubspec); |
| 50 | + final doc = loadYaml(content); |
| 51 | + final versionStr = doc['version']; |
| 52 | + |
| 53 | + final match = _Pattern.semanticVersion.firstMatch(versionStr); |
| 54 | + if (match == null) { |
| 55 | + throw Exception('Could not find version in ${_Path.pubspec}'); |
18 | 56 | } |
19 | 57 |
|
20 | | - print('Checking version $version'); |
| 58 | + return match.group(0)!; |
| 59 | +} |
21 | 60 |
|
22 | | - final files = [ |
23 | | - 'ios/livekit_client.podspec', |
24 | | - 'macos/livekit_client.podspec', |
25 | | - 'lib/src/livekit.dart' |
| 61 | +void checkVersionConsistency(String expectedVersion) { |
| 62 | + final filesToCheck = [ |
| 63 | + _Path.iosPodspec, |
| 64 | + _Path.macosPodspec, |
| 65 | + _Path.livekitVersion, |
26 | 66 | ]; |
27 | 67 |
|
28 | | - for (var file in files) { |
29 | | - final content = File(file).readAsStringSync(); |
30 | | - if (!content.contains(version)) { |
31 | | - final RegExp exp = RegExp(r'(\d+\.\d+\.\d+)'); |
32 | | - final RegExpMatch? match = exp.firstMatch(content); |
33 | | - // ignore: avoid_print |
34 | | - print( |
35 | | - 'Version mismatch in $file, pubspec.yaml version is $version != ${match![0]} in $file, please update'); |
36 | | - exit(1); |
| 68 | + for (final file in filesToCheck) { |
| 69 | + final content = readFile(file); |
| 70 | + if (!content.contains(expectedVersion)) { |
| 71 | + final match = _Pattern.semanticVersion.firstMatch(content); |
| 72 | + final foundVersion = match?.group(0) ?? 'unknown'; |
| 73 | + throw Exception('Version mismatch in $file: expected $expectedVersion, found $foundVersion'); |
37 | 74 | } |
38 | 75 | } |
39 | 76 | } |
| 77 | + |
| 78 | +void checkWebRtcSdkVersions() { |
| 79 | + final iosPodspec = readFile(_Path.iosPodspec); |
| 80 | + final macosPodspec = readFile(_Path.macosPodspec); |
| 81 | + |
| 82 | + final iosMatch = _Pattern.webrtcSdkVersion.firstMatch(iosPodspec); |
| 83 | + final macosMatch = _Pattern.webrtcSdkVersion.firstMatch(macosPodspec); |
| 84 | + |
| 85 | + if (iosMatch == null) { |
| 86 | + throw Exception('Could not find WebRTC-SDK version in ${_Path.iosPodspec}'); |
| 87 | + } |
| 88 | + |
| 89 | + if (macosMatch == null) { |
| 90 | + throw Exception('Could not find WebRTC-SDK version in ${_Path.macosPodspec}'); |
| 91 | + } |
| 92 | + |
| 93 | + final iosVersion = iosMatch.group(1)!; |
| 94 | + final macosVersion = macosMatch.group(1)!; |
| 95 | + |
| 96 | + if (iosVersion != macosVersion) { |
| 97 | + throw Exception('WebRTC-SDK version mismatch: iOS=$iosVersion, macOS=$macosVersion'); |
| 98 | + } |
| 99 | + |
| 100 | + print('${_Color.green}WebRTC-SDK versions match: $iosVersion${_Color.reset}'); |
| 101 | +} |
| 102 | + |
| 103 | +void main() { |
| 104 | + try { |
| 105 | + final version = extractVersionFromPubspec(); |
| 106 | + print('Checking version ${_Color.bold}${_Color.green}$version${_Color.reset}'); |
| 107 | + |
| 108 | + checkVersionConsistency(version); |
| 109 | + checkWebRtcSdkVersions(); |
| 110 | + |
| 111 | + print('${_Color.bold}${_Color.green}All version checks passed ✓${_Color.reset}'); |
| 112 | + } catch (e) { |
| 113 | + print('Error: $e'); |
| 114 | + exit(1); |
| 115 | + } |
| 116 | +} |
0 commit comments