Skip to content

Commit

Permalink
support for --just in dev and build (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson authored Nov 21, 2023
1 parent d40bbd9 commit 4491221
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 15 deletions.
51 changes: 48 additions & 3 deletions bin/src/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use clap::{ArgAction, ArgMatches, Command};

use crate::{
context::{self, Context},
error::Error,
executor::Executor,
modules::{pbo::Collapse, Binarize, Files, Hooks, Lint, Rapifier},
};

#[must_use]
pub fn cli() -> Command {
add_args(
add_just(add_args(
Command::new("build")
.about("Build the project for final testing")
.long_about(
"Build your project in release mode for testing, without signing for full release.",
),
)
))
}

#[must_use]
Expand All @@ -33,7 +34,51 @@ pub fn add_args(cmd: Command) -> Command {
)
}

/// Execute the build command
#[must_use]
pub fn add_just(cmd: Command) -> Command {
cmd.arg(
clap::Arg::new("just")
.long("just")
.help("Only build the given addon")
.action(ArgAction::Append),
)
}

/// Execute the build command, build a new executor
///
/// # Errors
/// [`Error`] depending on the modules
pub fn pre_execute(matches: &ArgMatches) -> Result<(), Error> {
let just = matches
.get_many::<String>("just")
.unwrap_or_default()
.map(std::string::String::as_str)
.collect::<Vec<_>>();
let mut ctx = Context::new(
std::env::current_dir()?,
"build",
if just.is_empty() {
context::PreservePrevious::Remove
} else {
warn!("keeping previous build artifacts");
context::PreservePrevious::Keep
},
)?;
if !just.is_empty() {
ctx = ctx.filter(|a, _| just.contains(&a.name()));
}
let mut executor = Executor::new(&ctx);

execute(matches, &mut executor)?;

if !just.is_empty() {
warn!("Use of `--just` is not recommended, only use it if you know what you're doing");
}

Ok(())
}

/// Execute the build command, with a given executor
///
/// # Errors
/// [`Error`] depending on the modules
Expand Down
31 changes: 28 additions & 3 deletions bin/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use crate::{
modules::{pbo::Collapse, Binarize, FilePatching, Files, Hooks, Lint, Rapifier},
};

use super::build::add_just;

#[must_use]
pub fn cli() -> Command {
add_args(
add_just(add_args(
Command::new("dev")
.about("Build the project for development")
.long_about("Build your project for local development and testing. It is built without binarization of .p3d and .rtm files."),
)
))
}

#[must_use]
Expand Down Expand Up @@ -54,7 +56,26 @@ pub fn execute(matches: &ArgMatches, launch_optionals: &[String]) -> Result<Cont
.map(std::string::String::as_str)
.collect::<Vec<_>>();

