Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kallazz committed Oct 6, 2023
1 parent e96ba5c commit 8b70131
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 72 deletions.
18 changes: 8 additions & 10 deletions src/build_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use std::fs;
use crate::StringVector;

const SOURCE_EXTENSIONS: [&str; 2] = ["c", "cpp"];
const SOURCE_EXTENSIONS: [&str; 3] = ["c", "cpp", "java"];
const HEADER_EXTENSIONS: [&str; 2] = ["h", "hpp"];

/// A struct that holds data for generating a Makefile.
Expand Down Expand Up @@ -67,7 +67,7 @@ impl BuildData {
}
else {
new_file.truncate(name_len - 5);
new_file.push_str(".java");
new_file.push_str(".class");
}

output_files.push(new_file);
Expand All @@ -87,12 +87,12 @@ impl BuildData {
/// # Returns
///
/// A `Result` containing the extracted `BuildData` or an error.
pub fn extract_names(paths: fs::ReadDir, allowed_extensions: &[&str]) -> Result<BuildData, Box<dyn std::error::Error>> {
pub fn extract_names(paths: fs::ReadDir) -> Result<BuildData, Box<dyn std::error::Error>> {
let mut files = BuildData::new();

for path_result in paths {
let path = path_result?;
let extension = FileType::get_extension_type(&path, &allowed_extensions);
let extension = FileType::get_extension_type(&path);
let name = path.path().file_name().unwrap().to_str().unwrap().to_string();

match extension {
Expand All @@ -116,16 +116,14 @@ enum FileType {
}

impl FileType {
fn get_extension_type(file_name: &fs::DirEntry, allowed_extensions: &[&str]) -> FileType {
fn get_extension_type(file_name: &fs::DirEntry) -> FileType {
let path = file_name.path();
let extension = path.extension();

match extension {
Some(ext) => {
let ext = &ext.to_str().unwrap();

if !allowed_extensions.contains(ext) { return FileType::Other }

if SOURCE_EXTENSIONS.contains(ext) { return FileType::Source }
else if HEADER_EXTENSIONS.contains(ext) { return FileType::Header }
else { return FileType::Other }
Expand All @@ -143,7 +141,7 @@ mod test {
fn extract_names_no_correct_files() {
let paths = fs::read_dir("./test-dirs/test-extracting-filenames/no-correct-files").unwrap();
let expected = BuildData::new();
let result = BuildData::extract_names(paths, &[".c", ".cpp", ".h", ".hpp"]).unwrap();
let result = BuildData::extract_names(paths).unwrap();

assert_eq!(expected, result);
}
Expand All @@ -160,7 +158,7 @@ mod test {
lflags: String::new(),
ldlibs: String::new(),
};
let result = BuildData::extract_names(paths, &[".c", ".cpp", ".h", ".hpp"]).unwrap();
let result = BuildData::extract_names(paths).unwrap();

assert_eq!(expected, result);
}
Expand All @@ -177,7 +175,7 @@ mod test {
lflags: String::new(),
ldlibs: String::new(),
};
let result = BuildData::extract_names(paths, &[".c", ".cpp", ".h", ".hpp"]).unwrap();
let result = BuildData::extract_names(paths).unwrap();

assert_eq!(expected, result);
}
Expand Down
6 changes: 0 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ pub struct CLI {
/// Represents the available commands for the MakeWiz CLI.
#[derive(Subcommand)]
pub enum Commands {
/// Generate a C Makefile
C,

/// Generate a C++ Makefile
Cpp,

/// Generate a Java Makefile
Java,

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![doc = include_str!("../README.md")]
//#![doc = include_str!("../README.md")]

pub mod cli;
pub mod build_data;
Expand Down
101 changes: 47 additions & 54 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
use makewiz::build_data;
use makewiz::cli::{self, Commands};
use makewiz::user_config::{self, UserConfig};

use clap::Parser;
use directories::ProjectDirs;

use std::fs;
use std::process;

const C_EXTENSIONS: [&str; 2] = ["c", "h"];
const CPP_EXTENSIONS: [&str; 2] = ["cpp", "hpp"];
const JAVA_EXTENSIONS: [&str; 1] = ["java"];

fn main() {
// Set directory where the config file will be placed
let paths_to_files = fs::read_dir(".").unwrap_or_else(|err| {
eprintln!("Error: {}", err);
process::exit(1);
});

let mut file_names = build_data::BuildData::extract_names(paths_to_files).unwrap_or_else(|err| {
eprintln!("Error: {}", err);
process::exit(1);
});

//Set directory where the config file will be placed
let config_dir = ProjectDirs::from("", "", "makewiz")
.expect("Valid home directory path for the config file couldn't be retrieved");
let config_path = config_dir.config_dir();

// If the directory doesn't exist, create it
//If the directory doesn't exist, create it
fs::create_dir_all(config_path).unwrap_or_else(|err| {
eprintln!("Error: {}", err);
process::exit(1);
});


let config_path= config_path.join("config.toml");
// Linux: /home/<username>/.config/makewiz/config.toml
// Windows: C:\Users\<username>\AppData\Roaming\makewiz\config.toml
// macOS: /Users/<username>/Library/Application Support/makewiz/config.toml
let config_path= config_path.join("config.toml");

//Initialize config
let config = UserConfig::get_current_config(&config_path);

//Set config values to later write them to the Makefile
file_names.compiler = config.compiler_name;
file_names.executable = config.executable_name;

// Get user arguments
//Get user arguments
let args = cli::CLI::parse();

// Check if both subcommand and flags are provided
Expand All @@ -39,71 +51,52 @@ fn main() {
std::process::exit(1);
}

// Handle subcommands
let file_extensions = [];
match &args.command {
Some(command) => match command {
Commands::C => {
let file_extensions: [&str; 2] = C_EXTENSIONS;
},
//Handle options
if let Some(executable) = &args.executable {
file_names.executable = executable.clone();
}

Commands::Cpp => {
let file_extensions: [&str; 2] = CPP_EXTENSIONS;
},
if let Some(compiler) = &args.compiler {
file_names.compiler = compiler.clone();
}

//Handle flags
let (lflags, ldlibs) = args.parse_flags();
file_names.lflags = lflags;
file_names.ldlibs = ldlibs;

//Handle subcommands
let mut java = false;
match &args.command {
Some(command) => match command {
Commands::Java => {
let file_extensions: [&str; 1] = JAVA_EXTENSIONS;
java = true;
},

Commands::SetCompiler(compiler) => {
UserConfig::update_config(user_config::Attribute::CompilerName(compiler.name.clone()), &config_path);
return;
},

Commands::SetExecutable(executable) => {
UserConfig::update_config(user_config::Attribute::ExecutableName(executable.name.clone()), &config_path);
return;
},

Commands::Default => {
UserConfig::print_config_values(&config_path);
return;
}
},
None => { }
}

let paths_to_files = fs::read_dir(".").unwrap_or_else(|err| {
eprintln!("Error: {}", err);
process::exit(1);
});

let mut file_names = build_data::BuildData::extract_names(paths_to_files, &file_extensions).unwrap_or_else(|err| {
eprintln!("Error: {}", err);
process::exit(1);
});

//Set config values to later write them to the Makefile
file_names.compiler = config.compiler_name;
file_names.executable = config.executable_name;

// Handle options
if let Some(executable) = &args.executable {
file_names.executable = executable.clone();
}

if let Some(compiler) = &args.compiler {
file_names.compiler = compiler.clone();
}

// Handle flags
let (lflags, ldlibs) = args.parse_flags();
file_names.lflags = lflags;
file_names.ldlibs = ldlibs;

// Don't create the Makefile for the subcommands
if args.subcommands_provided() { std::process::exit(0); }

// Creating the makefile
let makefile = makewiz::generate_makefile(&file_names);
// Create the makefile
let makefile = match java {
true => { makewiz::generate_java_makefile(&file_names) }
false => { makewiz::generate_makefile(&file_names) }
};

fs::write("./Makefile", makefile).expect("Unable to create a Makefile");
println!("Makefile successfully created");
}
}
Empty file.
1 change: 1 addition & 0 deletions test-dirs/test-java-makefile-creation/SecondClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

30 changes: 29 additions & 1 deletion tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod test {
fn makefile_creation() {
let paths_to_files = fs::read_dir("./test-dirs/test-makefile-creation").unwrap();

let mut file_names = build_data::BuildData::extract_names(paths_to_files, &[".c", ".cpp", ".h", ".hpp"]).unwrap();
let mut file_names = build_data::BuildData::extract_names(paths_to_files).unwrap();

let args = vec![String::from("target/debug/makewiz"), String::from("-e"),
String::from("executable"), String::from("-c"), String::from("compiler"),
Expand Down Expand Up @@ -62,4 +62,32 @@ clean:
\trm -f $(OBJS) $(OUT)\n";
assert_eq!(expected, makewiz::generate_makefile(&file_names));
}

#[test]
fn java_makefile_creation() {
let paths_to_files = fs::read_dir("./test-dirs/test-java-makefile-creation").unwrap();

let file_names = build_data::BuildData::extract_names(paths_to_files).unwrap();

let expected = "\
# Compiler and flags
JC = javac
JFLAGS = -g
# Source files and compiled classes
SOURCE = FirstClass.java SecondClass.java
CLASSES = FirstClass.class SecondClass.class
# Default target
default: $(CLASSES)
# Compilation rule
%.class: %.java
\t$(JC) $(JFLAGS) $<
# Clean rule to remove generated .class files
clean:
\trm -f $(CLASSES)\n";
assert_eq!(expected, makewiz::generate_java_makefile(&file_names));
}
}

0 comments on commit 8b70131

Please sign in to comment.