-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: move compiler args from runner to compiler #121
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use crate::core::file_utils::file_utils::get_full_path; | ||
use crate::core::file_utils::file_utils::get_absolute_path; | ||
|
||
#[derive(Debug)] | ||
pub enum Compiler { | ||
|
@@ -16,15 +16,33 @@ impl Compiler { | |
} | ||
|
||
/// Gets the correct arguments to launch the compiler | ||
/// TODO maybe this should also be responsible for adding -o in gcc/g++ | ||
/// Would make it easier to add new compilers without changing `compile_runner` | ||
pub fn args(&self, files: &Vec<std::path::PathBuf>) -> Vec<String> { | ||
pub fn args( | ||
&self, | ||
files: &Vec<std::path::PathBuf>, | ||
output_path: &std::path::PathBuf, | ||
) -> Vec<String> { | ||
match self { | ||
Compiler::Gcc => Compiler::collect_files_with_extension(files, &["c"]), | ||
Compiler::Gxx => Compiler::collect_files_with_extension(files, &["c", "cpp", "cc"]), | ||
Compiler::Gcc => Compiler::gxx_args(files, output_path, &["c"]), | ||
Compiler::Gxx => Compiler::gxx_args(files, output_path, &["c", "cpp", "cc"]), | ||
} | ||
} | ||
|
||
/// Gcc/G++ args generator | ||
fn gxx_args( | ||
files: &Vec<std::path::PathBuf>, | ||
output_path: &std::path::PathBuf, | ||
extensions: &[&str], | ||
) -> Vec<String> { | ||
let path = output_path.to_str().unwrap_or(""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid creating yet another |
||
let mut args = Compiler::collect_files_with_extension(files, extensions); | ||
args.extend([ | ||
String::from("-fdiagnostics-color=always"), | ||
String::from("-o"), | ||
String::from(path), | ||
]); | ||
return args; | ||
} | ||
|
||
/// Collects the files in `files` that have an extension found in `allowed_extensions` | ||
fn collect_files_with_extension( | ||
files: &Vec<std::path::PathBuf>, | ||
|
@@ -34,7 +52,7 @@ impl Compiler { | |
.iter() | ||
.filter_map(|file| { | ||
if Compiler::has_valid_extension(file, allowed_extensions) { | ||
return get_full_path(file).ok(); | ||
return get_absolute_path(file).ok(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure to see why we need to check the path furthermore ? Why does returning |
||
} | ||
None | ||
}) | ||
|
@@ -54,3 +72,136 @@ impl Compiler { | |
false | ||
} | ||
} | ||
#[cfg(test)] | ||
mod tests { | ||
|
||
use std::path::PathBuf; | ||
|
||
use crate::core::file_utils::file_utils::get_absolute_path; | ||
|
||
use super::Compiler; | ||
|
||
#[test] | ||
fn test_collect_files_extension_one_extension() { | ||
let files = vec![ | ||
PathBuf::from("main.c"), | ||
PathBuf::from("queue.c"), | ||
PathBuf::from("queue.h"), | ||
PathBuf::from("queue.cpp"), | ||
]; | ||
let expected = vec![ | ||
String::from( | ||
get_absolute_path(&PathBuf::from("main.c")) | ||
.unwrap() | ||
.to_str() | ||
.unwrap(), | ||
), | ||
Comment on lines
+93
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to refactor these pattern here and below (9 times) to a single line ? Maybe by creating a function |
||
String::from( | ||
get_absolute_path(&PathBuf::from("queue.c")) | ||
.unwrap() | ||
.to_str() | ||
.unwrap(), | ||
), | ||
]; | ||
|
||
let collected = Compiler::collect_files_with_extension(&files, &["c"]); | ||
assert_eq!(expected, collected); | ||
} | ||
|
||
#[test] | ||
fn test_collect_files_extension_multiple_extensions() { | ||
let files = vec![ | ||
PathBuf::from("main.c"), | ||
PathBuf::from("queue.c"), | ||
PathBuf::from("queue.h"), | ||
PathBuf::from("queue.cpp"), | ||
]; | ||
let expected = vec![ | ||
String::from( | ||
get_absolute_path(&PathBuf::from("main.c")) | ||
.unwrap() | ||
.to_str() | ||
.unwrap(), | ||
), | ||
String::from( | ||
get_absolute_path(&PathBuf::from("queue.c")) | ||
.unwrap() | ||
.to_str() | ||
.unwrap(), | ||
), | ||
String::from( | ||
get_absolute_path(&PathBuf::from("queue.cpp")) | ||
.unwrap() | ||
.to_str() | ||
.unwrap(), | ||
), | ||
]; | ||
|
||
let collected = Compiler::collect_files_with_extension(&files, &["c", "cpp"]); | ||
|
||
println!("{:#?}", collected); | ||
assert_eq!(expected, collected); | ||
} | ||
|
||
#[test] | ||
fn test_gxx_args_c() { | ||
let files = vec![ | ||
PathBuf::from("main.c"), | ||
PathBuf::from("queue.c"), | ||
PathBuf::from("queue.h"), | ||
]; | ||
let output_path_string = String::from("target"); | ||
|
||
let expected = vec![ | ||
String::from( | ||
get_absolute_path(&PathBuf::from("main.c")) | ||
.unwrap() | ||
.to_str() | ||
.unwrap(), | ||
), | ||
String::from( | ||
get_absolute_path(&PathBuf::from("queue.c")) | ||
.unwrap() | ||
.to_str() | ||
.unwrap(), | ||
), | ||
String::from("-fdiagnostics-color=always"), | ||
String::from("-o"), | ||
output_path_string.clone(), | ||
]; | ||
|
||
let collected = Compiler::gxx_args(&files, &PathBuf::from(output_path_string), &["c"]); | ||
assert_eq!(expected, collected); | ||
} | ||
#[test] | ||
fn test_gxx_args_cpp() { | ||
let files = vec![ | ||
PathBuf::from("main.c"), | ||
PathBuf::from("queue.cpp"), | ||
PathBuf::from("queue.h"), | ||
]; | ||
let output_path_string = String::from("target"); | ||
|
||
let expected = vec![ | ||
String::from( | ||
get_absolute_path(&PathBuf::from("main.c")) | ||
.unwrap() | ||
.to_str() | ||
.unwrap(), | ||
), | ||
String::from( | ||
get_absolute_path(&PathBuf::from("queue.cpp")) | ||
.unwrap() | ||
.to_str() | ||
.unwrap(), | ||
), | ||
String::from("-fdiagnostics-color=always"), | ||
String::from("-o"), | ||
output_path_string.clone(), | ||
]; | ||
|
||
let collected = | ||
Compiler::gxx_args(&files, &PathBuf::from(output_path_string), &["c", "cpp"]); | ||
assert_eq!(expected, collected); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,10 +31,16 @@ pub fn list_dir_files(dir: &std::path::PathBuf) -> Result<Vec<std::path::PathBuf | |
.collect()) | ||
} | ||
|
||
// From https://stackoverflow.com/a/38384901 | ||
/// Gets the absolute path of path | ||
/// This function checks if the file exists | ||
pub fn get_full_path(path: &std::path::PathBuf) -> Result<std::path::PathBuf, io::Error> { | ||
// From https://stackoverflow.com/a/38384901 | ||
dunce::canonicalize(path) | ||
} | ||
/// Gets the absolute path of path without checking if the path actually exists | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea to avoid useless syscalls |
||
pub fn get_absolute_path(path: &std::path::PathBuf) -> Result<std::path::PathBuf, io::Error> { | ||
std::path::absolute(path) | ||
} | ||
|
||
pub fn current_folder() -> Result<std::path::PathBuf, io::Error> { | ||
std::env::current_dir() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we agree that these paths must be like
introduction/hello-world/main.c
(relative to course folder), because taking theabsolute
path based on current folder (project folder) would not point to real files otherwise (if we took justmain.c
, the path would be invalid) ?Maybe adding a note about this would help future dev.