2024-10-09 05:32:22 +03:00
use std ::io ::Read ;
use std ::path ::{ Path , PathBuf } ;
use sha2 ::Digest ;
2024-08-10 03:03:18 +03:00
2024-10-08 05:35:57 +03:00
const CANONICAL_PACKAGES_ZIP_PATH : & str = " ../target/packages.zip " ;
2024-02-02 07:43:13 +03:00
2024-10-08 06:20:48 +03:00
macro_rules ! p {
( $( $tokens : tt ) * ) = > {
println! ( " cargo:warning= {} " , format! ( $( $tokens ) * ) )
}
}
2024-10-09 05:32:22 +03:00
fn compute_hash ( file_path : & Path ) -> anyhow ::Result < String > {
let input_file = std ::fs ::File ::open ( file_path ) ? ;
let mut reader = std ::io ::BufReader ::new ( input_file ) ;
let mut hasher = sha2 ::Sha256 ::new ( ) ;
let mut buffer = [ 0 ; 1024 ] ; // buffer for chunks of the file
loop {
let count = reader . read ( & mut buffer ) ? ;
if count = = 0 {
break ;
}
hasher . update ( & buffer [ .. count ] ) ;
}
Ok ( format! ( " {:x} " , hasher . finalize ( ) ) )
}
2024-02-13 07:51:02 +03:00
fn main ( ) -> anyhow ::Result < ( ) > {
2024-10-08 05:35:57 +03:00
let path_to_packages_zip = match std ::env ::var ( " PATH_TO_PACKAGES_ZIP " ) {
2024-10-08 06:20:48 +03:00
Ok ( env_var ) = > env_var ,
2024-10-08 05:35:57 +03:00
Err ( _ ) = > {
2024-10-08 06:20:48 +03:00
let canonical_path = PathBuf ::from ( CANONICAL_PACKAGES_ZIP_PATH ) ;
if canonical_path . exists ( ) {
p! ( " No path given via PATH_TO_PACKAGES_ZIP envvar. Defaulting to path of `kinode/target/packages.zip`. " ) ;
CANONICAL_PACKAGES_ZIP_PATH . to_string ( )
} else {
return Err ( anyhow ::anyhow! ( " You must build packages.zip with scripts/build_packages or set PATH_TO_PACKAGES_ZIP to point to your desired pacakges.zip (default path at kinode/target/packages.zip was not populated). " ) ) ;
2024-07-25 18:29:02 +03:00
}
2024-07-25 18:21:51 +03:00
}
2024-10-08 05:35:57 +03:00
} ;
let path = PathBuf ::from ( & path_to_packages_zip ) ;
if ! path . exists ( ) {
return Err ( anyhow ::anyhow! (
" Path to packages {path_to_packages_zip} does not exist. "
) ) ;
2024-07-25 18:21:51 +03:00
}
2024-10-08 05:35:57 +03:00
let path_to_packages_zip_path = PathBuf ::from ( & path_to_packages_zip ) . canonicalize ( ) ? ;
let canonical_packages_zip_path = PathBuf ::from ( CANONICAL_PACKAGES_ZIP_PATH ) . canonicalize ( ) ? ;
if path_to_packages_zip_path ! = canonical_packages_zip_path {
2024-10-09 05:32:22 +03:00
std ::fs ::copy ( & path_to_packages_zip_path , & canonical_packages_zip_path ) ? ;
}
2024-10-15 06:22:30 +03:00
if ! std ::env ::var ( " SKIP_BUILD_FRONTEND " ) . is_ok ( ) {
// build core frontends
let pwd = std ::env ::current_dir ( ) ? ;
let core_frontends = vec! [ " src/register-ui " ] ;
2024-10-09 05:32:22 +03:00
2024-10-15 06:22:30 +03:00
// for each frontend, execute build.sh
for frontend in core_frontends {
let status = std ::process ::Command ::new ( " sh " )
. current_dir ( pwd . join ( frontend ) )
. arg ( " ./build.sh " )
. status ( ) ? ;
if ! status . success ( ) {
return Err ( anyhow ::anyhow! ( " Failed to build frontend: {} " , frontend ) ) ;
}
2024-10-09 05:32:22 +03:00
}
2024-07-16 02:57:42 +03:00
}
2024-02-13 07:51:02 +03:00
2024-09-28 05:05:34 +03:00
let version = if let Ok ( version ) = std ::env ::var ( " DOCKER_BUILD_IMAGE_VERSION " ) {
// embed the DOCKER_BUILD_IMAGE_VERSION
version
} else {
" none " . to_string ( )
} ;
println! ( " cargo:rustc-env=DOCKER_BUILD_IMAGE_VERSION= {version} " ) ;
2024-10-09 05:32:22 +03:00
let packages_zip_hash = compute_hash ( & canonical_packages_zip_path ) ? ;
println! ( " cargo:rustc-env=PACKAGES_ZIP_HASH= {packages_zip_hash} " ) ;
2024-02-02 07:43:13 +03:00
Ok ( ( ) )
}