2026-02-14 22:15:09 -05:00
|
|
|
use crate::Result;
|
2026-02-14 13:31:21 -05:00
|
|
|
use crate::cli::parameters::BuildArgs;
|
|
|
|
|
use crate::config::Config;
|
|
|
|
|
use crate::config::TargetConfig;
|
2026-02-14 22:15:09 -05:00
|
|
|
use crate::database::db_handle::DbHandle;
|
2026-02-14 16:00:37 -05:00
|
|
|
use crate::fs_util::assert_directory;
|
|
|
|
|
use crate::fs_util::is_git_repo;
|
|
|
|
|
use crate::git_util::git_force_into_state;
|
|
|
|
|
use crate::git_util::git_init_at_rev;
|
2026-02-14 19:20:52 -05:00
|
|
|
use crate::nix_util::nixos_build_target;
|
2026-02-14 13:31:21 -05:00
|
|
|
|
2026-02-14 22:15:09 -05:00
|
|
|
pub(crate) async fn run_build(args: BuildArgs) -> Result<()> {
|
2026-02-14 13:31:21 -05:00
|
|
|
println!("{:?}", args);
|
|
|
|
|
let config = Config::load_from_file(args.config).await?;
|
|
|
|
|
println!("{:?}", config);
|
|
|
|
|
|
2026-02-14 22:15:09 -05:00
|
|
|
let database_path = config.get_database_path()?;
|
|
|
|
|
let database_parent = database_path
|
|
|
|
|
.parent()
|
|
|
|
|
.expect("Database should exist in a folder.");
|
|
|
|
|
let database_path = database_path.to_string_lossy();
|
|
|
|
|
assert_directory!(
|
|
|
|
|
database_parent,
|
|
|
|
|
"Creating database directory {}",
|
|
|
|
|
database_parent.to_string_lossy()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let db_handle = DbHandle::new(Some(database_path)).await?;
|
|
|
|
|
|
2026-02-14 13:31:21 -05:00
|
|
|
for target_name in args.target {
|
|
|
|
|
let target_config = {
|
|
|
|
|
let target_config = config.get_target_config(&target_name)?;
|
|
|
|
|
if let Some(conf) = target_config {
|
|
|
|
|
conf
|
|
|
|
|
} else {
|
|
|
|
|
return Err(format!("Could not find target {}", target_name).into());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-14 16:00:37 -05:00
|
|
|
prepare_flake_repo(&config, target_config).await?;
|
2026-02-15 01:45:42 -05:00
|
|
|
build_target(&db_handle, &config, target_config).await?;
|
2026-02-14 13:31:21 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-15 01:45:42 -05:00
|
|
|
db_handle.conn.close().await;
|
|
|
|
|
|
2026-02-14 13:31:21 -05:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 22:15:09 -05:00
|
|
|
async fn prepare_flake_repo(config_root: &Config, target_config: &TargetConfig) -> Result<()> {
|
2026-02-14 19:20:52 -05:00
|
|
|
let repo_directory = target_config.get_repo_directory(config_root)?;
|
2026-02-14 16:00:37 -05:00
|
|
|
assert_directory!(
|
2026-02-14 19:20:52 -05:00
|
|
|
&repo_directory,
|
|
|
|
|
"Creating repo directory {}",
|
2026-02-14 22:15:09 -05:00
|
|
|
repo_directory.to_string_lossy()
|
2026-02-14 16:00:37 -05:00
|
|
|
);
|
|
|
|
|
|
2026-02-14 19:20:52 -05:00
|
|
|
if is_git_repo(&repo_directory).await? {
|
2026-02-14 16:00:37 -05:00
|
|
|
// Clean up the existing clone
|
|
|
|
|
git_force_into_state(
|
2026-02-14 19:20:52 -05:00
|
|
|
&repo_directory,
|
2026-02-14 16:00:37 -05:00
|
|
|
target_config.get_branch()?,
|
|
|
|
|
"origin",
|
|
|
|
|
target_config.get_repo()?,
|
|
|
|
|
target_config.get_revision()?,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
} else {
|
|
|
|
|
git_init_at_rev(
|
2026-02-14 19:20:52 -05:00
|
|
|
&repo_directory,
|
2026-02-14 16:00:37 -05:00
|
|
|
target_config.get_branch()?,
|
|
|
|
|
target_config.get_repo()?,
|
|
|
|
|
target_config.get_revision()?,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
2026-02-14 13:31:21 -05:00
|
|
|
}
|
2026-02-14 19:20:52 -05:00
|
|
|
|
2026-02-15 01:45:42 -05:00
|
|
|
async fn build_target(
|
|
|
|
|
db_handle: &DbHandle,
|
|
|
|
|
config_root: &Config,
|
|
|
|
|
target_config: &TargetConfig,
|
|
|
|
|
) -> Result<()> {
|
2026-02-14 19:20:52 -05:00
|
|
|
let flake_directory = target_config.get_flake_directory(config_root)?;
|
|
|
|
|
let build_directory = target_config.get_build_directory(config_root)?;
|
|
|
|
|
assert_directory!(
|
|
|
|
|
&build_directory,
|
|
|
|
|
"Creating build directory {}",
|
2026-02-14 22:15:09 -05:00
|
|
|
build_directory.to_string_lossy()
|
2026-02-14 19:20:52 -05:00
|
|
|
);
|
|
|
|
|
|
2026-02-15 01:45:42 -05:00
|
|
|
nixos_build_target(
|
|
|
|
|
db_handle,
|
|
|
|
|
build_directory,
|
|
|
|
|
flake_directory,
|
|
|
|
|
target_config.get_attr()?,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
2026-02-14 19:20:52 -05:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|