2 # A simple system for making software releases
3 # (c) 2003--2010 Martin Mares <mj@ucw.cz>
13 my ($class,$basename) = @_;
15 "PACKAGE" => $basename,
17 # p=preprocess, s=subst, -=discard
18 '(^|/)(CVS|\.arch-ids|{arch}|\.git|tmp)/' => '-',
19 '\.(lsm|spec)$' => 'ps',
20 '(^|/)README$' => 's',
27 "DATE" => `date '+%Y-%m-%d' | tr -d '\n'`,
28 "LSMDATE" => `date '+%y%m%d' | tr -d '\n'`,
31 "archivedir" => "/home/mj/tmp/archives/$basename",
49 sub GetVersionFromFile($) {
50 my ($s,$file,$rx) = @_;
51 open F, $file or die "Unable to open $file for version autodetection";
56 print "Detected version $1 from $file\n" if $verbose;
61 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
62 return $s->{"VERSION"};
65 sub GetVersionsFromChangelog($) {
66 my ($s,$file,$rx) = @_;
67 open F, $file or die "Unable to open $file for version autodetection";
71 if (!defined $s->{"VERSION"}) {
73 print "Detected version $1 from $file\n" if $verbose;
74 } elsif ($s->{"VERSION"} eq $1) {
77 $s->{"OLDVERSION"} = $1;
78 print "Detected previous version $1 from $file\n" if $verbose;
84 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
85 return $s->{"VERSION"};
90 $s->{"DISTDIR"} = $dd;
91 print "Initializing dist directory $dd\n" if $verbose;
92 `rm -rf $dd`; die if $?;
93 `mkdir -p $dd`; die if $?;
98 if (defined $s->{$v}) {
101 die "Reference to unknown variable $v";
106 my ($s,$f,$dir,$action) = @_;
108 (my $d = $f) =~ s@(^|/)[^/]*$@@;
110 -d $d || `mkdir -p $d`; die if $?;
112 my $preprocess = ($action =~ /p/);
113 my $subst = ($action =~ /s/);
114 if ($preprocess || $subst) {
115 open I, "$f" or die "open($f): $?";
116 open O, ">$dir/$f" or die "open($dir/$f): $!";
117 my @ifs = (); # stack of conditions, 1=satisfied
118 my $empty = 0; # last line was empty
119 my $is_makefile = ($f =~ /(Makefile|.mk)$/);
122 s/@([0-9A-Za-z_]+)@/$s->ExpandVar($1)/ge;
125 if (/^#/ || $is_makefile) {
126 if (/^#?ifdef\s+(\w+)/) {
127 if (defined ${$s->{"conditions"}}{$1}) {
128 push @ifs, ${$s->{"conditions"}}{$1};
132 } elsif (/^#ifndef\s+(\w+)/) {
133 if (defined ${$s->{"conditions"}}{$1}) {
134 push @ifs, -${$s->{"conditions"}}{$1};
138 } elsif (/^#if\s+/) {
140 } elsif (/^#?endif/) {
142 defined $x or die "Improper nesting of conditionals";
144 } elsif (/^#?else/) {
146 defined $x or die "Improper nesting of conditionals";
151 @ifs && $ifs[$#ifs] < 0 && next;
155 } else { $empty = 0; }
161 ! -x $f or chmod(0755, "$dir/$f") or die "chmod($dir/$f): $!";
163 `cp -a "$f" "$dir/$f"`; die if $?;
169 $s->{"PKG"} = $s->{"PACKAGE"} . "-" . $s->{"VERSION"};
170 my $dd = $s->{"DISTDIR"};
171 my $pkg = $s->{"PKG"};
172 my $dir = "$dd/$pkg";
173 print "Generating $dir\n";
175 FILES: foreach my $f (`find . -type f`) {
179 my @rules = @{$s->{"rules"}};
181 my $rule = shift @rules;
182 my $act = shift @rules;
188 ($action =~ /-/) && next FILES;
189 print "$f ($action)\n" if $verbose;
190 $s->CopyFile($f, $dir, $action);
193 foreach my $d (@{$s->{"directories"}}) {
194 `mkdir -p $dir/$d`; die if $?;
197 if (-f "$dir/Makefile") {
198 print "Cleaning up\n";
199 `cd $dir && make distclean >&2`; die if $?;
202 print "Creating $dd/$pkg.tar.gz\n";
203 my $tarvv = $verbose ? "vv" : "";
204 `cd $dd && tar cz${tarvv}f $pkg.tar.gz $pkg >&2`; die if $?;
205 push @{$s->{"distfiles"}}, "$dd/$pkg.tar.gz";
207 my $adir = $s->{"archivedir"};
208 my $afile = "$adir/$pkg.tar.gz";
209 print "Archiving to $afile\n";
210 -d $adir or `mkdir -p $adir`;
211 `cp $dd/$pkg.tar.gz $afile`; die if $?;
218 my $sf = $s->{"DISTDIR"} . "/" . $s->{"PKG"} . "/$f";
219 my $df = $s->{"DISTDIR"} . "/$f";
220 print "Generating $df\n";
221 `cp $sf $df`; die if $?;
222 push @{$s->{"distfiles"}}, $df;
231 --[no]verbose Be chatty about the inner workings of the release system {verbose}
232 --[no]test Test the package before uploading {do_test}
233 --[no]patch Make a patch against the previous version {do_patch}
234 --diff-against=<ver> Set which version we create the patch against
235 --[no]upload Upload released files {do_upload}
236 --[no]git-tag Tag the Git repository with "v<version>" {do_git_tag}
237 --force-git-tag Rewrite the Git tag if it already exists {force_git_tag}
238 --[no]notify Call scripts to notify the world about the release {do_notify}
241 return "(default: " . ($_[0] ? "on" : "off") . ")";
243 $usage =~ s[{(\w+)}][state($s->{$1})]ge;
247 sub ParseOptions($) {
249 $s->{"do_git_tag"} = 1 if (-d ".git");
250 $s->{"do_notify"} = 1 if @{$s->{"notifiers"}};
252 "verbose!" => \$verbose,
253 "test!" => \$s->{"do_test"},
254 "patch!" => \$s->{"do_patch"},
255 "diff-against=s" => \$s->{"diff_against"},
256 "upload!" => \$s->{"do_upload"},
257 'git-tag!' => \$s->{"do_git_tag"},
258 'force-git-tag!' => \$s->{"force_git_tag"},
259 'notify!' => \$s->{"do_notify"},
265 my $dd = $s->{"DISTDIR"};
266 my $pkg = $s->{"PKG"};
267 my $log = "$dd/$pkg.log";
268 print "Doing a test compilation\n";
269 `( cd $dd/$pkg && make ) >$log 2>&1`;
270 die "There were errors. Please inspect $log" if $?;
271 `grep -q [Ww]arning $log`;
272 $? or print "There were warnings! Please inspect $log.\n";
273 print "Cleaning up\n";
274 `cd $dd/$pkg && make distclean`; die if $?;
279 my $dd = $s->{"DISTDIR"};
280 my $pkg1 = $s->{"PKG"};
282 if ($s->{"diff_against"} ne "") {
283 $oldver = $s->{"diff_against"};
284 } elsif (defined $s->{"OLDVERSION"}) {
285 $oldver = $s->{"OLDVERSION"};
287 print "WARNING: No previous version known. No patch generated.\n";
290 my $pkg0 = $s->{"PACKAGE"} . "-" . $oldver;
292 my $oldarch = $s->{"archivedir"} . "/" . $pkg0 . ".tar.gz";
293 -f $oldarch or die "MakePatch: $oldarch not found";
294 print "Unpacking $pkg0 from $oldarch\n";
295 `cd $dd && tar xzf $oldarch`; die if $?;
297 my $diff = $s->{"PACKAGE"} . "-" . $oldver . "-" . $s->{"VERSION"} . ".diff.gz";
298 print "Creating a patch from $pkg0 to $pkg1: $diff\n";
299 `cd $dd && diff -ruN $pkg0 $pkg1 | gzip >$diff`; die if $?;
300 push @{$s->{"distfiles"}}, "$dd/$diff";
305 foreach my $u (@{$s->{"uploads"}}) {
306 my $url = $u->{"url"};
307 print "Upload to $url :\n";
309 my $filter = $u->{"filter"} || ".*";
310 foreach my $f (@{$s->{"distfiles"}}) {
316 print "<confirm> "; <STDIN>;
317 if ($url =~ m@^scp://([^/]+)(.*)@) {
323 my $cmd = "scp @files $host:$dir\n";
325 } elsif ($url =~ m@ftp://([^/]+)(.*)@) {
328 open FTP, "|ftp -v $host" or die;
329 print FTP "cd $dir\n";
330 foreach my $f (@files) {
331 (my $ff = $f) =~ s@.*\/([^/].*)@$1@;
332 print FTP "put $f $ff\n";
338 die "Don't know how to handle this URL scheme";
345 my $tag = 'v' . $s->{'VERSION'};
346 my $force = ($s->{'force_git_tag'} ? '--force' : '');
347 print "Tagging Git repository with $tag\n";
348 `git tag $tag $force`; die if $?;
349 print "Pushing the tags upstream\n";
350 `git push --tags`; die if $?;
353 sub AddUcwNotifier($) {
355 push @{$r->{"notifiers"}}, sub {
357 print "Updating web pages\n";
358 my $pkg = $s->{'PACKAGE'};
359 my $ver = $s->{'VERSION'};
360 `ssh jw 'cd www && bin/release-prog $pkg $ver'`; die if $?;
366 $s->Test if $s->{"do_test"};
367 $s->MakePatch if $s->{"do_patch"};
368 $s->GitTag if $s->{"do_git_tag"};
369 $s->Upload if $s->{"do_upload"};
370 if ($s->{"do_notify"}) {
371 for my $f (@{$s->{"notifiers"}}) {