@auto-canary/cocoapods
Version:
Use auto to version your cocoapod
138 lines • 5.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@auto-it/core");
const semver_1 = require("semver");
const utilities_1 = require("./utilities");
const logPrefix = '[Cocoapods-Plugin]';
/** Regex used to pull the version line from the spec */
const versionRegex = /\.version\s*=\s*['|"](?<version>\d+\.\d+\.\d+)['|"]/;
/**
* Wrapper to add logPrefix to messages
*
* @param msg - The message to add a prefix to
* @returns Log message with prefix prepended
*/
const logMessage = (msg) => `${logPrefix} ${msg}`;
/**
* Returns the regex'd version of the podspec file
*
* @param podspecPath - The relative path to the podspec file
* @returns The regular expression array for use in replacing file contents
*/
function getParsedPodspecContents(podspecPath) {
const podspecContents = utilities_1.getPodspecContents(podspecPath);
return versionRegex.exec(podspecContents);
}
exports.getParsedPodspecContents = getParsedPodspecContents;
/**
* Retrieves the version currently in the podspec file
*
* @param podspecPath - The relative path to the podspec file
*/
function getVersion(podspecPath) {
const podspecContents = getParsedPodspecContents(podspecPath);
if (podspecContents &&
podspecContents.groups &&
podspecContents.groups.version) {
return podspecContents.groups.version;
}
throw new Error(`Version could not be found in podspec: ${podspecPath}`);
}
exports.getVersion = getVersion;
/**
* Updates the version in the podspec to the supplied version
*
* @param podspecPath - The relative path to the podspec file
* @param version - The version to update the podspec to
*/
async function updatePodspecVersion(podspecPath, version) {
const previousVersion = getVersion(podspecPath);
const parsedContents = getParsedPodspecContents(podspecPath);
const podspecContents = utilities_1.getPodspecContents(podspecPath);
try {
if (parsedContents && parsedContents[0]) {
const newVersionString = parsedContents[0].replace(previousVersion, version);
const newPodspec = podspecContents.replace(versionRegex, newVersionString);
utilities_1.writePodspecContents(podspecPath, newPodspec);
await core_1.execPromise('git', [
'commit',
'-am',
`"update version: ${version} [skip ci]"`,
'--no-verify'
]);
}
}
catch (error) {
console.log(error);
throw new Error(`Error updating version in podspec: ${podspecPath}`);
}
}
exports.updatePodspecVersion = updatePodspecVersion;
/** Use auto to version your cocoapod */
class CocoapodsPlugin {
/** Initialize the plugin with it's options */
constructor(options) {
/** The name of the plugin */
this.name = 'cocoapods';
this.options = options;
}
/** Tap into auto plugin points. */
apply(auto) {
auto.hooks.getPreviousVersion.tapPromise(this.name, async () => {
return auto.prefixRelease(getVersion(this.options.podspecPath));
});
auto.hooks.version.tapPromise(this.name, async (version) => {
const previousVersion = getVersion(this.options.podspecPath);
const releaseVersion = semver_1.inc(previousVersion, version);
if (!releaseVersion) {
throw new Error(`Could not increment previous version: ${previousVersion}`);
}
await updatePodspecVersion(this.options.podspecPath, releaseVersion);
// Ensure tag is on this commit, changelog will be added automatically
await core_1.execPromise('git', [
'tag',
releaseVersion,
'-m',
`"Update version to ${releaseVersion}"`
]);
});
auto.hooks.publish.tapPromise(this.name, async () => {
await core_1.execPromise('git', [
'push',
'--follow-tags',
'--set-upstream',
auto.remote,
auto.baseBranch
]);
if (this.options.specsRepo) {
try {
await core_1.execPromise('pod', [
'repo',
'add',
'autoPublishRepo',
this.options.specsRepo
]);
auto.logger.log.info(logMessage(`Pushing to specs repo: ${this.options.specsRepo}`));
await core_1.execPromise('pod', [
'repo',
'push',
'autoPublishRepo',
this.options.podspecPath
]);
}
catch (error) {
auto.logger.log.error(logMessage(`Error pushing to specs repo: ${this.options.specsRepo}. Error: ${error}`));
}
finally {
await core_1.execPromise('pod', ['repo', 'remove', 'autoPublishRepo']);
}
}
else {
auto.logger.log.info(logMessage(`Pushing to Cocoapods trunk`));
await core_1.execPromise('pod', ['trunk', 'push', this.options.podspecPath]);
}
});
}
}
exports.default = CocoapodsPlugin;
//# sourceMappingURL=index.js.map