Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Prefix and Suffix support to new versions #25

Merged
merged 4 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ interface BumpRule {
* What items in the version number need to be bumped
*/
bump?: string | string[],

/**
* Prefix to apply on the new version
*/
prefix?: string,

/**
* Reset elements in the version number
Expand All @@ -232,6 +237,11 @@ interface BumpRule {
* => 1.2.0
*/
reset?: string | string[],

/**
* Suffix to apply on the new version
*/
suffix?: string,

/**
* Indicate that this bump should add a tag to the commit with the new version number
Expand Down
101 changes: 101 additions & 0 deletions dist/tests/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,83 @@ describe("Get Current version from files", () => {
expect(e.message).toBe(`No match found in file. Unable to identify current version number.`);
}
}));
describe("Get current version with prefix", () => {
let filePath = "./src/tests/assets/INTEGRATION_VERSION.txt", options = {
scheme: "org_semantic",
versionFile: { path: filePath, line: 4 },
files: [],
rules: [
{
prefix: "v.",
branch: "prefix_1_branch",
bump: "build",
trigger: "commit"
},
{
prefix: "alpha.",
branch: "prefix_2_branch",
bump: "minor",
trigger: "commit"
},
{
suffix: "-ALPHA",
branch: "suffix_1_branch",
bump: "build",
trigger: "commit"
},
{
suffix: "rc",
branch: "suffix_2_branch",
bump: "minor",
trigger: "commit"
}
]
};
test("Get current version that has Prefix 1", () => __awaiter(void 0, void 0, void 0, function* () {
const version = yield utils_1.getCurVersion(options);
expect(version).toBe("v.1.0.2");
}));
test("Get current version that has Prefix 2", () => __awaiter(void 0, void 0, void 0, function* () {
options.versionFile.line = 5;
const version = yield utils_1.getCurVersion(options);
expect(version).toBe("alpha.2.3.5");
}));
test("Get correct version that has both prefixes in front", () => __awaiter(void 0, void 0, void 0, function* () {
options.versionFile.line = 6;
const version = yield utils_1.getCurVersion(options);
expect(version).toBe("v.2.3.5");
}));
test("Get current version that has Suffix 1", () => __awaiter(void 0, void 0, void 0, function* () {
options.versionFile.line = 7;
const version = yield utils_1.getCurVersion(options);
expect(version).toBe("2.3.5-ALPHA");
}));
test("Get current version that has Suffix 2", () => __awaiter(void 0, void 0, void 0, function* () {
options.versionFile.line = 8;
const version = yield utils_1.getCurVersion(options);
expect(version).toBe("6.7.8rc");
}));
test("Get current version that has Prefix 1 and Suffix 1", () => __awaiter(void 0, void 0, void 0, function* () {
options.versionFile.line = 9;
const version = yield utils_1.getCurVersion(options);
expect(version).toBe("v.1.0.2-ALPHA");
}));
test("Get current version that has Prefix 1 and Suffix 2", () => __awaiter(void 0, void 0, void 0, function* () {
options.versionFile.line = 10;
const version = yield utils_1.getCurVersion(options);
expect(version).toBe("v.4.5.6rc");
}));
test("Get current version that has Prefix 2 and Suffix 1", () => __awaiter(void 0, void 0, void 0, function* () {
options.versionFile.line = 11;
const version = yield utils_1.getCurVersion(options);
expect(version).toBe("alpha.3.5.6-ALPHA");
}));
test("Get current version that has Prefix 2 and Suffix 2", () => __awaiter(void 0, void 0, void 0, function* () {
options.versionFile.line = 12;
const version = yield utils_1.getCurVersion(options);
expect(version).toBe("alpha.7.8.9rc");
}));
});
});
describe("Get optional version items", () => {
test("No Optional case.", () => {
Expand Down Expand Up @@ -567,6 +644,20 @@ describe("Bump Version tests", () => {
branch: 'master',
bump: 'major',
reset: ['minor', 'build']
},
// on commit to release-candidate branch, bump major, reset minor and build, add -rc as suffix
{
trigger: 'commit',
branch: 'release-candidate',
bump: 'major',
reset: ['minor', 'build'],
suffix: "-rc"
},
// on commit to branch version-tag, add prefix: v.
{
trigger: 'commit',
branch: 'version-tag',
prefix: "v."
}
]
};
Expand Down Expand Up @@ -631,6 +722,16 @@ describe("Bump Version tests", () => {
let newVersion = yield utils_1.bumpVersion(options, 'manual', 'master');
expect(newVersion).toBe('2.0.1');
}));
test("Commit trigger with suffix", () => __awaiter(void 0, void 0, void 0, function* () {
options.versionFile.line = 1;
let newVersion = yield utils_1.bumpVersion(options, 'commit', 'release-candidate');
expect(newVersion).toBe('2.0.1-rc');
}));
test("Commit trigger with prefix", () => __awaiter(void 0, void 0, void 0, function* () {
options.versionFile.line = 1;
let newVersion = yield utils_1.bumpVersion(options, 'commit', 'version-tag');
expect(newVersion).toBe("v.1.2.4");
}));
// describe("Pull-request trigger tests", () => {});
// describe("Comment trigger", () => {});
});
Expand Down
94 changes: 78 additions & 16 deletions dist/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.bumpVersion = exports.versionMapToString = exports.getOptionalItems = exports.getOptional = exports.getVersionMap = exports.getTag = exports.getResetItems = exports.getBumpItems = exports.getRules = exports.getCurVersion = exports.getSchemeRegex = exports.getIntrabracketContent = exports.verifyTrigger = void 0;
exports.bumpVersion = exports.versionMapToString = exports.getSuffixes = exports.getPrefixes = exports.getOptionalItems = exports.getOptional = exports.getVersionMap = exports.getTag = exports.getResetItems = exports.getBumpItems = exports.getRules = exports.getCurVersion = exports.getSchemeRegex = exports.getIntrabracketContent = exports.verifyTrigger = void 0;
const fs = __importStar(require("fs"));
const readline = __importStar(require("readline"));
const regExpParser_1 = require("./regExpParser");
Expand Down Expand Up @@ -89,6 +89,26 @@ function getSchemeRegex(options) {
return regExpParser_1.generateSchemeRegexp(options.schemeDefinition);
}
exports.getSchemeRegex = getSchemeRegex;
/**
* Adds prefix and suffix recognition to version scheme regex
* only one prefix and one suffix will be detected maximally
* result of the form: (prefix1|prefix2)?<scheme regExp>(suffix1|suffix2)?
* @param {RegExp} schemeRegExp
* @param {BumperOptionsFile} options
* @returns {RegExp}
*/
function addPrefixAndSuffixRecognition(schemeRegExp, options) {
const prefixes = "(" + getPrefixes(options).map(p => escapeRegExp(p)).join('|') + ")?"; // (<prefixes>)?
const suffixes = "(" + getSuffixes(options).map(s => escapeRegExp(s)).join('|') + ")?"; // (<suffixes>)?
return new RegExp(prefixes + schemeRegExp.source + suffixes);
}
/**
* escapes the chars in a string that are regexp wildcards
* @param string
*/
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
/**
* Extracts the current version form the specified version file using the following strategy:
* - if user has specified a line number -> got to that line while identifying the initial match
Expand All @@ -100,8 +120,11 @@ exports.getSchemeRegex = getSchemeRegex;
function getCurVersion(options) {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
let { path, line } = options.versionFile, regExp = getSchemeRegex(options);
console.log(regExp);
let { path, line } = options.versionFile;
const schemeRegExp = getSchemeRegex(options);
console.info("scheme regExp: ", schemeRegExp);
const regExp = addPrefixAndSuffixRecognition(schemeRegExp, options);
console.info("final regExp: ", regExp);
// verify the path actually corresponds to a file
if (!fs.existsSync(path))
throw new Error(`Version file with path ${path} does not exist.`);
Expand Down Expand Up @@ -165,29 +188,41 @@ exports.getRules = getRules;
/**
* Extracts the items to bump based on the trigger and the branch
* The branch is set as the destination branch for pr requests
* @param options
* @param trigger
* @param branch
* @param rules {BumpRule[]} applicable rules for the current execution context
*/
function getBumpItems(options, trigger, branch) {
const rules = getRules(options, trigger, branch);
function getBumpItems(rules) {
return [...new Set(rules.map((rule) => rule.bump ? Array.isArray(rule.bump) ? rule.bump : [rule.bump] : [])
.reduce((pre, cur) => [...pre, ...cur], []))];
}
exports.getBumpItems = getBumpItems;
/**
* Extracts the items to reset based on the trigger and the branch
* The branch is set as the destination branch for pr requests
* @param options
* @param trigger
* @param branch
* @param rules {BumpRule[]} applicable rules for the current execution context
*/
function getResetItems(options, trigger, branch) {
const rules = getRules(options, trigger, branch);
function getResetItems(rules) {
return [...new Set(rules.map((rule) => rule.reset ? Array.isArray(rule.reset) ? rule.reset : [rule.reset] : [])
.reduce((pre, cur) => [...pre, ...cur], []))];
}
exports.getResetItems = getResetItems;
/**
* finds the first prefix definition int he applicable rules and returns it
* @param {BumpRule[]} rules
* @returns {BumpRule | undefined}
*/
function getApplicablePrefix(rules) {
var _a, _b;
return (_b = (_a = rules.find(r => r.prefix)) === null || _a === void 0 ? void 0 : _a.prefix) !== null && _b !== void 0 ? _b : '';
}
/**
* finds the first suffix definition in the applicable rules and returns it
* @param {BumpRule[]} rules
* @returns {BumpRule | undefined}
*/
function getApplicableSuffix(rules) {
var _a, _b;
return (_b = (_a = rules.find(r => r.suffix)) === null || _a === void 0 ? void 0 : _a.suffix) !== null && _b !== void 0 ? _b : '';
}
/**
* Find whether or not the commit should be tagged or not
* @param {BumperOptionsFile} options
Expand Down Expand Up @@ -247,6 +282,28 @@ function getOptionalItems(scheme) {
}, []);
}
exports.getOptionalItems = getOptionalItems;
/**
* Get all the possible prefixes from the rule bumps
* @param {BumperOptionsFile} options
* @returns {string[]}
*/
function getPrefixes(options) {
return [...options.rules
.map(r => r.prefix)
.reduce((acc, cur) => cur ? acc.add(cur) : acc, new Set())];
}
exports.getPrefixes = getPrefixes;
/**
* Get all the possible suffixes from the rule bumps
* @param {BumperOptionsFile} options
* @returns {string[]}
*/
function getSuffixes(options) {
return [...options.rules
.map(r => r.suffix)
.reduce((acc, cur) => cur ? acc.add(cur) : acc, new Set())];
}
exports.getSuffixes = getSuffixes;
/**
* Returns a string from the scheme with the correct values for each item
* @param options
Expand Down Expand Up @@ -288,13 +345,18 @@ exports.versionMapToString = versionMapToString;
*/
function bumpVersion(options, trigger, branch) {
return __awaiter(this, void 0, void 0, function* () {
const curVersion = yield getCurVersion(options), resetItems = getResetItems(options, trigger, branch), bumpItems = getBumpItems(options, trigger, branch);
let versionMap = getVersionMap(options, curVersion);
const curVersion = yield getCurVersion(options);
const rules = getRules(options, trigger, branch);
const resetItems = getResetItems(rules);
const bumpItems = getBumpItems(rules);
const prefix = getApplicablePrefix(rules);
const suffix = getApplicableSuffix(rules);
const versionMap = getVersionMap(options, curVersion);
for (let item of resetItems)
versionMap[item] = 0; // reset items
for (let item of bumpItems)
versionMap[item] += 1; // bump items
return versionMapToString(options, versionMap);
return prefix + versionMapToString(options, versionMap) + suffix;
});
}
exports.bumpVersion = bumpVersion;
16 changes: 16 additions & 0 deletions src/lib/types/OptionsFile.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ export interface BumpRule {
* => 1.2.0
*/
reset?: string | string[]

/**
* Set a prefix on the version when bumped
* with version desc = major.minor[.build] -> current 1.2.3
* set prefix: 'v.'
* => v.1.2.3
*/
prefix?: string

/**
* Set a suffix on the version when bumped
* with version desc = major.minor[.build] -> current 1.2.3
* set suffix: '-alpha'
* => 1.2.3-alpha
*/
suffix?: string
}

export type VersionScheme = 'custom' | any;
Expand Down
11 changes: 10 additions & 1 deletion src/tests/assets/INTEGRATION_VERSION.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
VERSION_SEMANTIC_FULL=1.2.3
VERSION_SEMANTIC_NO_BUILD=1.2
CUSTOM_MAJOR_MINOR_BUILD_PATCH=6.7.8-9
CUSTOM_MAJOR_MINOR_BUILD_PATCH=6.7.8-9
CUSTOM_PREFIX_1=v.1.0.2
CUSTOM_PREFIX_2=alpha.2.3.5
BAD_CUSTOM_PREFIX=alpha.v.2.3.5
CUSTOM_SUFFIX_1=2.3.5-ALPHA
CUSTOM_SUFFIX_2=6.7.8rc
CUSTOM_PREFIX_1_SUFFIX_1=v.1.0.2-ALPHA
CUSTOM_PREFIX_1_SUFFIX_2=v.4.5.6rc
CUSTOM_PREFIX_2_SUFFIX_1=alpha.3.5.6-ALPHA
CUSTOM_PREFIX_2_SUFFIX_2=alpha.7.8.9rc
Loading