Unbreak release.sh for final releases
[exim.git] / release-process / scripts / mk_exim_release.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use Carp;
6 use File::Copy;
7 use File::Spec;
8 use File::Path;
9 use File::Temp;
10 use FindBin;
11 use Getopt::Long;
12 use IO::File;
13 use Pod::Usage;
14
15 my $debug   = 0;
16 my $verbose = 0;
17
18 # ------------------------------------------------------------------
19
20 sub get_and_check_version {
21     my $release = shift;
22     my $context = shift;
23
24     # make sure this looks like a real release version
25     # which should (currently) be 4.xx or 4.xx_RCx
26     unless ( $release =~ /^(4\.\d\d(?:_RC\d+)?)$/ ) {
27         croak "The given version number does not look right - $release";
28     }
29     my $full_release  = $1;              # untainted here...
30     my $trunc_release = $full_release;
31     $trunc_release =~ s/^(4\.\d\d)(?:_RC\d+)?$/$1/;
32
33     $context->{release}  = $full_release;
34     $context->{trelease} = $trunc_release;
35 }
36
37 # ------------------------------------------------------------------
38
39 sub build_tag {
40     my $context = shift;
41
42     # The CVS tag consists of exim-$version where $version
43     # is the version number with . replaced with _
44     my $modversion = $context->{release};
45     $modversion =~ tr/0-9RC/_/cs;
46
47     return sprintf( 'exim-%s', $modversion );
48 }
49
50 # ------------------------------------------------------------------
51
52 sub deal_with_working_directory {
53     my $context = shift;
54     my $delete  = shift;
55
56     # Set default directory
57     $context->{directory} ||= File::Spec->rel2abs( sprintf( 'exim-packaging-%s', $context->{release} ) );
58     my $directory = $context->{directory};
59
60     # ensure the working directory is not in place
61     if ( -d $directory ) {
62         if ($delete) {
63             print "Deleting existing $directory\n" if ($verbose);
64             rmtree( $directory, { verbose => $debug } );
65         }
66         if ( -d $directory ) {
67             croak "Working directory $directory exists";
68         }
69     }
70
71     # create base directory
72     mkpath( $context->{directory}, { verbose => ( $verbose || $debug ) } );
73
74     # set and create subdirectories
75     foreach (qw(release_tree pkgs pkgdirs docbook tmp)) {
76         $context->{$_} = File::Spec->catdir( $context->{directory}, $_ );
77         mkpath( $context->{$_}, { verbose => ( $verbose || $debug ) } );
78     }
79 }
80
81 # ------------------------------------------------------------------
82
83 sub export_git_tree {
84     my $context = shift;
85
86     # build git command
87     my $archive_file = sprintf( '%s/%s-%s.tar', $context->{tmp}, $context->{pkgname}, $context->{release} );
88     $context->{tmp_archive_file} = $archive_file;
89     my @cmd = ( 'git', 'archive', '--format=tar', "--output=$archive_file", $context->{tag} );
90
91     # run git command
92     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
93     system(@cmd) == 0 || croak "Export failed";
94 }
95
96 # ------------------------------------------------------------------
97
98 sub unpack_tree {
99     my $context = shift;
100
101     die "Cannot see archive file\n" unless ( -f $context->{tmp_archive_file} );
102     my @cmd = ( 'tar', 'xf', $context->{tmp_archive_file}, '-C', $context->{release_tree} );
103
104     # run  command
105     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
106     system(@cmd) == 0 || croak "Unpack failed";
107 }
108
109 # ------------------------------------------------------------------
110
111 sub make_version_script {
112     my $context = shift;
113
114     my $variant = substr( $context->{release}, length($context->{trelease}) );
115     if ( $context->{release} ne $context->{trelease} . $variant ) {
116         die "Broken version numbering, I'm buggy";
117     }
118
119     my $srcdir    = File::Spec->catdir( $context->{release_tree}, 'src', 'src' );
120     chdir $srcdir or die "chdir $srcdir: $\n";
121
122     if ( -f "version.sh" ) {
123         print( "WARNING: version.sh already exists - leaving it in place\n" );
124         return;
125     }
126
127     my @cmd = ("../scripts/reversion", "release");
128     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
129     system(@cmd) == 0 || croak "reversion failed";
130
131     unlink "version.h";
132
133     -f "version.sh" or die "failed to create version.h";
134 }
135
136 # ------------------------------------------------------------------
137
138 sub build_html_documentation {
139     my $context = shift;
140
141     my $genpath   = $context->{webgen_base} . '/script/gen.pl';
142     my $templates = $context->{webgen_base} . '/templates';
143     my $dir       = File::Spec->catdir( $context->{release_tree}, 'html' );
144     my $spec      = File::Spec->catfile( $context->{docbook}, 'spec.xml' );
145     my $filter    = File::Spec->catfile( $context->{docbook}, 'filter.xml' );
146
147     mkdir($dir);
148
149     my @cmd =
150       ( $genpath, '--spec', $spec, '--filter', $filter, '--latest', $context->{trelease}, '--tmpl', $templates, '--docroot', $dir );
151
152     print "Executing ", join( ' ', @cmd ), "\n";
153     system(@cmd);
154
155     # move directory into right place
156     my $sourcedir = File::Spec->catdir( $context->{docbook}, 'filter.xml' );
157
158     rename(
159         File::Spec->catdir( $dir,                sprintf( 'exim-html-%s', $context->{trelease} ) ),
160         File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-html-%s', $context->{release} ) )
161     );
162 }
163
164 # ------------------------------------------------------------------
165
166 sub copy_docbook_files {
167     my $context = shift;
168
169     # where the generated docbook files can be found
170     my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
171
172     # where the website docbook source dir is - push files to here
173     my $webdir = File::Spec->catdir( $context->{webgen_base}, 'docbook', $context->{trelease} );
174     mkpath( $webdir, { verbose => ( $verbose || $debug ) } );
175
176     foreach my $file ( 'spec.xml', 'filter.xml' ) {
177         my $from  = File::Spec->catfile( $docdir,             $file );
178         my $to    = File::Spec->catfile( $context->{docbook}, $file );
179         my $webto = File::Spec->catfile( $webdir,             $file );
180         copy( $from, $to );
181         copy( $from, $webto );
182     }
183 }
184
185 # ------------------------------------------------------------------
186
187 sub build_documentation {
188     my $context = shift;
189
190     my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
191     system("cd '$docdir' && ./OS-Fixups && make everything") == 0
192       || croak "Doc build failed";
193
194     copy_docbook_files($context);
195     build_html_documentation($context);
196 }
197
198 # ------------------------------------------------------------------
199
200 sub move_text_docs_into_pkg {
201     my $context = shift;
202
203     my $old_docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
204     my $old_txtdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-txt' );
205     my $new_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc' );
206     mkpath( $new_docdir, { verbose => ( $verbose || $debug ) } );
207
208     # move generated documents from docbook stuff
209     foreach my $file (qw/exim.8 spec.txt filter.txt/) {
210         move( File::Spec->catfile( $old_docdir, $file ), File::Spec->catfile( $new_docdir, $file ) );
211     }
212
213     # move text documents across
214     foreach my $file ( glob( File::Spec->catfile( $old_txtdir, '*' ) ) ) {
215
216         # skip a few we dont want
217         my $fn = ( File::Spec->splitpath($file) )[2];
218         next
219           if ( ( $fn eq 'ABOUT' )
220             || ( $fn eq 'ChangeLog.0' )
221             || ( $fn eq 'test-harness.txt' ) );
222         move( $file, File::Spec->catfile( $new_docdir, $fn ) );
223     }
224 }
225
226 # ------------------------------------------------------------------
227
228 sub build_pspdfinfo_directory {
229     my $context = shift;
230
231     ##foreach my $format (qw/pdf postscript texinfo info/) {
232     foreach my $format (qw/pdf postscript/) {
233         my $target = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s-%s', $format, $context->{release} ), 'doc' );
234         mkpath( $target, { verbose => ( $verbose || $debug ) } );
235
236         # move documents across
237         foreach my $file (
238             glob(
239                 File::Spec->catfile(
240                     $context->{release_tree},
241                     'doc',
242                     'doc-docbook',
243                     (
244                         ( $format eq 'postscript' )
245                         ? '*.ps'
246                         : ( '*.' . $format )
247                     )
248                 )
249             )
250           )
251         {
252             move( $file, File::Spec->catfile( $target, ( File::Spec->splitpath($file) )[2] ) );
253         }
254     }
255 }
256
257 # ------------------------------------------------------------------
258
259 sub build_main_package_directory {
260     my $context = shift;
261
262     # build the exim package directory path
263     $context->{eximpkgdir} = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s', $context->{release} ) );
264
265     # initially we move the exim-src directory to the new directory name
266     rename( File::Spec->catdir( $context->{release_tree}, 'src' ), $context->{eximpkgdir} )
267       || croak "Rename of src dir failed - $!";
268
269     # add Local subdirectory
270     mkpath( File::Spec->catdir( $context->{eximpkgdir}, 'Local' ), { verbose => ( $verbose || $debug ) } );
271
272     # now add the text docs
273     move_text_docs_into_pkg($context);
274 }
275
276 # ------------------------------------------------------------------
277
278 sub build_package_directories {
279     my $context = shift;
280
281     build_main_package_directory($context);
282     build_pspdfinfo_directory($context);
283 }
284
285 # ------------------------------------------------------------------
286
287 sub do_cleanup {
288     my $context = shift;
289
290     print "Cleaning up\n" if ($verbose);
291     rmtree( $context->{release_tree}, { verbose => $debug } );
292     rmtree( $context->{docbook},      { verbose => $debug } );
293     rmtree( $context->{pkgdirs},      { verbose => $debug } );
294 }
295
296 # ------------------------------------------------------------------
297
298 sub create_tar_files {
299     my $context = shift;
300
301     my $pkgs    = $context->{pkgs};
302     my $pkgdirs = $context->{pkgdirs};
303     foreach my $dir ( glob( File::Spec->catdir( $pkgdirs, ( 'exim*-' . $context->{release} ) ) ) ) {
304         my $dirname = ( File::Spec->splitdir($dir) )[-1];
305         system("tar cfz ${pkgs}/${dirname}.tar.gz  -C ${pkgdirs} ${dirname}");
306         system("tar cfj ${pkgs}/${dirname}.tar.bz2 -C ${pkgdirs} ${dirname}");
307     }
308 }
309
310 # ------------------------------------------------------------------
311 {
312     my $man;
313     my $help;
314     my $context = {
315         pkgname     => 'exim',
316         orig_dir    => File::Spec->curdir(),
317         tmp_dir     => File::Temp->newdir(),
318         webgen_base => "$FindBin::Bin/../../../exim-website",
319     };
320     my $delete;
321     my $cleanup = 1;
322     ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'};
323
324     unless (
325         GetOptions(
326             'directory=s'   => \$context->{directory},
327             'webgen_base=s' => \$context->{webgen_base},
328             'verbose!'      => \$verbose,
329             'debug!'        => \$debug,
330             'help|?'        => \$help,
331             'man!'          => \$man,
332             'delete!'       => \$delete,
333             'cleanup!'      => \$cleanup,
334         )
335       )
336     {
337         pod2usage( -exitval => 1, -verbose => 0 );
338     }
339     pod2usage(0) if $help;
340     pod2usage( -verbose => 2 ) if $man;
341
342     get_and_check_version( shift, $context );
343     $context->{tag} = build_tag($context);
344     deal_with_working_directory( $context, $delete );
345     export_git_tree($context);
346     chdir( $context->{directory} ) || die;
347     unpack_tree($context);
348     make_version_script($context);
349     build_documentation($context);
350     build_package_directories($context);
351     create_tar_files($context);
352     do_cleanup($context) if ($cleanup);
353 }
354
355 1;
356
357 __END__
358
359 =head1 NAME
360
361 mk_exim_release.pl - Build an exim release
362
363 =head1 SYNOPSIS
364
365 mk_exim_release.pl [options] version
366
367  Options:
368    --debug             force debug mode (SQL Trace)
369    --verbose           force verbose mode
370    --help              display this help and exits
371    --man               displays man page
372    --directory=dir     dir to package
373    --delete            Delete packaging directory at start
374
375 =head1 OPTIONS
376
377 =over 4
378
379 =item B<--debug>
380
381 Forces debug mode cause all SQL statements generated by L<DBIx::Class>
382 to be output.
383
384 =item B<--verbose>
385
386 Force verbose mode - currently this has no effect
387
388 =item B<--help>
389
390 Display help and exits
391
392 =item B<--man>
393
394 Display man page
395
396 =back
397
398 =head1 DESCRIPTION
399
400 Builds an exim release.
401
402 Starting in a populated git repo that has already been tagged for
403 release, build docs, build packages etc.
404
405 Parameter is the version number to build as - ie 4.72 4.72RC1 etc
406
407 =head1 AUTHOR
408
409 Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>
410
411 =head1 COPYRIGHT
412
413 Copyright 2010 Exim Maintainers. All rights reserved.
414
415 =cut