0
0
Fork 0
mirror of https://github.com/renovatebot/renovate.git synced 2025-03-21 19:23:09 +00:00
renovatebot_renovate/lib/versioning/ruby/strategies/replace.ts

104 lines
3.1 KiB
TypeScript
Raw Normal View History

import { satisfies } from '@renovatebot/ruby-semver';
import { logger } from '../../../logger';
2020-05-01 16:03:48 +00:00
import bump from './bump';
function countInstancesOf(str: string, char: string): number {
return str.split(char).length - 1;
}
function isMajorRange(range: string): boolean {
const splitRange = range.split(',').map((part) => part.trim());
return (
splitRange.length === 1 &&
2020-08-11 06:08:16 +00:00
splitRange[0]?.startsWith('~>') &&
countInstancesOf(splitRange[0], '.') === 0
);
}
function isCommonRubyMajorRange(range: string): boolean {
const splitRange = range.split(',').map((part) => part.trim());
return (
splitRange.length === 2 &&
2020-08-11 06:08:16 +00:00
splitRange[0]?.startsWith('~>') &&
countInstancesOf(splitRange[0], '.') === 1 &&
2020-08-11 06:08:16 +00:00
splitRange[1]?.startsWith('>=')
);
}
function isCommonRubyMinorRange(range: string): boolean {
const splitRange = range.split(',').map((part) => part.trim());
return (
splitRange.length === 2 &&
2020-08-11 06:08:16 +00:00
splitRange[0]?.startsWith('~>') &&
countInstancesOf(splitRange[0], '.') === 2 &&
2020-08-11 06:08:16 +00:00
splitRange[1]?.startsWith('>=')
);
}
function reduceOnePrecision(version: string): string {
const versionParts = version.split('.');
// istanbul ignore if
if (versionParts.length === 1) {
return version;
}
versionParts.pop();
return versionParts.join('.');
}
2019-01-03 05:32:08 +00:00
export function matchPrecision(existing: string, next: string): string {
let res = next;
while (res.split('.').length > existing.split('.').length) {
res = reduceOnePrecision(res);
}
return res;
}
export default ({ to, range }: { range: string; to: string }): string => {
2019-01-13 10:06:28 +00:00
if (satisfies(to, range)) {
return range;
}
let newRange;
if (isCommonRubyMajorRange(range)) {
const firstPart = reduceOnePrecision(to);
newRange = `~> ${firstPart}, >= ${to}`;
} else if (isCommonRubyMinorRange(range)) {
const firstPart = reduceOnePrecision(to) + '.0';
newRange = `~> ${firstPart}, >= ${to}`;
} else if (isMajorRange(range)) {
const majorPart = to.split('.')[0];
newRange = '~>' + (range.includes(' ') ? ' ' : '') + majorPart;
} else {
const lastPart = range
.split(',')
.map((part) => part.trim())
.slice(-1)
.join();
const lastPartPrecision = lastPart.split('.').length;
const toPrecision = to.split('.').length;
let massagedTo: string = to;
if (!lastPart.startsWith('<') && toPrecision > lastPartPrecision) {
massagedTo = to.split('.').slice(0, lastPartPrecision).join('.');
}
const newLastPart = bump({ to: massagedTo, range: lastPart });
newRange = range.replace(lastPart, newLastPart);
const firstPart = range
.split(',')
.map((part) => part.trim())
.shift();
if (firstPart && !satisfies(to, firstPart)) {
let newFirstPart = bump({ to: massagedTo, range: firstPart });
newFirstPart = matchPrecision(firstPart, newFirstPart);
newRange = newRange.replace(firstPart, newFirstPart);
}
}
// istanbul ignore if
if (!satisfies(to, newRange)) {
logger.warn(
{ range, to, newRange },
'Ruby versioning getNewValue problem: to version is not satisfied by new range'
);
return range;
}
return newRange;
2019-01-13 10:06:28 +00:00
};