Skip to content

Commit

Permalink
cli: add ppman spec command
Browse files Browse the repository at this point in the history
  • Loading branch information
HexF committed Jun 13, 2021
1 parent d68a1d6 commit 1fcb760
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 1 deletion.
161 changes: 161 additions & 0 deletions cli/commands/ppman_commands/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
const chalk = require('chalk');
const fs = require('fs/promises');
const minimatch = require("minimatch");
const semver = require('semver');

exports.command = ['spec <specfile>'];
exports.aliases = ['s'];
exports.describe = 'Install the packages described in the spec file, uninstalling packages which aren\'t in the list'

function does_match(package, rule){
const nameMatch = minimatch(package.language, rule.package_selector);
const versionMatch = semver.satisfies(package.language_version, rule.version_selector)

return nameMatch && versionMatch;
}

exports.handler = async ({axios, specfile}) => {
const spec_contents = await fs.readFile(specfile);
const spec_lines = spec_contents.toString().split("\n");

const rules = [];

for(const line of spec_lines){
const rule = {
_raw: line.trim(),
comment: false,
package_selector: null,
version_selector: null,
negate: false
};

if(line.starts_with("#")){
rule.comment = true;
}else {
let l = line.trim();
if(line.starts_with("!")){
rule.negate = true;
l = line.slice(1).trim();
}

const [pkg, ver] = l.split(" ", 2);
rule.package_selector = pkg;
rule.version_selector = ver;
}

if(rule._raw.length != 0) rules.push(rule);
}

const packages_req = await axios.get('/api/v2/packages');
const packages = packages_req.data;

const installed = packages.filter(pkg => pkg.installed);

const ensure_packages = [];

for(const rule of rules){
if(rule.comment) continue;

const matches = [];

if(!rule.negate){
for(const package of packages){
if(does_match(package, rule))
matches.push(package)
}

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
}
);

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)
}
}else{
for(const package of ensure_packages){
if(does_match(package, rule))
ensure_packages.splice(ensure_packages.indexOf(package))
}
}


}

const operations = [];

for(const package of ensure_packages){
if(!package.installed)
operations.push({
type: "install",
package: package.language,
version: package.language_version
});
}

for(const installed_package of installed){
if(!ensure_packages.find(
pkg => pkg.language == installed_package.language &&
pkg.language_version == installed_package.language_version
))
operations.push({
type: "uninstall",
package: installed_package.language,
version: installed_package.language_version
})
}

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}`)
}

if(operations.length == 0){
console.log(chalk.gray("None"))
}

for(const op of operations){
if(op.type == "install"){
try{
const install = await axios.post(`/api/v2/packages`, {
language: op.package,
version: op.version
});

if(!install.data.language)
throw new Error(install.data.message); // Go to exception handler

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)
}
}
else if(op.type == "uninstall"){
try{
const install = await axios.delete(`/api/v2/packages`, {
data: {
language: op.package,
version: op.version
}
});

if(!install.data.language)
throw new Error(install.data.message); // Go to exception handler

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)
}
}
}



}
2 changes: 1 addition & 1 deletion cli/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

require('nocamel');
const axios = require('axios').default;

const axios_instance = argv => {
Expand Down
121 changes: 121 additions & 0 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"dependencies": {
"axios": "^0.21.1",
"chalk": "^4.1.0",
"minimatch": "^3.0.4",
"nocamel": "^1.0.2",
"semver": "^7.3.5",
"yargs": "^16.2.0"
}
}
Empty file added cli/ppman_ops.js
Empty file.
8 changes: 8 additions & 0 deletions dev.pps
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env -S piston ppman spec

# Development Piston Packages
# Defines packages to be installed by developers

# All packages, latest version
# Don't use this when connected to public repo, in excess of 10GB
* *
14 changes: 14 additions & 0 deletions public.pps
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env -S piston ppman spec

# Public Piston Packages
# Defines packages to be installed on the public piston installation

# All packages, latest version
* *

# Except python
!python *

# Install python 3.* and 2.*
python 3.*
python 2.*

0 comments on commit 1fcb760

Please sign in to comment.