修正PackageApplication -o参数不能指定相对目录的问题

PackageApplication是旧版本Xcode提供的导出IPA的脚本。
新版本的Xcode已经废弃了,取而代之是建议使用xcodebuild archive。

最近,在使用archive的时候,却遇到了个小问题,每次archive都要全部编译一次代码。而xcodebuild build命令则是默认增量编译的。于是,重新拿起PackageApplication。却发现,这货的-o参数竟然不能支持相对目录。
只好认真读一下这个脚本了。
发现问题在于:它先创建了一个临时目录,把文件拷贝过去,然后cd到那个临时目录再执行zip命令压缩,于是,我们给的-o参数,如果是相对目录的话,就变成是相对这个临时目录了。
找到问题的原因,修改一下就很简单了。在cd之前,先用abs_path获取这个相对目录的绝对路径就好了。


附上修改后的全部脚本内容

#!/usr/bin/perl
#
#    PackageApplication
#    
#    Copyright (c) 2009-2012 Apple Inc.  All rights reserved.
#
#    Package an iPhone Application into an .ipa wrapper
#

use Pod::Usage;
use Getopt::Long;
use File::Temp qw/ tempdir :POSIX /;
use File::Basename;
use File::Path qw/ remove_tree /;
use Cwd qw/ chdir /;
use Cwd qw/ abs_path/;

print "\n\n\nwarning: PackageApplication is deprecated, use `xcodebuild -exportArchive` instead.\n\n\n";

$|=1;   # don't buffer stdout

my $program = $0;

my %opt = ();
GetOptions ( \%opt,
             "sign|s=s",
             "embed|e=s",
             "output|o=s",
             "symbols=s",
             "verbose|v!",
             "help|h|?",
             "man",
             "plugin=s@" => \@plugins,
             ) or pod2usage(2);

pod2usage(2) if $opt{help};
pod2usage(-exitstatus => 0, -verbose => 2) if $opt{man};

fatal("An application was not specified.") unless $ARGV[0];
my $origApp = shift @ARGV;
fatal("Specified application doesn't exist or isn't a bundle directory : '$origApp'") unless -d $origApp;

if ( $opt{verbose} ) {
    print "Packaging application: '$origApp'\n";
    print "Arguments: ";
    while( ($key,$value) = each %opt)
    {
        print "$key=$value  ";
    }
    print "\n";
    print "Environment variables:\n";
    while( ($key,$value) = each %ENV)
    {
        print "$key = $value\n";
    }
    print "\n";
}

# check any plugins that might be specified 
foreach $plugin (@plugins) {
    print "Plugin: '$plugin'\n" if $opt{verbose};
    fatal("Specified plugin doesn't exist or isn't a bundle directory : '$plugin'") unless -d $plugin;
}

# setup the output name if it isn't specified

# setup the output if it isn't specified 
if ( !defined($opt{output})  ) {
    $opt{output} = dirname($origApp).'/'.basename($origApp, ".app")."\.ipa";
}
$opt{output} = abs_path($opt{output});
print "Output directory: '$opt{output}'\n" if $opt{verbose};
# Make sure we have a temp dir to work with
my $tmpDir = tempdir( CLEANUP => !defined($opt{verbose}) );
print "Temporary Directory: '$tmpDir'  (will NOT be deleted on exit when verbose set)\n" if $opt{verbose};


################## Start Packaging #####################

### Step One : Make a copy of the application (and any plugins)
my $appName = basename($origApp);
chomp $appName;
my $destAppDir = "$tmpDir/Payload";
my $destApp = "$destAppDir/$appName";

mkdir $destAppDir;
fatal("Unable to create directory : '$destAppDir'") unless -d $destAppDir;

runCmd("/bin/cp", "-Rp", $origApp, $destAppDir);
fatal("Unable to copy application '$origApp' into '$destAppDir'") unless -e $destApp;

foreach $plugin (@plugins) {
    my $pluginName = basename($plugin);
    chomp $pluginName;
    my $destPlugin = "$destAppDir/$pluginName";
    
    my $result = runCmd("/usr/bin/codesign", "--verify", "-vvvv", , $plugin );
    if ( $result !~ /valid on disk/ ) {
        fatal("Codesign check fails : $result\n");
    }
    
    runCmd("/bin/cp", "-Rp", $plugin, $destAppDir);
    fatal("Unable to copy application '$plugin' into '$destAppDir'") unless -e $destPlugin;
}

