blob: a35fe73def5de9b3e11b446316686870ffcf13aa [file] [log] [blame]
The Android Open Source Project8e35f3c2009-03-03 19:30:52 -08001#!/usr/bin/perl -w
2
3# Copyright (C) 2007 Apple Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8#
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14# 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15# its contributors may be used to endorse or promote products derived
16# from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29# Script to sort "files=(...);" sections in Xcode project.pbxproj files
30
31use strict;
32
33use File::Basename;
34use File::Temp qw(tempfile);
35use Getopt::Long;
36
37sub sortByFileName($$);
38
39my $printWarnings = 1;
40my $showHelp;
41
42my $getOptionsResult = GetOptions(
43 'h|help' => \$showHelp,
44 'w|warnings!' => \$printWarnings,
45);
46
47if (scalar(@ARGV) == 0) {
48 print STDERR "ERROR: No Xcode project files (project.pbxproj) listed on command-line.\n";
49 undef $getOptionsResult;
50}
51
52if (!$getOptionsResult || $showHelp) {
53 print STDERR <<__END__;
54Usage: @{[ basename($0) ]} [options] path/to/project.pbxproj [path/to/project.pbxproj ...]
55 -h|--help show this help message
56 -w|--[no-]warnings show or suppress warnings (default: show warnings)
57__END__
58 exit 1;
59}
60
61for my $projectFile (@ARGV) {
62 if (basename($projectFile) ne "project.pbxproj") {
63 print STDERR "WARNING: Not an Xcode project file: $projectFile\n" if $printWarnings;
64 next;
65 }
66
67 my ($OUT, $tempFileName) = tempfile(
68 basename($projectFile) . "-XXXXXXXX",
69 DIR => dirname($projectFile),
70 UNLINK => 0,
71 );
72
73 # Clean up temp file in case of die()
74 $SIG{__DIE__} = sub {
75 close(IN);
76 close($OUT);
77 unlink($tempFileName);
78 };
79
80 open(IN, "< $projectFile") || die "Could not open $projectFile: $!";
81 while (my $line = <IN>) {
82 if ($line =~ /^(\s*)files = \(\s*$/) {
83 print $OUT $line;
84 my $endMarker = $1 . ");";
85 my @files;
86 while (my $fileLine = <IN>) {
87 if ($fileLine =~ /^\Q$endMarker\E\s*$/) {
88 $endMarker = $fileLine;
89 last;
90 }
91 push @files, $fileLine;
92 }
93 print $OUT sort sortByFileName @files;
94 print $OUT $endMarker;
95 } else {
96 print $OUT $line;
97 }
98 }
99 close(IN);
100 close($OUT);
101
102 unlink($projectFile) || die "Could not delete $projectFile: $!";
103 rename($tempFileName, $projectFile) || die "Could not rename $tempFileName to $projectFile: $!";
104}
105
106exit 0;
107
108sub sortByFileName($$)
109{
110 my ($a, $b) = @_;
111 my $aFileName = $1 if $a =~ /^\s*[A-Z0-9]{24} \/\* (.+) in /;
112 my $bFileName = $1 if $b =~ /^\s*[A-Z0-9]{24} \/\* (.+) in /;
113 return $aFileName cmp $bFileName;
114}