Skip to content

Commit

Permalink
Cope with missing version attribute on module
Browse files Browse the repository at this point in the history
  • Loading branch information
lalten committed Jan 26, 2024
1 parent e765a50 commit c98cae5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
20 changes: 7 additions & 13 deletions e2e/__snapshots__/e2e.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,17 @@ exports[`e2e tests [snapshot] ruleset with unversioned module in source 1`] = `
"----------------------------------------------------
modules/unversioned/1.0.0/MODULE.bazel
----------------------------------------------------
module(
name = \\"unversioned\\",
version = \\"1.0.0\\",
)
module(name = \\"unversioned\\", version = \\"1.0.0\\")
----------------------------------------------------
modules/unversioned/1.0.0/patches/module_dot_bazel_version.patch
----------------------------------------------------
===================================================================
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -1,4 +1,4 @@
module(
name = \\"unversioned\\",
- version = \\"0.0.0\\",
+ version = \\"1.0.0\\",
)
\\\\ No newline at end of file
@@ -1,1 +1,1 @@
-module(name = \\"unversioned\\")
+module(name = \\"unversioned\\", version = \\"1.0.0\\")
----------------------------------------------------
modules/unversioned/1.0.0/presubmit.yml
Expand All @@ -204,11 +198,11 @@ bcr_test_module:
modules/unversioned/1.0.0/source.json
----------------------------------------------------
{
\\"integrity\\": \\"sha256-eXQOU+DNwg/Q29tX59jP0+SFLvRHrQf3z+KJVS/gIRk=\\",
\\"integrity\\": \\"sha256-X2ZIYATg3qceheFSj/qaTU0SqqW/hFc8UIGkrK3FmZQ=\\",
\\"strip_prefix\\": \\"unversioned-1.0.0\\",
\\"url\\": \\"https://github.com/testorg/unversioned/archive/refs/tags/v1.0.0.tar.gz\\",
\\"patches\\": {
\\"module_dot_bazel_version.patch\\": \\"sha256-y0kC8heeH9bQKhrfx2JuX+RK0KyjwHhPac3wBc4Nkg4=\\"
\\"module_dot_bazel_version.patch\\": \\"sha256-OoXz9ahBlGjW4IVb4QCmXMZInK7XbFEjCHya3wx8AAk=\\"
},
\\"patch_strip\\": 1
}
Expand Down
5 changes: 1 addition & 4 deletions e2e/fixtures/unversioned/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
module(
name = "unversioned",
version = "0.0.0",
)
module(name = "unversioned")
3 changes: 2 additions & 1 deletion src/domain/create-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ export class CreateEntryService {
): void {
if (moduleFile.version !== version) {
console.log(
"Archived MODULE.bazel version does not match release version. Creating a version patch."
`Archived MODULE.bazel version ${moduleFile.version} does not match release version ${version}.`,
"Creating a version patch.",
);
const patchFileName = "module_dot_bazel_version.patch";
const existingContent = moduleFile.content;
Expand Down
21 changes: 15 additions & 6 deletions src/domain/module-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,28 @@ export class ModuleFile {

public get version(): string {
const regex = /module\([^)]*?version\s*=\s*"(.+?)"/s;
const version = this.moduleContent.match(regex)[1];
return version;
const match = this.moduleContent.match(regex);
return match ? match[1] : "";
}

public get content(): string {
return this.moduleContent;
}

public stampVersion(version: string): void {
this.moduleContent = this.moduleContent.replace(
/(^.*?module\(.*?version\s*=\s*")[\w.]+(".*$)/s,
`$1${version}$2`
);
if (this.version) {
// update the version
this.moduleContent = this.moduleContent.replace(
/(^.*?module\(.*?version\s*=\s*")[\w.]+(".*$)/s,
`$1${version}$2`
);
} else {
// add the version
this.moduleContent = this.moduleContent.replace(
/(^.*?module\(.*?)\)/s,
`$1, version = "${version}")`
);
}
}

public save(destPath: string) {
Expand Down

0 comments on commit c98cae5

Please sign in to comment.