forked from renovatebot/renovate
/
parser.ts
146 lines (129 loc) · 3.59 KB
/
parser.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { query as q } from 'good-enough-parser';
import { CpanDatasource } from '../../datasource/cpan';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import * as perlVersioning from '../../versioning/perl';
import type { PackageDependency } from '../types';
import { cpanfile } from './language';
interface Ctx {
deps: PackageDependency[];
perlVersion?: string;
phase?: string;
tempPhase?: string;
depName?: string;
currentValue?: string;
}
// requires perl, '5.36.1';
// requires 'perl' => 5.036001;
const perlVersionMatch = q
.sym<Ctx>('requires')
.alt(q.sym('perl'), q.str('perl'))
.alt(q.op(','), q.op('=>'))
.alt(
q.num<Ctx>((ctx, { value: perlVersion }) => ({ ...ctx, perlVersion })),
q.str<Ctx>((ctx, { value: perlVersion }) => ({ ...ctx, perlVersion }))
)
.op(';')
.handler((ctx) => {
if (ctx.perlVersion) {
ctx.deps.push({
depName: 'perl',
packageName: 'Perl/perl5',
currentValue: ctx.perlVersion,
datasource: GithubTagsDatasource.id,
versioning: perlVersioning.id,
extractVersion: '^v(?<version>\\S+)',
});
}
return ctx;
});
const requirementMatch = q.sym<Ctx>(/^(?:requires|recommends|suggests)$/);
const phasedRequiresMatch = q.sym<Ctx>(
/^(?:configure|build|test|author)_requires$/,
(ctx, { value: phase }) => {
ctx.tempPhase = phase.replace(/_requires/, '').replace(/author/, 'develop');
return ctx;
}
);
// requires 'Foo::Bar';
//
// requires 'Foo::Bar', '1.23';
// recommends 'Foo::Bar', '1.23';
// suggests 'Foo::Bar', '1.23';
//
// configure_requires 'Foo::Bar' => 1.023;
// build_requires 'Foo::Bar' => 1.023;
// test_requires 'Foo::Bar' => 1.023;
// author_requires 'Foo::Bar' => 1.023;
const moduleMatch = q
.alt(requirementMatch, phasedRequiresMatch)
.str((ctx, { value: depName }) => ({ ...ctx, depName }))
.opt(
q.alt<Ctx>(q.op(','), q.op('=>')).alt(
q.num<Ctx>((ctx, { value: currentValue }) => ({ ...ctx, currentValue })),
q.str<Ctx>((ctx, { value }) => {
const currentValue = value.replace(/^(?:\s*(?:==|>=|>))?\s*v?/, '');
return { ...ctx, currentValue };
})
)
)
.op(';')
.handler((ctx) => {
const { phase, tempPhase, depName, currentValue } = ctx;
delete ctx.tempPhase;
delete ctx.depName;
delete ctx.currentValue;
if (depName) {
const dep: PackageDependency = {
depName,
};
if (currentValue) {
dep.currentValue = currentValue;
} else {
dep.skipReason = 'unspecified-version';
}
if (phase) {
dep.depType = phase;
} else if (tempPhase) {
dep.depType = tempPhase;
}
dep.datasource = CpanDatasource.id;
ctx.deps.push(dep);
}
return ctx;
});
const phaseRegex = /^(?:configure|build|test|runtime|develop)/;
const phaseMatch = q.alt<Ctx>(
q.sym(phaseRegex, (ctx, { value: phase }) => ({ ...ctx, phase })),
q.str(phaseRegex, (ctx, { value: phase }) => ({ ...ctx, phase }))
);
// on 'configure' => sub {
// on build => sub {
// on 'test' => sub {
// on runtime => sub {
// on 'develop' => sub {
const onMatch = q
.sym<Ctx>('on')
.join(phaseMatch)
.op('=>')
.sym('sub')
.tree({
type: 'wrapped-tree',
maxDepth: 1,
search: moduleMatch,
})
.handler((ctx) => {
delete ctx.phase;
return ctx;
});
const query = q.tree<Ctx>({
type: 'root-tree',
maxDepth: 4,
search: q.alt<Ctx>(perlVersionMatch, moduleMatch, onMatch),
});
export function parse(
content: string
): Pick<Ctx, 'deps' | 'perlVersion'> | null {
return cpanfile.query(content, query, {
deps: [],
});
}