1#![allow(dead_code)]
9
10use std::fs;
11use std::path::PathBuf;
12use std::process::Command;
13use typr_core::components::context::config::Environment;
14
15pub fn get_os_file(file: &str) -> String {
16 if cfg!(windows) {
17 file.replace("\\", r"\")
18 } else {
19 file.to_string()
20 }
21}
22
23pub fn read_file(path: &PathBuf) -> Option<String> {
24 let file = get_os_file(path.to_str().unwrap());
25 match fs::read_to_string(&file) {
26 Ok(res) => Some(res),
27 _ => None,
28 }
29}
30
31pub fn read_file_from_name(name: &str, environment: Environment) -> String {
32 let base = match environment {
33 Environment::StandAlone | Environment::Repl | Environment::Wasm => "",
34 Environment::Project => "TypR/",
35 };
36 let file = get_os_file(&format!("{}{}.ty", base, name));
37 fs::read_to_string(&file).expect(&format!("Can't Read file {}", name))
38}
39
40pub fn execute_r() -> () {
41 match Command::new("Rscript").arg(get_os_file("app.R")).output() {
42 Ok(output) => {
43 let stdout = String::from_utf8_lossy(&output.stdout);
44 let stderr = String::from_utf8_lossy(&output.stderr);
45
46 if output.status.success() {
47 println!("Execution: \n{}", stdout);
48 } else {
49 println!("Error (code {}): \n{}", output.status, stderr);
50 if !stdout.is_empty() {
51 println!("Standard output: \n{}", stdout);
52 }
53 }
54 }
55 Err(e) => {
56 println!("Failed to execute command: {}", e);
57 }
58 }
59}
60
61pub fn execute_r_with_path(execution_path: &PathBuf, file_name: &str) -> () {
62 match Command::new("Rscript")
63 .current_dir(execution_path)
64 .arg(get_os_file(file_name))
65 .output()
66 {
67 Ok(output) => {
68 let stdout = String::from_utf8_lossy(&output.stdout);
69 let stderr = String::from_utf8_lossy(&output.stderr);
70
71 if output.status.success() {
72 println!("Execution: \n{}", stdout);
73 } else {
74 println!("Error (code {}): \n{}", output.status, stderr);
75 if !stdout.is_empty() {
76 println!("Standard output: \n{}", stdout);
77 }
78 }
79 }
80 Err(e) => {
81 println!("Failed to execute command: {}", e);
82 }
83 }
84}
85
86pub fn execute_r_with_path2(execution_path: &PathBuf, file_name: &str) -> String {
87 match Command::new("Rscript")
88 .current_dir(execution_path)
89 .arg(get_os_file(file_name))
90 .output()
91 {
92 Ok(output) => {
93 let stdout = String::from_utf8_lossy(&output.stdout);
94 let stderr = String::from_utf8_lossy(&output.stderr);
95
96 if output.status.success() {
97 format!("{}", stdout)
98 } else {
99 println!("Error (code {}): \n{}", output.status, stderr);
100 if !stdout.is_empty() {
101 format!("Standard output: \n{}", stdout)
102 } else {
103 "".to_string()
104 }
105 }
106 }
107 Err(e) => {
108 format!("Failed to execute command: {}", e)
109 }
110 }
111}
112
113pub fn execute_typescript() -> () {
114 println!("TypeScript compilation: ");
115
116 let tsc_output = Command::new("tsc")
118 .arg("app.ts")
119 .output()
120 .expect("Failed during TypeScript compilation");
121
122 if !tsc_output.status.success() {
123 let stderr = String::from_utf8_lossy(&tsc_output.stderr);
124 println!("TypeScript compilation error: {}", stderr);
125 return;
126 }
127
128 println!("JavaScript execution: ");
129
130 let node_output = Command::new("node")
132 .arg("app.js")
133 .output()
134 .expect("Failed during Node.js execution");
135
136 let stdout = String::from_utf8_lossy(&node_output.stdout);
137 let stderr = String::from_utf8_lossy(&node_output.stderr);
138
139 if !node_output.status.success() {
140 println!("JavaScript execution error: {}", stderr);
141 } else {
142 println!("{}", stdout);
143 if !stderr.is_empty() {
144 println!("Warnings: {}", stderr);
145 }
146 }
147}