Skip to content

Commit

Permalink
Merge pull request #96 from volllly/template-helpers
Browse files Browse the repository at this point in the history
Add eval template helper
  • Loading branch information
volllly authored Sep 17, 2022
2 parents bdef78b + 7f830d3 commit 9db606a
Show file tree
Hide file tree
Showing 14 changed files with 311 additions and 108 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.8.0] - 2022-09-16

### Added

- Template helpers `#windows`, `#linx` and `#darwin` which work like `if`s for the respective os
- `eval` template helper which evaluates the given string on the shell

## [0.7.1] - 2022-09-12

### Fixed
Expand Down Expand Up @@ -152,7 +159,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Dotfile linking
- Error handling

[Unreleased]: https://github.com/volllly/rotz/compare/v0.7.1...HEAD
[Unreleased]: https://github.com/volllly/rotz/compare/v0.8.0...HEAD
[0.8.0]: https://github.com/volllly/rotz/releases/tag/v0.8.0
[0.7.1]: https://github.com/volllly/rotz/releases/tag/v0.7.1
[0.7.0]: https://github.com/volllly/rotz/releases/tag/v0.7.0
[0.6.1]: https://github.com/volllly/rotz/releases/tag/v0.6.1
Expand Down
72 changes: 36 additions & 36 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rotz"
version = "0.7.1"
version = "0.8.0"
edition = "2021"
authors = ["Paul Volavsek <paul.volavsek@gmail.com>"]
license = "MIT"
Expand Down
24 changes: 21 additions & 3 deletions docs/docs/configuration/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This allows for e.g. access to environment variables.
| `whoami` | A map of information about the environment (see [whoami](#whoami)). Provided by the [whoami](https://github.com/dirs-dev/directories-rs#features) crate. | `some.file: /home/{{ whoami.username }}/some.file` |
| `directories` | A map of directories (see [directories](#directories)). Provided by the [directories](https://github.com/ardaku/whoami#features) crate | `some.file: {{ directories.home }}/some.file` |

## `whoami`
### `whoami`

| Variable | Description |
| ------------- | ----------------------------------------- |
Expand All @@ -31,7 +31,7 @@ This allows for e.g. access to environment variables.
| `realname` | The users full name |
| `username` | The current users username |

## `directories`
### `directories`

| Group | Variable |
| ------ | ------------ |
Expand All @@ -50,4 +50,22 @@ This allows for e.g. access to environment variables.
| `user` | `picture` |
| `user` | `public` |
| `user` | `template` |
| `user` | `video` |
| `user` | `video` |

## Helpers

Rotz comes with helpers provided by the [handlebars_misc_helpers](https://github.com/davidb/handlebars_misc_helpers) crate.

Theres also a number of inbuilt helpers provided

### `#windows`, `#linx` and `#darwin`

These helpers are shorthands for checking the curent os.

Instea of `{{ #if (eq os "windows") }}{{ else }}{{ /if }}` they can be used like this `{{ #windows }}{{ else }}{{ /windows }}`.

### `eval`

The eval helper can be used to evalate a string on the shell configured by [`shell_command`](config.yaml.mdx#shell_command).

The helper can be used like this `{{ eval "some --shell command" }}`
18 changes: 10 additions & 8 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use velcro::hash_map;
use wax::{Glob, Pattern};

use super::Command;
use crate::{config::Config, dot::Installs, helpers, templating::HANDLEBARS};
use crate::{config::Config, dot::Installs, helpers, templating};

#[derive(thiserror::Error, Diagnostic, Debug)]
enum Error {
Expand Down Expand Up @@ -45,13 +45,14 @@ enum Error {
ParseGlob(String, #[source] wax::BuildError<'static>),
}

pub struct Install {
pub(crate) struct Install<'a> {
config: Config,
engine: templating::Engine<'a>,
}

impl Install {
pub const fn new(config: crate::config::Config) -> Self {
Self { config }
impl<'b> Install<'b> {
pub const fn new(config: crate::config::Config, engine: templating::Engine<'b>) -> Self {
Self { config, engine }
}

fn install<'a>(
Expand Down Expand Up @@ -109,7 +110,8 @@ impl Install {
let inner_cmd = installs.cmd.clone();

let cmd = if let Some(shell_command) = self.config.shell_command.as_ref() {
HANDLEBARS
self
.engine
.render_template(shell_command, &hash_map! { "cmd": &inner_cmd })
.map_err(|err| Error::RenderingTemplate(entry.0.clone(), err))?
} else {
Expand Down Expand Up @@ -151,12 +153,12 @@ impl Install {

type InstallsDots = (Option<Installs>, Option<HashSet<String>>);

impl Command for Install {
impl Command for Install<'_> {
type Args = (crate::cli::Globals, crate::cli::Install);
type Result = Result<()>;

fn execute(&self, (globals, install_command): Self::Args) -> Self::Result {
let dots = crate::dot::read_dots(&self.config.dotfiles, &["/**".to_owned()], &self.config)?
let dots = crate::dot::read_dots(&self.config.dotfiles, &["/**".to_owned()], &self.config, &self.engine)?
.into_iter()
.filter(|d| d.1.installs.is_some() || d.1.depends.is_some())
.map(|d| (d.0, (d.1.installs, d.1.depends)))
Expand Down
16 changes: 9 additions & 7 deletions src/commands/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use crossterm::style::{Attribute, Stylize};
use miette::{Diagnostic, Report, Result};
use tap::Pipe;

use super::Command;
use crate::{
config::{Config, LinkType},
USER_DIRS,
templating, USER_DIRS,
};

#[derive(thiserror::Error, Diagnostic, Debug)]
Expand All @@ -24,22 +25,23 @@ enum Error {
AlreadyExists(PathBuf),
}

pub struct Link {
pub(crate) struct Link<'a> {
config: Config,
engine: templating::Engine<'a>,
}

impl Link {
pub const fn new(config: crate::config::Config) -> Self {
Self { config }
impl<'a> Link<'a> {
pub const fn new(config: crate::config::Config, engine: templating::Engine<'a>) -> Self {
Self { config, engine }
}
}

impl super::Command for Link {
impl<'a> Command for Link<'a> {
type Args = (crate::cli::Globals, crate::cli::Link);
type Result = Result<()>;

fn execute(&self, (globals, link_command): Self::Args) -> Self::Result {
let links = crate::dot::read_dots(&self.config.dotfiles, &link_command.dots, &self.config)?
let links = crate::dot::read_dots(&self.config.dotfiles, &link_command.dots, &self.config, &self.engine)?
.into_iter()
.filter_map(|d| d.1.links.map(|l| (d.0, l)));

Expand Down
4 changes: 2 additions & 2 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ pub mod clone;
pub use clone::Clone;

pub mod install;
pub use install::Install;
pub(crate) use install::Install;

pub mod link;
pub use link::Link;
pub(crate) use link::Link;

pub mod sync;
pub use sync::Sync;
Expand Down
Loading

0 comments on commit 9db606a

Please sign in to comment.