2021-04-12 13:51:48 -06:00
|
|
|
import moo from 'moo';
|
2023-07-04 03:41:19 -06:00
|
|
|
import type { Category } from '../../../constants';
|
2022-03-03 02:35:26 -07:00
|
|
|
import { regEx } from '../../../util/regex';
|
2022-02-13 06:55:03 -07:00
|
|
|
import { NugetDatasource } from '../../datasource/nuget';
|
2023-02-19 05:43:48 -07:00
|
|
|
import type { PackageDependency, PackageFileContent } from '../types';
|
2021-04-12 13:51:48 -06:00
|
|
|
|
|
|
|
export const defaultConfig = {
|
|
|
|
fileMatch: ['\\.cake$'],
|
|
|
|
};
|
|
|
|
|
2023-07-04 03:41:19 -06:00
|
|
|
export const categories: Category[] = ['dotnet'];
|
|
|
|
|
2021-04-23 04:24:18 -06:00
|
|
|
const lexer = moo.states({
|
2021-04-12 13:51:48 -06:00
|
|
|
main: {
|
2021-11-29 12:16:05 -07:00
|
|
|
lineComment: { match: /\/\/.*?$/ }, // TODO #12870
|
|
|
|
multiLineComment: { match: /\/\*[^]*?\*\//, lineBreaks: true }, // TODO #12870
|
2021-04-12 13:51:48 -06:00
|
|
|
dependency: {
|
2021-11-29 12:16:05 -07:00
|
|
|
match: /^#(?:addin|tool|module|load|l)\s+(?:nuget|dotnet):.*$/, // TODO #12870
|
2021-04-12 13:51:48 -06:00
|
|
|
},
|
2021-04-23 01:43:19 -06:00
|
|
|
dependencyQuoted: {
|
2021-11-29 12:16:05 -07:00
|
|
|
match: /^#(?:addin|tool|module|load|l)\s+"(?:nuget|dotnet):[^"]+"\s*$/, // TODO #12870
|
2021-04-23 01:43:19 -06:00
|
|
|
value: (s: string) => s.trim().slice(1, -1),
|
|
|
|
},
|
2021-05-05 09:29:56 -06:00
|
|
|
unknown: moo.fallback,
|
2021-04-12 13:51:48 -06:00
|
|
|
},
|
2021-04-23 04:24:18 -06:00
|
|
|
});
|
2021-04-12 13:51:48 -06:00
|
|
|
|
|
|
|
function parseDependencyLine(line: string): PackageDependency | null {
|
|
|
|
try {
|
2021-10-19 22:38:49 -06:00
|
|
|
let url = line.replace(regEx(/^[^:]*:/), '');
|
2021-04-12 13:51:48 -06:00
|
|
|
const isEmptyHost = url.startsWith('?');
|
|
|
|
url = isEmptyHost ? `http://localhost/${url}` : url;
|
2021-04-23 01:43:19 -06:00
|
|
|
|
2024-01-18 10:37:53 -07:00
|
|
|
const { origin, pathname, protocol, searchParams } = new URL(url);
|
|
|
|
const registryUrl = `${origin}${pathname}`;
|
2021-04-12 13:51:48 -06:00
|
|
|
|
2022-04-17 06:34:26 -06:00
|
|
|
const depName = searchParams.get('package')!;
|
|
|
|
const currentValue = searchParams.get('version') ?? undefined;
|
2021-04-12 13:51:48 -06:00
|
|
|
|
2022-02-13 06:55:03 -07:00
|
|
|
const result: PackageDependency = {
|
|
|
|
datasource: NugetDatasource.id,
|
|
|
|
depName,
|
|
|
|
currentValue,
|
|
|
|
};
|
2021-04-12 13:51:48 -06:00
|
|
|
|
|
|
|
if (!isEmptyHost) {
|
|
|
|
if (protocol.startsWith('http')) {
|
|
|
|
result.registryUrls = [registryUrl];
|
|
|
|
} else {
|
2022-01-21 00:59:36 -07:00
|
|
|
result.skipReason = 'unsupported-url';
|
2021-04-12 13:51:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2024-08-14 04:33:02 -06:00
|
|
|
} catch {
|
2021-04-12 13:51:48 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-19 05:43:48 -07:00
|
|
|
export function extractPackageFile(content: string): PackageFileContent {
|
2022-04-17 06:34:26 -06:00
|
|
|
const deps: PackageDependency[] = [];
|
2021-04-12 13:51:48 -06:00
|
|
|
lexer.reset(content);
|
|
|
|
let token = lexer.next();
|
|
|
|
while (token) {
|
|
|
|
const { type, value } = token;
|
2021-04-23 01:43:19 -06:00
|
|
|
if (type === 'dependency' || type === 'dependencyQuoted') {
|
2021-04-12 13:51:48 -06:00
|
|
|
const dep = parseDependencyLine(value);
|
|
|
|
if (dep) {
|
|
|
|
deps.push(dep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token = lexer.next();
|
|
|
|
}
|
|
|
|
return { deps };
|
|
|
|
}
|
2022-01-19 01:06:21 -07:00
|
|
|
|
2022-02-13 06:55:03 -07:00
|
|
|
export const supportedDatasources = [NugetDatasource.id];
|