fix(bin/generate): Actually only generate what was specified

The binary did before generate both host and auxiliary files, even when
only one is specified.
This commit is contained in:
Benedikt Peetz 2024-03-26 16:36:03 +01:00
parent 89fd67c45e
commit 08df8e81d4
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
4 changed files with 111 additions and 51 deletions

View File

@ -76,6 +76,13 @@ pub enum GenCommand {
/// Generate auxiliary code for a language
Auxiliary,
/// Generate all code for every language
All {
#[arg(short, long, default_value_t=true)]
/// Don't generate vendored files (like c headers)
no_vendored: bool,
},
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]

View File

@ -4,17 +4,28 @@ use trixy::macros::config::trixy::TrixyConfig;
use crate::cli::{GenCommand, Language};
pub fn handle(gen_command: GenCommand, language: Language, api_file: &Path) {
pub fn handle(gen_command: GenCommand, _language: Language, api_file: &Path) {
let base_config = TrixyConfig::new("callback_function")
.trixy_path(api_file.to_owned())
.dist_dir_path("dist")
.out_dir("out/dir");
.out_dir_path("out/dir");
let final_config: TrixyConfig;
match gen_command {
GenCommand::Host => {
let file_tree = base_config.generate();
println!("{}", file_tree);
final_config = base_config.generate_auxiliary(false).generate_host(true);
}
GenCommand::Auxiliary => {
final_config = base_config.generate_auxiliary(true).generate_host(false);
}
GenCommand::All { no_vendored } => {
final_config = base_config
.generate_auxiliary(true)
.generate_host(true)
.add_c_headers(!no_vendored);
}
GenCommand::Auxiliary => todo!(),
}
let file_tree = final_config.generate();
println!("{}", file_tree);
}

View File

@ -33,7 +33,7 @@
use std::{env, path::PathBuf};
#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum Language {
Rust,
C,
@ -54,6 +54,9 @@ pub struct TrixyConfig {
/// This file is written in $OUT_DIR.
pub c_header_name: String,
/// Whether to add the pre-written c headers to the output
pub add_c_headers: bool,
/// The path from the root to the distribution directory.
/// Things like the c headers are copied in this dir.
/// When this is [None] no dist dir will be generated.
@ -69,7 +72,13 @@ pub struct TrixyConfig {
/// This path is used to place the outputted host code files.
/// Normally this would be the `$OUT_DIR` environment variable set by cargo at build time.
/// You have to set this, if you want to use trixy in an other context.
pub out_dir: PathBuf,
pub out_dir_path: PathBuf,
/// Generate auxiliary files
pub generate_auxiliary: bool,
/// Generate host files
pub generate_host: bool,
}
impl TrixyConfig {
@ -86,7 +95,10 @@ impl TrixyConfig {
callback_function: callback_function.into(),
host_code_name: "api.rs".into(),
c_header_name: "interface.h".into(),
out_dir,
out_dir_path: out_dir,
add_c_headers: true,
generate_auxiliary: true,
generate_host: true,
..Default::default()
}
}
@ -98,6 +110,26 @@ impl TrixyConfig {
}
}
pub fn add_c_headers<T: Into<bool>>(self, add_c_headers: T) -> Self {
Self {
add_c_headers: add_c_headers.into(),
..self
}
}
pub fn generate_host<T: Into<bool>>(self, host: T) -> Self {
Self {
generate_host: host.into(),
..self
}
}
pub fn generate_auxiliary<T: Into<bool>>(self, auxiliary: T) -> Self {
Self {
generate_auxiliary: auxiliary.into(),
..self
}
}
pub fn dist_dir_path<T: Into<PathBuf>>(self, dist_dir_path: T) -> Self {
Self {
dist_dir_path: Some(dist_dir_path.into()),
@ -112,9 +144,9 @@ impl TrixyConfig {
}
}
pub fn out_dir<T: Into<PathBuf>>(self, out_dir: T) -> Self {
pub fn out_dir_path<T: Into<PathBuf>>(self, out_dir: T) -> Self {
Self {
out_dir: out_dir.into(),
out_dir_path: out_dir.into(),
..self
}
}

View File

@ -20,7 +20,7 @@
*/
use std::{
fs, iter,
fs,
path::{Path, PathBuf},
};
@ -72,52 +72,62 @@ impl TrixyConfig {
});
// host code
let host_code = generate::host::generate(&trixy_code, &self);
file_tree.add_host_file(GeneratedFile::new_in_out_dir(
self.host_code_name.clone(),
host_code,
Language::Rust,
&self.out_dir,
));
if self.generate_host {
let host_code = generate::host::generate(&trixy_code, &self);
file_tree.add_host_file(GeneratedFile::new_in_out_dir(
self.host_code_name.clone(),
host_code,
Language::Rust,
&self.out_dir_path,
));
}
// auxiliary code
let c_header = generate::auxiliary::generate(&trixy_code, &self);
if self.generate_auxiliary {
let c_header = generate::auxiliary::generate(&trixy_code, &self);
if let Some(dist_dir) = &self.dist_dir_path {
let c_header_dist =
PathBuf::from(format!("{}/{}", dist_dir.display(), &self.c_header_name));
file_tree.add_auxiliary_file(GeneratedFile::new(
c_header_dist,
c_header,
Language::C,
));
if let Some(dist_dir) = &self.dist_dir_path {
let c_header_dist =
PathBuf::from(format!("{}/{}", dist_dir.display(), &self.c_header_name));
file_tree.add_auxiliary_file(GeneratedFile::new(c_header_dist, c_header, Language::C));
// // TODO(@soispha): Is this even necessary? <2024-03-25>
// let (interface_name, interface_content) = {
// let interface_header = format!(
// "\
// /* This file is automatcially generated by Trixy */ \n\
// #ifndef TRIXY_INTERFACE_H \n\
// #define TRIXY_INTERFACE_H \n\
// #include \"{}\" \n\
// #endif // TRIXY_INTERFACE_H \n\
// ",
// &self.c_header_name
// );
// ("interface.h", interface_header)
// };
// TODO(@soispha): Is this even necessary? <2024-03-25>
let (interface_name, interface_content) = {
let interface_header = format!(
"\
/* This file is automatcially generated by Trixy */ \n\
#ifndef TRIXY_INTERFACE_H \n\
#define TRIXY_INTERFACE_H \n\
#include \"{}\" \n\
#endif // TRIXY_INTERFACE_H \n\
",
&self.c_header_name
);
("interface.h", interface_header)
};
if self.add_c_headers {
C_TYPE_HEADER
.iter()
// .chain(iter::once(&(interface_name, &interface_content[..])))
.for_each(|(name, content)| {
let path: &Path = &Path::new(name);
C_TYPE_HEADER
.iter()
.chain(iter::once(&(interface_name, &interface_content[..])))
.for_each(|(name, content)| {
let path: &Path = &Path::new(name);
let header_path =
PathBuf::from(format!("{}/{}", dist_dir.display(), path.display()));
file_tree.add_auxiliary_file(GeneratedFile::new(
header_path,
content.to_string(),
Language::C,
));
});
let header_path =
PathBuf::from(format!("{}/{}", dist_dir.display(), path.display()));
file_tree.add_auxiliary_file(GeneratedFile::new(
header_path,
content.to_string(),
Language::C,
));
});
}
}
}
file_tree
}
}