Incremental improvement of release build script
[exim.git] / release-process / scripts / mk_exim_release.pl
CommitLineData
46c573de 1#!/usr/bin/env perl
8a483da6
NM
2#
3# $Cambridge: exim/release-process/scripts/mk_exim_release.pl,v 1.1 2010/06/03 12:00:38 nm4 Exp $
4#
5use strict;
6use warnings;
7use Carp;
8use File::Copy;
9use File::Spec;
10use File::Path;
8f29c950 11use File::Temp;
be914e6c 12use FindBin;
8a483da6
NM
13use Getopt::Long;
14use Pod::Usage;
15
16my $debug = 0;
17my $verbose = 0;
18
19# ------------------------------------------------------------------
20
21sub get_and_check_version {
22 my $release = shift;
be914e6c 23 my $context = shift;
8a483da6
NM
24
25 # make sure this looks like a real release version
8f29c950 26 # which should (currently) be 4.xx or 4.xx_RCx
8a483da6
NM
27 unless ( $release =~ /^(4\.\d\d(?:_RC\d+)?)$/ ) {
28 croak "The given version number does not look right - $release";
29 }
be914e6c
NM
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;
8a483da6
NM
36}
37
38# ------------------------------------------------------------------
39
8f29c950 40sub build_tag {
8a483da6
NM
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
53sub 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
46c573de 72 # create base directory
8a483da6 73 mkpath( $context->{directory}, { verbose => ( $verbose || $debug ) } );
46c573de
NM
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 }
8a483da6
NM
80}
81
82# ------------------------------------------------------------------
83
8f29c950 84sub export_git_tree {
8a483da6
NM
85 my $context = shift;
86
8f29c950
NM
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} );
8a483da6 91
8f29c950 92 # run git command
8a483da6
NM
93 print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
94 system(@cmd) == 0 || croak "Export failed";
95}
96
97# ------------------------------------------------------------------
98
8f29c950
NM
99sub unpack_tree {
100 my $context = shift;
101
102 die "Cannot see archive file\n" unless ( -f $context->{tmp_archive_file} );
46c573de 103 my @cmd = ( 'tar', 'xf', $context->{tmp_archive_file}, '-C', $context->{release_tree} );
8f29c950
NM
104
105 # run command
106 print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
107 system(@cmd) == 0 || croak "Unpack failed";
108}
109
110# ------------------------------------------------------------------
111
be914e6c
NM
112sub build_html_documentation {
113 my $context = shift;
114
115 my $genpath = $context->{webgen_base} . '/script/gen.pl';
116 my $templates = $context->{webgen_base} . '/templates';
46c573de
NM
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
be914e6c
NM
121 mkdir($dir);
122
46c573de
NM
123 my @cmd =
124 ( $genpath, '--spec', $spec, '--filter', $filter, '--latest', $context->{trelease}, '--tmpl', $templates, '--docroot', $dir );
be914e6c
NM
125
126 print "Executing ", join( ' ', @cmd ), "\n";
127 system(@cmd);
128
129 # move directory into right place
46c573de
NM
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
140sub 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 }
be914e6c
NM
157}
158
159# ------------------------------------------------------------------
160
8a483da6 161sub build_documentation {
be914e6c
NM
162 my $context = shift;
163
46c573de
NM
164 my $docdir = File::Spec->catdir( $context->{release_tree}, 'doc', 'doc-docbook' );
165 system("cd '$docdir' && ./OS-Fixups && make everything") == 0
8a483da6 166 || croak "Doc build failed";
be914e6c 167
46c573de 168 copy_docbook_files($context);
be914e6c 169 build_html_documentation($context);
8a483da6
NM
170}
171
172# ------------------------------------------------------------------
173
174sub move_text_docs_into_pkg {
175 my $context = shift;
176
46c573de
NM
177 my $old_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc', 'doc-docbook' );
178 my $new_docdir = File::Spec->catdir( $context->{eximpkgdir}, 'doc' );
8a483da6
NM
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
8f29c950 187 foreach my $file ( glob( File::Spec->catfile( 'doc/doc-txt', '*' ) ) ) {
8a483da6
NM
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
201sub build_pspdfinfo_directory {
202 my $context = shift;
203
204 ##foreach my $format (qw/pdf postscript texinfo info/) {
205 foreach my $format (qw/pdf postscript/) {
46c573de 206 my $target = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s-%s', $format, $context->{release} ), 'doc' );
8a483da6
NM
207 mkpath( $target, { verbose => ( $verbose || $debug ) } );
208
209 # move documents across
210 foreach my $file (
211 glob(
212 File::Spec->catfile(
46c573de
NM
213 $context->{release_tree},
214 'doc',
215 'doc-docbook',
8a483da6
NM
216 (
217 ( $format eq 'postscript' )
218 ? '*.ps'
219 : ( '*.' . $format )
220 )
221 )
222 )
223 )
224 {
46c573de 225 move( $file, File::Spec->catfile( $target, ( File::Spec->splitpath($file) )[2] ) );
8a483da6
NM
226 }
227 }
228}
229
230# ------------------------------------------------------------------
231
8a483da6
NM
232sub build_main_package_directory {
233 my $context = shift;
234
46c573de
NM
235 # build the exim package directory path
236 $context->{eximpkgdir} = File::Spec->catdir( $context->{pkgdirs}, sprintf( 'exim-%s', $context->{release} ) );
237
8a483da6 238 # initially we move the exim-src directory to the new directory name
46c573de
NM
239 rename( File::Spec->catdir( $context->{release_tree}, 'src' ), $context->{eximpkgdir} )
240 || croak "Rename of src dir failed - $!";
8a483da6
NM
241
242 # add Local subdirectory
46c573de 243 mkpath( File::Spec->catdir( $context->{eximpkgdir}, 'Local' ), { verbose => ( $verbose || $debug ) } );
8a483da6
NM
244
245 # now add the text docs
246 move_text_docs_into_pkg($context);
247}
248
249# ------------------------------------------------------------------
250
251sub build_package_directories {
252 my $context = shift;
253
254 build_main_package_directory($context);
255 build_pspdfinfo_directory($context);
8a483da6
NM
256}
257
258# ------------------------------------------------------------------
259
46c573de
NM
260sub 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
8a483da6
NM
271sub create_tar_files {
272 my $context = shift;
273
46c573de
NM
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}");
8a483da6
NM
280 }
281}
282
283# ------------------------------------------------------------------
284{
285 my $man;
286 my $help;
287 my $context = {
be914e6c
NM
288 pkgname => 'exim',
289 orig_dir => File::Spec->curdir(),
290 tmp_dir => File::Temp->newdir(),
291 webgen_base => "$FindBin::Bin/../../../exim-website",
8a483da6
NM
292 };
293 my $delete;
46c573de 294 my $cleanup = 1;
8f29c950 295 ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'};
8a483da6
NM
296
297 unless (
298 GetOptions(
be914e6c
NM
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,
46c573de 306 'cleanup!' => \$cleanup,
8a483da6
NM
307 )
308 )
309 {
310 pod2usage( -exitval => 1, -verbose => 0 );
311 }
312 pod2usage(0) if $help;
313 pod2usage( -verbose => 2 ) if $man;
314
be914e6c
NM
315 get_and_check_version( shift, $context );
316 $context->{tag} = build_tag($context);
8a483da6 317 deal_with_working_directory( $context, $delete );
8f29c950 318 export_git_tree($context);
8a483da6 319 chdir( $context->{directory} ) || die;
8f29c950 320 unpack_tree($context);
8a483da6
NM
321 build_documentation($context);
322 build_package_directories($context);
323 create_tar_files($context);
46c573de 324 do_cleanup($context) if ($cleanup);
8a483da6
NM
325}
326
3271;
328
329__END__
330
331=head1 NAME
332
333mk_exim_release.pl - Build an exim release
334
335=head1 SYNOPSIS
336
337mk_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
8a483da6
NM
345 --delete Delete packaging directory at start
346
347=head1 OPTIONS
348
349=over 4
350
351=item B<--debug>
352
353Forces debug mode cause all SQL statements generated by L<DBIx::Class>
354to be output.
355
356=item B<--verbose>
357
358Force verbose mode - currently this has no effect
359
360=item B<--help>
361
362Display help and exits
363
364=item B<--man>
365
366Display man page
367
368=back
369
370=head1 DESCRIPTION
371
372Builds an exim release.
373
8f29c950
NM
374Starting in a populated git repo that has already been tagged for
375release, build docs, build packages etc.
8a483da6
NM
376
377Parameter is the version number to build as - ie 4.72 4.72RC1 etc
378
379=head1 AUTHOR
380
381Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>
382
383=head1 COPYRIGHT
384
8f29c950 385Copyright 2010 Exim Maintainers. All rights reserved.
8a483da6
NM
386
387=cut