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

feat: make snippet executors multiplatform #282

Merged
merged 1 commit into from
Jul 13, 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
31 changes: 0 additions & 31 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,8 @@ fn build_themes(out_dir: &str) -> io::Result<()> {
Ok(())
}

fn build_executors(out_dir: &str) -> io::Result<()> {
let output_path = format!("{out_dir}/executors.rs");
let mut output_file = BufWriter::new(File::create(output_path)?);
output_file.write_all(b"use std::collections::BTreeMap as Map;\n")?;
output_file.write_all(b"use once_cell::sync::Lazy;\n")?;
output_file.write_all(b"static EXECUTORS: Lazy<Map<crate::markdown::elements::CodeLanguage, &'static [u8]>> = Lazy::new(|| Map::from([\n")?;

let mut paths = fs::read_dir("executors")?.collect::<io::Result<Vec<_>>>()?;
paths.sort_by_key(|e| e.path());
for file in paths {
let metadata = file.metadata()?;
if !metadata.is_file() {
panic!("found non file in executors directory");
}
let path = file.path();
let contents = fs::read(&path)?;
let file_name = path.file_name().unwrap().to_string_lossy();
let (language, extension) = file_name.split_once('.').unwrap();
if extension != "sh" {
panic!("extension must be 'sh'");
}
output_file.write_all(format!("(\"{language}\".parse().unwrap(), {contents:?}.as_slice()),\n").as_bytes())?;
}
output_file.write_all(b"]));\n")?;

// Rebuild if anything changes.
println!("cargo:rerun-if-changed=executors");
Ok(())
}

fn main() -> io::Result<()> {
let out_dir = env::var("OUT_DIR").unwrap();
build_themes(&out_dir)?;
build_executors(&out_dir)?;
Ok(())
}
39 changes: 39 additions & 0 deletions config-file-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,38 @@
},
"additionalProperties": false
},
"LanguageSnippetExecutionConfig": {
"description": "The snippet execution configuration for a specific programming language.",
"type": "object",
"required": [
"commands",
"filename"
],
"properties": {
"commands": {
"description": "The commands to be run when executing snippets for this programming language.",
"type": "array",
"items": {
"type": "array",
"items": {
"type": "string"
}
}
},
"environment": {
"description": "The environment variables to set before invoking every command.",
"default": {},
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"filename": {
"description": "The filename to use for the snippet input file.",
"type": "string"
}
}
},
"MermaidConfig": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -293,6 +325,13 @@
"enable"
],
"properties": {
"custom": {
"description": "Custom snippet executors.",
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/LanguageSnippetExecutionConfig"
}
},
"enable": {
"description": "Whether to enable snippet execution.",
"type": "boolean"
Expand Down
73 changes: 73 additions & 0 deletions executors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
bash:
filename: script.sh
commands:
- ["bash", "script.sh"]
c++:
filename: snippet.cpp
commands:
- ["g++", "-std=c++20", "snippet.cpp", "-o", "snippet"]
- ["./snippet"]
c:
filename: snippet.c
commands:
- ["gcc", "snippet.c", "-o", "snippet"]
- ["./snippet"]
fish:
filename: script.fish
commands:
- ["fish", "script.fish"]
go:
filename: snippet.go
environment:
GO11MODULE: off
commands:
- ["go", "run", "snippet.go"]
java:
filename: Snippet.java
commands:
- ["java", "Snippet.java"]
js:
filename: snippet.js
commands:
- ["node", "snippet.js"]
kotlin:
filename: snippet.kts
commands:
- ["kotlinc", "-script", "script.kts"]
lua:
filename: snippet.lua
commands:
- ["lua", "snippet.lua"]
nushell:
filename: snippet.nu
commands:
- ["nu", "snippet.nu"]
perl:
filename: snippet.pl
commands:
- ["perl", "snippet.pl"]
python:
filename: snippet.py
commands:
- ["python", "-u", "snippet.py"]
ruby:
filename: snippet.rb
commands:
- ["ruby", "snippet.rb"]
rust-script:
filename: snippet.rs
commands:
- ["rust-script", "snippet.rs"]
rust:
filename: snippet.rs
commands:
- ["rustc", "--crate-name", "presenterm_snippet", "snippet.rs", "-o", "snippet"]
- ["./snippet"]
sh:
filename: script.sh
commands:
- ["sh", "script.sh"]
zsh:
filename: script.sh
commands:
- ["zsh", "script.sh"]
5 changes: 0 additions & 5 deletions executors/c++.sh

This file was deleted.

5 changes: 0 additions & 5 deletions executors/c.sh

This file was deleted.

8 changes: 0 additions & 8 deletions executors/go.sh

This file was deleted.

7 changes: 0 additions & 7 deletions executors/java.sh

This file was deleted.

3 changes: 0 additions & 3 deletions executors/js.sh

This file was deleted.

7 changes: 0 additions & 7 deletions executors/kotlin.sh

This file was deleted.

3 changes: 0 additions & 3 deletions executors/lua.sh

This file was deleted.

3 changes: 0 additions & 3 deletions executors/nushell.sh

This file was deleted.

3 changes: 0 additions & 3 deletions executors/perl.sh

This file was deleted.

3 changes: 0 additions & 3 deletions executors/python.sh

This file was deleted.

3 changes: 0 additions & 3 deletions executors/ruby.sh

This file was deleted.

3 changes: 0 additions & 3 deletions executors/rust-script.sh

This file was deleted.

5 changes: 0 additions & 5 deletions executors/rust.sh

This file was deleted.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"src"
"themes"
"bat"
"executors"
"executors.yaml"
];

