-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdual-projects.psm1
229 lines (195 loc) · 8.26 KB
/
dual-projects.psm1
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
function Add-Solution {
[CmdletBinding()]
param(
[string] $solutionName,
[bool] $mkdir = $false,
[string] $projectName = $null
)
# Create the solution
if ($solutionName) {
$noExtensionSolutionName = $solutionName -replace ".sln", ""
Write-Verbose "noExtensionSolutionName: $noExtensionSolutionName"
# Create and move to solution directory
if ($mkdir) {
Write-Verbose "Creating solution diretory using command: mkdir $noExtensionSolutionName"
mkdir $noExtensionSolutionName
Write-Verbose "Navigating to diretory using command: Set-Location $noExtensionSolutionName"
Set-Location $noExtensionSolutionName
}
Write-Verbose "Creating a solution named $noExtensionSolutionName"
dotnet new sln -n $noExtensionSolutionName
}
else {
# Create and move to solution directory
if ($mkdir) {
Write-Verbose "Creating solution diretory using command: mkdir $projectName"
mkdir $projectName
Write-Verbose "Navigating to diretory using command: Set-Location $projectName"
Set-Location $projectName
}
Write-Verbose "Creating a new solution using command: dotnet new sln "
dotnet new sln
}
}
function Add-Project {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)][string] $projectDirectory,
[Parameter(Mandatory = $true)][string] $projectName,
[Parameter(Mandatory = $true)][string] $projectTemplate,
[string]$solutionName = $null
)
Write-Debug "projectDirectory: $projectDirectory"
Write-Debug "projectName: $projectName"
Write-Debug "projectTemplate: $projectTemplate"
# Create the src/test dir if they don't exist
if (!(Test-Path $projectDirectory)) {
Write-Verbose "Creating $projectDirectory diretory using command: mkdir $projectDirectory"
mkdir $projectDirectory
}
# Create the project
Set-Location $projectDirectory
Write-Verbose "Creating project using command: dotnet new $projectTemplate -n $projectName"
dotnet new $projectTemplate -n $projectName --no-restore
Set-Location ..
# Add the project to the solution
$projectPath = GetProjectPath $projectDirectory $projectName
Write-Debug "projectPath: $projectPath"
if ($solutionName) {
Write-Verbose "Adding project to solution using command: dotnet sln $solutionName add $projectPath"
dotnet sln $solutionName add $projectPath
}
else {
Write-Verbose "Adding project to solution using command: dotnet sln add $projectPath"
dotnet sln add $projectPath
}
}
function Add-DualProjects {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)][Alias("p")][string]$projectName,
[Alias("t")][string]$projectTemplate = "web",
[Alias("create-sln")][switch]$createSolution = $false,
[Alias("s")][string]$solutionName = $null,
[bool]$mkdir = $true,
[Alias("no-build")][switch]$noBuild,
[Alias("add-functional-tests")][switch]$addFunctionalTests,
[Alias("tests-props")][string]$customTestsPropsFile = $null,
[Alias("functional-tests-props")][string]$customFunctionalTestsPropsFile = $null
)
Write-Debug "projectName: $projectName"
Write-Debug "projectTemplate: $projectTemplate"
Write-Debug "createSolution: $createSolution"
Write-Debug "solutionName: $solutionName"
Write-Debug "mkdir: $mkdir"
Write-Debug "noBuild: $noBuild"
Write-Debug "addFunctionalTests: $addFunctionalTests"
Write-Debug "customTestsPropsFile: $customTestsPropsFile"
Write-Debug "customFunctionalTestsPropsFile: $customFunctionalTestsPropsFile"
if ($createSolution) {
Add-Solution $solutionName $mkdir $projectName
}
# Create the project
Add-Project src $projectName $projectTemplate $solutionName
# Create the test project
Add-Project test "$projectName.Tests" xunit $solutionName
# Add Reference to test project
$srcProjectPath = GetProjectPath src $projectName
$testProjectPath = GetProjectPath test "$projectName.Tests"
ReferenceSourceFromTest $srcProjectPath $testProjectPath
# Update the RootNamespace to the test project
UpdateRootNamespace $projectName $testProjectPath $customTestsPropsFile
# Create a functional test project
if ($addFunctionalTests) {
Add-FunctionalTests $projectName $solutionName -no-build -props $customFunctionalTestsPropsFile
}
# Execute post-creation actions
if (!$noBuild) {
BuildSolution $solutionName
}
}
function Add-FunctionalTests {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)][Alias("p")][string]$projectName,
[Alias("s")][string]$solutionName = $null,
[Alias("no-build")][switch]$noBuild,
[Alias("props")][string]$customPropsFile = $null #"..\FunctionalTests.Build.props"
)
Write-Debug "projectName: $projectName"
Write-Debug "solutionName: $solutionName"
Write-Debug "noBuild: $noBuild"
# Create the functional test project
Add-Project test "$projectName.FunctionalTests" xunit $solutionName
# Add Reference to test project
$srcProjectPath = GetProjectPath src $projectName
$testProjectPath = GetProjectPath test "$projectName.FunctionalTests"
ReferenceSourceFromTest $srcProjectPath $testProjectPath
# Add Microsoft.AspNetCore.Mvc.Testing
dotnet add $testProjectPath package Microsoft.AspNetCore.App
dotnet add $testProjectPath package Microsoft.AspNetCore.Mvc.Testing
# Include the RootNamespace and optionally a custom .props file to the project
UpdateRootNamespace $projectName $testProjectPath $customPropsFile
# Execute post-creation actions
if (!$noBuild) {
BuildSolution $solutionName
}
}
function UpdateRootNamespace($projectName, $testProjectPath, $customPropsFile = $null) {
$loc = Get-Location
Write-Debug "Location: $loc"
Write-Debug "testProjectPath: $testProjectPath"
# Convert to absolute path
$fullTestPath = "$loc\$testProjectPath"
Write-Verbose "Convert '$testProjectPath' to absolute '$fullTestPath'."
# Rewrite the csproj file
$i = 0;
$tmpFile = "$fullTestPath.tmp"
foreach ($line in [System.IO.File]::ReadLines($fullTestPath)) {
if ($i -eq 1) {
if ($customPropsFile) {
Write-Verbose "Adding '$customPropsFile' to '$fullTestPath'."
Add-Content -Path $tmpFile -Value " <Import Project=""$customPropsFile"" />"
Add-Content -Path $tmpFile -Value ""
}
# Set the RootNamespace
Write-Verbose "Setting RootNamespace to '$projectName'."
Add-Content -Path $tmpFile -Value " <PropertyGroup>"
Add-Content -Path $tmpFile -Value " <RootNamespace>$projectName</RootNamespace>"
Add-Content -Path $tmpFile -Value " </PropertyGroup>"
}
Add-Content -Path $tmpFile -Value $line
$i = $i + 1
}
# Delete original csproj
Write-Verbose "Deleting '$fullTestPath'."
Remove-Item –path $fullTestPath
# Rename .csproj.tmp to .csproj
Write-Verbose "Renaming '$tmpFile' to '$fullTestPath'."
Move-Item -Path $tmpFile -Destination $fullTestPath
}
function BuildSolution($solutionName) {
if ($solutionName) {
Write-Verbose "Building solution using command: dotnet restore $solutionName"
dotnet build $solutionName
}
else {
Write-Verbose "Building solution using command: dotnet restore"
dotnet build
}
}
function GetProjectPath($projectDirectory, $projectName) {
return "$projectDirectory\$projectName\$projectName.csproj";
}
function ReferenceSourceFromTest {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)][string]$srcProjectPath,
[Parameter(Mandatory = $true)][string]$testProjectPath
)
Write-Debug "srcProjectPath: $srcProjectPath"
Write-Debug "testProjectPath: $testProjectPath"
Write-Verbose "Adding reference to test project using command: dotnet add $testProjectPath reference $srcProjectPath"
dotnet add $testProjectPath reference $srcProjectPath
}
Export-ModuleMember -Function Add-DualProjects, Add-FunctionalTests