MCPcopy Index your code
hub / github.com/actions/setup-python / getVersionInputFromTomlFile

Function getVersionInputFromTomlFile

src/utils.ts:253–292  ·  view source on GitHub ↗
(versionFile: string)

Source from the content-addressed store, hash-verified

251 * If none is present, returns an empty list.
252 */
253export function getVersionInputFromTomlFile(versionFile: string): string[] {
254 core.debug(`Trying to resolve version from ${versionFile}`);
255
256 let pyprojectFile = fs.readFileSync(versionFile, 'utf8');
257 // Normalize the line endings in the pyprojectFile
258 pyprojectFile = pyprojectFile.replace(/\r\n/g, '\n');
259
260 const pyprojectConfig = toml.parse(pyprojectFile);
261 let keys = [];
262
263 if ('project' in pyprojectConfig) {
264 // standard project metadata (PEP 621)
265 keys = ['project', 'requires-python'];
266 } else {
267 // python poetry
268 keys = ['tool', 'poetry', 'dependencies', 'python'];
269 }
270 const versions = [];
271 const version = extractValue(pyprojectConfig, keys);
272 if (version !== undefined) {
273 versions.push(version);
274 }
275
276 core.info(`Extracted ${versions} from ${versionFile}`);
277 const rawVersions = Array.from(versions, version =>
278 version.split(',').join(' ')
279 );
280 const validatedVersions = rawVersions
281 .map(item => semver.validRange(item, true))
282 .filter((versionRange, index) => {
283 if (!versionRange) {
284 core.debug(
285 `The version ${rawVersions[index]} is not valid SemVer range`
286 );
287 }
288
289 return !!versionRange;
290 }) as string[];
291 return validatedVersions;
292}
293
294/**
295 * Python versions extracted from a plain text file.

Callers 1

getVersionInputFromFileFunction · 0.85

Calls 1

extractValueFunction · 0.85

Tested by

no test coverage detected