@@ -8,15 +8,17 @@ use FindBin;
88use lib " $FindBin::Bin /local/lib/perl5" ;
99
1010use Cwd qw( realpath getcwd) ;
11+ use File::Path qw( mkpath) ;
1112use IO::File;
1213use JSON::PP qw( encode_json) ;
1314use Data::Dumper;
1415use Digest::MD5;
16+ use Digest::SHA;
1517use Getopt::Long;
1618use HTTP::Tiny;
1719use Config::Tiny;
1820use YAML::Tiny qw( LoadFile Dump) ;
19- use File::Basename qw( basename fileparse) ;
21+ use File::Basename qw( basename fileparse dirname ) ;
2022use File::Spec::Functions qw( catfile) ;
2123use File::Temp qw( tempfile tmpnam tempdir) ;
2224
@@ -57,6 +59,50 @@ sub handle_recipe {
5759 # checout branch and collect links
5860 my @volumes ;
5961 for my $k ( keys %$data ) {
62+ if (my $archive = $data -> {$k }{archive }) {
63+ my $url = $archive -> {url };
64+ my $integrity = $archive -> {integrity };
65+
66+ die " archive url and integrity is required: $k "
67+ unless $url && $integrity ;
68+
69+ my $basename = basename($url );
70+ my $rel_path = catfile($k , $basename );
71+ my $full_path = catfile($archive_dir , $rel_path );
72+
73+ my ($algorithm ) = $integrity =~ m {\A sha(\d +)} ;
74+ my $sha = Digest::SHA-> new($algorithm );
75+
76+ if (-f $full_path ) {
77+ $sha -> addfile(IO::File-> new($full_path , ' r' ));
78+ my $digest = " sha$algorithm -" . $sha -> base64_padded_digest;
79+ if ($digest eq $integrity ) {
80+ $archives .= " $rel_path " ;
81+ next ;
82+ }
83+ }
84+
85+ mkpath(dirname($full_path ));
86+ my $response = HTTP::Tiny-> new-> get($url );
87+ die " Failed: @{[Dumper($response )]}"
88+ unless $response -> {success } && length $response -> {content };
89+
90+ $sha -> add($response -> {content });
91+ my $digest = " sha$algorithm -" . $sha -> base64_padded_digest;
92+ if ($digest ne $integrity ) {
93+ die " intergrity check failed: $url " ;
94+ }
95+
96+ my $fh = IO::File-> new($full_path , ' w' )
97+ or die " Can't open $full_path : $! " ;
98+ print $fh $response -> {content };
99+ close $fh ;
100+
101+ $archives .= " $rel_path " ;
102+
103+ next ;
104+ }
105+
60106 my $repo = $data -> {$k }{location };
61107 if (($ENV {CODESPACES } || ' ' ) eq ' true' ) {
62108 $repo =~ s { ^git\@ github.com:} { https://github.com/} ;
@@ -244,6 +290,8 @@ for my $r ( split /\s+/, $repos ) {
244290}
245291
246292for my $a ( split /\s +/, $archives ) {
293+ next unless $a ;
294+
247295 my $file_path ;
248296 my ( $basename , $ext ) = ( fileparse( $a , qr /\. [a-zA-Z\. ]+$ / ) )[ 0, 2 ];
249297
0 commit comments