if ( $opt{symbols} ) {
    my $destSymbols = "$tmpDir/Symbols";
    runCmd("/bin/cp", "-Rp", $opt{symbols}, $destSymbols);
    fatal("Unable to copy symbols '$opt{symbols}' into '$destSymbols'") unless -e $destSymbols;
}

### Step Two : recode sign it if necessary

if ( $opt{verbose} ) {
    print "### Checking original app\n";
    my $result = runCmd("/usr/bin/codesign", "--verify", "-vvvv", , $origApp );
    if ( $result !~ /valid on disk/ ) {
        print "Codesign check fails : $result\n";
    }
    print "Done checking the original app\n";
}


if ( defined $opt{sign} ) {
    if ( $opt{embed} ) {
        print "### Embedding '$opt{embed}'\n" if $opt{verbose};
        runCmd("/bin/rm", "-rf", "$destApp/embedded.mobileprovision" );
        fatal("Unable to remove '$destApp/embedded.mobileprovision'\n") if ( -e "$destApp/embedded.mobileprovision" );
        runCmd("/bin/cp", "-rp", $opt{embed}, "$destApp/embedded.mobileprovision");
        fatal("Unable to copy '$opt{embed}' to '$destApp/embedded.mobileprovision'\n") unless ( -e "$destApp/embedded.mobileprovision" );
    }

    my $entitlements_plist = File::Temp::tempnam($tmpDir, "entitlements_plist");

    # If re-signing with a distribution profile and get-task-allow is
    # true, the app store will reject the submission. Setting
    # get-task-allow to false here.
    if ( $opt{sign} =~ /^i(Phone|OS) Distribution/ ) {
        my $entitlements_raw = File::Temp::tempnam($tmpDir, "entitlements_raw");
        runCmd("/usr/bin/codesign", "-d", "--entitlements", $entitlements_raw, $destApp);
        $? == 0 or fatal("Failed to read entitlements from '$destApp'");
        if ( -e $entitlements_raw ) {
            $plist = read_raw_entitlements($entitlements_raw);
            unlink($entitlements_raw) or fatal("Cannot delete '$entitlements_raw': $!");

            open(my $ofh, '>', $entitlements_plist) or fatal("Cannot open '$entitlements_plist': $!");
            print $ofh $plist or fatal("Cannot write entitlements to '$entitlements_plist': $!");
            close($ofh) or fatal("Cannot close file handle for '$entitlements_plist': $!");

            runCmd('/usr/libexec/PlistBuddy', '-c', 'Set :get-task-allow NO', $entitlements_plist);
            # PlistBuddy will fail if get-task-allow doesn't exist. That's okay.
            runCmd('/usr/bin/plutil', '-lint', $entitlements_plist);
            $? == 0 or fatal("Invalid plist at '$entitlements_plist'");
        }
    }

    my @codesign_args = ("/usr/bin/codesign", "--force", "--preserve-metadata=identifier,entitlements,resource-rules",
                         "--sign", $opt{sign},
                         "--resource-rules=$destApp/ResourceRules.plist");

    if ( -e $entitlements_plist ) {
        push(@codesign_args, '--entitlements');
        push(@codesign_args, $entitlements_plist);
    }

    push(@codesign_args, $destApp);
    
    print "### Codesigning '$opt{embed}' with '$opt{sign}'\n" if $opt{verbose};
    my $codesign_output = runCmd(@codesign_args);
    $? == 0 or fatal("@codesign_args failed with error " . ($? >> 8) . ". Output: $codesign_output");

    if ( -e $entitlements_plist ) {
        unlink($entitlements_plist) or fatal("Cannot delete '$entitlements_plist': $!");
    }
}

### Step Three : zip up the package
remove_tree("$opt{output}");
fatal("Unable to remove older '$opt{output}'") if ( -e "$opt{output}" );

