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:
parent
89fd67c45e
commit
08df8e81d4
|
@ -76,6 +76,13 @@ pub enum GenCommand {
|
||||||
|
|
||||||
/// Generate auxiliary code for a language
|
/// Generate auxiliary code for a language
|
||||||
Auxiliary,
|
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)]
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
|
||||||
|
|
|
@ -4,17 +4,28 @@ use trixy::macros::config::trixy::TrixyConfig;
|
||||||
|
|
||||||
use crate::cli::{GenCommand, Language};
|
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")
|
let base_config = TrixyConfig::new("callback_function")
|
||||||
.trixy_path(api_file.to_owned())
|
.trixy_path(api_file.to_owned())
|
||||||
.dist_dir_path("dist")
|
.dist_dir_path("dist")
|
||||||
.out_dir("out/dir");
|
.out_dir_path("out/dir");
|
||||||
|
|
||||||
|
let final_config: TrixyConfig;
|
||||||
match gen_command {
|
match gen_command {
|
||||||
GenCommand::Host => {
|
GenCommand::Host => {
|
||||||
let file_tree = base_config.generate();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let file_tree = final_config.generate();
|
||||||
println!("{}", file_tree);
|
println!("{}", file_tree);
|
||||||
}
|
|
||||||
GenCommand::Auxiliary => todo!(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
|
|
||||||
use std::{env, path::PathBuf};
|
use std::{env, path::PathBuf};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub enum Language {
|
pub enum Language {
|
||||||
Rust,
|
Rust,
|
||||||
C,
|
C,
|
||||||
|
@ -54,6 +54,9 @@ pub struct TrixyConfig {
|
||||||
/// This file is written in $OUT_DIR.
|
/// This file is written in $OUT_DIR.
|
||||||
pub c_header_name: String,
|
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.
|
/// The path from the root to the distribution directory.
|
||||||
/// Things like the c headers are copied in this dir.
|
/// Things like the c headers are copied in this dir.
|
||||||
/// When this is [None] no dist dir will be generated.
|
/// 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.
|
/// 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.
|
/// 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.
|
/// 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 {
|
impl TrixyConfig {
|
||||||
|
@ -86,7 +95,10 @@ impl TrixyConfig {
|
||||||
callback_function: callback_function.into(),
|
callback_function: callback_function.into(),
|
||||||
host_code_name: "api.rs".into(),
|
host_code_name: "api.rs".into(),
|
||||||
c_header_name: "interface.h".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()
|
..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 {
|
pub fn dist_dir_path<T: Into<PathBuf>>(self, dist_dir_path: T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
dist_dir_path: Some(dist_dir_path.into()),
|
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 {
|
Self {
|
||||||
out_dir: out_dir.into(),
|
out_dir_path: out_dir.into(),
|
||||||
..self
|
..self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fs, iter,
|
fs,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -72,40 +72,47 @@ impl TrixyConfig {
|
||||||
});
|
});
|
||||||
|
|
||||||
// host code
|
// host code
|
||||||
|
if self.generate_host {
|
||||||
let host_code = generate::host::generate(&trixy_code, &self);
|
let host_code = generate::host::generate(&trixy_code, &self);
|
||||||
file_tree.add_host_file(GeneratedFile::new_in_out_dir(
|
file_tree.add_host_file(GeneratedFile::new_in_out_dir(
|
||||||
self.host_code_name.clone(),
|
self.host_code_name.clone(),
|
||||||
host_code,
|
host_code,
|
||||||
Language::Rust,
|
Language::Rust,
|
||||||
&self.out_dir,
|
&self.out_dir_path,
|
||||||
));
|
));
|
||||||
|
}
|
||||||
|
|
||||||
// auxiliary code
|
// auxiliary code
|
||||||
|
if self.generate_auxiliary {
|
||||||
let c_header = generate::auxiliary::generate(&trixy_code, &self);
|
let c_header = generate::auxiliary::generate(&trixy_code, &self);
|
||||||
|
|
||||||
if let Some(dist_dir) = &self.dist_dir_path {
|
if let Some(dist_dir) = &self.dist_dir_path {
|
||||||
let c_header_dist =
|
let c_header_dist =
|
||||||
PathBuf::from(format!("{}/{}", dist_dir.display(), &self.c_header_name));
|
PathBuf::from(format!("{}/{}", dist_dir.display(), &self.c_header_name));
|
||||||
file_tree.add_auxiliary_file(GeneratedFile::new(c_header_dist, c_header, Language::C));
|
file_tree.add_auxiliary_file(GeneratedFile::new(
|
||||||
|
c_header_dist,
|
||||||
|
c_header,
|
||||||
|
Language::C,
|
||||||
|
));
|
||||||
|
|
||||||
// TODO(@soispha): Is this even necessary? <2024-03-25>
|
// // TODO(@soispha): Is this even necessary? <2024-03-25>
|
||||||
let (interface_name, interface_content) = {
|
// let (interface_name, interface_content) = {
|
||||||
let interface_header = format!(
|
// let interface_header = format!(
|
||||||
"\
|
// "\
|
||||||
/* This file is automatcially generated by Trixy */ \n\
|
// /* This file is automatcially generated by Trixy */ \n\
|
||||||
#ifndef TRIXY_INTERFACE_H \n\
|
// #ifndef TRIXY_INTERFACE_H \n\
|
||||||
#define TRIXY_INTERFACE_H \n\
|
// #define TRIXY_INTERFACE_H \n\
|
||||||
#include \"{}\" \n\
|
// #include \"{}\" \n\
|
||||||
#endif // TRIXY_INTERFACE_H \n\
|
// #endif // TRIXY_INTERFACE_H \n\
|
||||||
",
|
// ",
|
||||||
&self.c_header_name
|
// &self.c_header_name
|
||||||
);
|
// );
|
||||||
("interface.h", interface_header)
|
// ("interface.h", interface_header)
|
||||||
};
|
// };
|
||||||
|
|
||||||
|
if self.add_c_headers {
|
||||||
C_TYPE_HEADER
|
C_TYPE_HEADER
|
||||||
.iter()
|
.iter()
|
||||||
.chain(iter::once(&(interface_name, &interface_content[..])))
|
// .chain(iter::once(&(interface_name, &interface_content[..])))
|
||||||
.for_each(|(name, content)| {
|
.for_each(|(name, content)| {
|
||||||
let path: &Path = &Path::new(name);
|
let path: &Path = &Path::new(name);
|
||||||
|
|
||||||
|
@ -118,6 +125,9 @@ impl TrixyConfig {
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
file_tree
|
file_tree
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue