-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor rust code structure and remove unused code
- Loading branch information
Showing
4 changed files
with
169 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::{env, error::Error, io, path::PathBuf, process::Command }; | ||
use tauri_commands::DOTNET_NEW_COMMAND; | ||
|
||
#[cfg(target_os = "windows")] | ||
use std::os::windows::process::CommandExt; | ||
|
||
use crate::tauri_commands; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
struct OutputStruct { | ||
status: i32, | ||
stdout: String, | ||
stderr: String, | ||
} | ||
|
||
const APPDATA_KEY: &str = "APPDATA"; | ||
const HOME_KEY: &str = "HOME"; | ||
const PROJECT_PATH_WIN: &str = "lernsoftwareDP//App"; | ||
const PROJECT_PATH_UNIX: &str = "lernsoftwareDP/App"; | ||
|
||
pub fn get_appdata_dir() -> Result<PathBuf, Box<dyn Error>> { | ||
let appdata_dir = if cfg!(target_os = "windows") { | ||
env::var(APPDATA_KEY) | ||
} else { | ||
env::var(HOME_KEY) | ||
}; | ||
|
||
match appdata_dir { | ||
Ok(dir) => { | ||
if cfg!(target_os = "windows") { | ||
Ok(PathBuf::from(dir).join(PROJECT_PATH_WIN)) | ||
} else { | ||
Ok(PathBuf::from(dir).join(PROJECT_PATH_UNIX)) | ||
} | ||
} | ||
Err(e) => Err(Box::new(e)), | ||
} | ||
} | ||
|
||
// Helper function to process Command output and serialize it to JSON | ||
pub fn process_output(output: std::process::Output) -> Result<String, String> { | ||
let result = OutputStruct { | ||
status: output.status.code().unwrap_or_default(), | ||
stdout: String::from_utf8_lossy(&output.stdout).to_string(), | ||
stderr: String::from_utf8_lossy(&output.stderr).to_string(), | ||
}; | ||
|
||
serde_json::to_string(&result).map_err(|e| e.to_string()) | ||
} | ||
|
||
pub fn create_dotnet_project(appdata_dir: &PathBuf) -> io::Result<()> { | ||
#[cfg(target_os = "windows")] | ||
{ | ||
let mut command = Command::new("cmd"); | ||
command.args(&["/C", &format!("cd {} && {}", appdata_dir.display(), DOTNET_NEW_COMMAND)]); | ||
command.creation_flags(0x08000000); // CREATE_NO_WINDOW | ||
let status = command.status()?; | ||
|
||
if !status.success() { | ||
return Err(io::Error::new(io::ErrorKind::Other, "Failed to create .NET project")); | ||
} | ||
} | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
{ | ||
let status = Command::new("sh") | ||
.arg("-c") | ||
.arg(&format!("cd {} && {}", appdata_dir.display(), DOTNET_NEW_COMMAND)) | ||
.status()?; | ||
|
||
if !status.success() { | ||
return Err(io::Error::new(io::ErrorKind::Other, "Failed to create .NET project")); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::{ fs, path::PathBuf, process::Command }; | ||
use crate::project_utils::{get_appdata_dir, process_output, create_dotnet_project}; | ||
|
||
#[cfg(target_os = "windows")] | ||
use std::os::windows::process::CommandExt; | ||
|
||
const PROGRAM_NAME: &str = "Program.cs"; | ||
const DOTNET_RUN_COMMAND: &str = "dotnet run --no-restore"; | ||
pub const DOTNET_NEW_COMMAND: &str = "dotnet new console --use-program-main"; | ||
|
||
#[tauri::command] | ||
pub fn get_dotnet_project_path() -> Result<String, String> { | ||
let appdata_dir: PathBuf = get_appdata_dir().map_err(|e| e.to_string())?; | ||
Ok(appdata_dir.display().to_string()) | ||
} | ||
|
||
#[tauri::command] | ||
pub fn delete_dotnet_project() -> Result<(), String> { | ||
let appdata_dir = get_appdata_dir().map_err(|e| e.to_string())?; | ||
fs::remove_dir_all(&appdata_dir).map_err(|e| e.to_string())?; | ||
Ok(()) | ||
} | ||
|
||
#[tauri::command] | ||
pub fn write_file_content(code: &str) -> Result<(), String> { | ||
let appdata_dir = get_appdata_dir().map_err(|e| e.to_string())?; | ||
fs::create_dir_all(&appdata_dir).map_err(|e| e.to_string())?; | ||
let project_exists = appdata_dir.join(PROGRAM_NAME).exists(); | ||
if !project_exists { | ||
create_dotnet_project(&appdata_dir).map_err(|e| e.to_string())?; | ||
} | ||
fs::write(appdata_dir.join(PROGRAM_NAME), code).map_err(|e| e.to_string())?; | ||
Ok(()) | ||
} | ||
|
||
#[tauri::command] | ||
pub fn run_prog() -> Result<String, String> { | ||
let appdata_dir = get_appdata_dir().map_err(|e| e.to_string())?; | ||
|
||
#[cfg(target_os = "windows")] | ||
{ | ||
let output = Command::new("cmd") | ||
.args(&[ | ||
"/C", | ||
&format!("cd {} && {}", appdata_dir.display(), DOTNET_RUN_COMMAND), | ||
]) | ||
.creation_flags(0x08000000) // CREATE_NO_WINDOW | ||
.output() | ||
.map_err(|e| e.to_string())?; | ||
|
||
return process_output(output); | ||
} | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
{ | ||
let output = Command::new("sh") | ||
.arg("-c") | ||
.arg(&format!("cd {} && {}", appdata_dir.display(), DOTNET_RUN_COMMAND)) | ||
.output() | ||
.map_err(|e| e.to_string())?; | ||
|
||
return process_output(output); | ||
} | ||
} | ||
|
||
#[tauri::command] | ||
pub fn get_dotnet_version() -> String { | ||
let mut command = Command::new("dotnet"); | ||
command.arg("--version"); | ||
|
||
// Set creation flags only on Windows to prevent opening a new window | ||
#[cfg(target_os = "windows")] | ||
command.creation_flags(0x08000000); // CREATE_NO_WINDOW | ||
|
||
let output = command.output(); | ||
|
||
match output { | ||
Ok(output) => { | ||
// Check if the command executed successfully | ||
if output.status.success() { | ||
String::from_utf8_lossy(&output.stdout).trim().to_string() | ||
} else { | ||
"Bitte installieren Sie ein .NET SDK >= 6".to_string() | ||
} | ||
}, | ||
Err(_) => "Bitte installieren Sie ein .NET SDK >= 6".to_string(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters