-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathindex.ts
More file actions
94 lines (82 loc) · 2.73 KB
/
index.ts
File metadata and controls
94 lines (82 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import moo from 'moo';
import type { Category } from '../../../constants/index.ts';
import { regEx } from '../../../util/regex.ts';
import { isHttpUrl } from '../../../util/url.ts';
import { NugetDatasource } from '../../datasource/nuget/index.ts';
import type { PackageDependency, PackageFileContent } from '../types.ts';
export const url = 'https://cakebuild.net/docs';
export const categories: Category[] = ['dotnet'];
export const defaultConfig = {
managerFilePatterns: ['/\\.cake$/'],
};
export const supportedDatasources = [NugetDatasource.id];
const lexer = moo.states({
main: {
lineComment: { match: /\/\/.*?$/ }, // TODO #12870
multiLineComment: { match: /\/\*[^]*?\*\//, lineBreaks: true }, // TODO #12870
dependency: {
match: /^#(?:addin|tool|module|load|l)\s+(?:nuget|dotnet):.*$/, // TODO #12870
},
dependencyQuoted: {
match: /^#(?:addin|tool|module|load|l)\s+"(?:nuget|dotnet):[^"]+"\s*$/, // TODO #12870
value: (s: string) => s.trim().slice(1, -1),
},
dependencyFromInstallTools: {
match: /(?:InstallTools?\s*\()[^)]+(?:\s*\)\s*;)/,
lineBreaks: true,
},
unknown: moo.fallback,
},
});
function parseDependencyLine(line: string): PackageDependency | null {
try {
let url = line.replace(regEx(/^[^:]*:/), '');
const isEmptyHost = url.startsWith('?');
url = isEmptyHost ? `http://localhost/${url}` : url;
const parsedUrl = new URL(url);
const { origin, pathname, searchParams } = parsedUrl;
const registryUrl = `${origin}${pathname}`;
const depName = searchParams.get('package')!;
const currentValue = searchParams.get('version') ?? undefined;
const result: PackageDependency = {
datasource: NugetDatasource.id,
depName,
currentValue,
};
if (!isEmptyHost) {
if (isHttpUrl(parsedUrl)) {
result.registryUrls = [registryUrl];
} else {
result.skipReason = 'unsupported-url';
}
}
return result;
} catch {
return null;
}
}
export function extractPackageFile(content: string): PackageFileContent {
const deps: PackageDependency[] = [];
lexer.reset(content);
let token = lexer.next();
while (token) {
const { type, value } = token;
if (type === 'dependency' || type === 'dependencyQuoted') {
const dep = parseDependencyLine(value);
if (dep) {
deps.push(dep);
}
} else if (type === 'dependencyFromInstallTools') {
const matches = value.matchAll(regEx(/"dotnet:[^"]+"/g));
for (const match of matches) {
const withoutQuote = match.toString().slice(1, -1);
const dep = parseDependencyLine(withoutQuote);
if (dep) {
deps.push(dep);
}
}
}
token = lexer.next();
}
return { deps };
}