@auto-canary/maven
Version:
Maven publishing plugin for auto
133 lines • 6.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
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 path_1 = tslib_1.__importDefault(require("path"));
const util_1 = require("util");
const pom_parser_1 = require("pom-parser");
const semver_1 = require("semver");
/** Ensure a value is an array */
const arrayify = (arg) => (Array.isArray(arg) ? arg : [arg]);
const parsePom = util_1.promisify(pom_parser_1.parse);
/** Get the maven pom.xml for a project */
const getPom = async () => parsePom({ filePath: path_1.default.join(process.cwd(), 'pom.xml') });
/** Get the previous version from the pom.xml */
async function getPreviousVersion(auto) {
var _a;
const pom = await getPom();
const previousVersion = (_a = pom.pomObject) === null || _a === void 0 ? void 0 : _a.project.version;
if (!previousVersion) {
throw new Error('Cannot read version from the pom.xml.');
}
auto.logger.verbose.info('Maven: Got previous version from package.json', previousVersion);
return previousVersion.replace('-SNAPSHOT', '');
}
/** Deploy project to maven */
class MavenPlugin {
constructor() {
/** The name of the plugin */
this.name = 'maven';
}
/** Tap into auto plugin points. */
apply(auto) {
auto.hooks.onCreateLogParse.tap(this.name, logParse => {
logParse.hooks.omitCommit.tap(this.name, commit => {
if (commit.subject.includes('[maven-release-plugin]')) {
return true;
}
});
});
auto.hooks.beforeShipIt.tap(this.name, async () => {
const { MAVEN_PASSWORD, MAVEN_USERNAME, MAVEN_SETTINGS } = process.env;
if (!MAVEN_PASSWORD && !MAVEN_SETTINGS) {
auto.logger.log.warn('No password detected in the environment. You may need this to publish.');
}
if (!MAVEN_USERNAME && !MAVEN_SETTINGS) {
auto.logger.log.warn('No username detected in the environment. You may need this to publish.');
}
});
auto.hooks.getRepository.tapPromise(this.name, async () => {
auto.logger.verbose.info('Maven: getting repo information from pom.xml');
const pom = await getPom();
const { scm } = pom.pomObject.project;
if (!scm) {
throw new Error('Could not find scm settings in pom.xml. Make sure you set one up that points to your project on GitHub or set owner+repo in your .autorc');
}
const github = arrayify(pom.pomObject.project.scm).find(remote => Boolean(remote.url.includes('github')));
if (!github) {
throw new Error('Could not find GitHub scm settings in pom.xml. Make sure you set one up that points to your project on GitHub or set owner+repo in your .autorc');
}
const repoInfo = parse_github_url_1.default(github.url);
if (!repoInfo || !repoInfo.owner || !repoInfo.name) {
throw new Error('Cannot read owner and project name from GitHub URL in pom.xml');
}
return {
owner: repoInfo.owner,
repo: repoInfo.name
};
});
auto.hooks.getAuthor.tapPromise(this.name, async () => {
var _a;
auto.logger.verbose.info('Maven: Getting repo information from pom.xml');
const pom = await getPom();
const developers = (_a = pom.pomObject.project.developers) === null || _a === void 0 ? void 0 : _a.developer;
const developer = developers ? arrayify(developers)[0] : undefined;
if (!developer || !developer.name || !developer.email) {
throw new Error('Cannot read author from the pom.xml. Please include at least 1 developer in the developers section.');
}
return developer;
});
auto.hooks.getPreviousVersion.tapPromise(this.name, () => getPreviousVersion(auto));
auto.hooks.version.tapPromise(this.name, async (version) => {
const previousVersion = await getPreviousVersion(auto);
const newVersion =
// 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
version === 'patch'
? previousVersion
: semver_1.inc(previousVersion, version);
if (!newVersion) {
throw new Error(`Could not increment previous version: ${previousVersion}`);
}
await core_1.execPromise('mvn', ['clean']);
await core_1.execPromise('mvn', [
'-B',
'release:prepare',
`-Dtag=v${newVersion}`,
`-DreleaseVersion=${newVersion}`,
'-DpushChanges=false'
]);
await core_1.execPromise('git', ['checkout', '-b', 'dev-snapshot']);
await core_1.execPromise('git', ['checkout', 'master']);
await core_1.execPromise('git', ['reset', '--hard', 'HEAD~1']);
});
auto.hooks.publish.tapPromise(this.name, async () => {
const { MAVEN_PASSWORD, MAVEN_USERNAME, MAVEN_SETTINGS } = process.env;
auto.logger.log.await('Performing maven release...');
await core_1.execPromise('git', [
'push',
'--follow-tags',
'--set-upstream',
'origin',
auto.baseBranch
]);
await core_1.execPromise('mvn', [
MAVEN_PASSWORD && `-Dpassword=${MAVEN_PASSWORD}`,
MAVEN_USERNAME && `-Dusername=${MAVEN_USERNAME}`,
MAVEN_SETTINGS && `-s=${MAVEN_SETTINGS}`,
'release:perform'
]);
auto.logger.log.success('Published code to maven!');
});
auto.hooks.afterShipIt.tapPromise(this.name, async () => {
// prepare for next development iteration
await core_1.execPromise('git', ['reset', '--hard', 'dev-snapshot']);
await core_1.execPromise('git', ['branch', '-d', 'dev-snapshot']);
await core_1.execPromise('git', ['push', 'origin', auto.baseBranch]);
});
}
}
exports.default = MavenPlugin;
//# sourceMappingURL=index.js.map