Skip to content

Commit

Permalink
fix: use consistent names for tera platform information (#2569)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Sep 12, 2024
1 parent ce9e3e5 commit c6eac80
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 132 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ indicatif = { version = "0.17.8", features = ["default", "improved_unicode"] }
indoc = "2.0.5"
itertools = "0.13"
log = "0.4.21"
num_cpus = "1.16.0" # gets cross-platform the number of CPU
num_cpus = "1"
once_cell = "1.19.0"
openssl = { version = "0.10.66", optional = true }
path-absolutize = "3.1.1"
petgraph = "0.6.4"
platform-info = "2.0.3" # cross-platform platform information
rand = "0.8.5"
rayon = "1.10.0"
regex = "1.10.4"
Expand Down
42 changes: 40 additions & 2 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,49 @@ Templates are used in the following locations:
The following context objects are available inside templates:

- `env: HashMap<String, String>` – current environment variables
- `config_root: PathBuf` – directory containing the `.mise.toml` file
- `cwd: PathBuf` – current working directory
- `config_root: PathBuf` – directory containing the `mise.toml` file or directory containing
`.mise` directory with config file.

As well as these functions:

- `exec(command: &str) -> String` – execute a command and return the output
- `exec(command) -> String` – execute a command and return the output
- `arch() -> String` – return the system architecture, e.g. `x86_64`, `arm64`
- `os() -> String` – return the operating system, e.g. `linux`, `macos`, `windows`
- `os_family() -> String` – return the operating system family, e.g. `unix`, `windows`
- `num_cpus() -> usize` – return the number of CPUs on the system

And these filters:

- `str | hash -> String` – return the SHA256 hash of the input string
- `str | hash(len=usize) -> String` – return the SHA256 hash of the input string truncated to `len`
characters
- `path | hash_file -> String` – return the SHA256 hash of the file at the input path
- `path | hash_file(len=usize) -> String` – return the SHA256 hash of the file at the input path
truncated to `len` characters
- `path | canonicalize -> String` – return the canonicalized path
- `path | dirname -> String` – return the directory path for a file, e.g. `/foo/bar/baz.txt` ->
`/foo/bar`
- `path | basename -> String` – return the base name of a file, e.g. `/foo/bar/baz.txt` -> `baz.txt`
- `path | extname -> String` – return the extension of a file, e.g. `/foo/bar/baz.txt` -> `.txt`
- `path | file_stem -> String` – return the file name without the extension, e.g.
`/foo/bar/baz.txt` -> `baz`
- `path | file_size -> String` – return the size of a file in bytes
- `path | last_modified -> String` – return the last modified time of a file
- `path[] | join_path -> String` – join an array of paths into a single path
- `str | quote -> String` – quote a string
- `str | kebabcase -> String` – convert a string to kebab-case
- `str | lowercamelcase -> String` – convert a string to lowerCamelCase
- `str | uppercamelcase -> String` – convert a string to UpperCamelCase
- `str | shoutycamelcase -> String` – convert a string to ShoutyCamelCase
- `str | snakecase -> String` – convert a string to snake_case
- `str | shoutysnakecase -> String` – convert a string to SHOUTY_SNAKE_CASE

And these testers:

- `if path is dir` – if the path is a directory
- `if path is file` – if the path is a file
- `if path is exists` – if the path exists

Templates are parsed with [tera](https://keats.github.io/tera/docs/)—which is quite powerful. For
example, this snippet will get the directory name of the project:
Expand Down
1 change: 1 addition & 0 deletions src/config/env_directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl EnvResults {
.map(Path::to_path_buf)
.or_else(|| dirs::CWD.clone())
.unwrap_or_default();
ctx.insert("cwd", &*dirs::CWD);
ctx.insert("config_root", &config_root);
let env_vars = env
.iter()
Expand Down
6 changes: 6 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ use zip::ZipArchive;

use crate::{dirs, env};

pub fn open<P: AsRef<Path>>(path: P) -> Result<File> {
let path = path.as_ref();
trace!("open {}", display_path(path));
File::open(path).wrap_err_with(|| format!("failed open: {}", display_path(path)))
}

pub fn remove_all<P: AsRef<Path>>(path: P) -> Result<()> {
let path = path.as_ref();
match path.metadata().map(|m| m.file_type()) {
Expand Down
15 changes: 10 additions & 5 deletions src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
use std::collections::HashMap;
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::{Read, Write};
use std::path::Path;

use crate::file;
use crate::file::display_path;
use crate::ui::progress_report::SingleReport;
use eyre::{ensure, Result};
use rayon::prelude::*;
use sha2::{Digest, Sha256};
use siphasher::sip::SipHasher;

use crate::file::display_path;
use crate::ui::progress_report::SingleReport;

pub fn hash_to_str<T: Hash>(t: &T) -> String {
let mut s = SipHasher::new();
t.hash(&mut s);
format!("{:x}", s.finish())
}

pub fn hash_sha256_to_str(s: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(s);
format!("{:x}", hasher.finalize())
}

pub fn file_hash_sha256(path: &Path) -> Result<String> {
file_hash_sha256_prog(path, None)
}

pub fn file_hash_sha256_prog(path: &Path, pr: Option<&dyn SingleReport>) -> Result<String> {
let mut file = File::open(path)?;
let mut file = file::open(path)?;
if let Some(pr) = pr {
pr.set_length(file.metadata()?.len());
}
Expand Down
Loading

0 comments on commit c6eac80

Please sign in to comment.