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

add devenv.schema.json #1169

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
run: devenv shell devenv-generate-languages-example
- name: Generate docs
run: devenv shell devenv-generate-docs
- name: Generate JSON schema
run: devenv generate-json-schema
- uses: EndBug/add-and-commit@v9
if: ${{ github.event_name == 'push' }}
with:
Expand Down
25 changes: 25 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions devenv.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# yaml-language-server: $schema=./docs/devenv.schema.json
inputs:
devenv:
url: path:.?dir=src/modules
Expand Down
1 change: 1 addition & 0 deletions devenv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ whoami = "1.5.1"
xdg = "2.5.2"

devenv_core = { path = "../devenv_core" }
schemars = "0.8.16"
1 change: 1 addition & 0 deletions devenv/init/devenv.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# yaml-language-server: $schema=https://devenv.sh/devenv.schema.json
inputs:
nixpkgs:
url: github:cachix/devenv-nixpkgs/rolling
Expand Down
24 changes: 13 additions & 11 deletions devenv/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use miette::{IntoDiagnostic, Result};
use schematic::{schema::JsonSchemaRenderer, schema::SchemaGenerator, ConfigLoader};
use schemars::{schema_for, JsonSchema};
use schematic::ConfigLoader;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::Path};

const YAML_CONFIG: &str = "devenv.yaml";

#[derive(schematic::Config, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(schematic::Config, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[config(rename_all = "camelCase")]
#[serde(rename_all = "camelCase")]
pub struct Input {
Expand Down Expand Up @@ -34,7 +35,7 @@ impl Input {
}
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
pub struct FlakeInput {
pub url: Option<String>,
pub inputs: HashMap<String, Input>,
Expand Down Expand Up @@ -66,14 +67,14 @@ fn is_false(b: &bool) -> bool {
!*b
}

#[derive(schematic::Config, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(schematic::Config, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Clean {
pub enabled: bool,
pub keep: Vec<String>,
// TODO: executables?
}

#[derive(schematic::Config, Clone, Serialize, Debug)]
#[derive(schematic::Config, Clone, Serialize, Debug, JsonSchema)]
#[config(rename_all = "camelCase")]
#[serde(rename_all = "camelCase")]
pub struct Config {
Expand All @@ -88,19 +89,20 @@ pub struct Config {
pub imports: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub permitted_insecure_packages: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none", default = "std::option::None")]
#[setting(nested)]
#[serde(skip_serializing_if = "Option::is_none", default)]
pub clean: Option<Clean>,
#[serde(skip_serializing_if = "is_false", default = "false_default")]
pub impure: bool,
}

// TODO: https://github.com/moonrepo/schematic/issues/105
pub fn write_json_schema() {
let mut generator = SchemaGenerator::default();
generator.add::<Config>();
generator
.generate("devenv.schema.json", JsonSchemaRenderer::default())
.expect("can't generate schema");
let schema = schema_for!(Config);
let schema = serde_json::to_string_pretty(&schema).unwrap();
let path = Path::new("docs/devenv.schema.json");
std::fs::write(path, schema)
.unwrap_or_else(|_| panic!("Failed to write json schema to {}", path.display()));
}

impl Config {
Expand Down
96 changes: 96 additions & 0 deletions docs/devenv.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Config",
"type": "object",
"properties": {
"allowBroken": {
"type": "boolean"
},
"allowUnfree": {
"type": "boolean"
},
"clean": {
"anyOf": [
{
"$ref": "#/definitions/Clean"
},
{
"type": "null"
}
]
},
"imports": {
"type": "array",
"items": {
"type": "string"
}
},
"impure": {
"type": "boolean"
},
"inputs": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/Input"
}
},
"permittedInsecurePackages": {
"type": "array",
"items": {
"type": "string"
}
}
},
"definitions": {
"Clean": {
"type": "object",
"required": [
"enabled",
"keep"
],
"properties": {
"enabled": {
"type": "boolean"
},
"keep": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"Input": {
"type": "object",
"properties": {
"flake": {
"type": "boolean"
},
"follows": {
"type": [
"string",
"null"
]
},
"inputs": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/Input"
}
},
"overlays": {
"type": "array",
"items": {
"type": "string"
}
},
"url": {
"type": [
"string",
"null"
]
}
}
}
}
}
Loading