diff --git a/plugins/cocoapods/__tests__/cocoapods.test.ts b/plugins/cocoapods/__tests__/cocoapods.test.ts index c327ee79b..1e902a8f0 100644 --- a/plugins/cocoapods/__tests__/cocoapods.test.ts +++ b/plugins/cocoapods/__tests__/cocoapods.test.ts @@ -78,6 +78,7 @@ describe("Cocoapods Plugin", () => { logger: logger, prefixRelease, git: { + getLastTagNotInBaseBranch: async () => undefined, getLatestRelease: async () => "0.0.1", getPullRequest: async () => ({ data: { @@ -438,6 +439,50 @@ describe("Cocoapods Plugin", () => { }); }); + describe("next hook", () => { + test("should return prerelease versions on dryrun", async () => { + mockPodspec(specWithVersion("0.0.1")); + + const versions = await hooks.next.promise(["0.0.1", "0.0.2"], { + bump: Auto.SEMVER.minor, + dryRun: true, + commits: [], + fullReleaseNotes: "", + releaseNotes: "", + }); + expect(versions).toStrictEqual(["0.0.1", "0.0.2", "0.1.0-next.0"]); + }); + test("should tag with next version", async () => { + jest.spyOn(Auto, "getCurrentBranch").mockReturnValue("next"); + let podSpec = specWithVersion("0.0.1"); + jest + .spyOn(utilities, "getPodspecContents") + .mockImplementation(() => podSpec); + const mock = jest + .spyOn(utilities, "writePodspecContents") + .mockImplementation((path, contents) => { + podSpec = contents; + }); + + const versions = await hooks.next.promise([], { + bump: Auto.SEMVER.major, + dryRun: false, + commits: [], + fullReleaseNotes: "", + releaseNotes: "", + }); + + expect(versions).toContain("1.0.0-next.0"); + expect(exec).toBeCalledTimes(4); + expect(exec).toHaveBeenCalledWith("git", ["checkout", "./Test.podspec"]); + + expect(mock).toHaveBeenLastCalledWith( + expect.any(String), + specWithVersion("1.0.0-next.0") + ); + }); + }); + describe("publish hook", () => { test("should push to trunk if no specsRepo in options", async () => { mockPodspec(specWithVersion("0.0.1")); diff --git a/plugins/cocoapods/src/index.ts b/plugins/cocoapods/src/index.ts index da2512cd0..a483cfcba 100644 --- a/plugins/cocoapods/src/index.ts +++ b/plugins/cocoapods/src/index.ts @@ -5,6 +5,9 @@ import { validatePluginConfiguration, ILogger, getPrNumberFromEnv, + DEFAULT_PRERELEASE_BRANCHES, + getCurrentBranch, + determineNextVersion, } from "@auto-it/core"; import { inc, ReleaseType } from "semver"; @@ -277,6 +280,57 @@ export default class CocoapodsPlugin implements IPlugin { } ); + auto.hooks.next.tapPromise( + this.name, + async (preReleaseVersions, { bump, dryRun }) => { + if (!auto.git) { + return preReleaseVersions; + } + + const prereleaseBranches = + auto.config?.prereleaseBranches ?? DEFAULT_PRERELEASE_BRANCHES; + const branch = getCurrentBranch() || ""; + const prereleaseBranch = prereleaseBranches.includes(branch) + ? branch + : prereleaseBranches[0]; + const lastRelease = await auto.git.getLatestRelease(); + const current = + (await auto.git.getLastTagNotInBaseBranch(prereleaseBranch)) || + (await auto.getCurrentVersion(lastRelease)); + const prerelease = determineNextVersion( + lastRelease, + current, + bump, + prereleaseBranch + ); + + preReleaseVersions.push(prerelease); + + if (dryRun) { + return preReleaseVersions; + } + + await execPromise("git", [ + "tag", + prerelease, + "-m", + `"Tag pre-release: ${prerelease}"`, + ]); + + await execPromise("git", ["push", auto.remote, branch, "--tags"]); + + updatePodspecVersion(this.options.podspecPath, prerelease); + + // Publish the next podspec, committing it isn't needed for specs push + await this.publishPodSpec(podLogLevel); + + // Reset changes to podspec file since it doesn't need to be committed + await execPromise("git", ["checkout", this.options.podspecPath]); + + return preReleaseVersions; + } + ); + auto.hooks.beforeShipIt.tapPromise(this.name, async ({ dryRun }) => { if (dryRun) { auto.logger.log.info(logMessage('dryRun - running "pod lib lint"'));