Skip to content

Commit

Permalink
chore: update melos preCommit hook for data connect version (#13402)
Browse files Browse the repository at this point in the history
  • Loading branch information
russellwheatley authored Sep 25, 2024
1 parent 181f208 commit 40521d1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
4 changes: 3 additions & 1 deletion melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ command:
hooks:
preCommit: |
dart run scripts/generate_vertexai_version.dart && \
git add packages/firebase_vertexai/firebase_vertexai/lib/src/vertex_version.dart
dart run scripts/generate_dataconnect_version.dart && \
git add packages/firebase_vertexai/firebase_vertexai/lib/src/vertex_version.dart && \
git add packages/firebase_data_connect/firebase_data_connect/lib/src/dataconnect_version.dart
bootstrap:
# It seems so that running "pub get" in parallel has some issues (like
Expand Down
65 changes: 65 additions & 0 deletions scripts/generate_dataconnect_version.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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.

import 'dart:io' show Directory, File;
import 'package:path/path.dart' show joinAll;
import 'package:yaml/yaml.dart' show YamlMap, loadYaml;

Future<void> main() async {
final outputPath = joinAll(
[
Directory.current.path,
'packages',
'firebase_data_connect',
'firebase_data_connect',
'lib',
'src',
'dataconnect_version.dart',
],
);

final pubspecPath = joinAll(
[
Directory.current.path,
'packages',
'firebase_data_connect',
'firebase_data_connect',
'pubspec.yaml'
],
);
final yamlMap = loadYaml(File(pubspecPath).readAsStringSync()) as YamlMap;
final currentVersion = yamlMap['version'] as String;
final fileContents = File(outputPath).readAsStringSync();

final lines = fileContents.split('\n');

const versionLinePrefix = 'const packageVersion = ';
bool versionLineFound = false;
for (int i = 0; i < lines.length; i++) {
if (lines[i].startsWith(versionLinePrefix)) {
lines[i] = "$versionLinePrefix'$currentVersion';";
versionLineFound = true;
break;
}
}

if (!versionLineFound) {
lines.add("$versionLinePrefix'$currentVersion';");
}

// Join the lines back into a single string
final newFileContents = lines.join('\n');

await File(outputPath).writeAsString(newFileContents);
}

0 comments on commit 40521d1

Please sign in to comment.