From 8651035785b128d9f0cec1607320cb0a1fab32cd Mon Sep 17 00:00:00 2001 From: Justin Bennett Date: Wed, 2 Mar 2022 14:33:52 -0800 Subject: [PATCH] Update lockfile when writing new version --- plugins/gem/__tests__/gem.test.ts | 45 ++++++++++++++++++++++++++++++- plugins/gem/src/index.ts | 39 +++++++++++++-------------- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/plugins/gem/__tests__/gem.test.ts b/plugins/gem/__tests__/gem.test.ts index c58a3959b..5c9707a4f 100644 --- a/plugins/gem/__tests__/gem.test.ts +++ b/plugins/gem/__tests__/gem.test.ts @@ -226,7 +226,7 @@ describe("Gem Plugin", () => { }); describe("version", () => { - test("bump version", async () => { + test("bump version without lock", async () => { mockFs({ 'test.gemspec': endent` Gem::Specification.new do |spec| @@ -248,6 +248,49 @@ describe("Gem Plugin", () => { `); }) + + test("bump version with lock", async () => { + mockFs({ + 'gem.gemspec': endent` + Gem::Specification.new do |spec| + spec.version = "0.1.0" + end + `, + 'Gemfile.lock': endent` + PATH + remote: . + specs: + gem (0.1.0) + GEM + remote: https://rubygems.org/ + specs: + foo-gem (0.1.0) + ` + }) + + const plugin = new Gem(); + const hooks = makeHooks(); + + plugin.apply({ hooks, logger } as any); + await hooks.version.promise({ bump: SEMVER.minor }); + + expect(await fs.readFile('gem.gemspec', { encoding: 'utf-8' })).toBe(endent` + Gem::Specification.new do |spec| + spec.version = "0.2.0" + end + `); + expect(await fs.readFile('Gemfile.lock', { encoding: 'utf-8' })).toBe(endent` + PATH + remote: . + specs: + gem (0.2.0) + GEM + remote: https://rubygems.org/ + specs: + foo-gem (0.1.0) + `); + }) + test("throws with invalid version", async () => { mockFs({ 'test.gemspec': endent` diff --git a/plugins/gem/src/index.ts b/plugins/gem/src/index.ts index 773c2f0f4..a457f698a 100644 --- a/plugins/gem/src/index.ts +++ b/plugins/gem/src/index.ts @@ -136,21 +136,18 @@ export default class GemPlugin implements IPlugin { await this.writeNewVersion(version, canaryVersion, versionFile) - /** Commit the new version. we wait because "rake build" changes the lock file */ - /** we don't push that version, is just to clean the stage */ - const commitVersion = async () => - execPromise("git", [ - "commit", - "-am", - `"update version: ${canaryVersion} [skip ci]"`, - "--no-verify", - ]); + /** Commit the new version which we don't push. It's just to clean the stage */ + await execPromise("git", [ + "commit", + "-am", + `"update version: ${canaryVersion} [skip ci]"`, + "--no-verify", + ]); auto.logger.verbose.info("Running default release command"); const buildResult = await execPromise("bundle", ["exec", "rake", "build"]); const gemPath = GEM_PKG_BUILD_REGEX.exec(buildResult)?.[0] - await commitVersion(); // will push the canary gem await execPromise("gem", ["push", `${gemPath}`]); @@ -170,23 +167,19 @@ export default class GemPlugin implements IPlugin { const [version] = await this.getVersion(auto); - /** Commit the new version. we wait because "rake build" changes the lock file */ - const commitVersion = async () => - execPromise("git", [ - "commit", - "-am", - `"update version: ${version} [skip ci]"`, - "--no-verify", - ]); + await execPromise("git", [ + "commit", + "-am", + `"update version: ${version} [skip ci]"`, + "--no-verify", + ]); if (this.options.releaseCommand) { auto.logger.verbose.info("Running custom release command"); - await commitVersion(); execSync(this.options.releaseCommand, { stdio: "inherit" }); } else { auto.logger.verbose.info("Running default release command"); await execPromise("bundle", ["exec", "rake", "build"]); - await commitVersion(); // will tag and push await execPromise("bundle", ["exec", "rake", "release"]); } @@ -265,6 +258,12 @@ export default class GemPlugin implements IPlugin { const content = await readFile(versionFile, { encoding: "utf8" }); await writeFile(versionFile, content.replace(version, newVersion)); + /** Update the lockfile to ensure no changes would be made on bundle exec rake build */ + const lockFile = 'Gemfile.lock'; + if (fs.existsSync(lockFile)) { + const lockContent = await readFile(lockFile, { encoding: "utf8" }) + await writeFile(lockFile, lockContent.replace(` ${this.name} (${version})`, ` ${this.name} (${newVersion})`)) + } } /** Get the current version of the gem and where it was found */