Skip to content

Commit

Permalink
Drop handlebars for upon
Browse files Browse the repository at this point in the history
- `upon` is faster, see benchmarks https://github.com/rossmacarthur/upon
- `upon` is more lightweight
- I prefer the block syntax `{% for file in files %} ...`
  • Loading branch information
rossmacarthur committed Oct 7, 2022
1 parent 5f290a7 commit c2127c3
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 104 deletions.
59 changes: 25 additions & 34 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -28,7 +28,6 @@ curl = "0.4.43"
fmutex = "0.1.0"
git2 = { version = "0.14.4", features = ["vendored-libgit2"] }
globwalk = "0.8.1"
handlebars = "4.3.1"
home = "0.5.3"
indexmap = { version = "1.9.1", features = ["rayon", "serde"] }
itertools = "0.10.3"
Expand All @@ -40,6 +39,7 @@ serde = { version = "1.0.139", features = ["derive"] }
thiserror = "1.0.31"
toml = { version = "0.5.9", features = ["preserve_order"] }
toml_edit = "0.14.4"
upon = { version = "0.4.0", default-features = false, features = ["serde"] }
url = { version = "2.2.2", features = ["serde"] }
walkdir = "2.3.2"
which = { version = "4.2.5", default-features = false }
Expand All @@ -48,7 +48,7 @@ which = { version = "4.2.5", default-features = false }
anyhow = "1.0.58"

