| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | # Movable Type (r) (C) 2001-2009 Six Apart, Ltd. All Rights Reserved. |
|---|
| 4 | # This code cannot be redistributed without permission from www.sixapart.com. |
|---|
| 5 | # For more information, consult your Movable Type license. |
|---|
| 6 | # |
|---|
| 7 | # $Id$ |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use lib 'extlib', 'lib'; |
|---|
| 11 | use ExtUtils::Manifest qw( maniread manicopy mkmanifest ); |
|---|
| 12 | use File::Copy; |
|---|
| 13 | use File::Find; |
|---|
| 14 | use File::Spec::Functions; |
|---|
| 15 | use File::Basename; |
|---|
| 16 | use Getopt::Long; |
|---|
| 17 | |
|---|
| 18 | my $langlist = ''; |
|---|
| 19 | my $stamp = ''; |
|---|
| 20 | my $package = ''; |
|---|
| 21 | my $license = ''; |
|---|
| 22 | my $silent = 0; |
|---|
| 23 | |
|---|
| 24 | no warnings; |
|---|
| 25 | # WHY, oh why? |
|---|
| 26 | *ExtUtils::Manifest::cp = sub { |
|---|
| 27 | my ($srcFile, $dstFile) = @_; |
|---|
| 28 | my ($perm,$access,$mod) = (stat $srcFile)[2,8,9]; |
|---|
| 29 | copy($srcFile,$dstFile); |
|---|
| 30 | utime $access, $mod, $dstFile; |
|---|
| 31 | # chmod a+rX-w,go-w |
|---|
| 32 | chmod( 0644 | ( $perm & 0111 ? 0111 : 0 ), $dstFile ) |
|---|
| 33 | unless ($^O eq 'MacOS'); |
|---|
| 34 | }; |
|---|
| 35 | use warnings; |
|---|
| 36 | |
|---|
| 37 | # disables resource fork handling for tar command on OS X |
|---|
| 38 | $ENV{COPYFILE_DISABLE} = 'true'; |
|---|
| 39 | |
|---|
| 40 | GetOptions( |
|---|
| 41 | 'language:s' => \$langlist, |
|---|
| 42 | 'package:s' => \$package, |
|---|
| 43 | 'silent' => \$silent, |
|---|
| 44 | 'license:s' => \$license, |
|---|
| 45 | 'stamp:s' => \$stamp, |
|---|
| 46 | ) or die "ERROR: Couldn't get the command-line options"; |
|---|
| 47 | |
|---|
| 48 | my %options; |
|---|
| 49 | |
|---|
| 50 | my $make = 'make'; |
|---|
| 51 | $make .= ' -s' if $silent; |
|---|
| 52 | $ExtUtils::Manifest::Quiet = $silent; |
|---|
| 53 | $ExtUtils::Manifest::Verbose = ! $silent; |
|---|
| 54 | |
|---|
| 55 | my $skip = 'MANIFEST.SKIP'; |
|---|
| 56 | my $skip_bak = "$skip.bak"; |
|---|
| 57 | |
|---|
| 58 | my $orig = eval { |
|---|
| 59 | verbose_command("$make clean"); |
|---|
| 60 | verbose_command("$make lib/MT.pm"); |
|---|
| 61 | require MT; |
|---|
| 62 | $package . '-' . MT->VERSION; |
|---|
| 63 | } or die "ERROR: Failed to get version from package $package"; |
|---|
| 64 | |
|---|
| 65 | my $skip_rules = `cat $skip`; |
|---|
| 66 | if ($skip_rules =~ m/^#\?/m) { # conditional rule |
|---|
| 67 | move($skip, $skip_bak); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | my @languages = split(/,/, $langlist); |
|---|
| 71 | @languages = qw( en_US nl fr de es ja ) unless @languages; |
|---|
| 72 | for my $lang (@languages) { |
|---|
| 73 | $options{language} = $lang; |
|---|
| 74 | $options{package} = $package; |
|---|
| 75 | $options{license} = $license; |
|---|
| 76 | |
|---|
| 77 | print join(" ", @languages), "\n"; |
|---|
| 78 | my $short_lang = $lang; |
|---|
| 79 | # If there are not any _'s in the language double it: xx_XX. |
|---|
| 80 | my $long_lang = ($lang =~ /_/ ? $lang : $lang . '_' . uc($lang)); |
|---|
| 81 | # ..unless we are Japanese. |
|---|
| 82 | $long_lang = 'ja' if ($lang eq 'ja'); |
|---|
| 83 | |
|---|
| 84 | # Override the distribution name if we are given a stamp. |
|---|
| 85 | my $distname = defined $stamp && $stamp ne '' |
|---|
| 86 | ? $stamp |
|---|
| 87 | : $orig . '-' . $long_lang; |
|---|
| 88 | |
|---|
| 89 | print "---------------- Building $lang ----------------\n"; |
|---|
| 90 | verbose_command("$make clean"); # to clean MT.pm |
|---|
| 91 | verbose_command($make); |
|---|
| 92 | |
|---|
| 93 | make_manifest_skip($skip_rules, $skip); |
|---|
| 94 | |
|---|
| 95 | mkmanifest(); |
|---|
| 96 | |
|---|
| 97 | # Copy files for non-GPL license since we will be modifying the content |
|---|
| 98 | manicopy(maniread(), $distname, ( $license eq 'GPL' ? 'best' : 'cp' )); |
|---|
| 99 | |
|---|
| 100 | if (($license || '') ne 'GPL') { |
|---|
| 101 | assign_license( $distname ); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | verbose_command("find $distname -name .exists | xargs rm"); |
|---|
| 105 | verbose_command("chmod +x $distname/*.cgi"); |
|---|
| 106 | verbose_command("chmod +x $distname/tools/*"); |
|---|
| 107 | |
|---|
| 108 | my $options = $silent ? 'cf' : 'cvf'; |
|---|
| 109 | verbose_command("tar $options $distname.tar --wildcards --exclude '*.zip' $distname"); |
|---|
| 110 | $options = $silent ? '-q' : ''; |
|---|
| 111 | verbose_command("gzip $options --best $distname.tar"); |
|---|
| 112 | $options = $silent ? '-rq' : '-r'; |
|---|
| 113 | verbose_command("zip $options $distname.zip $distname -x $distname/extras/\*.gz"); |
|---|
| 114 | |
|---|
| 115 | verbose_command("rm -rf $distname"); |
|---|
| 116 | unlink("MANIFEST"); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | END { |
|---|
| 120 | move($skip_bak, $skip) if -e $skip_bak; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | sub assign_license { |
|---|
| 124 | my ($dir) = @_; |
|---|
| 125 | find( { wanted => \&process_file_for_license, no_chdir => 1 }, |
|---|
| 126 | $dir ); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | sub process_file_for_license { |
|---|
| 130 | my $file = $_; |
|---|
| 131 | |
|---|
| 132 | # skip any '.git', '.svn' directory |
|---|
| 133 | return if $file =~ m! / \. !x; |
|---|
| 134 | |
|---|
| 135 | # must be a file, not a directory |
|---|
| 136 | return unless -f $file; |
|---|
| 137 | |
|---|
| 138 | # must have a source file extension or be a utility script (extensionless) |
|---|
| 139 | return unless $file =~ m! / ( [A-Za-z0-9_.-]+ \. ( pl | php | pm | cgi | js | t ) | [a-z0-9_-] + ) $ !x; |
|---|
| 140 | |
|---|
| 141 | my $content; |
|---|
| 142 | open FIN, "<$file"; |
|---|
| 143 | { |
|---|
| 144 | local $/; |
|---|
| 145 | $content = <FIN>; |
|---|
| 146 | } |
|---|
| 147 | close FIN; |
|---|
| 148 | |
|---|
| 149 | my $open = quotemeta('# Movable Type (r) Open Source (C)'); |
|---|
| 150 | |
|---|
| 151 | # skip file if it doesn't contain the Open Source license |
|---|
| 152 | return unless $content =~ m/$open/; |
|---|
| 153 | |
|---|
| 154 | my $close = quotemeta('# GNU General Public License, version 2.'); |
|---|
| 155 | |
|---|
| 156 | # non-distributable license text |
|---|
| 157 | my $license = <<EOT; |
|---|
| 158 | |
|---|
| 159 | # This code cannot be redistributed without permission from www.sixapart.com. |
|---|
| 160 | # For more information, consult your Movable Type license. |
|---|
| 161 | EOT |
|---|
| 162 | chomp($license); |
|---|
| 163 | |
|---|
| 164 | if ($content =~ s{$open (\d+(-\d+)?).*?$close} |
|---|
| 165 | { '# Movable Type (r) (C) ' . $1 . ' Six Apart, Ltd. All Rights Reserved.' . $license }se) { |
|---|
| 166 | |
|---|
| 167 | ## print "applying license changes to $file...\n"; return; |
|---|
| 168 | |
|---|
| 169 | open FOUT, ">$file"; |
|---|
| 170 | print FOUT $content; |
|---|
| 171 | close FOUT; |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | sub make_manifest_skip { |
|---|
| 176 | my ($rules, $file) = @_; |
|---|
| 177 | my @rules = split /\n/, $rules; |
|---|
| 178 | |
|---|
| 179 | my @result; |
|---|
| 180 | foreach my $rule (@rules) { |
|---|
| 181 | if ($rule =~ m/^#\?([^=]+)=([^ ]+) +(.+)$/) { |
|---|
| 182 | # a conditional rule |
|---|
| 183 | my $var = $1; |
|---|
| 184 | my $val = $2; |
|---|
| 185 | my $skip = $3; |
|---|
| 186 | |
|---|
| 187 | if (($options{$var} || '') eq $val) { |
|---|
| 188 | push @result, $skip; |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | push @result, $rule; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | my $result = join "\n", @result; |
|---|
| 195 | |
|---|
| 196 | local *FOUT; |
|---|
| 197 | open *FOUT, ">$file"; |
|---|
| 198 | print FOUT $result; |
|---|
| 199 | close FOUT; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | sub verbose_command { |
|---|
| 203 | my $command = shift; |
|---|
| 204 | print 'Execute:', " $command" unless $silent; |
|---|
| 205 | system $command; |
|---|
| 206 | if( $? == -1 ) { |
|---|
| 207 | die "ERROR: Failed to execute: $!"; |
|---|
| 208 | } |
|---|
| 209 | elsif( $? & 127 ) { |
|---|
| 210 | die sprintf( "ERROR: Child died with signal %d, with%s coredump\n", |
|---|
| 211 | ( $? & 127 ), ( $? & 128 ? '' : 'out' ) |
|---|
| 212 | ); |
|---|
| 213 | } |
|---|
| 214 | else { |
|---|
| 215 | # printf "Child exited with value %d\n", $? >> 8; |
|---|
| 216 | } |
|---|
| 217 | return $command; |
|---|
| 218 | } |
|---|