Skip to content

Commit

Permalink
Merge pull request #265 from spenserblack/main
Browse files Browse the repository at this point in the history
Set linguist attributes for `pixi.lock`
  • Loading branch information
baszalmstra authored Aug 18, 2023
2 parents c2cfc46 + d744baa commit f38415e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pixi.lock linguist-language=YAML
37 changes: 32 additions & 5 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use miette::IntoDiagnostic;
use minijinja::{context, Environment};
use rattler_conda_types::Platform;
use std::io::{Error, ErrorKind};
use std::{fs, path::PathBuf};
use std::{
fs,
path::{Path, PathBuf},
};

/// Creates a new project
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -42,11 +45,17 @@ const GITIGNORE_TEMPLATE: &str = r#"# pixi environments
"#;

const GITATTRIBUTES_TEMPLATE: &str = r#"# GitHub syntax highlighting
pixi.lock linguist-language=YAML
"#;

pub async fn execute(args: Args) -> miette::Result<()> {
let env = Environment::new();
let dir = get_dir(args.path).into_diagnostic()?;
let manifest_path = dir.join(consts::PROJECT_MANIFEST);
let gitignore_path = dir.join(".gitignore");
let gitattributes_path = dir.join(".gitattributes");

// Check if the project file doesnt already exist. We don't want to overwrite it.
if fs::metadata(&manifest_path).map_or(false, |x| x.is_file()) {
Expand Down Expand Up @@ -92,10 +101,17 @@ pub async fn execute(args: Args) -> miette::Result<()> {

// create a .gitignore if one is missing
if !gitignore_path.is_file() {
let rv = env
.render_named_str("gitignore.txt", GITIGNORE_TEMPLATE, ())
.into_diagnostic()?;
fs::write(&gitignore_path, rv).into_diagnostic()?;
write_contextless_file(&env, gitignore_path, "gitignore.txt", GITIGNORE_TEMPLATE)?;
}

// create a .gitattributes if one is missing
if !gitattributes_path.is_file() {
write_contextless_file(
&env,
gitattributes_path,
"gitattributes.txt",
GITATTRIBUTES_TEMPLATE,
)?;
}

// Emit success
Expand Down Expand Up @@ -128,6 +144,17 @@ fn get_dir(path: PathBuf) -> Result<PathBuf, Error> {
}
}

fn write_contextless_file<P: AsRef<Path>>(
env: &Environment,
path: P,
name: &str,
template: &str,
) -> miette::Result<()> {
let rv = env.render_named_str(name, template, ()).into_diagnostic()?;
fs::write(&path, rv).into_diagnostic()?;
Ok(())
}

#[cfg(test)]
mod tests {
use crate::cli::init::get_dir;
Expand Down

0 comments on commit f38415e

Please sign in to comment.