-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_project.ps1
More file actions
53 lines (41 loc) · 1.75 KB
/
new_project.ps1
File metadata and controls
53 lines (41 loc) · 1.75 KB
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
param (
[Parameter(Mandatory=$true)]
[string]$SourceProjectDir,
[Parameter(Mandatory=$true)]
[string]$NewProjectName
)
# Check if sln file exist in current directory
if (-not (Test-Path "*.sln")) {
Write-Error ".sln file was not found in current directory"
exit
}
# Copy project directory
$destDir = Join-Path (Get-Location) $NewProjectName
Copy-Item -Recurse -Force $SourceProjectDir $destDir
# Rename .csproj.*
$sourceProjFile = Get-ChildItem -Path $SourceProjectDir -Filter "*.vcxproj" | Select-Object -First 1
Get-ChildItem -Path $destDir -Filter "*.vcxproj.*" | ForEach-Object {
$newName = $_.Name -replace $sourceProjFile.BaseName, $NewProjectName
Rename-Item -Path $_.FullName -NewName $newName
}
# Replace project name in .csproj
$targetProjFile = Get-ChildItem -Path $destDir -Filter "*.vcxproj" | Select-Object -First 1
$newProjectGuid = [guid]::NewGuid().ToString().ToUpper()
[xml]$projFileContent = Get-Content $sourceProjFile.FullName
$projFileContent.Project.PropertyGroup[0].ProjectGuid = $newProjectGuid
$projFileContent.Project.PropertyGroup[0].ProjectName = $NewProjectName
$projFileContent.Project.PropertyGroup[0].RootNamespace = $NewProjectName
$projFileContent.Save($targetProjFile.FullName)
# Add new project to .sln
$slnContent = Get-Content "*.sln"
$projectExists = $slnContent | Select-String -Pattern $NewProjectName
if (-not $projectExists) {
$newProject =
@"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "$NewProjectName", "$NewProjectName\$NewProjectName.vcxproj", "{$newProjectGuid}"
EndProject
"@
$newContent = $slnContent -replace "(^Global$)", "$newProject`r`nGlobal$1"
Set-Content -Path "*.sln" -Value $newContent
}
Write-Host "Project $NewProjectName has been created!" -ForegroundColor Green