-
Notifications
You must be signed in to change notification settings - Fork 81
/
lib.rs
284 lines (260 loc) · 8.84 KB
/
lib.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2019-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use dialoguer::{theme::ColorfulTheme, Confirm, Input, Select};
use std::{ffi::OsString, fs, process::exit};
use crate::{colors::*, package_manager::PackageManager};
mod cli;
mod colors;
mod package_manager;
mod template;
pub mod internal {
//! Re-export of create-tauri-app internals
//!
//! ## Warning
//!
//! This is meant to be used internally only so use at your own risk
//! and expect APIs to break without a prior notice.
pub mod package_manager {
pub use crate::package_manager::*;
}
pub mod template {
pub use crate::template::*;
}
}
pub fn run<I, A>(args: I, bin_name: Option<String>)
where
I: IntoIterator<Item = A>,
A: Into<OsString> + Clone,
{
if let Err(e) = try_run(args, bin_name) {
eprintln!(
"{BOLD}{RED}error{RESET}: {:#}",
e,
BOLD = BOLD,
RED = RED,
RESET = RESET
);
exit(1);
}
}
fn try_run<I, A>(args: I, bin_name: Option<String>) -> anyhow::Result<()>
where
I: IntoIterator<Item = A>,
A: Into<OsString> + Clone,
{
let args = cli::parse(args.into_iter().map(Into::into).collect(), bin_name)?;
let defaults = cli::Args::default();
let skip = args.skip_prompts;
let cwd = std::env::current_dir()?;
// when invoked from pnpm, it seems like pnpm forgets to end its output with a new line
// and it obscures the first question
// this ensures we are on a new line before presenting our prompts
println!();
let project_name = args.project_name.unwrap_or_else(|| {
if skip {
defaults.project_name.unwrap()
} else {
Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("Project name")
.default("tauri-app".into())
.interact_text()
.unwrap()
}
});
let target_dir = cwd.join(&project_name);
let package_name = if is_valid_pkg_name(&project_name) {
project_name.clone()
} else {
let valid_name = to_valid_pkg_name(&project_name);
if skip {
valid_name
} else {
Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("Package name")
.default(valid_name.clone())
.with_initial_text(valid_name)
.validate_with(|input: &String| {
if is_valid_pkg_name(input) {
Ok(())
} else {
Err("Package name should only include alphanumeric character and hyphens \"-\" and doesn't start with numbers")
}
})
.interact_text()?
}
};
if target_dir.exists() && target_dir.read_dir()?.next().is_some() {
let overrwite = if skip {
false
} else {
Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(format!(
"{} directory is not empty, do you want to overwrite?",
if target_dir == cwd {
"Current directory".to_string()
} else {
target_dir
.file_name()
.unwrap()
.to_string_lossy()
.to_string()
}
))
.default(false)
.interact()?
};
if !overrwite {
eprintln!(
"{BOLD}{RED}✘{RESET} Operation Cancelled",
BOLD = BOLD,
RED = RED,
RESET = RESET
);
exit(1);
}
};
let pkg_manager = args.manager.unwrap_or_else(|| {
if skip {
defaults.manager.unwrap()
} else {
let managers = PackageManager::ALL.to_vec();
let managers = args
.template
.map(|t| {
managers
.iter()
.copied()
.filter(|p| p.templates().contains(&t))
.collect::<Vec<_>>()
})
.unwrap_or(managers);
if managers.len() == 1 {
managers[0]
} else {
let index = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Choose your package manager")
.items(&managers)
.default(0)
.interact()
.unwrap();
managers[index]
}
}
});
let templates = pkg_manager.templates();
let template = args.template.unwrap_or_else(|| {
if skip {
defaults.template.unwrap()
} else {
let index = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Choose your UI template")
.items(templates)
.default(0)
.interact()
.unwrap();
templates[index]
}
});
if !templates.contains(&template) {
eprintln!(
"{BOLD}{RED}error{RESET}: the {GREEN}{}{RESET} template is not suppported for the {GREEN}{pkg_manager}{RESET} package manager\n possible templates for {GREEN}{pkg_manager}{RESET} are: [{}]",
template,
templates.iter().map(|e|format!("{GREEN}{}{RESET}", e, GREEN = GREEN, RESET = RESET)).collect::<Vec<_>>().join(", "),
pkg_manager = pkg_manager,
BOLD = BOLD,
RED = RED,
RESET = RESET,
GREEN = GREEN,
);
exit(1);
}
if target_dir.exists() {
// safe to remove, because upon reaching this line, the user accepted to overwrite
fs::remove_dir_all(&target_dir)?
};
fs::create_dir_all(&target_dir)?;
template.render(&target_dir, pkg_manager, &package_name)?;
println!();
println!(
"{ITALIC}{DIM}Please follow{DIMRESET} {BLUE}https://tauri.app/v1/guides/getting-started/prerequisites{WHITE} {DIM}to install the needed prerequisites, if you haven't already.{DIMRESET}{RESET}",
ITALIC = ITALIC,
DIM = DIM,
DIMRESET = DIMRESET,
WHITE = WHITE,
BLUE = BLUE,
RESET=RESET
);
if let Some(info) = template.post_init_info(pkg_manager) {
println!("{}", info);
}
println!();
println!("Done, Now run:");
println!(" cd {}", project_name);
if let Some(cmd) = pkg_manager.install_cmd() {
println!(" {}", cmd);
}
println!(" {} tauri dev", pkg_manager.run_cmd());
println!();
Ok(())
}
fn is_valid_pkg_name(project_name: &str) -> bool {
!project_name
.chars()
.next()
.map(|c| c.is_ascii_digit())
.unwrap_or_default()
&& !project_name
.chars()
.any(|ch| !(ch.is_alphanumeric() || ch == '-' || ch == '_'))
}
fn to_valid_pkg_name(project_name: &str) -> String {
let mut ret = project_name
.trim()
.to_lowercase()
.replace('.', "")
.replace(':', "-")
.replace(';', "-")
.replace(' ', "-")
.replace('\\', "")
.replace('/', "")
.replace('~', "-");
if let Some(ch) = ret.chars().next() {
if ch.is_ascii_digit() || ch == '-' {
ret.remove(0);
}
}
ret
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn valiadtes_pkg_name() {
assert!(is_valid_pkg_name("tauri-app"));
assert!(is_valid_pkg_name("tauri_app"));
assert!(is_valid_pkg_name("t2auriapp"));
assert!(!is_valid_pkg_name("1tauriapp"));
assert!(!is_valid_pkg_name("tauri app"));
assert!(!is_valid_pkg_name("tauri:app"));
assert!(!is_valid_pkg_name("tauri.app"));
assert!(!is_valid_pkg_name("tauri/app"));
assert!(!is_valid_pkg_name("tauri\\app"));
assert!(!is_valid_pkg_name("tauri~app"));
}
#[test]
fn converts_to_valid_pkg_name() {
assert_eq!(to_valid_pkg_name("tauri-app"), "tauri-app");
assert_eq!(to_valid_pkg_name("tauri_app"), "tauri_app");
assert_eq!(to_valid_pkg_name("t2auriapp"), "t2auriapp");
assert_eq!(to_valid_pkg_name("1tauriapp"), "tauriapp");
assert_eq!(to_valid_pkg_name("tauri app"), "tauri-app");
assert_eq!(to_valid_pkg_name("tauri:app"), "tauri-app");
assert_eq!(to_valid_pkg_name("tauri;app"), "tauri-app");
assert_eq!(to_valid_pkg_name("tauri.app"), "tauriapp");
assert_eq!(to_valid_pkg_name("tauri/app"), "tauriapp");
assert_eq!(to_valid_pkg_name("tauri\\app"), "tauriapp");
assert_eq!(to_valid_pkg_name("tauri~app"), "tauri-app");
assert_eq!(to_valid_pkg_name("-tauri.app"), "tauriapp");
}
}