piston/cli/commands/ppman_commands/spec.js

184 lines
5.4 KiB
JavaScript
Raw Permalink Normal View History

2021-06-13 08:41:01 +02:00
const chalk = require('chalk');
const fs = require('fs/promises');
2021-10-08 15:16:57 +02:00
const minimatch = require('minimatch');
2021-06-13 08:41:01 +02:00
const semver = require('semver');
exports.command = ['spec <specfile>'];
exports.aliases = ['s'];
2021-10-08 15:16:57 +02:00
exports.describe =
"Install the packages described in the spec file, uninstalling packages which aren't in the list";
2021-06-13 08:41:01 +02:00
2021-10-08 15:16:57 +02:00
function does_match(package, rule) {
2021-06-13 08:41:01 +02:00
const nameMatch = minimatch(package.language, rule.package_selector);
2021-10-08 15:16:57 +02:00
const versionMatch = semver.satisfies(
package.language_version,
rule.version_selector
);
2021-06-13 08:41:01 +02:00
return nameMatch && versionMatch;
}
2021-10-08 15:16:57 +02:00
exports.handler = async ({ axios, specfile }) => {
2021-06-13 08:41:01 +02:00
const spec_contents = await fs.readFile(specfile);
2021-10-08 15:16:57 +02:00
const spec_lines = spec_contents.toString().split('\n');
2021-06-13 08:41:01 +02:00
const rules = [];
2021-10-08 15:16:57 +02:00
for (const line of spec_lines) {
2021-06-13 08:41:01 +02:00
const rule = {
_raw: line.trim(),
comment: false,
package_selector: null,
version_selector: null,
2021-10-08 15:16:57 +02:00
negate: false,
2021-06-13 08:41:01 +02:00
};
2021-10-08 15:16:57 +02:00
if (line.starts_with('#')) {
2021-06-13 08:41:01 +02:00
rule.comment = true;
2021-10-08 15:16:57 +02:00
} else {
2021-06-13 08:41:01 +02:00
let l = line.trim();
2021-10-08 15:16:57 +02:00
if (line.starts_with('!')) {
2021-06-13 08:41:01 +02:00
rule.negate = true;
l = line.slice(1).trim();
}
2021-10-08 15:16:57 +02:00
const [pkg, ver] = l.split(' ', 2);
2021-06-13 08:41:01 +02:00
rule.package_selector = pkg;
rule.version_selector = ver;
}
2021-10-08 15:16:57 +02:00
if (rule._raw.length != 0) rules.push(rule);
2021-06-13 08:41:01 +02:00
}
const packages_req = await axios.get('/api/v2/packages');
const packages = packages_req.data;
const installed = packages.filter(pkg => pkg.installed);
let ensure_packages = [];
2021-06-13 08:41:01 +02:00
2021-10-08 15:16:57 +02:00
for (const rule of rules) {
if (rule.comment) continue;
2021-06-13 08:41:01 +02:00
const matches = [];
2021-10-08 15:16:57 +02:00
if (!rule.negate) {
for (const package of packages) {
if (does_match(package, rule)) matches.push(package);
2021-06-13 08:41:01 +02:00
}
2021-10-08 15:16:57 +02:00
const latest_matches = matches.filter(pkg => {
const versions = matches
.filter(x => x.language == pkg.language)
.map(x => x.language_version)
.sort(semver.rcompare);
return versions[0] == pkg.language_version;
});
2021-06-13 08:41:01 +02:00
2021-10-08 15:16:57 +02:00
for (const match of latest_matches) {
if (
!ensure_packages.find(
pkg =>
pkg.language == match.language &&
pkg.language_version == match.language_version
)
)
ensure_packages.push(match);
2021-06-13 08:41:01 +02:00
}
2021-10-08 15:16:57 +02:00
} else {
ensure_packages = ensure_packages.filter(
pkg => !does_match(pkg, rule)
2021-10-08 15:16:57 +02:00
);
2021-06-13 08:41:01 +02:00
}
}
const operations = [];
2021-10-08 15:16:57 +02:00
for (const package of ensure_packages) {
if (!package.installed)
2021-06-13 08:41:01 +02:00
operations.push({
2021-10-08 15:16:57 +02:00
type: 'install',
2021-06-13 08:41:01 +02:00
package: package.language,
2021-10-08 15:16:57 +02:00
version: package.language_version,
2021-06-13 08:41:01 +02:00
});
}
2021-10-08 15:16:57 +02:00
for (const installed_package of installed) {
if (
!ensure_packages.find(
pkg =>
pkg.language == installed_package.language &&
pkg.language_version == installed_package.language_version
)
)
2021-06-13 08:41:01 +02:00
operations.push({
2021-10-08 15:16:57 +02:00
type: 'uninstall',
2021-06-13 08:41:01 +02:00
package: installed_package.language,
2021-10-08 15:16:57 +02:00
version: installed_package.language_version,
});
2021-06-13 08:41:01 +02:00
}
2021-10-08 15:16:57 +02:00
console.log(chalk.bold.yellow('Actions'));
for (const op of operations) {
console.log(
(op.type == 'install'
? chalk.green('Install')
: chalk.red('Uninstall')) + ` ${op.package} ${op.version}`
);
2021-06-13 08:41:01 +02:00
}
2021-10-08 15:16:57 +02:00
if (operations.length == 0) {
console.log(chalk.gray('None'));
2021-06-13 08:41:01 +02:00
}
2021-10-08 15:16:57 +02:00
for (const op of operations) {
if (op.type == 'install') {
try {
2021-06-13 08:41:01 +02:00
const install = await axios.post(`/api/v2/packages`, {
language: op.package,
2021-10-08 15:16:57 +02:00
version: op.version,
2021-06-13 08:41:01 +02:00
});
2021-10-08 15:16:57 +02:00
if (!install.data.language)
2021-06-13 08:41:01 +02:00
throw new Error(install.data.message); // Go to exception handler
2021-10-08 15:16:57 +02:00
console.log(
chalk.bold.green('Installed'),
op.package,
op.version
);
} catch (e) {
console.log(
chalk.bold.red('Failed to install') +
` ${op.package} ${op.version}:`,
e.message
);
2021-06-13 08:41:01 +02:00
}
2021-10-08 15:16:57 +02:00
} else if (op.type == 'uninstall') {
try {
2021-06-13 08:41:01 +02:00
const install = await axios.delete(`/api/v2/packages`, {
data: {
language: op.package,
2021-10-08 15:16:57 +02:00
version: op.version,
},
2021-06-13 08:41:01 +02:00
});
2021-10-08 15:16:57 +02:00
if (!install.data.language)
2021-06-13 08:41:01 +02:00
throw new Error(install.data.message); // Go to exception handler
2021-10-08 15:16:57 +02:00
console.log(
chalk.bold.green('Uninstalled'),
op.package,
op.version
);
} catch (e) {
console.log(
chalk.bold.red('Failed to uninstall') +
` ${op.package} ${op.version}:`,
e.message
);
2021-06-13 08:41:01 +02:00
}
}
}
2021-10-08 15:16:57 +02:00
};