Skip to content

Commit

Permalink
feat: 0.1.4 Add -C flag functionality, improve C header gen (#60)
Browse files Browse the repository at this point in the history
- Add commit info to C header
- Add EXPECTED_AMBOSO_API_LEVEL
- Add --no-color arg to turn off color output
- Fix generated C header being stuck with helapordo
  • Loading branch information
jgabaut authored Dec 15, 2023
1 parent 51f462a commit db96022
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 18 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Changelog

## [0.1.4] - 2023-12-15

### Added

- C header now includes:
- head info for repo (somewhat compliant with original implementation)
- commit message as a string (in extended header section)
- --no-color extension to turn off color output

- New -C flag to pass config file (contents used for config call during autotools prep)

### Fixed

- C header is no longer generating with "helapordo" in some places

### Changed

- Define EXPECTED_AMBOSO_API_LEVEL

- C header is no longer generated with flat 1.9.6

## [0.1.3] - 2023-12-14

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "invil"
description = "A port of amboso to Rust"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
license = "GPL-3.0-only"
homepage = "https://github.com/jgabaut/invil"
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,24 @@
- The original implementation itself does not expect autotools prep for base mode, but it can be done trivially.
- Git mode: full support
- The original implementation itself expects git mode tags to contain a `Makefile` in repo root.
- C header gen: basic support
- The original implementation also prepares git commit info for the header.
- C header gen: complete support (\*)
- The original implementation print time as a pre-formatted string.
- Test mode: complete support (\*)
- Run executable found in test directories
- Handle test macro flag to run on all valid queries
- Record test output with `-b`
- Not compliant with amboso <1.9.7 expectations: missing trailing `$`.
- Passing configure arguments: (\*)
- Amboso 1.9.8 expects -C flag to be passing the arguments directly, not by reading a file.
- Subcommands:
- build Quickly build latest version for current mode
- init Prepare new project with amboso
- version Print invil version

Flags support status:

- [x] Basic env flags: `-D`, `-K`, `-M`, `-S`, `-E`
- [ ] Clock flag: `-C <startTime>`
- [ ] Clock flag: `-Y <startTime>`
- [x] Linter mode: `-x`
- [ ] Lint only: `-l`
- [ ] Report lex: `-L`
Expand All @@ -67,14 +70,17 @@
- [x] Warranty flag: `-W`
- [x] Ignore gitcheck flag: `-X`
- [ ] Silent: `-s`
- [ ] Pass config argument: `-C` (See above)


## Extensions

- [x] `--logged` to output full log to file
- Outputs to `./invil.log`. Not backwards compatible with repos not ignoring the file explicitly.
- [x] `-G` flag also includes a string for build OS.
- From `env::consts::OS`
- [x] `-G` flag also includes:
- a string for build OS (from `env::consts::OS`)
- HEAD commit message
- [x] `--no-color` to disable color output

## See how it behaves <a name = "try_anvil"></a>

Expand Down
31 changes: 31 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub const ANVIL_AUTOMAKE_VERS_KEYNAME: &str = "automakevers";
pub const ANVIL_TESTSDIR_KEYNAME: &str = "tests";
pub const ANVIL_BONEDIR_KEYNAME: &str = "testsdir";
pub const ANVIL_KULPODIR_KEYNAME: &str = "errortestsdir";
pub const EXPECTED_AMBOSO_API_LEVEL: &str = "1.9.7";

#[derive(Parser, Debug, Clone)]
#[command(author, version, about = format!("{} - A simple build tool leveraging make", INVIL_NAME), long_about = format!("{} - A drop-in replacement for amboso", INVIL_NAME), disable_version_flag = true)]
Expand Down Expand Up @@ -149,6 +150,14 @@ pub struct Args {
#[arg(long, default_value = "false")]
pub logged: bool,

/// Disable color output
#[arg(long, default_value = "false")]
pub no_color: bool,

/// Pass configuration argument
#[arg(short = 'C', long, value_name = "CONFIG_ARG")]
pub config: Option<String>,

//TODO: Handle -C flag for passing start time for recursive calls

/// Subcommand
Expand Down Expand Up @@ -202,6 +211,9 @@ pub struct AmbosoEnv {
/// Table with supported versions for git mode and description
pub gitmode_versions_table: BTreeMap<String, String>,

/// String used for configure command argument
pub configure_arg: String,

/// Allow test mode run
pub support_testmode: bool,

Expand Down Expand Up @@ -731,6 +743,7 @@ pub fn parse_stego_toml(stego_path: &PathBuf) -> Result<AmbosoEnv,String> {
do_init : false,
do_purge : false,
start_time: start_time,
configure_arg: "".to_string(),
};
trace!("Toml value: {{{}}}", y);
if let Some(build_table) = y.get("build").and_then(|v| v.as_table()) {
Expand Down Expand Up @@ -1149,8 +1162,26 @@ pub fn check_passed_args(args: &mut Args) -> Result<AmbosoEnv,String> {
do_init : false,
do_purge : false,
start_time: start_time,
configure_arg: "".to_string(),
};

match args.config {
Some (ref x) => {
let config_read_res = fs::read_to_string(x);
match config_read_res {
Ok(config_str) => {
trace!("Read config file: {{{}}}", config_str);
anvil_env.configure_arg = config_str;
}
Err(e) => {
error!("Failed reading config file from {{{}}}. Err: {e}", x);
return Err("Failed reading config file".to_string());
}
}
}
None => {}
}

match args.linter {
Some(ref x) => {
info!("Linter for file: {{{}}}", x.display());
Expand Down
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,29 @@ fn main() -> ExitCode {
.set_thread_mode(ThreadLogMode::Both)
.build();

let color_choice;

match args.no_color {
true => {
color_choice = ColorChoice::Never;
}
false => {
color_choice = ColorChoice::Always;
}
}

match args.logged {
false => {
CombinedLogger::init(
vec![
TermLogger::new(log_level, config, TerminalMode::Mixed, ColorChoice::Always),
TermLogger::new(log_level, config, TerminalMode::Mixed, color_choice),
]
).unwrap();
}
true => {
CombinedLogger::init(
vec![
TermLogger::new(log_level, config.clone(), TerminalMode::Mixed, ColorChoice::Always),
TermLogger::new(log_level, config.clone(), TerminalMode::Mixed, color_choice),
WriteLogger::new(LevelFilter::Trace, config, File::create(INVIL_LOG_FILE).unwrap()),
]
).unwrap();
Expand Down
75 changes: 66 additions & 9 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::core::{Args, AmbosoEnv, AmbosoMode, INVIL_VERSION, INVIL_OS};
use crate::core::{Args, AmbosoEnv, AmbosoMode, INVIL_VERSION, INVIL_OS, EXPECTED_AMBOSO_API_LEVEL};
use std::process::Command;
use std::io::{self, Write};
use std::path::PathBuf;
use is_executable::is_executable;
use std::collections::BTreeMap;
use std::fs::{self, File};
use git2::Repository;

pub fn do_build(env: &AmbosoEnv, args: &Args) -> Result<String,String> {
match args.tag {
Expand Down Expand Up @@ -88,7 +89,7 @@ pub fn do_build(env: &AmbosoEnv, args: &Args) -> Result<String,String> {
} else {
let output = Command::new("sh")
.arg("-c")
.arg(format!("aclocal ; autoconf ; automake --add-missing ; ./configure"))
.arg(format!("aclocal ; autoconf ; automake --add-missing ; ./configure \"{}\"", env.configure_arg))
.output()
.expect("failed to execute process");

Expand Down Expand Up @@ -813,18 +814,68 @@ pub fn run_test(test_path: &PathBuf, record: bool) -> Result<String,String> {
}

pub fn gen_c_header(target_path: &PathBuf, target_tag: &String, bin_name: &String) -> Result<String,String> {
let repo = Repository::discover(target_path);
let mut head_author_name = "".to_string();
let id;
let commit_time;
let mut commit_message = "".to_string();
match repo {
Ok(r) => {
let head = r.head();
match head {
Ok(head) => {
let commit = head.peel_to_commit();
match commit {
Ok(commit) => {
if let Some(msg) = commit.message() {
info!("Commit message: {{{}}}", msg);
commit_message = msg.escape_default().to_string();
}
id = commit.id().to_string();
info!("Commit id: {{{}}}", id);
let author = commit.author();
let name = author.name();
match name {
Some(name) => {
head_author_name = name.to_string();
info!("Commit author: {{{}}}", head_author_name);
}
None => {
warn!("Commit author is empty: {}", head_author_name);
}
}
commit_time = commit.time().seconds();
info!("Commit time: {{{}}}", commit_time);
}
Err(e) => {
error!("Failed peel to head commit for {{{}}}. Err: {e}", target_path.display());
return Err("Failed peel to head commit for repo".to_string());
}
}
}
Err(e) => {
error!("Failed getting head for {{{}}}. Err: {e}", target_path.display());
return Err("Failed getting head for repo".to_string());
}
}
}
Err(e) => {
error!("Failed discovering repo for {{{}}}. Err: {e}", target_path.display());
return Err("Failed discover of repo".to_string());
}
}
let header_path = format!("{}/anvil__{}.h", target_path.display(), bin_name);
trace!("Generating C header. Target path: {{{}}} Tag: {{{}}}", header_path, target_tag);
let output = File::create(header_path);
let header_string = format!("//Generated by invil v{INVIL_VERSION}\n
//Repo at https://github.com/jgabaut/invil\n
#ifndef ANVIL__{bin_name}__\n
#define ANVIL__{bin_name}__\n
static const char ANVIL__API_LEVEL__STRING[] = \"1.9.6\"; /**< Represents amboso version used for [anvil__{bin_name}.h] generated header.*/\n
static const char ANVIL__API_LEVEL__STRING[] = \"{EXPECTED_AMBOSO_API_LEVEL}\"; /**< Represents amboso version used for [anvil__{bin_name}.h] generated header.*/\n
static const char ANVIL__{bin_name}__VERSION_STRING[] = \"{target_tag}\"; /**< Represents current version for [anvil__{bin_name}.h] generated header.*/\n
static const char ANVIL__{bin_name}__VERSION_DESC[] = \"\"; /**< Represents current version info for [anvil__{bin_name}.h] generated header.*/\n
static const char ANVIL__{bin_name}__VERSION_DATE[] = \"\"; /**< Represents date for current version for [anvil__{bin_name}.h] generated header.*/\n
static const char ANVIL__{bin_name}__VERSION_AUTHOR[] = \"\"; /**< Represents author for current version for [anvil__{bin_name}.h] generated header.*/\n
static const char ANVIL__{bin_name}__VERSION_DESC[] = \"{id}\"; /**< Represents current version info for [anvil__{bin_name}.h] generated header.*/\n
static const char ANVIL__{bin_name}__VERSION_DATE[] = \"{commit_time}\"; /**< Represents date for current version for [anvil__{bin_name}.h] generated header.*/\n
static const char ANVIL__{bin_name}__VERSION_AUTHOR[] = \"{head_author_name}\"; /**< Represents author for current version for [anvil__{bin_name}.h] generated header.*/\n
const char *get_ANVIL__API__LEVEL__(void); /**< Returns a version string for amboso API of [anvil__{bin_name}.h] generated header.*/\n
const char *get_ANVIL__VERSION__(void); /**< Returns a version string for [anvil__{bin_name}.h] generated header.*/\n
const char *get_ANVIL__VERSION__DESC__(void); /**< Returns a version info string for [anvil__{bin_name}.h] generated header.*/\n
Expand All @@ -834,8 +885,10 @@ const char *get_ANVIL__VERSION__AUTHOR(void); /**< Returns a version author stri
#define INVIL__{bin_name}__HEADER__
static const char INVIL__VERSION__STRING[] = \"{INVIL_VERSION}\"; /**< Represents invil version used for [anvil__{bin_name}.h] generated header.*/\n
static const char INVIL__OS__STRING[] = \"{INVIL_OS}\"; /**< Represents build os used for [anvil__{bin_name}.h] generated header.*/\n
static const char INVIL__COMMIT__DESC__STRING[] = \"{commit_message}\"; /**< Represents message for HEAD commit used for [anvil__{bin_name}.h] generated header.*/\n
const char *get_INVIL__API__LEVEL__(void); /**< Returns a version string for invil version of [anvil__{bin_name}.h] generated header.*/\n
const char *get_INVIL__OS__(void); /**< Returns a version string for os used for [anvil__{bin_name}.h] generated header.*/\n
const char *get_INVIL__COMMIT__DESC__(void); /**< Returns a string for HEAD commit message used for [anvil__{bin_name}.h] generated header.*/\n
#endif // INVIL__{bin_name}__HEADER__
#endif");
match output {
Expand Down Expand Up @@ -869,21 +922,25 @@ const char *get_ANVIL__API__LEVEL__(void)
}}\n
const char *get_ANVIL__VERSION__DESC__(void)
{{
return ANVIL__helapordo__VERSION_DESC;
return ANVIL__{bin_name}__VERSION_DESC;
}}\n
const char *get_ANVIL__VERSION__DATE__(void)
{{
return ANVIL__helapordo__VERSION_DATE;
return ANVIL__{bin_name}__VERSION_DATE;
}}\n
const char *get_ANVIL__VERSION__AUTHOR__(void)
{{
return ANVIL__helapordo__VERSION_AUTHOR;
return ANVIL__{bin_name}__VERSION_AUTHOR;
}}\n
#ifdef INVIL__{bin_name}__HEADER__
const char *get_INVIL__API__LEVEL__(void)
{{
return INVIL__VERSION__STRING;
}}\n
const char *get_INVIL__COMMIT__DESC__(void)
{{
return INVIL__COMMIT__DESC__STRING;
}}\n
const char *get_INVIL__OS__(void)
{{
return INVIL__OS__STRING;
Expand Down

0 comments on commit db96022

Please sign in to comment.