-
Notifications
You must be signed in to change notification settings - Fork 4
/
Renamer.ps1
63 lines (50 loc) · 2 KB
/
Renamer.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[String]$apiName,
[Parameter(Mandatory = $true)]
[String]$alternateName
)
if ($apiName -match '-') {
Write-Host "`nModifying Input to avoid C# namespaces naming error ('-' not allowed)."
Write-Host $("Changing input from '{0}' to '{1}'.`n" -f $apiName, $apiName.replace('-', '_'))
$apiName = $apiName.replace('-', '_')
}
Write-Host "`nScanning files...`n"
Get-ChildItem -Path $PSScriptRoot -File -Recurse -exclude *.ps1 | % {
$contents = (Get-Content $_.PSPath)
$fileName = $_.Name
if ($contents -match "BaseApi") {
$contents -replace 'BaseApi', $apiName | Set-Content $_.PSPath
Write-Host $("'{0}': contents changed." -f $fileName)
$contents = (Get-Content $_.PSPath)
}
if ($contents -match "base-api") {
$contents -replace 'base-api', $alternateName | Set-Content $_.PSPath
Write-Host $("'{0}': contents changed." -f $fileName)
}
if ($fileName -match 'BaseApi') {
$newName = $_.Name -replace 'BaseApi', $apiName
Rename-Item -Path $_.PSPath -NewName $newName
Write-Host $("File renamed from '{0}' to '{1}'." -f $fileName, $newName)
}
}
Write-Host "`nScanning directories...`n"
Get-ChildItem -Path $PSScriptRoot -Directory -Recurse |
Sort-Object -Descending FullName |
Where-Object { $_.Name -match 'BaseApi' } | % {
Write-Host $("Editing directory: '{0}'." -f $_.FullName)
$newDirName = $_.Name -replace 'BaseApi', $apiName
Rename-Item -Path $_.FullName -NewName $newDirName
Write-Host $("Directory renamed from '{0}' to '{1}'." -f $_.Name, $newDirName)
}
Write-Host "Renaming done.`n`n"
Write-Host $("Do you want to delete Renamer script file?`nTarget filepath: '$PSCommandPath'.")
do { $myInput = (Read-Host 'Delete Script? (Y/N)').ToLower() } while ($myInput -notin @('y', 'n'))
if ($myInput -eq 'y') {
Remove-Item -Force $PSCommandPath
Write-Host "Script file was deleted."
}
else {
Write-Host "Keeping the script file."
}