split-file-by-class 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/perl -w
  2. # Copyright (C) 2006 Apple Computer, 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. # Used for splitting a single file into multiple class files
  28. # Usage: split-class <header file>
  29. use strict;
  30. use File::Copy;
  31. use FindBin;
  32. use lib $FindBin::Bin;
  33. use SpacingHeuristics;
  34. for my $filename (@ARGV) {
  35. $filename =~ m/^(\w+)\.h$/ or die "Command line args must be .h files.\n";
  36. my $basename = $1;
  37. open(OLDFILE, "<", $filename) or die "File does not exist: $filename\n";
  38. print "Splitting class $filename.{h,cpp}:\n";
  39. my $currentClassName = "";
  40. my $classIndent = "";
  41. my $fileContent = "";
  42. my %classDefs = ();
  43. while (my $line = <OLDFILE>) {
  44. if ($currentClassName) {
  45. $classDefs{$currentClassName} .= $line;
  46. if ($line =~ /^$classIndent};\s*$/) {
  47. $currentClassName = "";
  48. }
  49. } else {
  50. if ($line =~ /^(\s*)class\s+(\w+)\s+[^;]*$/) {
  51. $classIndent = $1;
  52. $currentClassName = $2;
  53. $classDefs{$currentClassName} .= $line;
  54. $fileContent .= "###CLASS###$currentClassName\n";
  55. } else {
  56. $fileContent .= $line;
  57. }
  58. }
  59. }
  60. close(OLDFILE);
  61. if (scalar(keys(%classDefs)) == 1) { # degenerate case
  62. my ($classname) = keys(%classDefs);
  63. if (!($classname eq $basename)) {
  64. print "Skipping $filename, already correctly named.\n";
  65. } else {
  66. print "$filename only includes one class, renaming to $classname.h\n";
  67. system("svn rm --force $classname.h") if (-r "$classname.h");
  68. system "svn mv $basename.h $classname.h";
  69. }
  70. } else {
  71. while (my ($classname, $classDef) = each(%classDefs)) {
  72. if (($classname eq $basename)) {
  73. print "Skipping $filename, already correctly named.\n";
  74. } else {
  75. print "Using SVN to copy $basename.{h,cpp} to $classname.{h,cpp}\n";
  76. system("svn rm --force $classname.h") if (-r "$classname.h");
  77. system "svn cp $basename.h $classname.h";
  78. system("svn rm --force $classname.cpp") if (-r "$classname.cpp");
  79. system "svn cp $basename.cpp $classname.cpp";
  80. }
  81. print "Fixing $classname.h as much as possible.\n";
  82. open(NEWHEADER, ">", "$classname.h") or die "File does not exist: $filename\n";
  83. my @lines = split("\n", $fileContent);
  84. foreach my $line (@lines) {
  85. if ($line =~ /^###CLASS###(\w+)/) {
  86. if ($1 eq $classname) {
  87. print NEWHEADER $classDef . "\n";
  88. }
  89. } else {
  90. print NEWHEADER $line . "\n";
  91. }
  92. }
  93. close(NEWHEADER);
  94. print "Fixing $classname.cpp as much as possible.\n";
  95. copy("$classname.cpp", "$classname.cpp.original");
  96. open(OLDCPP, "<", "$classname.cpp.original") or die "Failed to copy file for reading: $filename\n";
  97. open(NEWCPP, ">", "$classname.cpp") or die "File does not exist: $filename\n";
  98. my $insideMemberFunction = 0;
  99. my $shouldPrintMemberFunction = 0;
  100. resetSpacingHeuristics();
  101. while (my $line = <OLDCPP>) {
  102. if ($insideMemberFunction) {
  103. if ($shouldPrintMemberFunction) {
  104. print NEWCPP $line;
  105. #setPreviousAllowedLine($line);
  106. } else {
  107. ignoringLine($line);
  108. }
  109. if ($line =~ /^}\s*$/) {
  110. $insideMemberFunction = 0;
  111. }
  112. } elsif ($line =~ /$filename/) {
  113. print NEWCPP "#include \"config.h\"\n";
  114. print NEWCPP "#include \"$classname.h\"\n";
  115. } elsif ($line =~ /#include/ || $line =~ /#import/) {
  116. next; # skip includes, they're generally wrong or unecessary anyway.
  117. } else {
  118. $line =~ s/DOM:://;
  119. $line =~ s/khtml:://;
  120. $line =~ s/namespace DOM/namespace WebCore/;
  121. $line =~ s/namespace khtml/namespace WebCore/;
  122. if ($line =~ /^(.*?\s+)?(\*|&)?(\w+)::(~)?\w+\s*\(/) {
  123. $insideMemberFunction = 1;
  124. $shouldPrintMemberFunction = ($classname eq $3);
  125. if ($shouldPrintMemberFunction) {
  126. printPendingEmptyLines(*NEWCPP, $line);
  127. print NEWCPP $line;
  128. }
  129. } else {
  130. next if isOnlyWhiteSpace($line);
  131. next if ($line =~ m/------------/);
  132. printPendingEmptyLines(*NEWCPP, $line);
  133. applySpacingHeuristicsAndPrint(*NEWCPP, $line);
  134. }
  135. }
  136. }
  137. close(NEWCPP);
  138. close(OLDCPP);
  139. unlink("$classname.cpp.original");
  140. }
  141. }
  142. print "Opening new files...\n";
  143. system("open " . join(".* ", keys(%classDefs)) . ".*");
  144. }