sort-Xcode-project-file 5.7 KB

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