Advertisement
Guest User

Untitled

a guest
Sep 29th, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. class NamespaceFixer
  7. {
  8.     private const string PhpDocAttrMatch = "^ *\\* *@(return|var) +(.+)$";
  9.     private const string PhpDocTagEnd = "^ *\\*/";
  10.     private const string PhpDocTagStart = "^ */\\*\\*";
  11.     private const string PhpFileSearchPattern = "*.php";
  12.  
  13.     private readonly string _path;
  14.     private readonly string _rootNamespace;
  15.  
  16.     private static void Main(string[] args)
  17.     {
  18.         if (args.Length != 1)
  19.         {
  20.             Console.Out.WriteLine("No directory specified");
  21.             Environment.Exit(0);
  22.         }
  23.  
  24.         if (!Directory.Exists(args[0]))
  25.         {
  26.             Console.Out.WriteLine("Directory not found: \"" + args[0] + "\"");
  27.             Environment.Exit(0);
  28.         }
  29.  
  30.         var namespaceFixer = new NamespaceFixer(args[0]);
  31.         namespaceFixer.Run();
  32.     }
  33.  
  34.     public NamespaceFixer(string path)
  35.     {
  36.         _path = path;
  37.         _rootNamespace = '\\' + GetLastFolderName(path);
  38.     }
  39.  
  40.     public void Run()
  41.     {
  42.         Console.Out.WriteLine("Processing directory: \"" + _path + "\"");
  43.         Console.Out.WriteLine();
  44.         Recurse(_path, _rootNamespace);
  45.     }
  46.  
  47.     private static void FixFile(string path, string currentNamespace)
  48.     {
  49.         // Read all lines of the file.
  50.         var lines = File.ReadAllLines(path);
  51.  
  52.         // Is the loop currently in a PHPDoc block.
  53.         var isPhpDoc = false;
  54.  
  55.         // Has the contents of the file been changed.
  56.         var isModified = false;
  57.  
  58.         for (var i = 0; i < lines.Length; i++)
  59.         {
  60.             if (isPhpDoc)
  61.             {
  62.                 // Locate the PHPDoc @return statement.
  63.                 var match = Regex.Match(lines[i], PhpDocAttrMatch);
  64.  
  65.                 if (match.Success)
  66.                 {
  67.                     var declaredNamespace = match.Groups[2].Value;
  68.                     if (declaredNamespace.StartsWith("Doctrine\\"))
  69.                     {
  70.                         var returnPrefix = lines[i].Substring(0, match.Groups[2].Index);
  71.                         lines[i] = returnPrefix + '\\' + declaredNamespace;
  72.  
  73.                         isModified = true;
  74.                     }
  75.                 }
  76.  
  77.                 if (Regex.Match(lines[i], PhpDocTagEnd).Success)
  78.                 {
  79.                     isPhpDoc = false;
  80.                 }
  81.  
  82.                 continue;
  83.             }
  84.  
  85.             isPhpDoc = Regex.Match(lines[i], PhpDocTagStart).Success;
  86.         }
  87.  
  88.         if (isModified)
  89.         {
  90.             Console.Out.WriteLine("Modified: \"" + path + "\"");
  91.             File.WriteAllText(path, string.Join("\n", lines));
  92.         }
  93.     }
  94.  
  95.     private static string GetLastFolderName(string path)
  96.     {
  97.         return path.Split('\\').Last();
  98.     }
  99.  
  100.     private static void Recurse(string path, string currentNamespace)
  101.     {
  102.         foreach (var directory in Directory.GetDirectories(path))
  103.         {
  104.             // Skip SVN directories.
  105.             if (directory.ToLower().EndsWith(".svn"))
  106.             {
  107.                 continue;
  108.             }
  109.  
  110.             Recurse(
  111.                 directory,
  112.                 currentNamespace + '\\' + GetLastFolderName(directory));
  113.         }
  114.  
  115.         foreach (var file in Directory.GetFiles(path, PhpFileSearchPattern))
  116.         {
  117.             FixFile(file, currentNamespace);
  118.         }
  119.     }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement