Skip to content

Commit

Permalink
feat(Plugins): Add schema function for obtaining schema of plugins; f…
Browse files Browse the repository at this point in the history
…ix typings
  • Loading branch information
nokome committed Apr 23, 2021
1 parent 2466e20 commit 360f551
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 21 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

15 changes: 13 additions & 2 deletions node/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

import { JSONSchema7 } from 'json-schema'
import { fromJSON, toJSON } from './prelude'
import { PluginInstallation } from './plugins'

const addon = require('../index.node')

// Warning: The following types are hand written and may become out of sync
// with the actual JSON data returned by the functions below.
// Use the `schema()` function as the authoritative source of the shape of
// the config object.

type LoggingLevel = 'debug' | 'info' | 'warn' | 'error' | 'never'

type LoggingFormat = 'plain' | 'pretty' | 'json'
export interface Config {
logging: {
stderr: {
level: LoggingLevel
format: 'plain' | 'pretty' | 'json'
format: LoggingFormat
}
desktop: {
level: LoggingLevel
}
file: {
path: string
Expand All @@ -24,10 +34,11 @@ export interface Config {
insecure: boolean
}
plugins: {
kinds: Array<'docker' | 'binary' | 'package'>
installations: Array<PluginInstallation>
aliases: Record<string, string>
}
upgrade: {
plugins: boolean
confirm: boolean
verbose: boolean
auto: string
Expand Down
1 change: 1 addition & 0 deletions node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod subscriptions;

#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("pluginsSchema", plugins::schema)?;
cx.export_function("pluginsList", plugins::list)?;
cx.export_function("pluginsInstall", plugins::install)?;
cx.export_function("pluginsUninstall", plugins::uninstall)?;
Expand Down
4 changes: 2 additions & 2 deletions node/src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

const addon = require('../index.node')

export function init(): Plugin[] {
export function init(): void {
return addon.loggingInit()
}

export function test(): Plugin[] {
export function test(): void {
return addon.loggingTest()
}
6 changes: 6 additions & 0 deletions node/src/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ pub fn obtain(cx: &mut FunctionContext) -> NeonResult<MutexGuard<'static, Plugin
}
}

/// Get plugin schema
pub fn schema(mut cx: FunctionContext) -> JsResult<JsString> {
let schema = plugins::schema();
Ok(cx.string(schema))
}

/// List plugins
pub fn list(mut cx: FunctionContext) -> JsResult<JsString> {
let aliases = &config::obtain(&mut cx)?.plugins.aliases;
Expand Down
15 changes: 14 additions & 1 deletion node/src/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { list, install, uninstall, upgrade } from './plugins'
import { install, list, schema, uninstall, upgrade } from './plugins'

describe('plugins', () => {
test('schema', () => {
expect(schema()).toEqual(
expect.objectContaining({
$schema: 'http://json-schema.org/draft-07/schema#',
title: 'Plugin',
type: 'object',
properties: expect.objectContaining({
name: { description: 'The name of the plugin', type: 'string' },
}),
})
)
})

test('list', () => {
expect(list()).toEqual(expect.arrayContaining([]))
})
Expand Down
28 changes: 25 additions & 3 deletions node/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
// Node.js bindings for ../../rust/src/plugins.rs, see there for more documentation.

import { JSONSchema7 } from 'json-schema'
import { fromJSON } from './prelude'

const addon = require('../index.node')

export type Installation = 'docker' | 'binary' | 'js' | 'py' | 'r' | 'link'
// Warning: The following types are hand written and may become out of sync
// with the actual JSON data returned by the functions below.
// Use the `schema()` function as the authoritative source of the shape of
// the plugin objects.

export type PluginInstallation =
| 'docker'
| 'binary'
| 'javascript'
| 'python'
| 'r'
| 'link'

export interface Plugin {
// Properties from the plugin's manifest file
Expand All @@ -17,12 +29,22 @@ export interface Plugin {

// Properties that are derived / updated

installation?: Installation
installation?: PluginInstallation
refreshed?: string
next?: Plugin
alias?: string
}

/**
* Get the JSON schema for a plugin object
*
* @returns A JSON Schema v7 object describing the properties of
* a plugin object
*/
export function schema(): JSONSchema7 {
return fromJSON<JSONSchema7>(addon.pluginsSchema())
}

/**
* List plugins in registry and/or installed
*
Expand All @@ -44,7 +66,7 @@ export function list(): Plugin[] {
*/
export function install(
spec: string,
installations?: Installation | Installation[]
installations?: PluginInstallation | PluginInstallation[]
): Plugin[] {
return fromJSON<Plugin[]>(addon.pluginsInstall(spec, installations ?? []))
}
Expand Down
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ once_cell = "1.7.2"
rand = "0.8.3"
regex = "1.4.5"
reqwest = { version = "0.11.3", optional = true, features = ["json"] }
schemars = "0.8.3"
schemars = { version = "0.8.3", features = ["preserve_order", "chrono"] }
self_update = { version = "0.26.0", optional = true, features = [
"archive-tar",
"archive-zip",
Expand Down
42 changes: 30 additions & 12 deletions rust/src/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use jsonschema::JSONSchema;
use once_cell::sync::Lazy;
use rand::Rng;
use regex::Regex;
use schemars::JsonSchema;
use schemars::{schema_for, JsonSchema};
use semver::Version;
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -49,7 +49,7 @@ pub enum PluginInstallation {
///
/// Property names use the Rust convention of snake_case but are renamed
/// to schema.org camelCase on serialization.
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
#[derive(Debug, Default, Clone, JsonSchema, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Plugin {
// Properties that are read from the plugin's manifest file
Expand Down Expand Up @@ -92,6 +92,12 @@ pub struct Plugin {
alias: Option<String>,
}

/// Get the JSON Schema for a plugin
pub fn schema() -> String {
let schema = schema_for!(Plugin);
serde_json::to_string_pretty(&schema).unwrap()
}

impl Plugin {
/// The name of the plugin file within the plugin directory
const FILE_NAME: &'static str = "codemeta.json";
Expand Down Expand Up @@ -1571,18 +1577,26 @@ pub mod cli {
setting = structopt::clap::AppSettings::ColoredHelp
)]
List,

Show(Show),
#[structopt(
about = "List registered plugin aliases",
setting = structopt::clap::AppSettings::ColoredHelp
)]
Aliases,
Install(Install),
Link(Link),
Upgrade(Upgrade),
Uninstall(Uninstall),
Unlink(Unlink),
Refresh(Refresh),

#[structopt(
about = "List registered plugin aliases",
setting = structopt::clap::AppSettings::ColoredHelp
)]
Aliases,

#[structopt(
about = "Get the JSON Schema for plugins",
setting = structopt::clap::AppSettings::ColoredHelp
)]
Schema,
}

#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -1799,11 +1813,6 @@ pub mod cli {
}
Ok(())
}
Action::Aliases => {
let md = plugins.display_aliases(aliases)?;
println!("{}", skin.term_text(md.as_str()));
Ok(())
}
Action::Install(action) => {
let Install {
docker,
Expand Down Expand Up @@ -1868,6 +1877,15 @@ pub mod cli {

Plugin::refresh_list(list, aliases, plugins).await
}
Action::Aliases => {
let md = plugins.display_aliases(aliases)?;
println!("{}", skin.term_text(md.as_str()));
Ok(())
}
Action::Schema => {
println!("{}", schema());
Ok(())
}
}
}
}
Expand Down

0 comments on commit 360f551

Please sign in to comment.