Updated mk_exim_release.pl to work with git
[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 Getopt::Long;
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
23     # make sure this looks like a real release version
24     # which should (currently) be 4.xx or 4.xx_RCx
25     unless ( $release =~ /^(4\.\d\d(?:_RC\d+)?)$/ ) {
26         croak "The given version number does not look right - $release";
27     }
28     return $1;    # untainted here...
29 }
30
31 # ------------------------------------------------------------------
32
33 sub build_tag {
34     my $context = shift;
35
36     # The CVS tag consists of exim-$version where $version
37     # is the version number with . replaced with _
38     my $modversion = $context->{release};
39     $modversion =~ tr/0-9RC/_/cs;
40
41     return sprintf( 'exim-%s', $modversion );
42 }
43
44 # ------------------------------------------------------------------
45
46 sub deal_with_working_directory {
47     my $context = shift;
48     my $delete  = shift;
49
50     # Set default directory
51     $context->{directory} ||= File::Spec->rel2abs( sprintf( 'exim-packaging-%s', $context->{release} ) );
52     my $directory = $context->{directory};
53
54     # ensure the working directory is not in place
55     if ( -d $directory ) {
56         if ($delete) {
57             print "Deleting existing $directory\n" if ($verbose);
58             rmtree( $directory, { verbose => $debug } );
59         }
60         if ( -d $directory ) {
61             croak "Working directory $directory exists";
62         }
63     }
64
65     mkpath( $context->{directory}, { verbose => ( $verbose || $debug ) } );
66 }
67
68 # ------------------------------------------------------------------
69
70 sub export_git_tree {
71     my $context = shift;
72
73     # build git command
74     my $archive_file = sprintf( '%s/%s-%s.tar', $context->{tmp_dir}, $context->{pkgname}, $context->{release} );
75     $context->{tmp_archive_file} = $archive_file;
76     my @cmd = ( 'git', 'archive', '--format=tar', "--output=$archive_file", $context->{tag} );
77
78     # run git command
79     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
80     system(@cmd) == 0 || croak "Export failed";
81 }
82
83 # ------------------------------------------------------------------
84
85 sub unpack_tree {
86     my $context = shift;
87
88     die "Cannot see archive file\n" unless ( -f $context->{tmp_archive_file} );
89     my @cmd = ( 'tar', 'xf', $context->{tmp_archive_file} );
90
91     # run  command
92     print( "Running: ", join( ' ', @cmd ), "\n" ) if ($verbose);
93     system(@cmd) == 0 || croak "Unpack failed";
94 }
95
96 # ------------------------------------------------------------------
97
98 sub build_documentation {
99     system("cd doc/doc-docbook && ./OS-Fixups && make everything") == 0
100       || croak "Doc build failed";
101 }
102
103 # ------------------------------------------------------------------
104
105 sub move_text_docs_into_pkg {
106     my $context = shift;
107
108     my $old_docdir = 'doc/doc-docbook';
109     my $new_docdir = File::Spec->catdir( $context->{pkgdir}, 'doc' );
110     mkpath( $new_docdir, { verbose => ( $verbose || $debug ) } );
111
112     # move generated documents from docbook stuff
113     foreach my $file (qw/exim.8 spec.txt filter.txt/) {
114         move( File::Spec->catfile( $old_docdir, $file ), File::Spec->catfile( $new_docdir, $file ) );
115     }
116
117     # move text documents across
118     foreach my $file ( glob( File::Spec->catfile( 'doc/doc-txt', '*' ) ) ) {
119
120         # skip a few we dont want
121         my $fn = ( File::Spec->splitpath($file) )[2];
122         next
123           if ( ( $fn eq 'ABOUT' )
124             || ( $fn eq 'ChangeLog.0' )
125             || ( $fn eq 'test-harness.txt' ) );
126         move( $file, File::Spec->catfile( $new_docdir, $fn ) );
127     }
128 }
129
130 # ------------------------------------------------------------------
131
132 sub build_pspdfinfo_directory {
133     my $context = shift;
134
135     ##foreach my $format (qw/pdf postscript texinfo info/) {
136     foreach my $format (qw/pdf postscript/) {
137         my $dir = sprintf( 'exim-%s-%s', $format, $context->{release} );
138         my $target = File::Spec->catdir( $dir, 'doc' );
139         mkpath( $target, { verbose => ( $verbose || $debug ) } );
140
141         # move documents across
142         foreach my $file (
143             glob(
144                 File::Spec->catfile(
145                     'doc/doc-docbook',
146                     (
147                         ( $format eq 'postscript' )
148                         ? '*.ps'
149                         : ( '*.' . $format )
150                     )
151                 )
152             )
153           )
154         {
155             my $fn = ( File::Spec->splitpath($file) )[2];
156             move( $file, File::Spec->catfile( $target, $fn ) );
157         }
158     }
159 }
160
161 # ------------------------------------------------------------------
162
163 sub build_html_directory {
164     my $context = shift;
165
166     my $dir = sprintf( 'exim-%s-%s', 'html', $context->{release} );
167     my $target = File::Spec->catdir( $dir, 'doc', 'html' );
168     mkpath( $target, { verbose => ( $verbose || $debug ) } );
169
170     # move documents across
171     move( File::Spec->catdir( 'doc/doc-docbook', 'spec_html' ), File::Spec->catdir( $target, 'spec_html' ) );
172     foreach my $file ( glob( File::Spec->catfile( 'doc/doc-docbook', '*.html' ) ) ) {
173         my $fn = ( File::Spec->splitpath($file) )[2];
174         move( $file, File::Spec->catfile( $target, $fn ) );
175     }
176 }
177
178 # ------------------------------------------------------------------
179
180 sub build_main_package_directory {
181     my $context = shift;
182
183     # initially we move the exim-src directory to the new directory name
184     my $pkgdir = sprintf( 'exim-%s', $context->{release} );
185     $context->{pkgdir} = $pkgdir;
186     rename( 'src', $pkgdir ) || croak "Rename of src dir failed - $!";
187
188     # add Local subdirectory
189     my $target = File::Spec->catdir( $pkgdir, 'Local' );
190     mkpath( $target, { verbose => ( $verbose || $debug ) } );
191
192     # now add the text docs
193     move_text_docs_into_pkg($context);
194 }
195
196 # ------------------------------------------------------------------
197
198 sub build_package_directories {
199     my $context = shift;
200
201     build_main_package_directory($context);
202     build_pspdfinfo_directory($context);
203     build_html_directory($context);
204 }
205
206 # ------------------------------------------------------------------
207
208 sub create_tar_files {
209     my $context = shift;
210
211     foreach my $dir ( glob( 'exim*-' . $context->{release} ) ) {
212         system("tar cfz ${dir}.tar.gz ${dir}");
213         system("tar cfj ${dir}.tar.bz2 ${dir}");
214     }
215 }
216
217 # ------------------------------------------------------------------
218 {
219     my $man;
220     my $help;
221     my $context = {
222         pkgname  => 'exim',
223         orig_dir => File::Spec->curdir(),
224         tmp_dir  => File::Temp->newdir(),
225     };
226     my $delete;
227     ##$ENV{'PATH'} = '/opt/local/bin:' . $ENV{'PATH'};
228
229     unless (
230         GetOptions(
231             'directory=s' => \$context->{directory},
232             'verbose!'    => \$verbose,
233             'debug!'      => \$debug,
234             'help|?'      => \$help,
235             'man!'        => \$man,
236             'delete!'     => \$delete,
237         )
238       )
239     {
240         pod2usage( -exitval => 1, -verbose => 0 );
241     }
242     pod2usage(0) if $help;
243     pod2usage( -verbose => 2 ) if $man;
244
245     $context->{release} = get_and_check_version(shift);
246     $context->{tag}     = build_tag($context);
247     deal_with_working_directory( $context, $delete );
248     export_git_tree($context);
249     chdir( $context->{directory} ) || die;
250     unpack_tree($context);
251     build_documentation($context);
252     build_package_directories($context);
253     create_tar_files($context);
254 }
255
256 1;
257
258 __END__
259
260 =head1 NAME
261
262 mk_exim_release.pl - Build an exim release
263
264 =head1 SYNOPSIS
265
266 mk_exim_release.pl [options] version
267
268  Options:
269    --debug             force debug mode (SQL Trace)
270    --verbose           force verbose mode
271    --help              display this help and exits
272    --man               displays man page
273    --directory=dir     dir to package
274    --delete            Delete packaging directory at start
275
276 =head1 OPTIONS
277
278 =over 4
279
280 =item B<--debug>
281
282 Forces debug mode cause all SQL statements generated by L<DBIx::Class>
283 to be output.
284
285 =item B<--verbose>
286
287 Force verbose mode - currently this has no effect
288
289 =item B<--help>
290
291 Display help and exits
292
293 =item B<--man>
294
295 Display man page
296
297 =back
298
299 =head1 DESCRIPTION
300
301 Builds an exim release.
302
303 Starting in a populated git repo that has already been tagged for
304 release, build docs, build packages etc.
305
306 Parameter is the version number to build as - ie 4.72 4.72RC1 etc
307
308 =head1 AUTHOR
309
310 Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>
311
312 =head1 COPYRIGHT
313
314 Copyright 2010 Exim Maintainers. All rights reserved.
315
316 =cut