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