Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading python version from mise config #834

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,30 @@ describe('Version from file test', () => {
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
}
);
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
'Version from mise .mise.toml test',
async _fn => {
await io.mkdirP(tempDir);
const pythonVersionFileName = '.mise.toml';
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
const pythonVersion = '3.7.0';
const pythonVersionFileContent = `[tools]\npython = "${pythonVersion}"`;
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
}
);
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
'Version from mise verbose .mise.toml test',
async _fn => {
await io.mkdirP(tempDir);
const pythonVersionFileName = '.mise.toml';
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
const pythonVersion = '3.7.0';
const pythonVersionFileContent = `[tools]\npython = { version="${pythonVersion}", virtualenv=".venv" }`;
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
}
);
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
'Version undefined',
async _fn => {
Expand Down
29 changes: 21 additions & 8 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91829,6 +91829,9 @@ function getOSInfo() {
});
}
exports.getOSInfo = getOSInfo;
function isString(value) {
return typeof value === 'string' || value instanceof String;
}
/**
* Extract a value from an object by following the keys path provided.
* If the value is present, it is returned. Otherwise undefined is returned.
Expand All @@ -91839,9 +91842,12 @@ function extractValue(obj, keys) {
if (keys.length > 1 && value !== undefined) {
return extractValue(value, keys.slice(1));
}
else {
else if (isString(value)) {
return value;
}
else {
return;
}
}
else {
return;
Expand All @@ -91859,19 +91865,26 @@ function getVersionInputFromTomlFile(versionFile) {
core.debug(`Trying to resolve version form ${versionFile}`);
const pyprojectFile = fs_1.default.readFileSync(versionFile, 'utf8');
const pyprojectConfig = toml.parse(pyprojectFile);
let keys = [];
let keyPaths = [];
if ('project' in pyprojectConfig) {
// standard project metadata (PEP 621)
keys = ['project', 'requires-python'];
keyPaths = [['project', 'requires-python']];
}
else {
// python poetry
keys = ['tool', 'poetry', 'dependencies', 'python'];
keyPaths = [
// python poetry
['tool', 'poetry', 'dependencies', 'python'],
// mise
['tools', 'python'],
['tools', 'python', 'version']
];
}
const versions = [];
const version = extractValue(pyprojectConfig, keys);
if (version !== undefined) {
versions.push(version);
for (const keys of keyPaths) {
const value = extractValue(pyprojectConfig, keys);
if (value !== undefined) {
versions.push(value);
}
}
core.info(`Extracted ${versions} from ${versionFile}`);
const rawVersions = Array.from(versions, version => version.split(',').join(' '));
Expand Down
32 changes: 24 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ export async function getOSInfo() {
}
}

function isString(value: unknown): value is string {
return typeof value === 'string' || value instanceof String;
}

/**
* Extract a value from an object by following the keys path provided.
* If the value is present, it is returned. Otherwise undefined is returned.
Expand All @@ -205,8 +209,10 @@ function extractValue(obj: any, keys: string[]): string | undefined {
const value = obj[keys[0]];
if (keys.length > 1 && value !== undefined) {
return extractValue(value, keys.slice(1));
} else {
} else if (isString(value)) {
return value;
} else {
return;
}
} else {
return;
Expand All @@ -226,19 +232,29 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] {

const pyprojectFile = fs.readFileSync(versionFile, 'utf8');
const pyprojectConfig = toml.parse(pyprojectFile);
let keys = [];

let keyPaths = [];

if ('project' in pyprojectConfig) {
// standard project metadata (PEP 621)
keys = ['project', 'requires-python'];
keyPaths = [['project', 'requires-python']];
} else {
// python poetry
keys = ['tool', 'poetry', 'dependencies', 'python'];
keyPaths = [
// python poetry
['tool', 'poetry', 'dependencies', 'python'],
// mise
['tools', 'python'],
['tools', 'python', 'version']
];
}

const versions = [];
const version = extractValue(pyprojectConfig, keys);
if (version !== undefined) {
versions.push(version);

for (const keys of keyPaths) {
const value = extractValue(pyprojectConfig, keys);
if (value !== undefined) {
versions.push(value);
}
}

core.info(`Extracted ${versions} from ${versionFile}`);
Expand Down