3
# Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions
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.
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.
29
# Script to sort "children" and "files" sections in Xcode project.pbxproj files
35
use File::Temp qw(tempfile);
38
sub sortChildrenByFileName($$);
39
sub sortFilesByFileName($$);
41
# Files (or products) without extensions
42
my %isFile = map { $_ => 1 } qw(
50
my $printWarnings = 1;
53
my $getOptionsResult = GetOptions(
54
'h|help' => \$showHelp,
55
'w|warnings!' => \$printWarnings,
58
if (scalar(@ARGV) == 0 && !$showHelp) {
59
print STDERR "ERROR: No Xcode project files (project.pbxproj) listed on command-line.\n";
60
undef $getOptionsResult;
63
if (!$getOptionsResult || $showHelp) {
64
print STDERR <<__END__;
65
Usage: @{[ basename($0) ]} [options] path/to/project.pbxproj [path/to/project.pbxproj ...]
66
-h|--help show this help message
67
-w|--[no-]warnings show or suppress warnings (default: show warnings)
72
for my $projectFile (@ARGV) {
73
if (basename($projectFile) =~ /\.xcodeproj$/) {
74
$projectFile = File::Spec->catfile($projectFile, "project.pbxproj");
77
if (basename($projectFile) ne "project.pbxproj") {
78
print STDERR "WARNING: Not an Xcode project file: $projectFile\n" if $printWarnings;
82
# Grab the mainGroup for the project file
84
open(IN, "< $projectFile") || die "Could not open $projectFile: $!";
85
while (my $line = <IN>) {
86
$mainGroup = $2 if $line =~ m#^(\s*)mainGroup = ([0-9A-F]{24} /\* .+ \*/);$#;
90
my ($OUT, $tempFileName) = tempfile(
91
basename($projectFile) . "-XXXXXXXX",
92
DIR => dirname($projectFile),
96
# Clean up temp file in case of die()
100
unlink($tempFileName);
104
open(IN, "< $projectFile") || die "Could not open $projectFile: $!";
105
while (my $line = <IN>) {
106
if ($line =~ /^(\s*)files = \(\s*$/) {
108
my $endMarker = $1 . ");";
110
while (my $fileLine = <IN>) {
111
if ($fileLine =~ /^\Q$endMarker\E\s*$/) {
112
$endMarker = $fileLine;
115
push @files, $fileLine;
117
print $OUT sort sortFilesByFileName @files;
118
print $OUT $endMarker;
119
} elsif ($line =~ /^(\s*)children = \(\s*$/) {
121
my $endMarker = $1 . ");";
123
while (my $childLine = <IN>) {
124
if ($childLine =~ /^\Q$endMarker\E\s*$/) {
125
$endMarker = $childLine;
128
push @children, $childLine;
130
if ($lastTwo[0] =~ m#^\s+\Q$mainGroup\E = \{$#) {
131
# Don't sort mainGroup
132
print $OUT @children;
134
print $OUT sort sortChildrenByFileName @children;
136
print $OUT $endMarker;
141
push @lastTwo, $line;
142
shift @lastTwo if scalar(@lastTwo) > 2;
147
unlink($projectFile) || die "Could not delete $projectFile: $!";
148
rename($tempFileName, $projectFile) || die "Could not rename $tempFileName to $projectFile: $!";
153
sub sortChildrenByFileName($$)
156
my $aFileName = $1 if $a =~ /^\s*[A-Z0-9]{24} \/\* (.+) \*\/,$/;
157
my $bFileName = $1 if $b =~ /^\s*[A-Z0-9]{24} \/\* (.+) \*\/,$/;
158
my $aSuffix = $1 if $aFileName =~ m/\.([^.]+)$/;
159
my $bSuffix = $1 if $bFileName =~ m/\.([^.]+)$/;
160
if ((!$aSuffix && !$isFile{$aFileName} && $bSuffix) || ($aSuffix && !$bSuffix && !$isFile{$bFileName})) {
161
return !$aSuffix ? -1 : 1;
163
return lc($aFileName) cmp lc($bFileName);
166
sub sortFilesByFileName($$)
169
my $aFileName = $1 if $a =~ /^\s*[A-Z0-9]{24} \/\* (.+) in /;
170
my $bFileName = $1 if $b =~ /^\s*[A-Z0-9]{24} \/\* (.+) in /;
171
return lc($aFileName) cmp lc($bFileName);