daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Nov 9th, 2018 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param(
  2.     [Parameter(Position=0,Mandatory=$true)]
  3.     [Alias("Path")]
  4.     $rootPath,
  5.     [Parameter(Position=1,Mandatory=$true)]
  6.     [Alias("Namespace")]
  7.     $rootNamespace
  8. )
  9.  
  10. $namespacePattern = "namespace\s+([^\s]+)"
  11. $classPattern = "(public\s+)?partial\s+class\s+([^\s]+)"
  12.  
  13. $files = Get-ChildItem $rootPath -Recurse -Include *.cs
  14. #$table = @()
  15.  
  16. foreach ($file in $files)
  17. {
  18.     $newNamespace = $file.FullName.Substring($rootPath.Length + 1)
  19.     $newNamespace = $newNamespace.Substring(0, $newNamespace.IndexOf(".")).Replace("\", ".").Replace(" ", "").Replace("_", "")
  20.     $newNamespace = [regex]::replace("${rootNamespace}.${newNamespace}", "\.([\w])(\w+)",{ ".$($args[0].Groups[1].Value.ToUpper())$($args[0].Groups[2].Value)" })
  21.     $newNamespace = $newNamespace.Substring(0, $newNamespace.LastIndexOf("."))
  22.  
  23.     $newClassName = $file.Name.Substring(0, $file.Name.IndexOf("."))
  24.  
  25.     $content = Get-Content -Path $file | Out-String
  26.     $match = $content | Select-String -Pattern $namespacePattern
  27.     $currentNamespace = ""
  28.  
  29.     if ($null -ne $match.Matches)
  30.     {
  31.         $currentNamespace = $match.Matches[0].Groups[1]
  32.         $content -replace $namespacePattern,"namespace $newNamespace" `
  33.                  -replace $classPattern,"public partial class $newClassName" `
  34.                  | Set-Content -Path "${file}" -Encoding UTF8
  35.     }
  36.     else
  37.     {
  38.         $content -replace $classPattern,"namespace ${newNamespace}`r`n{`r`npublic partial class $newClassName" `
  39.                  -replace "\}\s*\Z","}`r`n}" `
  40.                  | Set-Content -Path "${file}" -Encoding UTF8
  41.     }
  42.  
  43.     $aspx = "$($file.DirectoryName)\$($file.Name.Substring(0, $file.Name.LastIndexOf(".")))"
  44.    
  45.     if (Test-Path -Path $aspx)
  46.     {
  47.         (Get-Content $aspx) -replace 'Inherits="([^"]+)"', ('Inherits="{0}.{1}"' -f $newNamespace,$newClassName) `
  48.                             | Set-Content "${aspx}"
  49.     }
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top