UNPKG

@auto-it/maven

Version:

Maven publishing plugin for auto

227 lines • 10.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPom = void 0; const tslib_1 = require("tslib"); const core_1 = require("@auto-it/core"); const parse_github_url_1 = tslib_1.__importDefault(require("parse-github-url")); const util_1 = require("util"); const t = tslib_1.__importStar(require("io-ts")); const pom_parser_1 = require("pom-parser"); const semver_1 = require("semver"); const auto_1 = require("@auto-it/core/dist/auto"); const jsdom = tslib_1.__importStar(require("jsdom")); const nativeVersionUpdate = tslib_1.__importStar(require("./native-version-update")); const maven = tslib_1.__importStar(require("./maven")); const snapshotSuffix = "-SNAPSHOT"; /** Ensure a value is an array **/ const arrayify = (arg) => (Array.isArray(arg) ? arg : [arg]); /** Parse the pom.xml file **/ const parsePom = util_1.promisify(pom_parser_1.parse); /** Get the maven pom.xml for a project **/ const getPom = async (filePath = "pom.xml") => parsePom({ filePath: filePath }); exports.getPom = getPom; const pluginOptions = t.partial({ /** The maven binary to release the project with **/ mavenCommand: t.string, /** A list of maven command customizations to pass to maven **/ mavenOptions: t.array(t.string), /** A list of maven goals to pass to maven for release **/ mavenReleaseGoals: t.array(t.string), /** Path to maven settings file **/ mavenSettings: t.string, /** Print width for prettier formatting **/ printWidth: t.number, /** Tab width for prettier formatting **/ tabWidth: t.number, /** * The username to use when deploying to the maven repository * * @deprecated since 9.38.0 **/ mavenUsername: t.string, /** * The password to use when deploying to the maven repository * * @deprecated since 9.38.0 **/ mavenPassword: t.string, }); /** Deploy project to maven repository */ class MavenPlugin { /** Initialize the plugin with its options **/ constructor(options = {}) { /** The name of the plugin */ this.name = "maven"; /** cached properties **/ this.properties = {}; /** should this release be a snapshot release **/ this.snapshotRelease = false; /** should pom.xml versions be handled with the "versions-maven-plugin" **/ this.versionsMavenPlugin = false; const { MAVEN_COMMAND, MAVEN_OPTIONS, MAVEN_RELEASE_GOALS, MAVEN_SETTINGS, MAVEN_PASSWORD, MAVEN_USERNAME, } = process.env; this.options = { mavenCommand: MAVEN_COMMAND || options.mavenCommand || "/usr/bin/mvn", mavenOptions: (MAVEN_OPTIONS === null || MAVEN_OPTIONS === void 0 ? void 0 : MAVEN_OPTIONS.split(" ")) || options.mavenOptions || [], mavenReleaseGoals: (MAVEN_RELEASE_GOALS === null || MAVEN_RELEASE_GOALS === void 0 ? void 0 : MAVEN_RELEASE_GOALS.split(" ")) || options.mavenReleaseGoals || ["deploy", "site-deploy"], mavenSettings: MAVEN_SETTINGS || options.mavenSettings || "", printWidth: options.printWidth || 120, tabWidth: options.tabWidth || 4, mavenUsername: MAVEN_USERNAME || options.mavenUsername || "", mavenPassword: MAVEN_PASSWORD || options.mavenPassword || "", }; } /** Detect whether the parent pom.xml has the versions-maven-plugin **/ static async detectVersionMavenPlugin() { const pom = await exports.getPom(); const pomDom = new jsdom.JSDOM(pom.pomXml, { contentType: "text/xml" }) .window.document; const versionsMavenPluginNode = pomDom.evaluate("/project/build/plugins/plugin/artifactId[normalize-space(text())='versions-maven-plugin']", pomDom.documentElement, pomDom.createNSResolver(pomDom.documentElement), 9 // XPathResult.FIRST_ORDERED_NODE_TYPE ); if (versionsMavenPluginNode === null || versionsMavenPluginNode === void 0 ? void 0 : versionsMavenPluginNode.singleNodeValue) { return true; } const versionsMavenPluginManagementNode = pomDom.evaluate("/project/build/pluginManagement/plugins/plugin/artifactId[normalize-space(text())='versions-maven-plugin']", pomDom.documentElement, pomDom.createNSResolver(pomDom.documentElement), 9 // XPathResult.FIRST_ORDERED_NODE_TYPE ); return Boolean(versionsMavenPluginManagementNode === null || versionsMavenPluginManagementNode === void 0 ? void 0 : versionsMavenPluginManagementNode.singleNodeValue); } /** Get the properties from the pom.xml file **/ static async getProperties() { var _a, _b, _c, _d, _e; const pom = await exports.getPom(); const { scm } = ((_a = pom.pomObject) === null || _a === void 0 ? void 0 : _a.project) || {}; let github; let repoInfo; if (scm) { github = arrayify(scm).find((remote) => Boolean(remote.url.includes("github"))); } if (github) { repoInfo = parse_github_url_1.default(github.url); } const developers = (_d = (_c = (_b = pom.pomObject) === null || _b === void 0 ? void 0 : _b.project) === null || _c === void 0 ? void 0 : _c.developers) === null || _d === void 0 ? void 0 : _d.developer; const developer = developers ? arrayify(developers)[0] : undefined; return { version: (_e = pom.pomObject) === null || _e === void 0 ? void 0 : _e.project.version, owner: (repoInfo === null || repoInfo === void 0 ? void 0 : repoInfo.owner) || undefined, repo: (repoInfo === null || repoInfo === void 0 ? void 0 : repoInfo.name) || undefined, developer: developer, }; } /** Update pom file, using whatever engine is configured */ async updatePoms(version, auto, commitMessage) { if (this.versionsMavenPlugin) { await maven.updatePoms(version, this.options, auto, commitMessage); } else { await nativeVersionUpdate.updatePoms(version, this.options, auto); } } /** Tap into auto plugin points. */ apply(auto) { auto.hooks.beforeRun.tapPromise(this.name, async () => { this.properties = await MavenPlugin.getProperties(); const { version = "" } = this.properties; if (version === null || version === void 0 ? void 0 : version.endsWith(snapshotSuffix)) { this.snapshotRelease = true; } this.versionsMavenPlugin = await MavenPlugin.detectVersionMavenPlugin(); }); auto.hooks.validateConfig.tapPromise(this.name, async (name, options) => { if (name === this.name || name === `@auto-it/${this.name}`) { return auto_1.validatePluginConfiguration(this.name, pluginOptions, options); } }); auto.hooks.onCreateLogParse.tap(this.name, (logParse) => { logParse.hooks.omitCommit.tap(this.name, (commit) => { if (commit.subject.includes("Prepare version")) { return true; } }); }); auto.hooks.getPreviousVersion.tapPromise(this.name, async () => auto.prefixRelease(await this.getVersion(auto))); auto.hooks.getAuthor.tapPromise(this.name, async () => { const { developer } = this.properties; auto.logger.verbose.info(`Found author information in pom.xml: ${developer}`); return developer; }); auto.hooks.getRepository.tapPromise(this.name, async () => { const { owner, repo } = this.properties; auto.logger.verbose.info(`Found repo information in pom.xml: ${owner}/${repo}`); return { owner: owner, repo: repo, }; }); auto.hooks.version.tapPromise(this.name, async ({ bump, dryRun, quiet }) => { const previousVersion = await this.getVersion(auto); const releaseVersion = // After release we bump the version by a patch and add -SNAPSHOT // Given that we do not need to increment when versioning, since // it has already been done this.snapshotRelease && bump === "patch" ? previousVersion : semver_1.inc(previousVersion, bump); if (dryRun && releaseVersion) { if (quiet) { console.log(releaseVersion); } else { auto.logger.log.info(`Would have published: ${releaseVersion}`); } return; } if (releaseVersion) { await this.updatePoms(releaseVersion, auto, `"Release ${releaseVersion} [skip ci]"`); const newVersion = auto.prefixRelease(releaseVersion); // Ensure tag is on this commit, changelog will be added automatically await core_1.execPromise("git", [ "tag", newVersion, "-m", `"Update version to ${newVersion}"`, ]); } }); auto.hooks.publish.tapPromise(this.name, async () => { auto.logger.verbose.warn(`Running "publish"`); await maven.executeReleaseGoals(this.options); await core_1.execPromise("git", [ "push", "--follow-tags", "--set-upstream", auto.remote, auto.baseBranch, ]); }); auto.hooks.afterShipIt.tapPromise(this.name, async ({ dryRun }) => { if (!this.snapshotRelease || dryRun) { return; } auto.logger.verbose.info("Running afterShipIt for maven update"); const releaseVersion = await this.getVersion(auto); // snapshots precede releases, so if we had a minor/major release, // then we need to set up snapshots on the next version const newVersion = `${semver_1.inc(releaseVersion, "patch")}${snapshotSuffix}`; await this.updatePoms(newVersion, auto, `"Prepare version ${newVersion} [skip ci]"`); await core_1.execPromise("git", [ "push", "--follow-tags", "--set-upstream", auto.remote, auto.baseBranch, ]); }); } /** Get the version from the current pom.xml **/ async getVersion(auto) { this.properties = await MavenPlugin.getProperties(); const { version } = this.properties; if (version) { auto.logger.verbose.info(`Found version in pom.xml: ${version}`); return version.replace(snapshotSuffix, ""); } return "0.0.0"; } } exports.default = MavenPlugin; //# sourceMappingURL=index.js.map