buildSrc = flakeboxLib.filterSubPaths {
Expand Down
25 changes: 24 additions & 1 deletion src/custom.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use crate::{
input::user::KeyBinding,
markdown::elements::CodeLanguage,
media::{emulator::TerminalEmulator, kitty::KittyMode},
GraphicsMode,
};
use clap::ValueEnum;
use schemars::JsonSchema;
use serde::Deserialize;
use std::{fs, io, path::Path};
use std::{
collections::{BTreeMap, HashMap},
fs, io,
path::Path,
};

#[derive(Clone, Debug, Default, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
Expand Down Expand Up @@ -134,6 +139,10 @@ pub struct SnippetConfig {
pub struct SnippetExecConfig {
/// Whether to enable snippet execution.
pub enable: bool,

/// Custom snippet executors.
#[serde(default)]
pub custom: BTreeMap<CodeLanguage, LanguageSnippetExecutionConfig>,
}

#[derive(Clone, Debug, Deserialize, JsonSchema)]
Expand Down Expand Up @@ -190,6 +199,20 @@ pub(crate) fn default_mermaid_scale() -> u32 {
2
}

/// The snippet execution configuration for a specific programming language.
#[derive(Clone, Debug, Deserialize, JsonSchema)]
pub struct LanguageSnippetExecutionConfig {
/// The filename to use for the snippet input file.
pub filename: String,

/// The environment variables to set before invoking every command.
#[serde(default)]
pub environment: HashMap<String, String>,

/// The commands to be run when executing snippets for this programming language.
pub commands: Vec<Vec<String>>,
}

#[derive(Clone, Debug, Default, Deserialize, ValueEnum, JsonSchema)]
#[serde(rename_all = "kebab-case")]
pub enum ImageProtocol {
Expand Down
4 changes: 2 additions & 2 deletions src/demo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
execute::CodeExecutor,
execute::SnippetExecutor,
input::{
source::Command,
user::{CommandKeyBindings, UserInput},
Expand Down Expand Up @@ -102,7 +102,7 @@ impl<W: TerminalWrite> ThemesDemo<W> {
let mut resources = Resources::new("non_existent", image_registry.clone());
let mut third_party = ThirdPartyRender::default();
let options = PresentationBuilderOptions::default();
let executer = Rc::new(CodeExecutor::default());
let executer = Rc::new(SnippetExecutor::default());
let bindings_config = Default::default();
let builder = PresentationBuilder::new(
theme,
Expand Down
Loading
Loading