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',
26 "DATE" => `date '+%Y-%m-%d' | tr -d '\n'`,
27 "LSMDATE" => `date '+%y%m%d' | tr -d '\n'`,
30 "archivedir" => "/home/mj/tmp/archives/$basename",
48 sub GetVersionFromFile($) {
49 my ($s,$file,$rx) = @_;
50 open F, $file or die "Unable to open $file for version autodetection";
55 print "Detected version $1 from $file\n" if $verbose;
60 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
61 return $s->{"VERSION"};
64 sub GetVersionsFromChangelog($) {
65 my ($s,$file,$rx) = @_;
66 open F, $file or die "Unable to open $file for version autodetection";
70 if (!defined $s->{"VERSION"}) {
72 print "Detected version $1 from $file\n" if $verbose;
73 } elsif ($s->{"VERSION"} eq $1) {
76 $s->{"OLDVERSION"} = $1;
77 print "Detected previous version $1 from $file\n" if $verbose;
83 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
84 return $s->{"VERSION"};
89 $s->{"DISTDIR"} = $dd;
90 print "Initializing dist directory $dd\n" if $verbose;
91 `rm -rf $dd`; die if $?;
92 `mkdir -p $dd`; die if $?;
97 if (defined $s->{$v}) {
100 die "Reference to unknown variable $v";
105 my ($s,$f,$dir,$action) = @_;
107 (my $d = $f) =~ s@(^|/)[^/]*$@@;
109 -d $d || `mkdir -p $d`; die if $?;
111 my $preprocess = ($action =~ /p/);
112 my $subst = ($action =~ /s/);
113 if ($preprocess || $subst) {
114 open I, "$f" or die "open($f): $?";
115 open O, ">$dir/$f" or die "open($dir/$f): $!";
116 my @ifs = (); # stack of conditions, 1=satisfied
117 my $empty = 0; # last line was empty
118 my $is_makefile = ($f =~ /(Makefile|.mk)$/);
121 s/@([0-9A-Za-z_]+)@/$s->ExpandVar($1)/ge;
124 if (/^#/ || $is_makefile) {
125 if (/^#?ifdef\s+(\w+)/) {
126 if (defined ${$s->{"conditions"}}{$1}) {
127 push @ifs, ${$s->{"conditions"}}{$1};
131 } elsif (/^#ifndef\s+(\w+)/) {
132 if (defined ${$s->{"conditions"}}{$1}) {
133 push @ifs, -${$s->{"conditions"}}{$1};
137 } elsif (/^#if\s+/) {
139 } elsif (/^#?endif/) {
141 defined $x or die "Improper nesting of conditionals";
143 } elsif (/^#?else/) {
145 defined $x or die "Improper nesting of conditionals";
150 @ifs && $ifs[$#ifs] < 0 && next;
154 } else { $empty = 0; }
160 ! -x $f or chmod(0755, "$dir/$f") or die "chmod($dir/$f): $!";
162 `cp -a "$f" "$dir/$f"`; die if $?;
168 $s->{"PKG"} = $s->{"PACKAGE"} . "-" . $s->{"VERSION"};
169 my $dd = $s->{"DISTDIR"};
170 my $pkg = $s->{"PKG"};
171 my $dir = "$dd/$pkg";
172 print "Generating $dir\n";
174 FILES: foreach my $f (`find . -type f`) {
178 my @rules = @{$s->{"rules"}};
180 my $rule = shift @rules;
181 my $act = shift @rules;
187 ($action =~ /-/) && next FILES;
188 print "$f ($action)\n" if $verbose;
189 $s->CopyFile($f, $dir, $action);
192 foreach my $d (@{$s->{"directories"}}) {
193 `mkdir -p $dir/$d`; die if $?;
196 if (-f "$dir/Makefile") {
197 print "Cleaning up\n";
198 `cd $dir && make distclean >&2`; die if $?;
201 print "Creating $dd/$pkg.tar.gz\n";
202 my $tarvv = $verbose ? "vv" : "";
203 `cd $dd && tar cz${tarvv}f $pkg.tar.gz $pkg >&2`; die if $?;
204 push @{$s->{"distfiles"}}, "$dd/$pkg.tar.gz";
206 my $adir = $s->{"archivedir"};
207 my $afile = "$adir/$pkg.tar.gz";
208 print "Archiving to $afile\n";
209 -d $adir or `mkdir -p $adir`;
210 `cp $dd/$pkg.tar.gz $afile`; die if $?;
217 my $sf = $s->{"DISTDIR"} . "/" . $s->{"PKG"} . "/$f";
218 my $df = $s->{"DISTDIR"} . "/$f";
219 print "Generating $df\n";
220 `cp $sf $df`; die if $?;
221 push @{$s->{"distfiles"}}, $df;
230 --[no]verbose Be chatty about the inner workings of the release system {verbose}
231 --[no]test Test the package before uploading {do_test}
232 --[no]patch Make a patch against the previous version {do_patch}
233 --diff-against=<ver> Set which version we create the patch against
234 --[no]upload Upload released files {do_upload}
235 --[no]git-tag Tag the Git repository with "v<version>" {do_git_tag}
236 --force-git-tag Rewrite the Git tag if it already exists {force_git_tag}
237 --[no]notify Call scripts to notify the world about the release {do_notify}
240 return "(default: " . ($_[0] ? "on" : "off") . ")";
242 $usage =~ s[{(\w+)}][state($s->{$1})]ge;
246 sub ParseOptions($) {
248 $s->{"do_git_tag"} = 1 if (-d ".git");
249 $s->{"do_notify"} = 1 if @{$s->{"notifiers"}};
251 "verbose!" => \$verbose,
252 "test!" => \$s->{"do_test"},
253 "patch!" => \$s->{"do_patch"},
254 "diff-against=s" => \$s->{"diff_against"},
255 "upload!" => \$s->{"do_upload"},
256 'git-tag!' => \$s->{"do_git_tag"},
257 'force-git-tag!' => \$s->{"force_git_tag"},
258 'notify!' => \$s->{"do_notify"},
264 my $dd = $s->{"DISTDIR"};
265 my $pkg = $s->{"PKG"};
266 my $log = "$dd/$pkg.log";
267 print "Doing a test compilation\n";
268 `( cd $dd/$pkg && make ) >$log 2>&1`;
269 die "There were errors. Please inspect $log" if $?;
270 `grep -q [Ww]arning $log`;
271 $? or print "There were warnings! Please inspect $log.\n";
272 print "Cleaning up\n";
273 `cd $dd/$pkg && make distclean`; die if $?;
278 my $dd = $s->{"DISTDIR"};
279 my $pkg1 = $s->{"PKG"};
281 if ($s->{"diff_against"} ne "") {
282 $oldver = $s->{"diff_against"};
283 } elsif (defined $s->{"OLDVERSION"}) {
284 $oldver = $s->{"OLDVERSION"};
286 print "WARNING: No previous version known. No patch generated.\n";
289 my $pkg0 = $s->{"PACKAGE"} . "-" . $oldver;
291 my $oldarch = $s->{"archivedir"} . "/" . $pkg0 . ".tar.gz";
292 -f $oldarch or die "MakePatch: $oldarch not found";
293 print "Unpacking $pkg0 from $oldarch\n";
294 `cd $dd && tar xzf $oldarch`; die if $?;
296 my $diff = $s->{"PACKAGE"} . "-" . $oldver . "-" . $s->{"VERSION"} . ".diff.gz";
297 print "Creating a patch from $pkg0 to $pkg1: $diff\n";
298 `cd $dd && diff -ruN $pkg0 $pkg1 | gzip >$diff`; die if $?;
299 push @{$s->{"distfiles"}}, "$dd/$diff";
304 foreach my $u (@{$s->{"uploads"}}) {
305 my $url = $u->{"url"};
306 print "Upload to $url :\n";
308 my $filter = $u->{"filter"} || ".*";
309 foreach my $f (@{$s->{"distfiles"}}) {
315 print "<confirm> "; <STDIN>;
316 if ($url =~ m@^scp://([^/]+)(.*)@) {
322 my $cmd = "scp @files $host:$dir\n";
324 } elsif ($url =~ m@ftp://([^/]+)(.*)@) {
327 open FTP, "|ftp -v $host" or die;
328 print FTP "cd $dir\n";
329 foreach my $f (@files) {
330 (my $ff = $f) =~ s@.*\/([^/].*)@$1@;
331 print FTP "put $f $ff\n";
337 die "Don't know how to handle this URL scheme";
344 my $tag = 'v' . $s->{'VERSION'};
345 my $force = ($s->{'force_git_tag'} ? '--force' : '');
346 print "Tagging Git repository with $tag\n";
347 `git tag $tag $force`; die if $?;
348 print "Pushing the tags upstream\n";
349 `git push --tags`; die if $?;
352 sub AddUcwNotifier($) {
354 push @{$r->{"notifiers"}}, sub {
356 print "Updating web pages\n";
357 my $pkg = $s->{'PACKAGE'};
358 my $ver = $s->{'VERSION'};
359 `ssh jw 'cd www && bin/release-prog $pkg $ver'`; die if $?;
365 $s->Test if $s->{"do_test"};
366 $s->MakePatch if $s->{"do_patch"};
367 $s->GitTag if $s->{"do_git_tag"};
368 $s->Upload if $s->{"do_upload"};
369 if ($s->{"do_notify"}) {
370 for my $f (@{$s->{"notifiers"}}) {