This repository has been archived by the owner on Apr 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RenameProject.ps1
93 lines (73 loc) · 3.01 KB
/
RenameProject.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
$defaultNamespace = "Coalesce.Starter";
$defaultDatabase = "CoalesceStarterDb";
$defaultConnection = "Server=(localdb)\\MSSQLLocalDB;Database=$defaultDatabase;Trusted_Connection=True;MultipleActiveResultSets=True;";
Write-Host ""
Write-Host "This script helps you rename the $defaultNamespace project to fit your application."
Write-Host "No changes will be made until you confirm all your inputs."
Write-Host "Please close Visual Studio before running this script." -ForegroundColor Green
Write-Host ""
Write-Host "The default root namespace is " -NoNewline
Write-Host -ForegroundColor Yellow "$defaultNamespace."
$rootNamespace = Read-Host -Prompt "Enter the desired root namespace"
if (-not $rootNamespace){
throw "Namespace must not be empty"
}
$nsParts = $rootNamespace.Split(".")
$dbName = "$($nsParts[$nsParts.Length - 1])Db"
Write-Host "The default connection string is " -NoNewline
Write-Host -ForegroundColor Yellow $defaultConnection
Write-Host "Enter the desired default connection string. Leave blank to set the Database to " -NoNewline
Write-Host $dbName -ForegroundColor Yellow -NoNewline
Write-Host ": " -NoNewline
$connectionString = Read-Host
if (-not $connectionString){
$connectionString = $defaultConnection.Replace($defaultDatabase, $dbName)
}
Write-Host ""
Write-Host "Please confirm your input: " -ForegroundColor Green
Write-Host " Root Namespace: " -NoNewline
Write-Host $rootNamespace -ForegroundColor Yellow
Write-Host "Connection String: " -NoNewline
Write-Host $connectionString -ForegroundColor Yellow
Write-Host ""
Write-Host "Please close Visual Studio before continuing." -ForegroundColor Green
$confirmation = Read-Host "Are you sure you want to continue and rename this project? (y/n)"
if ($confirmation -eq 'y') {
Write-Host ""
$vs = "$PSScriptRoot\.vs"
if (Test-Path $vs){
Write-Host "Deleting $vs"
Remove-Item $vs -Recurse -Force
}
$files = Get-ChildItem $PSScriptRoot -File -Recurse |
?{ $_.fullname -ne $PSCommandPath } |
?{ $_.fullname -notmatch "\\bin\\?" } |
?{ $_.fullname -notmatch "\\obj\\?" } |
?{ $_.fullname -notmatch "\\packages\\?" } |
?{ $_.fullname -notmatch "\\submodules\\?" } |
?{ $_.fullname -notmatch "\\node_modules\\?" }
foreach ($file in $files)
{
$content = Get-Content $file.FullName -Raw
if ($content){
$newContent = $content -replace $defaultConnection, $connectionString
$newContent = $newContent -replace $defaultNamespace, $rootNamespace
if ($content -ne $newContent) {
# Set-Content throws errors a lot of the time.
# Set-Content $file.FullName -Value $newContent
Write-Host "Replacing text in $($file.FullName)"
[System.IO.File]::WriteAllText($file.FullName,$newContent)
}
}
}
$files = Get-ChildItem $PSScriptRoot -Recurse |
?{ $_.fullname -ne $PSCommandPath } |
sort FullName -Descending
foreach ($file in $files)
{
if ($file.Name.Contains($defaultNamespace)){
Write-Host "Renaming $($file.FullName)"
$file | Rename-Item -NewName $file.Name.Replace($defaultNamespace, $rootNamespace)
}
}
}