let ctx = Context::new(std::env::current_dir()?, "dev")?.filter(|a, config| {
let just = matches
.get_many::<String>("just")
.unwrap_or_default()
.map(std::string::String::as_str)
.collect::<Vec<_>>();

let ctx = Context::new(
std::env::current_dir()?,
"dev",
if just.is_empty() {
crate::context::PreservePrevious::Remove
} else {
warn!("keeping previous build artifacts");
crate::context::PreservePrevious::Keep
},
)?
.filter(|a, config| {
if !just.is_empty() && !just.contains(&a.name()) {
return false;
}
if launch_optionals.iter().any(|o| o == a.name()) {
return true;
}
Expand Down Expand Up @@ -96,5 +117,9 @@ pub fn execute(matches: &ArgMatches, launch_optionals: &[String]) -> Result<Cont
executor.check()?;
executor.build()?;

if !just.is_empty() {
warn!("Use of `--just` is not recommended, only use it if you know what you're doing");
}

Ok(ctx)
}
6 changes: 5 additions & 1 deletion bin/src/commands/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ pub fn cli() -> Command {
/// # Errors
/// [`Error`] depending on the modules
pub fn execute(matches: &ArgMatches) -> Result<(), Error> {
let ctx = Context::new(std::env::current_dir()?, "release")?;
let ctx = Context::new(
std::env::current_dir()?,
"release",
crate::context::PreservePrevious::Remove,
)?;
let mut executor = Executor::new(&ctx);

if matches.get_one::<bool>("no-sign") != Some(&true) && ctx.config().hemtt().release().sign() {
Expand Down
7 changes: 6 additions & 1 deletion bin/src/commands/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ pub fn execute(matches: &ArgMatches) -> Result<(), Error> {
.map(std::string::String::as_str)
.collect::<Vec<_>>();

let ctx = Context::new(std::env::current_dir()?, "script")?.filter(|a, config| {
let ctx = Context::new(
std::env::current_dir()?,
"script",
crate::context::PreservePrevious::Remove,
)?
.filter(|a, config| {
if a.location() == &Location::Optionals && !all_optionals && !optionals.contains(&a.name())
{
debug!("ignoring optional {}", a.name());
Expand Down
17 changes: 15 additions & 2 deletions bin/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ use hemtt_common::workspace::{Workspace, WorkspacePath};

use crate::{addons::Addon, error::Error};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Should the current contents of .hemttout\{} be preserved
pub enum PreservePrevious {
/// The contents should be removed
Remove,
/// The contents should be preserved
Keep,
}

#[derive(Debug, Clone)]
pub struct Context {
config: ProjectConfig,
Expand All @@ -33,7 +42,11 @@ impl Context {
///
/// # Panics
/// If the project folder is not a valid [`OsStr`] (UTF-8)
pub fn new(root: PathBuf, folder: &str) -> Result<Self, Error> {
pub fn new(
root: PathBuf,
folder: &str,
preserve_previous: PreservePrevious,
) -> Result<Self, Error> {
let config = {
let path = root.join(".hemtt").join("project.toml");
if !path.exists() {
Expand Down Expand Up @@ -66,7 +79,7 @@ impl Context {
std::fs::File::create(out_folder.join("ci_annotation.txt"))?;
let build_folder = out_folder.join(folder);
trace!("using build folder: {:?}", build_folder.display());
if build_folder.exists() {
if preserve_previous == PreservePrevious::Remove && build_folder.exists() {
remove_dir_all(&build_folder)?;
}
create_dir_all(&build_folder)?;
Expand Down
5 changes: 1 addition & 4 deletions bin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use clap::{ArgAction, ArgMatches, Command};
use context::Context;
pub use error::Error;

#[macro_use]
Expand Down Expand Up @@ -115,9 +114,7 @@ pub fn execute(matches: &ArgMatches) -> Result<(), Error> {
Ok(())
}
Some(("build", matches)) => {
let ctx = Context::new(std::env::current_dir()?, "build")?;
let mut executor = executor::Executor::new(&ctx);
commands::build::execute(matches, &mut executor).map_err(std::convert::Into::into)
commands::build::pre_execute(matches).map_err(std::convert::Into::into)
}
Some(("release", matches)) => {
commands::release::execute(matches).map_err(std::convert::Into::into)
Expand Down
15 changes: 15 additions & 0 deletions bin/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ use crate::error::Error;
pub fn create_link(link: &PathBuf, target: &PathBuf) -> Result<(), Error> {
use std::process::Command;

if link.exists() {
warn!(
"link {:?} already exists, intended to point to {:?}",
link, target
);
return Ok(());
}

// attempt junction
trace!("junction link {:?} => {:?}", link, target);
let mut out = Command::new("cmd")
Expand Down Expand Up @@ -49,6 +57,13 @@ pub fn create_link(link: &PathBuf, target: &PathBuf) -> Result<(), Error> {
/// # Errors
/// - [`std::io::Error`] if the link could not be created
pub fn create_link(link: &PathBuf, target: &PathBuf) -> Result<(), Error> {
if link.exists() {
warn!(
"link {:?} already exists, intended to point to {:?}",
link, target
);
return Ok(());
}
trace!("symlink {:?} => {:?}", link, target);
std::os::unix::fs::symlink(target, link)?;
Ok(())
Expand Down
7 changes: 6 additions & 1 deletion bin/src/modules/pbo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ fn _build(
path.set_extension("pbo");
path
};
create_dir_all(target_pbo.parent().unwrap())?;
if target_pbo.parent().unwrap().exists() {
debug!("{:?} already exists", target_pbo.parent().unwrap());
} else {
debug!("creating {:?}", target_pbo.parent().unwrap());
create_dir_all(target_pbo.parent().unwrap())?;
}
debug!(
"building {:?} => {:?}",
addon.folder(),
Expand Down
3 changes: 3 additions & 0 deletions book/commands/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Options:
<a href="#--no-rap">--no-rap</a>
Do not rapify files

<a href="index.md#--just">--just &lt;just&gt;</a>
Only build the specified addon

<a href="index.md#-t---threads">-t, --threads &lt;threads&gt;</a>
Number of threads, defaults to # of CPUs

Expand Down
3 changes: 3 additions & 0 deletions book/commands/dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Options:
<a href="#-o---all-optionals">-O, --all-optionals</a>
Include all optional addon folders

<a href="index.md#--just">--just &lt;just&gt;</a>
Only build the specified addon

<a href="index.md#-t---threads">-t, --threads &lt;threads&gt;</a>
Number of threads, defaults to # of CPUs

Expand Down
17 changes: 17 additions & 0 deletions book/commands/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@

- [hemtt release](./release.md) - Build the project for release

## Options

### --just

The [`build`](./build.md) and [`dev`](./dev.md) commands can be used to build a single addon. It can be used multiple times to build multiple addons.

```bash
hemtt build --just myAddon
```

```admonish danger
It is advised to only use this on very large projects that take a long time to build.
It is advised to only use this after running the command once without `--just` to ensure all addons are built.
Anytime you run any git commands that can modify files, you should run without `--just` to ensure all addons are up to date.
Before reporting any unexpected behavior, try running without `--just` first.
```

## Global Options

### -t, --threads
Expand Down

0 comments on commit 4491221

Please sign in to comment.