Support switching file pathes to wsl if file can not be located
This commit is contained in:
parent
4d5a7814f7
commit
eba022bc10
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tool_helper"
|
name = "tool_helper"
|
||||||
version = "0.9.2"
|
version = "0.9.5"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
@ -12,4 +12,5 @@ cdtypes = {path = "../cdtypes"}
|
||||||
colored = "2.0.4"
|
colored = "2.0.4"
|
||||||
envmnt = "0.10.4"
|
envmnt = "0.10.4"
|
||||||
lz4 = "1.24.0"
|
lz4 = "1.24.0"
|
||||||
paste = "1.0.14"
|
paste = "1.0.14"
|
||||||
|
wslpath = {path = "../wslpath"}
|
|
@ -1,6 +1,6 @@
|
||||||
use colored::*;
|
use colored::*;
|
||||||
use envmnt::{ExpandOptions, ExpansionType};
|
use envmnt::{ExpandOptions, ExpansionType};
|
||||||
use std::{boxed::Box, io::{BufRead, BufReader, BufWriter, Read, Write}, path::PathBuf};
|
use std::{boxed::Box, io::{BufRead, BufReader, BufWriter, Read, Write}, path::PathBuf, str::FromStr};
|
||||||
|
|
||||||
pub mod bits;
|
pub mod bits;
|
||||||
pub mod compress;
|
pub mod compress;
|
||||||
|
@ -173,7 +173,7 @@ pub fn path_with_env_from(path: &str) -> PathBuf {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn open_output_file(output_path: &PathBuf) -> Result<BufWriter<std::fs::File>, Error> {
|
pub fn open_output_file(output_path: &PathBuf) -> Result<BufWriter<std::fs::File>, Error> {
|
||||||
Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?))
|
Ok(std::io::BufWriter::new(std::fs::File::create(win_or_wsl_path(output_path)?)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn open_output(output_file: &Option<PathBuf>) -> Result<Output, Error> {
|
pub fn open_output(output_file: &Option<PathBuf>) -> Result<Output, Error> {
|
||||||
|
@ -191,7 +191,7 @@ pub fn open_input(input_file: Option<PathBuf>) -> Result<Input, Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn open_input_file_buffered(input_path: &PathBuf) -> Result<BufferedInputFile, Error> {
|
pub fn open_input_file_buffered(input_path: &PathBuf) -> Result<BufferedInputFile, Error> {
|
||||||
Ok(BufReader::new(std::fs::File::open(input_path)?))
|
Ok(BufReader::new(std::fs::File::open(win_or_wsl_path(input_path)?)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result<String, Error> {
|
pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result<String, Error> {
|
||||||
|
@ -221,14 +221,14 @@ pub fn read_file(file_path: &PathBuf) -> Result<Vec<u8>, Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match std::fs::read(file_path) {
|
match std::fs::read(win_or_wsl_path(file_path)?) {
|
||||||
Ok(data) => Ok(data),
|
Ok(data) => Ok(data),
|
||||||
Err(error) => create_file_read_error(file_path, error),
|
Err(error) => create_file_read_error(file_path, error),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_file_to_string(file_path: &PathBuf) -> Result<String, Error> {
|
pub fn read_file_to_string(file_path: &PathBuf) -> Result<String, Error> {
|
||||||
match std::fs::read_to_string(file_path) {
|
match std::fs::read_to_string(win_or_wsl_path(file_path)?) {
|
||||||
Ok(string) => Ok(string),
|
Ok(string) => Ok(string),
|
||||||
Err(error) => create_file_read_error(file_path, error),
|
Err(error) => create_file_read_error(file_path, error),
|
||||||
}
|
}
|
||||||
|
@ -236,4 +236,18 @@ pub fn read_file_to_string(file_path: &PathBuf) -> Result<String, Error> {
|
||||||
|
|
||||||
fn create_file_read_error<T>(file_path: &PathBuf, error: std::io::Error) -> Result<T, Error> {
|
fn create_file_read_error<T>(file_path: &PathBuf, error: std::io::Error) -> Result<T, Error> {
|
||||||
Err(Error::from_text(format!("Failed reading file {} with error: \"{}\"", file_path.display(), error)))
|
Err(Error::from_text(format!("Failed reading file {} with error: \"{}\"", file_path.display(), error)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn win_or_wsl_path(path: &PathBuf) -> Result<PathBuf, Error> {
|
||||||
|
let path = path.clone();
|
||||||
|
if path.exists() {
|
||||||
|
Ok(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
match path.into_os_string().into_string() {
|
||||||
|
Ok(path) => Ok(PathBuf::from_str(wslpath::force_convert(path).as_str())?),
|
||||||
|
Err(error) => Err(Error::from_text(format!("Failed converting {:?} to string for win/wsl mapping", error)))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "wslpath"
|
name = "wslpath"
|
||||||
version = "1.0.0"
|
version = "1.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
|
|
@ -2,12 +2,15 @@ pub fn convert(path: String) -> String {
|
||||||
replace_drive_letter(convert_slashes(path))
|
replace_drive_letter(convert_slashes(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn force_convert(path: String) -> String {
|
||||||
|
force_replace_drive_letter(convert_slashes(path))
|
||||||
|
}
|
||||||
|
|
||||||
fn convert_slashes(path: String) -> String {
|
fn convert_slashes(path: String) -> String {
|
||||||
path.replace('\\', "/")
|
path.replace('\\', "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
fn force_replace_drive_letter(mut path: String) -> String {
|
||||||
fn replace_drive_letter(mut path: String) -> String {
|
|
||||||
let has_drive_letter = {
|
let has_drive_letter = {
|
||||||
let drive_letter = path.get(0..2);
|
let drive_letter = path.get(0..2);
|
||||||
|
|
||||||
|
@ -39,6 +42,11 @@ fn replace_drive_letter(mut path: String) -> String {
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
fn replace_drive_letter(path: String) -> String {
|
||||||
|
force_replace_drive_letter(path)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
fn replace_drive_letter(path: String) -> String {
|
fn replace_drive_letter(path: String) -> String {
|
||||||
path
|
path
|
||||||
|
|
Loading…
Reference in New Issue