/
collections.ts
166 lines (154 loc) · 4.61 KB
/
collections.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { regEx } from '../../../util/regex';
import { GalaxyCollectionDatasource } from '../../datasource/galaxy-collection';
import { GitTagsDatasource } from '../../datasource/git-tags';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import type { PackageDependency } from '../types';
import type { AnsibleGalaxyPackageDependency } from './types';
import {
blockLineRegEx,
galaxyDepRegex,
nameMatchRegex,
newBlockRegEx,
} from './util';
function interpretLine(
lineMatch: RegExpMatchArray,
dependency: AnsibleGalaxyPackageDependency
): void {
const localDependency = dependency;
const key = lineMatch[2];
const value = lineMatch[3].replace(regEx(/["']/g), '');
switch (key) {
case 'name': {
localDependency.managerData.name = value;
break;
}
case 'version': {
localDependency.managerData.version = value;
localDependency.currentValue = value;
break;
}
case 'source': {
localDependency.managerData.source = value;
if (value?.startsWith('git@')) {
localDependency.packageName = value;
} else {
localDependency.registryUrls = value ? [value] : [];
}
break;
}
case 'type': {
localDependency.managerData.type = value;
break;
}
default: {
// fail if we find an unexpected key
localDependency.skipReason = 'unsupported';
}
}
}
function handleGitDep(
dep: AnsibleGalaxyPackageDependency,
nameMatch: RegExpExecArray | null
): void {
dep.datasource = GitTagsDatasource.id;
if (nameMatch?.groups) {
// if a github.com repository is referenced use github-tags instead of git-tags
if (nameMatch.groups.hostname === 'github.com') {
dep.datasource = GithubTagsDatasource.id;
} else {
dep.datasource = GitTagsDatasource.id;
}
// source definition without version appendix
const source = nameMatch.groups.source;
const massagedDepName = nameMatch.groups.depName.replace(
regEx(/.git$/),
''
);
dep.depName = `${nameMatch.groups.hostname}/${massagedDepName}`;
// remove leading `git+` from URLs like `git+https://...`
dep.packageName = source.replace(regEx(/git\+/), '');
// if version is declared using version appendix `<source url>,v1.2.0`, use it
if (nameMatch.groups.version) {
dep.currentValue = nameMatch.groups.version;
} else {
dep.currentValue = dep.managerData.version;
}
}
}
function handleGalaxyDep(dep: AnsibleGalaxyPackageDependency): void {
dep.datasource = GalaxyCollectionDatasource.id;
dep.depName = dep.managerData.name;
dep.registryUrls = dep.managerData.source ? [dep.managerData.source] : [];
dep.currentValue = dep.managerData.version;
}
function finalize(dependency: AnsibleGalaxyPackageDependency): boolean {
const dep = dependency;
dep.depName = dep.managerData.name;
const name = dep.managerData.name;
const nameMatch = nameMatchRegex.exec(name);
// use type if defined
switch (dependency.managerData.type) {
case 'galaxy':
handleGalaxyDep(dep);
break;
case 'git':
handleGitDep(dep, nameMatch);
break;
case 'file':
dep.skipReason = 'local-dependency';
break;
case null:
// try to find out type based on source
if (nameMatch) {
handleGitDep(dep, nameMatch);
break;
}
if (galaxyDepRegex.exec(dep.managerData.name)) {
dep.datasource = GalaxyCollectionDatasource.id;
dep.depName = dep.managerData.name;
break;
}
dep.skipReason = 'no-source-match';
break;
default:
dep.skipReason = 'unsupported';
return true;
}
if (!dependency.currentValue && !dep.skipReason) {
dep.skipReason = 'unspecified-version';
}
return true;
}
export function extractCollections(lines: string[]): PackageDependency[] {
const deps: PackageDependency[] = [];
for (let lineNumber = 0; lineNumber < lines.length; lineNumber += 1) {
let lineMatch = newBlockRegEx.exec(lines[lineNumber]);
if (lineMatch) {
const dep: AnsibleGalaxyPackageDependency = {
depType: 'galaxy-collection',
managerData: {
name: null,
version: null,
type: null,
source: null,
},
};
do {
interpretLine(lineMatch, dep);
const line = lines[lineNumber + 1];
if (!line) {
break;
}
lineMatch = blockLineRegEx.exec(line);
if (lineMatch) {
lineNumber += 1;
}
} while (lineMatch);
if (finalize(dep)) {
delete (dep as PackageDependency).managerData;
deps.push(dep);
}
}
}
return deps;
}