Skip to content

Commit 6cb176a

Browse files
committed
supports windows path
1 parent c9c80df commit 6cb176a

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

cli/src/bin.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -203,34 +203,39 @@ enum ConfigCommands {
203203

204204
fn rename_plugin(template: String, name: String, path: Option<String>) -> WasmoResult<()> {
205205
let complete_path = match &path {
206-
Some(p) => format!("{}/{}", p, name),
207-
None => format!("./{}", name),
206+
Some(p) => Path::new(p).join(&name),
207+
None => Path::new("./").join(&name),
208208
};
209209

210210
let _ = match &path {
211211
Some(p) => fs::create_dir_all(p),
212212
None => Result::Ok(()),
213213
};
214214

215-
logger::indent_println(format!("<yellow>Write</> plugin to {}", &complete_path));
216-
match std::fs::rename(format!("./{}", template), &complete_path) {
215+
let manifest_dir = std::env::temp_dir();
216+
217+
logger::indent_println(format!("<yellow>Write</> plugin from {} to {}",
218+
&Path::new(&manifest_dir).join(format!("{}", template)).to_string_lossy(),
219+
&complete_path.to_string_lossy()));
220+
221+
match std::fs::rename(Path::new(&manifest_dir).join(format!("{}", template)), &complete_path) {
217222
Ok(()) => {
218-
update_metadata_file(&complete_path.to_string(), &name, &template)?;
223+
update_metadata_file(&complete_path, &name, &template)?;
219224
logger::println("<green>Plugin created</>".to_string());
220225
Ok(())
221226
},
222227
Err(e) => Err(WasmoError::PluginCreationFailed(e.to_string())),
223228
}
224229
}
225230

226-
fn update_metadata_file(path: &String, name: &String, template: &String) -> WasmoResult<()> {
231+
fn update_metadata_file(path: &PathBuf, name: &String, template: &String) -> WasmoResult<()> {
227232
let metadata_file = match template.as_str() {
228233
"go" => "go.mod",
229234
"rust" => "cargo.toml",
230235
_ => "package.json"
231236
};
232237

233-
let complete_path = format!("{}/{}", path, metadata_file);
238+
let complete_path = path.join(metadata_file);
234239

235240
let content = match fs::read_to_string(&complete_path) {
236241
Err(err) => return Err(WasmoError::FileSystem(err.to_string())),
@@ -275,7 +280,7 @@ fn initialize(template: String, name: String, path: Option<String>) -> WasmoResu
275280
logger::indent_println("<yellow>Unzipping</> the template ...".to_string());
276281
let zip_action = zip_extensions::read::zip_extract(
277282
&PathBuf::from(zip_path),
278-
&PathBuf::from("./".to_string()),
283+
&manifest_dir,
279284
);
280285

281286
match zip_action {
@@ -317,11 +322,11 @@ fn format(str: Option<String>) -> std::collections::HashMap<String, String> {
317322
}
318323
}
319324

320-
fn configuration_file_to_hashmap(configuration_path: String) -> HashMap<String, String> {
325+
fn configuration_file_to_hashmap(configuration_path: &PathBuf) -> HashMap<String, String> {
321326
let complete_path = if configuration_path.ends_with(".wasmo") {
322-
configuration_path
327+
configuration_path.clone()
323328
} else {
324-
format!("{}/.wasmo", &configuration_path)
329+
Path::new(configuration_path).join(".wasmo")
325330
};
326331

327332
match std::path::Path::new(&complete_path).exists() {
@@ -338,15 +343,15 @@ fn read_configuration() -> WasmoResult<HashMap<String, String>> {
338343
let wasmo_server = option_env!("WASMO_SERVER");
339344

340345
let envs: HashMap<String, String> = if wasmo_server.is_none() || wasmo_server.is_none() {
341-
let configuration_path = match option_env!("WASMO_PATH") {
342-
Some(path) => path.to_owned(),
343-
None => get_home()?,
346+
let configuration_path: PathBuf = match option_env!("WASMO_PATH") {
347+
Some(path) => Path::new(path).to_path_buf(),
348+
None => get_home().unwrap(),
344349
};
345350

346-
let envs = configuration_file_to_hashmap(format!("{}/.wasmo", configuration_path));
351+
let envs = configuration_file_to_hashmap(&configuration_path.join(".wasmo"));
347352

348353
match envs.get("WASMO_PATH") {
349-
Some(p) if !p.is_empty() => configuration_file_to_hashmap(format!("{}/.wasmo", p)),
354+
Some(p) if !p.is_empty() => configuration_file_to_hashmap(&Path::new(p).join(".wasmo")),
350355
_ => envs,
351356
}
352357
} else {
@@ -523,7 +528,7 @@ fn reset_configuration() -> WasmoResult<()> {
523528
logger::loading("<yellow>Reset</> configuration".to_string());
524529
let home_path = get_home()?;
525530

526-
let complete_path = format!("{}/.wasmo", &home_path);
531+
let complete_path = home_path.join(".wasmo");
527532

528533
let _ = fs::remove_file(complete_path);
529534

@@ -539,9 +544,9 @@ fn get_option_home() -> String {
539544
}
540545
}
541546

542-
fn get_home() -> WasmoResult<String> {
547+
fn get_home() -> WasmoResult<PathBuf> {
543548
match dirs::home_dir() {
544-
Some(p) => Ok(p.into_os_string().into_string().unwrap()),
549+
Some(p) => Ok(p),
545550
None => Err(WasmoError::FileSystem(format!("Impossible to get your home dir!"))),
546551
}
547552
}
@@ -585,8 +590,8 @@ fn set_configuration(token: Option<String>, server: Option<String>, path: Option
585590
let home_path = get_home()?;
586591

587592
let complete_path = match &path {
588-
Some(p) => format!("{}/.wasmo", p),
589-
None => format!("{}/.wasmo", &home_path),
593+
Some(p) => Path::new(p).join(".wasmo"),
594+
None => home_path.join(".wasmo"),
590595
};
591596

592597
let contents = match std::path::Path::new(&complete_path).exists() {
@@ -596,7 +601,7 @@ fn set_configuration(token: Option<String>, server: Option<String>, path: Option
596601
}
597602
format(None)
598603
}
599-
true => configuration_file_to_hashmap(complete_path.to_string()),
604+
true => configuration_file_to_hashmap(&complete_path),
600605
};
601606

602607
let wasmo_token = extract_variable_or_default(&contents, WASMO_TOKEN, token);
@@ -610,7 +615,7 @@ fn set_configuration(token: Option<String>, server: Option<String>, path: Option
610615
wasmo_server,
611616
wasmo_docker_authorization);
612617

613-
match fs::write(format!("{}/.wasmo", &home_path), new_content) {
618+
match fs::write(home_path.join(".wasmo"), new_content) {
614619
Ok(()) => logger::println(format!("wasmo configuration patched")),
615620
Err(e) => panic!(
616621
"Should have been able to write the wasmo configuration, {:#?}",
@@ -624,7 +629,7 @@ fn set_configuration(token: Option<String>, server: Option<String>, path: Option
624629
);
625630
let content_at_default_path = format!("WASMO_PATH={}", wasmo_path);
626631

627-
let home_file = format!("{}/.wasmo", &home_path);
632+
let home_file = home_path.join(".wasmo");
628633

629634
// println!("Write in home {} - {}", format!("{}/.wasmo", &home_path), content_at_default_path);
630635
let _ = fs::remove_file(&home_file);
@@ -637,12 +642,15 @@ fn set_configuration(token: Option<String>, server: Option<String>, path: Option
637642
}
638643

639644
let project_file = match &path {
640-
Some(p) => format!("{}/.wasmo", p),
645+
Some(p) => Path::new(p).join(".wasmo"),
641646
None => match wasmo_path.is_empty() {
642-
true => format!("{}/.wasmo", &home_path),
643-
false => format!("{}/.wasmo", &wasmo_path)
644-
}.replace(".wasmo.wasmo", ".wasmo") // guard
645-
};
647+
true => home_path.join(".wasmo"),
648+
false => Path::new(&wasmo_path).join(".wasmo")
649+
}
650+
}
651+
.to_string_lossy()
652+
.to_string()
653+
.replace(".wasmo.wasmo", ".wasmo"); // guard
646654

647655
// println!("Write inside project, {} - {:#?}", project_file, content_at_path);
648656
let _ = fs::remove_file(&project_file);

0 commit comments

Comments
 (0)