Release script now generates the HTML documentation
[exim.git] / release-process / scripts / mk_exim_release.pl
1 #!/usr/bin/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     mkpath( $context->{directory}, { verbose => ( $verbose || $debug ) } );
73 }
74
75 # ------------------------------------------------------------------
76
77 sub export_git_tree {
78     my $context = shift;
79
80     # build git command
81     my $archive_file = sprintf( '%s/%s-%s.tar', $context->{tmp_dir}, $context->{pkgname}, $context->{release} );
82     $context->{tmp_archive_file} = $archive_file;
83     my @cmd = ( 'git', 'archive', '--format=tar', "--output=$archive_file", $context->{tag} );
84
85     # run git command
86     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
87     system(@cmd) == 0 || croak "Export failed";
88 }
89
90 # ------------------------------------------------------------------
91
92 sub unpack_tree {
93     my $context = shift;
94
95     die "Cannot see archive file\n" unless ( -f $context->{tmp_archive_file} );
96     my @cmd = ( 'tar', 'xf', $context->{tmp_archive_file} );
97
98     # run  command
99     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
100     system(@cmd) == 0 || croak "Unpack failed";
101 }
102
103 # ------------------------------------------------------------------
104
105 sub build_html_documentation {
106     my $context = shift;
107
108     my $genpath   = $context->{webgen_base} . '/script/gen.pl';
109     my $templates = $context->{webgen_base} . '/templates';
110     my $dir       = 'html';
111     mkdir($dir);
112
113     my @cmd = (
114         $genpath,                     '--spec',    'doc/doc-docbook/spec.xml', '--filter',
115         'doc/doc-docbook/filter.xml', '--latest',  $context->{trelease},       '--tmpl',
116         $templates,                   '--docroot', $dir
117     );
118
119     print "Executing ", join( ' ', @cmd ), "\n";
120     system(@cmd);
121
122     # move directory into right place
123     rename( sprintf( 'html/exim-html-%s', $context->{trelease} ), sprintf( 'exim-html-%s', $context->{release} ) );
124 }
125
126 # ------------------------------------------------------------------
127
128 sub build_documentation {
129     my $context = shift;
130
131     system("cd doc/doc-docbook && ./OS-Fixups && make everything") == 0
132       || croak "Doc build failed";
133
134     build_html_documentation($context);
135 }
136
137 # ------------------------------------------------------------------
138
139 sub move_text_docs_into_pkg {
140     my $context = shift;
141
142     my $old_docdir = 'doc/doc-docbook';
143     my $new_docdir = File::Spec->catdir( $context->{pkgdir}, 'doc' );
144     mkpath( $new_docdir, { verbose => ( $verbose || $debug ) } );
145
146     # move generated documents from docbook stuff
147     foreach my $file (qw/exim.8 spec.txt filter.txt/) {
148         move( File::Spec->catfile( $old_docdir, $file ), File::Spec->catfile( $new_docdir, $file ) );
149     }
150
151     # move text documents across
152     foreach my $file ( glob( File::Spec->catfile( 'doc/doc-txt', '*' ) ) ) {
153
154         # skip a few we dont want
155         my $fn = ( File::Spec->splitpath($file) )[2];
156         next
157           if ( ( $fn eq 'ABOUT' )
158             || ( $fn eq 'ChangeLog.0' )
159             || ( $fn eq 'test-harness.txt' ) );
160         move( $file, File::Spec->catfile( $new_docdir, $fn ) );
161     }
162 }
163
164 # ------------------------------------------------------------------
165
166 sub build_pspdfinfo_directory {
167     my $context = shift;
168
169     ##foreach my $format (qw/pdf postscript texinfo info/) {
170     foreach my $format (qw/pdf postscript/) {
171         my $dir = sprintf( 'exim-%s-%s', $format, $context->{release} );
172         my $target = File::Spec->catdir( $dir, 'doc' );
173         mkpath( $target, { verbose => ( $verbose || $debug ) } );
174
175         # move documents across
176         foreach my $file (
177             glob(
178                 File::Spec->catfile(
179                     'doc/doc-docbook',
180                     (
181                         ( $format eq 'postscript' )
182                         ? '*.ps'
183                         : ( '*.' . $format )
184                     )
185                 )
186             )
187           )
188         {
189             my $fn = ( File::Spec->splitpath($file) )[2];
190             move( $file, File::Spec->catfile( $target, $fn ) );
191         }
192     }
193 }
194
195 # ------------------------------------------------------------------
196
197 sub build_main_package_directory {
198     my $context = shift;
199
200     # initially we move the exim-src directory to the new directory name
201     my $pkgdir = sprintf( 'exim-%s', $context->{release} );
202     $context->{pkgdir} = $pkgdir;
203     rename( 'src', $pkgdir ) || croak "Rename of src dir failed - $!";
204
205     # add Local subdirectory
206     my $target = File::Spec->catdir( $pkgdir, 'Local' );
207     mkpath( $target, { verbose => ( $verbose || $debug ) } );
208
209     # now add the text docs
210     move_text_docs_into_pkg($context);
211 }
212
213 # ------------------------------------------------------------------
214
215 sub build_package_directories {
216     my $context = shift;
217
218     build_main_package_directory($context);
219     build_pspdfinfo_directory($context);
220 }
221
222 # ------------------------------------------------------------------
223
224 sub create_tar_files {
225     my $context = shift;
226
227     foreach my $dir ( glob( 'exim*-' . $context->{release} ) ) {
228         system("tar cfz ${dir}.tar.gz ${dir}");
229         system("tar cfj ${dir}.tar.bz2 ${dir}");
230     }
231 }
232
233 # ------------------------------------------------------------------
234 {
235     my $man;
236     my $help;
237     my $context = {
238         pkgname     => 'exim',
239         orig_dir    => File::Spec->curdir(),
240         tmp_dir     => File::Temp->newdir(),
241         webgen_base => "$FindBin::Bin/../../../exim-website",
242     };
243     my $delete;
244     ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'};
245
246     unless (
247         GetOptions(
248             'directory=s'   => \$context->{directory},
249             'webgen_base=s' => \$context->{webgen_base},
250             'verbose!'      => \$verbose,
251             'debug!'        => \$debug,
252             'help|?'        => \$help,
253             'man!'          => \$man,
254             'delete!'       => \$delete,
255         )
256       )
257     {
258         pod2usage( -exitval => 1, -verbose => 0 );
259     }
260     pod2usage(0) if $help;
261     pod2usage( -verbose => 2 ) if $man;
262
263     get_and_check_version( shift, $context );
264     $context->{tag} = build_tag($context);
265     deal_with_working_directory( $context, $delete );
266     export_git_tree($context);
267     chdir( $context->{directory} ) || die;
268     unpack_tree($context);
269     build_documentation($context);
270     build_package_directories($context);
271     create_tar_files($context);
272 }
273
274 1;
275
276 __END__
277
278 =head1 NAME
279
280 mk_exim_release.pl - Build an exim release
281
282 =head1 SYNOPSIS
283
284 mk_exim_release.pl [options] version
285
286  Options:
287    --debug             force debug mode (SQL Trace)
288    --verbose           force verbose mode
289    --help              display this help and exits
290    --man               displays man page
291    --directory=dir     dir to package
292    --delete            Delete packaging directory at start
293
294 =head1 OPTIONS
295
296 =over 4
297
298 =item B<--debug>
299
300 Forces debug mode cause all SQL statements generated by L<DBIx::Class>
301 to be output.
302
303 =item B<--verbose>
304
305 Force verbose mode - currently this has no effect
306
307 =item B<--help>
308
309 Display help and exits
310
311 =item B<--man>
312
313 Display man page
314
315 =back
316
317 =head1 DESCRIPTION
318
319 Builds an exim release.
320
321 Starting in a populated git repo that has already been tagged for
322 release, build docs, build packages etc.
323
324 Parameter is the version number to build as - ie 4.72 4.72RC1 etc
325
326 =head1 AUTHOR
327
328 Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>
329
330 =head1 COPYRIGHT
331
332 Copyright 2010 Exim Maintainers. All rights reserved.
333
334 =cut