Skip to content

Commit c2127c3

Browse files
committed
Drop handlebars for upon
- `upon` is faster, see benchmarks https://github.com/rossmacarthur/upon - `upon` is more lightweight - I prefer the block syntax `{% for file in files %} ...`
1 parent 5f290a7 commit c2127c3

14 files changed

Lines changed: 85 additions & 104 deletions

File tree

Cargo.lock

Lines changed: 25 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ curl = "0.4.43"
2828
fmutex = "0.1.0"
2929
git2 = { version = "0.14.4", features = ["vendored-libgit2"] }
3030
globwalk = "0.8.1"
31-
handlebars = "4.3.1"
3231
home = "0.5.3"
3332
indexmap = { version = "1.9.1", features = ["rayon", "serde"] }
3433
itertools = "0.10.3"
@@ -40,6 +39,7 @@ serde = { version = "1.0.139", features = ["derive"] }
4039
thiserror = "1.0.31"
4140
toml = { version = "0.5.9", features = ["preserve_order"] }
4241
toml_edit = "0.14.4"
42+
upon = { version = "0.4.0", default-features = false, features = ["serde"] }
4343
url = { version = "2.2.2", features = ["serde"] }
4444
walkdir = "2.3.2"
4545
which = { version = "4.2.5", default-features = false }
@@ -48,7 +48,7 @@ which = { version = "4.2.5", default-features = false }
4848
anyhow = "1.0.58"
4949

5050
[dev-dependencies]
51-
goldie = "0.3.0"
51+
goldie = "0.4.1"
5252
pest = "2.1.3"
5353
pest_derive = "2.1.0"
5454
pretty_assertions = "1.2.1"

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
- Arbitrary remote scripts or binary plugins.
1919
- Local plugins.
2020
- Inline plugins.
21-
- Highly configurable install methods using
22-
[handlebars](http://handlebarsjs.com) templating.
21+
- Highly configurable install methods using templates.
2322
- Shell agnostic, with sensible defaults for Zsh.
2423
- Super-fast plugin loading and parallel installation. See [benchmarks].
2524
- Config file using [TOML](https://toml.io) syntax.

docs/README.hbs renamed to docs/README_TEMPLATE.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<!-- automatically generated by `cargo run --package gen-readme` -->
2+
13
# sheldon
24

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

2829
## Table of Contents
2930

30-
{{{ toc ~}}}
31+
{{ toc -}}
3132
- [💡 Examples](#-examples)
3233
- [License](#license)
3334

34-
{{{ contents }}}
35+
{{ contents }}
3536

3637
## 💡 Examples
3738

docs/src/Introduction.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ Sheldon is a fast, configurable, command-line tool to manage your shell plugins.
55
## How does it work?
66

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

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

src/cli/testdata/raw_opt_help.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
sheldon {version}
1+
sheldon {{ version }}
22
Ross MacArthur <ross@macarthur.io>
33
Fast, configurable, shell plugin manager.
44

src/cli/tests.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use super::*;
33
use std::iter;
44

55
use pretty_assertions::assert_eq;
6-
use serde::Serialize;
76

87
use crate::cli::color_choice::ColorChoice;
98

@@ -49,16 +48,7 @@ fn raw_opt_long_version() {
4948
#[test]
5049
fn raw_opt_help() {
5150
setup();
52-
53-
#[derive(Serialize)]
54-
struct Context {
55-
version: &'static str,
56-
}
57-
58-
let ctx = Context {
59-
version: build::CRATE_RELEASE,
60-
};
61-
51+
let ctx = upon::value! { version: build::CRATE_RELEASE };
6252
for opt in &["-h", "--help"] {
6353
let err = raw_opt_err(&[opt]);
6454
goldie::assert_template!(&ctx, err.to_string());

src/config/normalize.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use url::Url;
99

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

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

35+
// Check that the templates can be compiled.
3436
for (name, template) in &templates {
35-
// Check that the templates can be compiled.
36-
handlebars::Template::compile(&template.value)
37+
TEMPLATE_ENGINE
38+
.compile(&template.value)
3739
.with_context(s!("failed to compile template `{}`", name))?;
3840
}
3941

src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl ToMessage for &Path {
185185
pub fn log_error(no_color: bool, color: Color, prefix: &str, err: &Error) {
186186
let pretty = err
187187
.chain()
188-
.map(|c| c.to_string().replace("Template error: ", ""))
188+
.map(|c| c.to_string())
189189
.collect::<Vec<_>>()
190190
.join("\n due to: ");
191191
if no_color {

src/lock/plugin.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ use std::path::{Path, PathBuf};
33
use anyhow::{bail, Context as ResultExt, Result};
44
use indexmap::IndexMap;
55
use maplit::hashmap;
6+
use serde::Serialize;
67

78
use crate::config::{ExternalPlugin, Source, Template};
89
use crate::context::Context;
910
use crate::lock::file::LockedExternalPlugin;
1011
use crate::lock::source::LockedSource;
12+
use crate::util::TEMPLATE_ENGINE;
1113

1214
/// Consume the [`ExternalPlugin`] and convert it to a [`LockedExternalPlugin`].
1315
pub fn lock(
@@ -39,10 +41,6 @@ pub fn lock(
3941
apply,
4042
}
4143
} else {
42-
// Handlebars instance to do the rendering
43-
let mut hbs = handlebars::Handlebars::new();
44-
hbs.set_strict_mode(true);
45-
4644
// Data to use in template rendering
4745
let mut data = hashmap! {
4846
"data_dir" => ctx
@@ -54,9 +52,7 @@ pub fn lock(
5452

5553
let source_dir = locked_source.dir;
5654
let plugin_dir = if let Some(dir) = dir {
57-
let rendered = hbs
58-
.render_template(&dir, &data)
59-
.with_context(s!("failed to render template `{}`", dir))?;
55+
let rendered = render_template(&dir, &data)?;
6056
Some(source_dir.join(rendered))
6157
} else {
6258
None
@@ -73,20 +69,15 @@ pub fn lock(
7369
if let Some(uses) = &uses {
7470
let patterns = uses
7571
.iter()
76-
.map(|u| {
77-
hbs.render_template(u, &data)
78-
.with_context(s!("failed to render template `{}`", u))
79-
})
72+
.map(|u| render_template(u, &data))
8073
.collect::<Result<Vec<_>>>()?;
8174
if !match_globs(dir, &patterns, &mut files)? {
8275
bail!("failed to find any files matching any of `{:?}`", patterns);
8376
}
8477
// Otherwise we try to figure out which files to use...
8578
} else {
8679
for g in global_matches {
87-
let pattern = hbs
88-
.render_template(g, &data)
89-
.with_context(s!("failed to render template `{}`", g))?;
80+
let pattern = render_template(g, &data)?;
9081
if match_globs(dir, &[pattern], &mut files)? {
9182
break;
9283
}
@@ -110,6 +101,17 @@ pub fn lock(
110101
})
111102
}
112103

104+
fn render_template<S>(template: &str, ctx: S) -> Result<String>
105+
where
106+
S: Serialize,
107+
{
108+
TEMPLATE_ENGINE
109+
.compile(template)
110+
.with_context(s!("failed to compile template `{}`", template))?
111+
.render(ctx)
112+
.with_context(s!("failed to render template `{}`", template))
113+
}
114+
113115
fn match_globs(dir: &Path, patterns: &[String], files: &mut Vec<PathBuf>) -> Result<bool> {
114116
let debug = || {
115117
patterns

0 commit comments

Comments
 (0)