Skip to content

Commit

Permalink
Merge pull request #1169 from cachix/json-schema
Browse files Browse the repository at this point in the history
add devenv.schema.json
  • Loading branch information
domenkozar authored Apr 24, 2024
2 parents caa4149 + fd510c2 commit b26b52a
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 11 deletions.
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"
]
}
}
}
}
}

0 comments on commit b26b52a

Please sign in to comment.