Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set linguist attributes for pixi.lock #265

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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