-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpack
More file actions
executable file
·163 lines (132 loc) · 3.9 KB
/
pack
File metadata and controls
executable file
·163 lines (132 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env perl
# vim:ft=perl:ts=4:sts=4:sw=4
use strict;
use warnings;
use Digest::SHA;
use MIME::Base64 qw(encode_base64);
use POSIX qw(strftime);
######################################################################
system "perl -c -Ilib -Iext bin/verse";
die "Perl compilation failed, not packing\n" if $?;
# ---
# Build package structure
# ---
system 'rm -rf pkg/ pkg.tar.gz && mkdir pkg';
die "Could not create a clean package staging area\n" if $?;
system 'cp -a lib pkg/ && cp -a ext/* pkg/lib/';
die "Could not copy library into the package staging area\n" if $?;
# ---
# Version Handling
# ---
my $stub_out = ($ENV{GEESE_PACK_PATH}||".")."/verse-";
my $VERSION = $ARGV[0] || '';
chomp(my $sha1 = qx(git rev-parse HEAD));
$sha1 = substr($sha1, 0, 10);
if ($VERSION) {
$stub_out .= $VERSION;
} else {
$VERSION = '0.x.x-dev';
$stub_out .= "dev-$sha1";
}
if (qx(git status --porcelain bin/verse bin/verse-v1 lib/)) {
$sha1 .= '+';
$stub_out .= '-dirty';
}
my $BUILD = "($sha1) build ".strftime("%Y%m%d.%H%M%S", gmtime);
# ---
# Brand script with version
# ---
open my $out, ">", "pkg/verse"
or die "Failed to open pkg/verse for writing: $!\n";
open my $in, "<", "bin/verse"
or die "Failed to open bin/verse for reading: $!\n";
while (<$in>) {
s/^(\$Verse::VERSION) = .*$/$1 = \"$VERSION\";/;
s/^(\$Verse::BUILD) = .*$/$1 = \" $BUILD\";/;
print $out $_;
}
close $out;
close $in;
chmod 0755, "pkg/verse";
# ---
# Assemble the tarball (with embedded checksum)
# ---
my $fh;
system('tar -cf pkg.tar -C pkg .') == 0
or die "Failed to compile runtime archive: $!\n";
open $fh, "<", "pkg.tar"
or die "Failed to reopen runtime archive for reading: $!\n";
my $sha = Digest::SHA->new('sha1');
$sha->addfile($fh);
my $checksum = $sha->hexdigest;
open $fh, ">", "pkg/checksum"
or die "Failed to open checksum file for writing: $!\n";
print $fh "$checksum\n";
close $fh;
system('tar -rf pkg.tar -C pkg ./checksum') == 0
or die "Failed to append checksum file to runtime archive: $!\n";
system('gzip -9 pkg.tar') == 0
or die "Failed to compress runtime archive: $!\n";
open my $o, ">", $stub_out
or die "Failed to open $stub_out for writing: $!\n";
print $o $_ for <DATA>;
print $o "__DATA__\n$checksum\n";
my $buf;
open my $tbfh, "< :raw :bytes", "pkg.tar.gz"
or die "Could not open tarball for reading: $!\n";
while (read($tbfh, $buf, 60*57)) {
print $o encode_base64($buf);
}
close $o;
chmod 0755, $stub_out;
print "GEESE RUNTIME ARCHIVE:\n";
system("tar -tzvf pkg.tar.gz");
print "\n---\n";
print "packaged v$VERSION as $stub_out\n\n";
# clean up after the geese
system 'rm -rf pkg/ pkg.tar.gz';
__DATA__
#!/usr/bin/perl
use strict;
use warnings;
use MIME::Base64 qw/decode_base64/;
use FindBin;
# Genesis Extraction and Execution Stub Engine (GEESE)
#
# This perl script contains the entire runtime environment needed to execute
# Verse. It unpacks the compressed contents into your ~/.verse/ directory
# then passes execution to the ~/.verse/verse Perl script that was
# extracted, using the libraries located in ~/.verse/lib.
#
# Once extracted, further execution of this stub will use the previously
# extracted library after checking that the checksum is identical. This way,
# new versions will be properly handled.
die "You have no \$HOME. Please set one and re-run.\n"
unless $ENV{HOME};
my $root = "$ENV{HOME}/.verse";
mkdir $root unless -d $root;
# check the checksum to see if we should untar
my ($fh, $want, $have) = (undef, "", "");
chomp($want = <DATA>);
open $fh, "<", "$root/checksum" or $fh = undef;
if ($fh) {
chomp($have = <$fh>);
close $fh;
}
if ($want ne $have) {
system("rm -rf $root/*");
# extract the payload
print STDERR "(inflating verse runtime...)\n";
open my $tar, "|-", "tar -xzf - -C $root"
or die "Failed to do stuff\n";
while (<DATA>) {
chomp;
print $tar decode_base64($_);
}
close $tar;
}
# run it!
$ENV{PERL5LIB} = "$root/lib";
chmod 0755, "$root/verse";
exec "$root/verse", @ARGV;
exit(1);