[dev-dependencies]
goldie = "0.3.0"
goldie = "0.4.1"
pest = "2.1.3"
pest_derive = "2.1.0"
pretty_assertions = "1.2.1"
Expand Down
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -18,8 +18,7 @@
- Arbitrary remote scripts or binary plugins.
- Local plugins.
- Inline plugins.
- Highly configurable install methods using
[handlebars](http://handlebarsjs.com) templating.
- Highly configurable install methods using templates.
- Shell agnostic, with sensible defaults for Zsh.
- Super-fast plugin loading and parallel installation. See [benchmarks].
- Config file using [TOML](https://toml.io) syntax.
Expand Down
9 changes: 5 additions & 4 deletions docs/README.hbs → docs/README_TEMPLATE.md
@@ -1,3 +1,5 @@
<!-- automatically generated by `cargo run --package gen-readme` -->

# sheldon

*Fast, configurable, shell plugin manager*
Expand All @@ -16,8 +18,7 @@
- Arbitrary remote scripts or binary plugins.
- Local plugins.
- Inline plugins.
- Highly configurable install methods using
[handlebars](http://handlebarsjs.com) templating.
- Highly configurable install methods using templates.
- Shell agnostic, with sensible defaults for Zsh.
- Super-fast plugin loading and parallel installation. See [benchmarks].
- Config file using [TOML](https://toml.io) syntax.
Expand All @@ -27,11 +28,11 @@

## Table of Contents

{{{ toc ~}}}
{{ toc -}}
- [💡 Examples](#-examples)
- [License](#license)

{{{ contents }}}
{{ contents }}

## 💡 Examples

Expand Down
3 changes: 1 addition & 2 deletions docs/src/Introduction.md
Expand Up @@ -5,8 +5,7 @@ Sheldon is a fast, configurable, command-line tool to manage your shell plugins.
## How does it work?

Plugins are specified in a [TOML](https://toml.io) configuration file and
Sheldon renders an install script using user configurable
[handlebars](http://handlebarsjs.com) templates.
Sheldon renders an install script using user configurable templates.

A `~/.zshrc` or `~/.bashrc` that uses Sheldon simply contains the following.

Expand Down
2 changes: 1 addition & 1 deletion src/cli/testdata/raw_opt_help.golden
@@ -1,4 +1,4 @@
sheldon {version}
sheldon {{ version }}
Ross MacArthur <ross@macarthur.io>
Fast, configurable, shell plugin manager.

Expand Down
12 changes: 1 addition & 11 deletions src/cli/tests.rs
Expand Up @@ -3,7 +3,6 @@ use super::*;
use std::iter;

use pretty_assertions::assert_eq;
use serde::Serialize;

use crate::cli::color_choice::ColorChoice;

Expand Down Expand Up @@ -49,16 +48,7 @@ fn raw_opt_long_version() {
#[test]
fn raw_opt_help() {
setup();

#[derive(Serialize)]
struct Context {
version: &'static str,
}

let ctx = Context {
version: build::CRATE_RELEASE,
};

let ctx = upon::value! { version: build::CRATE_RELEASE };
for opt in &["-h", "--help"] {
let err = raw_opt_err(&[opt]);
goldie::assert_template!(&ctx, err.to_string());
Expand Down
6 changes: 4 additions & 2 deletions src/config/normalize.rs
Expand Up @@ -9,6 +9,7 @@ use url::Url;

use crate::config::file::{GitProtocol, RawConfig, RawPlugin};
use crate::config::{Config, ExternalPlugin, InlinePlugin, Plugin, Shell, Source, Template};
use crate::util::TEMPLATE_ENGINE;

/// The Gist domain host.
const GIST_HOST: &str = "gist.github.com";
Expand All @@ -31,9 +32,10 @@ pub fn normalize(raw_config: RawConfig, warnings: &mut Vec<Error>) -> Result<Con
warnings.push(anyhow!("unused config key: `{}`", key))
});

// Check that the templates can be compiled.
for (name, template) in &templates {
// Check that the templates can be compiled.
handlebars::Template::compile(&template.value)
TEMPLATE_ENGINE
.compile(&template.value)
.with_context(s!("failed to compile template `{}`", name))?;
}

Expand Down
2 changes: 1 addition & 1 deletion src/context.rs
Expand Up @@ -185,7 +185,7 @@ impl ToMessage for &Path {
pub fn log_error(no_color: bool, color: Color, prefix: &str, err: &Error) {
let pretty = err
.chain()
.map(|c| c.to_string().replace("Template error: ", ""))
.map(|c| c.to_string())
.collect::<Vec<_>>()
.join("\n due to: ");
if no_color {
Expand Down
30 changes: 16 additions & 14 deletions src/lock/plugin.rs
Expand Up @@ -3,11 +3,13 @@ use std::path::{Path, PathBuf};
use anyhow::{bail, Context as ResultExt, Result};
use indexmap::IndexMap;
use maplit::hashmap;
use serde::Serialize;

use crate::config::{ExternalPlugin, Source, Template};
use crate::context::Context;
use crate::lock::file::LockedExternalPlugin;
use crate::lock::source::LockedSource;
use crate::util::TEMPLATE_ENGINE;

/// Consume the [`ExternalPlugin`] and convert it to a [`LockedExternalPlugin`].
pub fn lock(
Expand Down Expand Up @@ -39,10 +41,6 @@ pub fn lock(
apply,
}
} else {
// Handlebars instance to do the rendering
let mut hbs = handlebars::Handlebars::new();
hbs.set_strict_mode(true);

// Data to use in template rendering
let mut data = hashmap! {
"data_dir" => ctx
Expand All @@ -54,9 +52,7 @@ pub fn lock(

let source_dir = locked_source.dir;
let plugin_dir = if let Some(dir) = dir {
let rendered = hbs
.render_template(&dir, &data)
.with_context(s!("failed to render template `{}`", dir))?;
let rendered = render_template(&dir, &data)?;
Some(source_dir.join(rendered))
} else {
None
Expand All @@ -73,20 +69,15 @@ pub fn lock(
if let Some(uses) = &uses {
let patterns = uses
.iter()
.map(|u| {
hbs.render_template(u, &data)
.with_context(s!("failed to render template `{}`", u))
})
.map(|u| render_template(u, &data))
.collect::<Result<Vec<_>>>()?;
if !match_globs(dir, &patterns, &mut files)? {
bail!("failed to find any files matching any of `{:?}`", patterns);
}
// Otherwise we try to figure out which files to use...
} else {
for g in global_matches {
let pattern = hbs
.render_template(g, &data)
.with_context(s!("failed to render template `{}`", g))?;
let pattern = render_template(g, &data)?;
if match_globs(dir, &[pattern], &mut files)? {
break;
}
Expand All @@ -110,6 +101,17 @@ pub fn lock(
})
}

fn render_template<S>(template: &str, ctx: S) -> Result<String>
where
S: Serialize,
{
TEMPLATE_ENGINE
.compile(template)
.with_context(s!("failed to compile template `{}`", template))?
.render(ctx)
.with_context(s!("failed to render template `{}`", template))
}

fn match_globs(dir: &Path, patterns: &[String], files: &mut Vec<PathBuf>) -> Result<bool> {
let debug = || {
patterns
Expand Down

0 comments on commit c2127c3

Please sign in to comment.