Skip to content

Commit

Permalink
feat: 支持运行 Python
Browse files Browse the repository at this point in the history
用了一种很狗屎的办法
  • Loading branch information
mslxl committed Dec 3, 2023
1 parent b07f808 commit 65f8965
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 21 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace",
rusqlite = { version = "0.30.0", features = ["bundled"] }
gethostname = "0.4.3"
which = "5.0.0"
toml = "0.8.8"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
36 changes: 18 additions & 18 deletions src-tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ fn gcc(inp: &str, oup: &str) {
.success());
}
fn main() {
// if cfg!(windows) {
// gcc(
// "src-util/consolepauser.windows.cpp",
// "src-util/build/consolepauser",
// )
// } else {
// gcc(
// "src-util/consolepauser.unix.cpp",
// "src-util/build/consolepauser",
// )
// }
// gcc("src-util/ncmp.cpp", "src-util/build/ncmp");
// gcc("src-util/rcmp.cpp", "src-util/build/rcmp");
// gcc("src-util/rcmp4.cpp", "src-util/build/rcmp4");
// gcc("src-util/rcmp6.cpp", "src-util/build/rcmp6");
// gcc("src-util/rcmp9.cpp", "src-util/build/ncmp9");
// gcc("src-util/wcmp.cpp", "src-util/build/wcmp");
// gcc("src-util/yesno.cpp", "src-util/build/yesno");
if cfg!(windows) {
gcc(
"src-util/consolepauser.windows.cpp",
"src-util/build/consolepauser",
)
} else {
gcc(
"src-util/consolepauser.unix.cpp",
"src-util/build/consolepauser",
)
}
gcc("src-util/ncmp.cpp", "src-util/build/ncmp");
gcc("src-util/rcmp.cpp", "src-util/build/rcmp");
gcc("src-util/rcmp4.cpp", "src-util/build/rcmp4");
gcc("src-util/rcmp6.cpp", "src-util/build/rcmp6");
gcc("src-util/rcmp9.cpp", "src-util/build/ncmp9");
gcc("src-util/wcmp.cpp", "src-util/build/wcmp");
gcc("src-util/yesno.cpp", "src-util/build/yesno");
tauri_build::build();
}
4 changes: 2 additions & 2 deletions src-tauri/src/ipc/rt/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::sync::RwLock;

use crate::{ipc::LanguageMode, util::keylock::KeyLock};

use super::gnu_gcc::GNUGccCompiler;
use super::{gnu_gcc::GNUGccCompiler, interpreter::Interpreter};

#[derive(Debug, Serialize, Deserialize)]
pub struct CompileLint {
Expand Down Expand Up @@ -84,7 +84,7 @@ pub async fn compile_source(

let compiler_handle: Box<dyn Compiler> = match mode {
LanguageMode::CXX => Box::new(GNUGccCompiler),
LanguageMode::PY => unimplemented!(),
LanguageMode::PY => Box::new(Interpreter::with_ext(String::from("py"))),
};

let result = compiler_handle
Expand Down
77 changes: 77 additions & 0 deletions src-tauri/src/ipc/rt/interpreter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::{
path::{Path, PathBuf},
process::Stdio,
};

use crate::{ipc::rt::compiler::CompileLint, util::console};

use super::compiler::{CompileResult, Compiler, CompilerOptions};
use anyhow::{Ok, Result};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use tokio::{fs, process::Command};

#[derive(Debug, Serialize, Deserialize)]
pub struct InterpreterStore {
pub interpreter: String,
pub script: String,
pub args: Vec<String>,
}

// It just record its compiler into a config file, not compile it
// it always success
pub struct Interpreter {
ext: String,
}
impl Interpreter {
pub fn with_ext(ext: String) -> Self {
Self { ext }
}
}

#[async_trait]
impl Compiler for Interpreter {
async fn compile(
&self,
source: String,
compiler_options: CompilerOptions,
working_path: PathBuf,
) -> Result<CompileResult> {
let source_file = working_path.join(format!("source.{}", self.ext));
let config_file = working_path.join(format!("source.{}.toml", self.ext));
let compiler = if let Some(compiler) = compiler_options.path {
compiler
} else {
return Ok(CompileResult::Error {
data: vec![CompileLint {
position: Some((0, 0)),
ty: Some(String::from("Error")),
description: String::from("An compiler or interpreter must be specificed"),
}],
});
};
let compiler_args = compiler_options.args.unwrap_or(vec![]);
let config_content = toml::to_string(&InterpreterStore {
interpreter: compiler,
script: source_file.to_str().unwrap().to_owned(),
args: compiler_args,
})
.unwrap();
fs::write(&source_file, source).await?;
fs::write(&config_file, config_content).await?;
Ok(CompileResult::Success {
data: config_file.to_str().unwrap().to_owned(),
})
}
}

pub async fn build_command_from_config<P: AsRef<Path>>(
config_file: P,
) -> Result<std::process::Command> {
let config = tokio::fs::read_to_string(config_file).await?;
let config = toml::from_str::<InterpreterStore>(&config)?;
let mut cmd = std::process::Command::new(&config.interpreter);
cmd.args(&config.args);
cmd.arg(config.script);
Ok(cmd)
}
1 change: 1 addition & 0 deletions src-tauri/src/ipc/rt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod compiler;
pub mod interpreter;
pub mod runner;
pub mod checker;

Expand Down
5 changes: 4 additions & 1 deletion src-tauri/src/ipc/rt/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, Command};
use tokio::sync::Mutex;

use crate::ipc::rt::interpreter::build_command_from_config;
use crate::{
ipc::LanguageMode,
util::{
Expand Down Expand Up @@ -154,7 +155,9 @@ pub async fn run_redirect<R: Runtime>(
let working_dir = PathBuf::from(&exec_target).parent().unwrap().to_path_buf();
let mut cmd = match mode {
LanguageMode::CXX => std::process::Command::new(exec_target),
_ => unimplemented!(),
LanguageMode::PY => build_command_from_config(PathBuf::from(exec_target))
.await
.map_err(|e| e.to_string())?,
};

cmd.current_dir(working_dir);
Expand Down

0 comments on commit 65f8965

Please sign in to comment.