diff --git a/e2e/__snapshots__/e2e.spec.ts.snap b/e2e/__snapshots__/e2e.spec.ts.snap index 63a3ef5..f7f8162 100644 --- a/e2e/__snapshots__/e2e.spec.ts.snap +++ b/e2e/__snapshots__/e2e.spec.ts.snap @@ -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 @@ -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 } diff --git a/e2e/fixtures/unversioned/MODULE.bazel b/e2e/fixtures/unversioned/MODULE.bazel index 38276ac..e00336e 100644 --- a/e2e/fixtures/unversioned/MODULE.bazel +++ b/e2e/fixtures/unversioned/MODULE.bazel @@ -1,4 +1 @@ -module( - name = "unversioned", - version = "0.0.0", -) \ No newline at end of file +module(name = "unversioned") diff --git a/src/domain/create-entry.ts b/src/domain/create-entry.ts index fea6ee0..9d93949 100644 --- a/src/domain/create-entry.ts +++ b/src/domain/create-entry.ts @@ -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; diff --git a/src/domain/module-file.ts b/src/domain/module-file.ts index ef66ea7..36817ac 100644 --- a/src/domain/module-file.ts +++ b/src/domain/module-file.ts @@ -23,8 +23,8 @@ 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 { @@ -32,10 +32,19 @@ export class ModuleFile { } 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) {