-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathinit.rs
202 lines (174 loc) · 5.73 KB
/
init.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use crate::{config::get_default_author, consts};
use clap::Parser;
use miette::IntoDiagnostic;
use minijinja::{context, Environment};
use rattler_conda_types::Platform;
use std::io::{Error, ErrorKind, Write};
use std::path::Path;
use std::{fs, path::PathBuf};
/// Creates a new project
#[derive(Parser, Debug)]
pub struct Args {
/// Where to place the project (defaults to current path)
#[arg(default_value = ".")]
pub path: PathBuf,
/// Channels to use in the project.
#[arg(short, long = "channel", id = "channel")]
pub channels: Option<Vec<String>>,
/// Platforms that the project supports.
#[arg(short, long = "platform", id = "platform")]
pub platforms: Vec<String>,
}
/// The default channels to use for a new project.
const DEFAULT_CHANNELS: &[&str] = &["conda-forge"];
/// The pixi.toml template
///
/// This uses a template just to simplify the flexibility of emitting it.
const PROJECT_TEMPLATE: &str = r#"[project]
name = "{{ name }}"
version = "{{ version }}"
description = "Add a short description here"
{%- if author %}
authors = ["{{ author[0] }} <{{ author[1] }}>"]
{%- endif %}
channels = [{%- if channels %}"{{ channels|join("\", \"") }}"{%- endif %}]
platforms = ["{{ platforms|join("\", \"") }}"]
[tasks]
[dependencies]
"#;
const GITIGNORE_TEMPLATE: &str = r#"# pixi environments
.pixi
"#;
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 doesn't already exist. We don't want to overwrite it.
if fs::metadata(&manifest_path).map_or(false, |x| x.is_file()) {
miette::bail!("{} already exists", consts::PROJECT_MANIFEST);
}
// Fail silently if it already exists or cannot be created.
fs::create_dir_all(&dir).ok();
// Write pixi.toml
let name = dir
.file_name()
.ok_or_else(|| {
miette::miette!(
"Cannot get file or directory name from the path: {}",
dir.to_string_lossy()
)
})?
.to_string_lossy();
let version = "0.1.0";
let author = get_default_author();
let channels = if let Some(channels) = args.channels {
channels
} else {
DEFAULT_CHANNELS
.iter()
.copied()
.map(ToOwned::to_owned)
.collect()
};
let platforms = if args.platforms.is_empty() {
vec![Platform::current().to_string()]
} else {
args.platforms
};
let rv = env
.render_named_str(
consts::PROJECT_MANIFEST,
PROJECT_TEMPLATE,
context! {
name,
version,
author,
channels,
platforms
},
)
.unwrap();
fs::write(&manifest_path, rv).into_diagnostic()?;
// create a .gitignore if one is missing
create_or_append_file(&gitignore_path, GITIGNORE_TEMPLATE)?;
// create a .gitattributes if one is missing
create_or_append_file(&gitattributes_path, GITATTRIBUTES_TEMPLATE)?;
// Emit success
eprintln!(
"{}Initialized project in {}",
console::style(console::Emoji("✔ ", "")).green(),
dir.display()
);
Ok(())
}
// Checks if string is in file.
// If search string is multiline it will check if any of those lines is in the file.
fn string_in_file(path: &Path, search: &str) -> bool {
let content = fs::read_to_string(path).unwrap_or_default();
search.lines().any(|line| content.contains(line))
}
// When the specific template is not in the file or the file does not exist.
// Make the file and append the template to the file.
fn create_or_append_file(path: &Path, template: &str) -> miette::Result<()> {
if !path.is_file() || !string_in_file(path, template) {
fs::OpenOptions::new()
.append(true)
.create(true)
.open(path)
.into_diagnostic()?
.write_all(template.as_bytes())
.into_diagnostic()?;
}
Ok(())
}
fn get_dir(path: PathBuf) -> Result<PathBuf, Error> {
if path.components().count() == 1 {
Ok(std::env::current_dir().unwrap_or_default().join(path))
} else {
path.canonicalize().map_err(|e| match e.kind() {
ErrorKind::NotFound => Error::new(
ErrorKind::NotFound,
format!(
"Cannot find '{}' please make sure the folder is reachable",
path.to_string_lossy()
),
),
_ => Error::new(
ErrorKind::InvalidInput,
"Cannot canonicalize the given path",
),
})
}
}
#[cfg(test)]
mod tests {
use crate::cli::init::get_dir;
use std::path::PathBuf;
#[test]
fn test_get_name() {
assert_eq!(
get_dir(PathBuf::from(".")).unwrap(),
std::env::current_dir().unwrap()
);
assert_eq!(
get_dir(PathBuf::from("test_folder")).unwrap(),
std::env::current_dir().unwrap().join("test_folder")
);
assert_eq!(
get_dir(std::env::current_dir().unwrap()).unwrap(),
std::env::current_dir().unwrap().canonicalize().unwrap()
);
}
#[test]
fn test_get_name_panic() {
match get_dir(PathBuf::from("invalid/path")) {
Ok(_) => panic!("Expected error, but got OK"),
Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::NotFound),
}
}
}