Files
2026-03-13 11:19:40 +00:00

86 lines
2.8 KiB
TypeScript

import { logger } from '../../../logger/index.ts';
import { coerceArray } from '../../../util/array.ts';
import { newlineRegex, regEx } from '../../../util/regex.ts';
import { MavenDatasource } from '../../datasource/maven/index.ts';
import type { PackageDependency, PackageFileContent } from '../types.ts';
import type { MavenVersionExtract, Version } from './types.ts';
// https://regex101.com/r/IcOs7P/1
const DISTRIBUTION_URL_REGEX = regEx(
'^(?:distributionUrl\\s*=\\s*)(?<replaceString>\\S*-(?<version>\\d+\\.\\d+(?:\\.\\d+)?(?:-\\w+)*)-(?<type>bin|all)\\.zip)\\s*$',
);
const WRAPPER_URL_REGEX = regEx(
'^(?:wrapperUrl\\s*=\\s*)(?<replaceString>\\S*-(?<version>\\d+\\.\\d+(?:\\.\\d+)?(?:-\\w+)*)(?:.jar))',
);
// https://regex101.com/r/7x1Otq/3
const WRAPPER_VERSION_REGEX = regEx(
'^(?:wrapperVersion\\s*=\\s*)(?<replaceString>(?<version>\\d+\\.\\d+(?:\\.\\d+)?))',
);
const WRAPPER_VERSION_MVNW_REGEX = regEx(
'^(#|@REM) Apache Maven Wrapper startup batch script, version (?<replaceString>(?<version>\\d+\\.\\d+(?:\\.\\d+)?))',
);
function extractVersions(fileContent: string): MavenVersionExtract {
const lines = coerceArray(fileContent?.split(newlineRegex));
const maven = extractLineInfo(lines, DISTRIBUTION_URL_REGEX) ?? undefined;
const wrapper =
extractLineInfo(
lines,
WRAPPER_URL_REGEX,
WRAPPER_VERSION_REGEX,
WRAPPER_VERSION_MVNW_REGEX,
) ?? undefined;
return { maven, wrapper };
}
function extractLineInfo(lines: string[], ...regexs: RegExp[]): Version | null {
for (const regex of regexs) {
for (const line of lines) {
if (line.match(regex)) {
const match = regex.exec(line);
if (match?.groups) {
return {
replaceString: match.groups.replaceString,
version: match.groups.version,
};
}
}
}
}
return null;
}
export function extractPackageFile(
fileContent: string,
): PackageFileContent | null {
logger.trace('maven-wrapper.extractPackageFile()');
const extractResult = extractVersions(fileContent);
const deps = [];
if (extractResult.maven?.version) {
const maven: PackageDependency = {
depName: 'maven',
packageName: 'org.apache.maven:apache-maven',
currentValue: extractResult.maven?.version,
replaceString: extractResult.maven?.replaceString,
datasource: MavenDatasource.id,
};
deps.push(maven);
}
if (extractResult.wrapper?.version) {
const wrapper: PackageDependency = {
depName: 'maven-wrapper',
packageName: 'org.apache.maven.wrapper:maven-wrapper',
currentValue: extractResult.wrapper?.version,
replaceString: extractResult.wrapper?.replaceString,
datasource: MavenDatasource.id,
};
deps.push(wrapper);
}
return deps.length ? { deps } : null;
}