chdir $tmpDir;
if($opt{verbose}) {
    runCmd("/usr/bin/zip", "--symlinks", "--verbose", "--recurse-paths", "$opt{output}", ".");
} else {
    runCmd("/usr/bin/zip", "--symlinks", "--quiet", "--recurse-paths", "$opt{output}", ".");
}

fatal("Unable to create '$opt{output}'") if ( ! -e "$opt{output}" );

print "Results at '$opt{output}' \n" if $opt{verbose};

################## Finished ############################

exit 0;

########################################################

sub fatal {
    my ($msg) = @_;
    
    print STDERR "error: $msg\n";
    
    exit 1;
}

use POSIX qw(:sys_wait_h);

sub runCmd {
    my (@cmds) = @_;
    
    my $output = ();
    if ( $opt{verbose} ) {
        my $_cmd = join(" ", @cmds);
        print "+ $_cmd\n" ;
    }
    my $program = shift @cmds;
    
    my ($readme, $writeme);
    pipe $readme, $writeme;
    my $pid = fork;
    defined $pid or die "cannot fork: $!";
    if ( $pid == 0 ) {
        # child
        open(STDOUT, ">&=", $writeme) or die "can't redirect STDOUT: $!";
        open(STDERR, ">&=", $writeme) or die "can't redirect STDERR: $!";
        close $readme;
        exec($program, @cmds) or die "can't run $program: $!";
    }

    close $writeme;
    while(<$readme>) {
        $output .= $_;
    }
    close $readme;
    
    my $waitpid_ret;
    do { $waitpid_ret = waitpid($pid, 0); } while ( $waitpid_ret == 0 );
    $waitpid_ret == $pid or die "waitpid returned $waitpid_ret: $!";
    my $return_code = $? >> 8;
    
    $opt{verbose} and print "Program $program returned $return_code : [$output]\n";

# Let clients handle error checking.
#    $return_code == 0 or fatal("Program $program failed with return code $return_code.");
    
    return $output;
}

# Param: path to codesign --entitlements output file. Returns xml plist in a string.
sub read_raw_entitlements {
    my $entitlements_raw = shift;

    open(my $ifh, '<', $entitlements_raw) or fatal("Cannot open '$entitlements_raw': $!");

    read($ifh, my $format_tag, 4) or fatal("Cannot read format tag from '$entitlements_raw': $!");
    $format_tag = unpack('H*', $format_tag);
    my $expected_format_tag = 'fade7171';
    $format_tag eq $expected_format_tag
        or fatal("Format tag '0x$format_tag' does not match '0x$expected_format_tag'.");

    read($ifh, my $plist_size, 4) or fatal("Cannot read plist size from '$entitlements_raw': $!");
    $plist_size = unpack('L>', $plist_size);
    read($ifh, my $plist, $plist_size) or fatal("Cannot read $plist_size bytes of plist data from '$entitlements_raw': $!");

    close($ifh) or fatal("Cannot close file handle for '$entitlements_raw': $!");

    return $plist;
}

__END__
=head1 NAME

PackageApplication - prepare an application for submission to AppStore or installation by iTunes.

=head1 SYNOPSIS

PackageApplication [-s signature] application [-o output_directory] [-verbose] [-plugin plugin] || -man || -help 

Options:

    -s <signature>  certificate name to resign application before packaging
    -o              specify output filename
    -plugin         specify an optional plugin
    -help           brief help message
    -man            full documentation
    -v[erbose]      provide details during operation


=head1 OPTIONS

=over 8

=item B<-s>

Optional codesigning certificate identity common name.  If provided, the application is will be re-codesigned prior to packaging.

=item B<-o>

Optional output filename.  The packaged application will be written to this location.

=item B<-plugin>

Specify optional plugin.  The packaged application will include the specified plugin(s).

=item B<-help>

Print a brief help message and exits.

=item B<-man>

Prints the manual page and exits.

=item B<-verbose>

Provides additional details during the packaging.

=back

=head1 DESCRIPTION

This program will package the specified application for submission to the AppStore or installation by iTunes.

=cut

最后贴个Github链接
https://github.com/dourgulf/PackageApplication

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,053评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,527评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,779评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,685评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,699评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,609评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,989评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,654评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,890评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,634评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,716评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,394评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,976评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,950评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,191评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,849评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,458评论 2 342

推荐阅读更多精彩内容