-
Notifications
You must be signed in to change notification settings - Fork 446
/
rename.ps1
81 lines (67 loc) · 3.21 KB
/
rename.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
# set output encoding
$OutputEncoding = [Text.UTF8Encoding]::UTF8
# company name placeholder
$oldCompanyName="AbpCompanyName"
# your company name
$newCompanyName="YourCompanyName"
# project name placeholder
$oldProjectName="EventCloud"
# your project name
$newProjectName="YourProjectName"
# file type
$fileType="FileInfo"
# directory type
$dirType="DirectoryInfo"
# copy
Write-Host 'Start copy folders...'
$newRoot=$newCompanyName+"."+$newProjectName
mkdir $newRoot
Copy-Item -Recurse .\aspnet-core\ .\$newRoot\
Copy-Item -Recurse .\vue\ .\$newRoot\
Copy-Item .gitignore .\$newRoot\
Copy-Item LICENSE .\$newRoot\
Copy-Item README.md .\$newRoot\
# folders to deal with
$slnFolder = (Get-Item -Path "./$newRoot/aspnet-core/" -Verbose).FullName
$vueFolder = (Get-Item -Path "./$newRoot/vue/" -Verbose).FullName
function Rename {
param (
$TargetFolder,
$PlaceHolderCompanyName,
$PlaceHolderProjectName,
$NewCompanyName,
$NewProjectName
)
# file extensions to deal with
$include=@("*.cs","*.cshtml","*.asax","*.ps1","*.ts","*.csproj","*.sln","*.xaml","*.json","*.js","*.xml","*.config","Dockerfile")
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
Write-Host "[$TargetFolder]Start rename folder..."
# rename folder
Ls $TargetFolder -Recurse | Where { $_.GetType().Name -eq $dirType -and ($_.Name.Contains($PlaceHolderCompanyName) -or $_.Name.Contains($PlaceHolderProjectName)) } | ForEach-Object{
Write-Host 'directory ' $_.FullName
$newDirectoryName=$_.Name.Replace($PlaceHolderCompanyName,$NewCompanyName).Replace($PlaceHolderProjectName,$NewProjectName)
Rename-Item $_.FullName $newDirectoryName
}
Write-Host "[$TargetFolder]End rename folder."
Write-Host '-------------------------------------------------------------'
# replace file content and rename file name
Write-Host "[$TargetFolder]Start replace file content and rename file name..."
Ls $TargetFolder -Include $include -Recurse | Where { $_.GetType().Name -eq $fileType} | ForEach-Object{
$fileText = Get-Content $_ -Raw -Encoding UTF8
if($fileText.Length -gt 0 -and ($fileText.contains($PlaceHolderCompanyName) -or $fileText.contains($PlaceHolderProjectName))){
$fileText.Replace($PlaceHolderCompanyName,$NewCompanyName).Replace($PlaceHolderProjectName,$NewProjectName) | Set-Content $_ -Encoding UTF8
Write-Host 'file(change text) ' $_.FullName
}
If($_.Name.contains($PlaceHolderCompanyName) -or $_.Name.contains($PlaceHolderProjectName)){
$newFileName=$_.Name.Replace($PlaceHolderCompanyName,$NewCompanyName).Replace($PlaceHolderProjectName,$NewProjectName)
Rename-Item $_.FullName $newFileName
Write-Host 'file(change name) ' $_.FullName
}
}
Write-Host "[$TargetFolder]End replace file content and rename file name."
Write-Host '-------------------------------------------------------------'
$elapsed.stop()
write-host "[$TargetFolder]Total Time Cost: $($elapsed.Elapsed.ToString())"
}
Rename -TargetFolder $slnFolder -PlaceHolderCompanyName $oldCompanyName -PlaceHolderProjectName $oldProjectName -NewCompanyName $newCompanyName -NewProjectName $newProjectName
Rename -TargetFolder $vueFolder -PlaceHolderCompanyName $oldCompanyName -PlaceHolderProjectName $oldProjectName -NewCompanyName $newCompanyName -NewProjectName $newProjectName