@@ -5,20 +5,18 @@ use std::{
55 path:: { Path , PathBuf } ,
66} ;
77
8- use anyhow:: { bail, Result } ;
8+ use anyhow:: { anyhow , bail, Result } ;
99use bzip2:: read:: BzDecoder ;
1010use camino:: Utf8Path ;
1111use flate2:: read:: GzDecoder ;
1212use indicatif:: ProgressBar ;
1313use reqwest:: { Client , Url } ;
14+ use sha2:: { Digest , Sha256 as Sha256Hasher } ;
1415use tar:: Archive ;
1516use tracing:: info;
1617use xz2:: read:: XzDecoder ;
1718
18- use crate :: {
19- check_hash, create_tarball,
20- package:: { Package , Source , StepVariant } ,
21- } ;
19+ use crate :: package:: { Package , Source , StepVariant } ;
2220
2321pub async fn build ( ) -> Result < ( ) > {
2422 let package_path = current_dir ( ) ?. join ( "package.toml" ) ;
@@ -165,3 +163,31 @@ fn unpack_archive<R: Read>(decoder: R) -> Result<()> {
165163
166164 Ok ( ( ) )
167165}
166+
167+ pub fn check_hash < P : AsRef < Path > > ( path : P , hash : & str ) -> Result < bool > {
168+ let file = fs:: read ( path) ?;
169+ let ( hash_type, hash) = hash
170+ . split_once ( ':' )
171+ . ok_or ( anyhow ! ( "Invalid checksum format" ) ) ?;
172+
173+ let computed_hash = match hash_type {
174+ "blake3" => blake3:: hash ( & file) . to_hex ( ) . to_string ( ) ,
175+ "sha256" => base16ct:: lower:: encode_string ( Sha256Hasher :: digest ( & file) . as_slice ( ) ) ,
176+ _ => bail ! ( "Unsupported hash" ) ,
177+ } ;
178+
179+ Ok ( hash == computed_hash)
180+ }
181+
182+ pub fn create_tarball < P : AsRef < Path > > ( package_path : P , package : & Package ) -> Result < ( ) > {
183+ let tarball_name = format ! ( "{}-{}.peach" , package. info. name, package. info. version) ;
184+ let tarball_path = current_dir ( ) ?. join ( & tarball_name) ;
185+ let tar_gz = File :: create ( & tarball_path) ?;
186+ let enc = zstd:: Encoder :: new ( tar_gz, 22 ) ?;
187+ let mut tar = tar:: Builder :: new ( enc) ;
188+
189+ tar. append_dir_all ( "." , package_path) ?;
190+
191+ info ! ( "Created package: {}" , tarball_name) ;
192+ Ok ( ( ) )
193+ }
0 commit comments