Skip to content

Commit

Permalink
Refactor rust code structure and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-may committed Mar 1, 2024
1 parent b1c9b49 commit 056f581
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 158 deletions.
160 changes: 3 additions & 157 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,163 +1,9 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use serde::{Deserialize, Serialize};
use std::{env, error::Error, fs, io, path::PathBuf, process::Command };

#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;

const APPDATA_KEY: &str = "APPDATA";
const HOME_KEY: &str = "HOME";
const PROJECT_PATH_WIN: &str = "lernsoftwareDP//App";
const PROJECT_PATH_UNIX: &str = "lernsoftwareDP/App";
const PROGRAM_NAME: &str = "Program.cs";
const DOTNET_RUN_COMMAND: &str = "dotnet run --no-restore";
const DOTNET_NEW_COMMAND: &str = "dotnet new console --use-program-main";

#[derive(Serialize, Deserialize, Debug)]
struct OutputStruct {
status: i32,
stdout: String,
stderr: String,
}

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)),
}
}

#[tauri::command]
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]
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]
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(),
}
}

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(())
}

#[tauri::command]
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]
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);
}
}

// Helper function to process Command output and serialize it to JSON
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())
}
mod project_utils;
mod tauri_commands;
use tauri_commands::{write_file_content, run_prog, get_dotnet_version, get_dotnet_project_path, delete_dotnet_project};

fn main() {
tauri::Builder::default()
Expand Down
78 changes: 78 additions & 0 deletions src-tauri/src/project_utils.rs
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(())
}
88 changes: 88 additions & 0 deletions src-tauri/src/tauri_commands.rs
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(),
}
}
1 change: 0 additions & 1 deletion src/components/Tour.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export const Tour = () => {
disableCloseOnEsc
hideCloseButton
callback={(data) => {
console.log(data);
if (data.index == 0 && data.action == "start") {
}
if (data.index == 1 && data.action == "update") {
Expand Down

0 comments on commit 056f581

Please sign in to comment.