-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathextract.ts
More file actions
212 lines (198 loc) · 6.14 KB
/
extract.ts
File metadata and controls
212 lines (198 loc) · 6.14 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {
isArray,
isFunction,
isNonEmptyObject,
isNonEmptyString,
isObject,
isString,
} from '@sindresorhus/is';
import { logger } from '../../../logger/index.ts';
import { regEx } from '../../../util/regex.ts';
import type { StaticTooling } from '../asdf/upgradeable-tooling.ts';
import type { PackageDependency, PackageFileContent } from '../types.ts';
import type { BackendToolingConfig } from './backends.ts';
import {
createAquaToolConfig,
createCargoToolConfig,
createDotnetToolConfig,
createGemToolConfig,
createGithubToolConfig,
createGoToolConfig,
createNpmToolConfig,
createPipxToolConfig,
createSpmToolConfig,
createUbiToolConfig,
} from './backends.ts';
import type { MiseTool, MiseToolOptions } from './schema.ts';
import type { ToolingDefinition } from './upgradeable-tooling.ts';
import { asdfTooling, miseTooling } from './upgradeable-tooling.ts';
import { parseTomlFile } from './utils.ts';
// Tool names can have options in the tool name
// e.g. ubi:tamasfe/taplo[matching=full,exe=taplo]
const optionInToolNameRegex = regEx(/^(?<name>.+?)(?:\[(?<options>.+)\])?$/);
export function extractPackageFile(
content: string,
packageFile: string,
): PackageFileContent | null {
logger.trace(`mise.extractPackageFile(${packageFile})`);
const misefile = parseTomlFile(content, packageFile);
if (!misefile) {
return null;
}
const deps: PackageDependency[] = [];
const tools = misefile.tools;
if (tools) {
for (const [name, toolData] of Object.entries(tools)) {
const version = parseVersion(toolData);
// Parse the tool options in the tool name
const { name: depName, options: optionsInName } =
optionInToolNameRegex.exec(name.trim())!.groups!;
const delimiterIndex = name.indexOf(':');
const backend = depName.substring(0, delimiterIndex);
const toolName = depName.substring(delimiterIndex + 1);
const options = parseOptions(
optionsInName,
isNonEmptyObject(toolData) ? toolData : {},
);
const toolConfig =
version === null
? null
: getToolConfig(backend, toolName, version, options);
const dep = createDependency(depName, version, toolConfig);
deps.push(dep);
}
}
return deps.length ? { deps } : null;
}
function parseVersion(toolData: MiseTool): string | null {
if (isNonEmptyString(toolData)) {
// Handle the string case
// e.g. 'erlang = "23.3"'
return toolData;
}
if (isArray(toolData, isString)) {
// Handle the array case
// e.g. 'erlang = ["23.3", "24.0"]'
return toolData.length ? toolData[0] : null; // Get the first version in the array
}
if (isObject(toolData) && isNonEmptyString(toolData.version)) {
// Handle the object case with a string version
// e.g. 'python = { version = "3.11.2" }'
return toolData.version;
}
return null; // Return null if no version is found
}
function parseOptions(
optionsInName: string,
toolOptions: MiseToolOptions,
): MiseToolOptions {
const options = isNonEmptyString(optionsInName)
? Object.fromEntries(
optionsInName.split(',').map((option) => option.split('=', 2)),
)
: {};
// Options in toolOptions will override options in the tool name
return {
...options,
...toolOptions,
};
}
function getToolConfig(
backend: string,
toolName: string,
version: string,
toolOptions: MiseToolOptions,
): StaticTooling | BackendToolingConfig | null {
switch (backend) {
case '':
// If the tool name does not specify a backend, it should be a short name or an alias defined by users
return getRegistryToolConfig(toolName, version);
// We can specify core, asdf, vfox, aqua backends for tools in the default registry
// e.g. 'core:rust', 'asdf:rust', 'vfox:clang', 'aqua:act'
case 'core':
return getConfigFromTooling(miseTooling, toolName, version);
case 'asdf':
return getConfigFromTooling(asdfTooling, toolName, version);
case 'vfox':
return getRegistryToolConfig(toolName, version);
case 'aqua':
return (
getRegistryToolConfig(toolName, version) ??
createAquaToolConfig(toolName, version)
);
case 'cargo':
return createCargoToolConfig(toolName, version);
case 'dotnet':
return createDotnetToolConfig(toolName);
case 'gem':
return createGemToolConfig(toolName);
case 'github':
return createGithubToolConfig(toolName, version, toolOptions);
case 'go':
return createGoToolConfig(toolName);
case 'npm':
return createNpmToolConfig(toolName);
case 'pipx':
return createPipxToolConfig(toolName);
case 'spm':
return createSpmToolConfig(toolName);
case 'ubi':
return createUbiToolConfig(toolName, version, toolOptions);
default:
// Unsupported backend
return null;
}
}
/**
* Get the tooling config for a short name defined in the default registry
* @link https://mise.jdx.dev/registry.html
*/
function getRegistryToolConfig(
short: string,
version: string,
): StaticTooling | null {
// Try to get the config from miseTooling first, then asdfTooling
return (
getConfigFromTooling(miseTooling, short, version) ??
getConfigFromTooling(asdfTooling, short, version)
);
}
function getConfigFromTooling(
toolingSource: Record<string, ToolingDefinition>,
name: string,
version: string,
): StaticTooling | null {
const toolDefinition = toolingSource[name];
if (!toolDefinition) {
return null;
} // Return null if no toolDefinition is found
return (
(isFunction(toolDefinition.config)
? toolDefinition.config(version)
: toolDefinition.config) ?? null
); // Ensure null is returned instead of undefined
}
function createDependency(
name: string,
version: string | null,
config: StaticTooling | BackendToolingConfig | null,
): PackageDependency {
if (version === null) {
return {
depName: name,
skipReason: 'unspecified-version',
};
}
if (config === null) {
return {
depName: name,
skipReason: 'unsupported-datasource',
};
}
return {
depName: name,
currentValue: version,
// Spread the config last to override other properties
...config,
};
}