Skip to content

Commit 6465e45

Browse files
committed
Track Project files instead of folders for DotNet
Also split pack and push
1 parent ddb09b6 commit 6465e45

9 files changed

+81
-55
lines changed

DockerBuild.Task.ps1

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ Add-BuildTask DockerBuild @{
22
# This task can only be skipped if the images are newer than the source files
33
If = $dotnetProjects
44
Inputs = {
5-
$dotnetProjects.Where{ Get-ChildItem $_ -File -Filter Dockerfile } |
5+
$dotnetProjects.Where{ Get-ChildItem (Split-Path $_) -File -Filter Dockerfile } |
66
Get-ChildItem -File
77
}
88
Outputs = {
99
# We use the iidfile as a standing for date of the image
10-
$dotnetProjects.Where{ Get-ChildItem $_ -File -Filter Dockerfile } |
11-
Split-Path -Leaf | Join-Path -Path $OutputRoot -ChildPath { $_.ToLower() }
10+
# Projects that have an adjacent Dockerfile
11+
$dotnetProjects
12+
| Where-Object { Get-ChildItem (Split-Path $_) -File -Filter Dockerfile }
13+
| Join-Path -Path $OutputRoot -ChildPath { (Split-Path $_ -LeafBase).ToLower() }
1214
}
1315
Jobs = {
14-
foreach ($project in $dotnetProjects.Where{ Get-ChildItem $_ -File -Filter Dockerfile }) {
15-
Set-Location $project
16-
$name = (Split-Path $project -Leaf).ToLower()
16+
foreach ($project in $dotnetProjects.Where{ Get-ChildItem (Split-Path $_) -File -Filter Dockerfile }) {
17+
Set-Location (Split-Path $project)
18+
$name = (Split-Path $project -LeafBase).ToLower()
1719

1820
Write-Build Gray "docker build . --tag $name --iidfile $(Join-Path $OutputRoot $name)"
1921
docker build . --tag $name --iidfile (Join-Path $OutputRoot $name)

DotNetBuild.Task.ps1

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ Add-BuildTask DotNetBuild @{
33
If = $dotnetProjects
44
Inputs = {
55
# Exclude generated source files in /obj/ folders
6-
Get-ChildItem $dotnetProjects -Recurse -File -Filter *.cs |
6+
Get-ChildItem (Split-Path $dotnetProjects) -Recurse -File -Filter *.cs |
77
Where-Object FullName -NotMatch "[\\/]obj[\\/]"
88
}
99
Outputs = {
1010
foreach ($project in $dotnetProjects) {
11-
$BaseName = (Get-Item $project -Filter *.csproj).BaseName
12-
(Get-ChildItem $project/bin -Filter "$BaseName.dll" -Recurse -ErrorAction Ignore) ?? $BuildRoot
11+
$BaseName = Split-Path $project -LeafBase
12+
(Get-ChildItem (Join-Path (Split-Path $project) bin) -Filter "$BaseName.dll" -Recurse -ErrorAction Ignore) ?? $BuildRoot
1313
}
1414
}
1515
Jobs = "DotNetRestore", "GitVersion", {
@@ -21,8 +21,8 @@ Add-BuildTask DotNetBuild @{
2121
}
2222

2323
foreach ($project in $dotnetProjects) {
24-
if (Test-Path "Variable:GitVersion.$((Split-Path $project -Leaf).ToLower())") {
25-
$options["p"] = "Version=$((Get-Variable "GitVersion.$((Split-Path $project -Leaf).ToLower())" -ValueOnly).InformationalVersion)"
24+
if (Test-Path "Variable:GitVersion.$((Split-Path $project -LeafBase).ToLower())") {
25+
$options["p"] = "Version=$((Get-Variable "GitVersion.$((Split-Path $project -LeafBase).ToLower())" -ValueOnly).InformationalVersion)"
2626
}
2727

2828
Write-Build Gray "dotnet build $project --configuration $configuration -p $($options["p"])"

DotNetPack.Task.ps1

+6-20
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,17 @@ Add-BuildTask DotNetPack @{
77
$script:DotNetPublishRoot = New-Item $script:DotNetPublishRoot -ItemType Directory -Force -ErrorAction SilentlyContinue | Convert-Path
88

99
foreach ($project in $dotnetProjects) {
10-
if (Test-Path "Variable:GitVersion.$((Split-Path $project -Leaf).ToLower())") {
11-
$options["p"] = "Version=$((Get-Variable "GitVersion.$((Split-Path $project -Leaf).ToLower())" -ValueOnly).InformationalVersion)"
10+
$Name = Split-Path $project -LeafBase
11+
if (Test-Path "Variable:GitVersion.$($Name.ToLower())") {
12+
$options["p"] = "Version=$((Get-Variable "GitVersion.$($Name.ToLower())" -ValueOnly).InformationalVersion)"
1213
}
1314

14-
Write-Host "Publishing $project"
15-
$Name = Split-Path $project -Leaf
15+
Write-Host "Publishing $Name"
1616

17-
Set-Location $project
18-
$OutputFolder = @($dotnetProjects).Count -gt 1 ? "$DotNetPublishRoot${/}$Name" : $DotNetPublishRoot
17+
Set-Location (Split-Path $project)
18+
$OutputFolder = $DotNetPublishRoot
1919
Write-Build Gray "dotnet pack $project --output '$OutputFolder' --no-build --configuration $configuration -p $($options["p"])"
2020
dotnet pack $project --output "$OutputFolder" --no-build --configuration $configuration @options --include-symbols
21-
22-
if ($BuildSystem -ne 'None' -and
23-
$BranchName -in "master", "main" -and
24-
-not [string]::IsNullOrWhiteSpace($NuGetPublishKey)) {
25-
Write-Host "$OutputFolder" "-Recurse" "-Filter" "*$Name*$($GitVersion.MajorMinorPatch).nupkg"
26-
$Package = Get-ChildItem $OutputFolder -Recurse -Filter "*$Name*$($GitVersion.MajorMinorPatch).nupkg"
27-
Write-Build Gray "dotnet nuget push $package --api-key $NuGetPublishKey --source $NuGetPublishUri"
28-
dotnet nuget push $package --api-key $NuGetPublishKey --source $NuGetPublishUri
29-
} else {
30-
Write-Warning ("Skipping push: To push, ensure that...`n" +
31-
"`t* You are in a known build system (Current: $BuildSystem)`n" +
32-
"`t* You are committing to the main branch (Current: $BranchName) `n" +
33-
"`t* The repository APIKey is defined in `$NuGetPublishKey (Current: $(![string]::IsNullOrWhiteSpace($NuGetPublishKey)))")
34-
}
3521
}
3622
}
3723
}

DotNetPublish.Task.ps1

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ Add-BuildTask DotNetPublish @{
33
If = $dotnetProjects
44
Inputs = {
55
# Exclude generated source files in /obj/ folders
6-
Get-ChildItem $dotnetProjects -Recurse -File -Filter *.cs |
6+
Get-ChildItem (Split-Path $dotnetProjects) -Recurse -File -Filter *.cs |
77
Where-Object FullName -NotMatch "[\\/]obj[\\/]"
88
}
99
Outputs = {
1010
foreach ($project in $dotnetProjects) {
11-
$Name = Split-Path $project -Leaf
11+
$Name = Split-Path $project -LeafBase
1212
$OutputFolder = @($dotnetProjects).Count -gt 1 ? "$DotNetPublishRoot${/}$Name" : $DotNetPublishRoot
1313
$Expected = Join-Path $OutputFolder -ChildPath "$Name.dll"
1414
Write-Host "Expected Output: $Expected"
@@ -26,16 +26,16 @@ Add-BuildTask DotNetPublish @{
2626
}
2727

2828
foreach ($project in $dotnetProjects) {
29-
Write-Host "Publishing $project"
30-
$Name = Split-Path $project -Leaf
31-
if (Test-Path "Variable:GitVersion.$((Split-Path $project -Leaf).ToLower())") {
32-
$options["p"] = "Version=$((Get-Variable "GitVersion.$((Split-Path $project -Leaf).ToLower())" -ValueOnly).InformationalVersion)"
29+
$Name = Split-Path $project -LeafBase
30+
Write-Host "Publishing $Name"
31+
if (Test-Path "Variable:GitVersion.$($Name.ToLower())") {
32+
$options["p"] = "Version=$((Get-Variable "GitVersion.$($Name.ToLower())" -ValueOnly).InformationalVersion)"
3333
}
3434

35-
Set-Location $project
35+
Set-Location (Split-Path $project)
3636
$OutputFolder = @($dotnetProjects).Count -gt 1 ? "$DotNetPublishRoot${/}$Name" : $DotNetPublishRoot
37-
Write-Build Gray "dotnet publish --output $OutputFolder --no-build --configuration $configuration -p $($options["p"])"
38-
dotnet publish --output "$OutputFolder" --no-build --configuration $configuration
37+
Write-Build Gray "dotnet publish $project --output $OutputFolder --no-build --configuration $configuration -p $($options["p"])"
38+
dotnet publish $project --output "$OutputFolder" --no-build --configuration $configuration
3939
}
4040
}
4141
}

DotNetPush.Task.ps1

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Add-BuildTask DotNetPush @{
2+
# This task should be skipped if there are no C# projects to build
3+
If = $dotnetProjects
4+
Jobs = "DotNetPack", {
5+
$local:options = @{} # + $script:dotnetOptions
6+
7+
$script:DotNetPublishRoot = New-Item $script:DotNetPublishRoot -ItemType Directory -Force -ErrorAction SilentlyContinue | Convert-Path
8+
9+
foreach ($project in $dotnetProjects) {
10+
$Name = Split-Path $project -LeafBase
11+
if (Test-Path "Variable:GitVersion.$($Name.ToLower())") {
12+
$options["p"] = "Version=$((Get-Variable "GitVersion.$($Name.ToLower())" -ValueOnly).InformationalVersion)"
13+
}
14+
15+
Write-Host "Publishing $name"
16+
17+
Set-Location (Split-Path $project)
18+
$OutputFolder = @($dotnetProjects).Count -gt 1 ? "$DotNetPublishRoot${/}$Name" : $DotNetPublishRoot
19+
20+
$Package = Get-ChildItem $OutputFolder -Recurse -Filter "*$Name*$($GitVersion.MajorMinorPatch).nupkg"
21+
22+
if ($BuildSystem -ne 'None' -and
23+
$BranchName -in "master", "main" -and
24+
-not [string]::IsNullOrWhiteSpace($NuGetPublishKey)) {
25+
Write-Host "$OutputFolder" "-Recurse" "-Filter" "*$Name*$($GitVersion.MajorMinorPatch).nupkg"
26+
Write-Build Gray "dotnet nuget push $package --api-key $NuGetPublishKey --source $NuGetPublishUri"
27+
dotnet nuget push $package --api-key $NuGetPublishKey --source $NuGetPublishUri
28+
} else {
29+
Write-Warning ("Skipping push: To push $Package ensure that...`n" +
30+
"`t* You are in a known build system (Current: $BuildSystem)`n" +
31+
"`t* You are committing to the main branch (Current: $BranchName) `n" +
32+
"`t* The repository APIKey is defined in `$NuGetPublishKey (Current: $(![string]::IsNullOrWhiteSpace($NuGetPublishKey)))")
33+
}
34+
}
35+
}
36+
}

DotNetRestore.Task.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ Add-BuildTask DotNetRestore @{
22
# This task should be skipped if there are no C# projects to build
33
If = $dotnetProjects
44
Inputs = {
5-
Get-ChildItem $dotnetProjects -Recurse -File -Filter *.*proj
5+
Get-Item $dotnetProjects
66
}
77
Outputs = {
8-
Join-Path $dotnetProjects obj project.assets.json
8+
$dotnetProjects.ForEach{ Join-Path (Split-Path $_) obj project.assets.json }
99
}
1010
Jobs = {
1111
$local:options = @{} + $script:dotnetOptions

DotNetTest.Task.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ Add-BuildTask DotNetTest @{
22
# This task should be skipped if there are no C# projects to build
33
If = $dotnetTestProjects
44
Inputs = {
5-
Get-ChildItem $dotnetProjects -Recurse -File -Filter *.cs |
6-
Where-Object FullName -NotMatch "[\\/]obj[\\/]"
5+
Get-ChildItem (Split-Path $dotnetProjects) -Recurse -File -Filter *.cs
6+
| Where-Object FullName -NotMatch "[\\/]obj[\\/]"
77
}
88
Outputs = {
99
(Get-ChildItem $TestResultsRoot -Filter *.trx -Recurse -ErrorAction Ignore) ?? $TestResultsRoot

PSModuleBinaryTrim.Task.ps1

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ Add-BuildTask PSModuleBinaryTrim @{
33
Jobs = {
44
$InformationPreference = "Continue"
55
Get-ChildItem $PSModuleOutputPath -Recurse -Filter System.*.dll | Remove-Item
6+
Get-ChildItem $PSModuleOutputPath -Recurse -Filter *.nupkg | Move-Item -Destination $PSModuleOutputPath -Verbose
67
}
78
}

_Initialize.ps1

+12-11
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,18 @@ if (([bool]$DotNet = $dotnetProjects -or $DotNetPublishRoot)) {
122122
# The DotNetPublishRoot is the "publish" folder within the OutputRoot (used for dotnet publish output)
123123
$script:DotNetPublishRoot ??= Join-Path $script:OutputRoot publish
124124

125-
# The projects are expected to each be in their own folder
126-
# Dotnet allows us to pass it the _folder_ that we want to build/test
127-
# So our $buildProjects are the names of the folders that contain the projects
125+
# Our $buildProjects are either:
126+
# - Just the name
127+
# - The full path to a csproj file
128+
# We're going to normalize to the full csproj path
128129
$script:dotnetProjects = @(
129130
if (!$dotnetProjects) {
130131
Write-Information " No `$DotNetProjects specified"
131-
Get-ChildItem -Path $BuildRoot -Include *.*proj -Recurse | Split-Path
132+
Get-ChildItem -Path $BuildRoot -Include *.*proj -Recurse
132133
} elseif (![IO.Path]::IsPathRooted(@($dotnetProjects)[0])) {
133134
Write-Information " Relative `$DotNetProjects specified"
134135
Get-ChildItem -Path $BuildRoot -Include *.*proj -Recurse |
135-
Where-Object { $dotnetProjects -contains $_.BaseName } | Split-Path
136+
Where-Object { $dotnetProjects -contains $_.BaseName }
136137
} else {
137138
$dotnetProjects
138139
}
@@ -142,11 +143,11 @@ if (([bool]$DotNet = $dotnetProjects -or $DotNetPublishRoot)) {
142143
$script:dotnetTestProjects = @(
143144
if (!$dotnetTestProjects) {
144145
Write-Information " No `$DotNetTestProjects specified"
145-
Get-ChildItem -Path $BuildRoot -Include *Test.*proj -Recurse | Split-Path
146+
Get-ChildItem -Path $BuildRoot -Include *Test.*proj -Recurse
146147
} elseif (![IO.Path]::IsPathRooted(@($dotnetTestProjects)[0])) {
147148
Write-Information " Relative `$DotNetTestProjects specified"
148-
Get-ChildItem -Path $BuildRoot -Include *Test.*proj -Recurse |
149-
Where-Object { $dotnetTestProjects -contains $_.BaseName } | Split-Path
149+
Get-ChildItem -Path $BuildRoot -Include *.*proj -Recurse |
150+
Where-Object { $dotnetTestProjects -contains $_.BaseName }
150151
} else {
151152
$dotnetTestProjects
152153
}
@@ -246,7 +247,7 @@ if ($PSModuleName) {
246247
# PackageNames allows you to build and tag multiple packages from the same repository
247248
$script:PackageNames = $script:PackageNames ?? @(
248249
if ($dotnetProjects) {
249-
(Split-Path $dotnetProjects -Leaf).ToLower()
250+
(Split-Path $dotnetProjects -LeafBase).ToLower()
250251
} elseif ($PSModuleName) {
251252
@($PSModuleName)
252253
} else {
@@ -258,15 +259,15 @@ if ($dotnetProjects) {
258259
if ($PSModuleName -and $dotnetProjects -or $DotNetPublishRoot) {
259260
Add-BuildTask Test Build, DotNetTest, PSModuleAnalyze, PSModuleTest
260261
Add-BuildTask Build DotNetRestore, PSModuleRestore, GitVersion, DotNetBuild, DotNetPublish, PSModuleBuild, PSModuleBinaryTrim #, PSModuleBuildHelp
261-
Add-BuildTask Publish TagSource, DotNetPack, PSModulePublish
262+
Add-BuildTask Publish TagSource, DotNetPack, DotNetPush, PSModulePublish
262263
} elseif ($PSModuleName) {
263264
Add-BuildTask Test Build, PSModuleAnalyze, PSModuleTest
264265
Add-BuildTask Build PSModuleRestore, GitVersion, PSModuleBuild #, PSModuleBuildHelp
265266
Add-BuildTask Publish Test, TagSource, PSModulePublish
266267
} elseif ($dotnetProjects) {
267268
Add-BuildTask Test Build, DotNetTest
268269
Add-BuildTask Build DotNetRestore, GitVersion, DotNetBuild, DotNetPublish
269-
Add-BuildTask Publish Test, TagSource, DotNetPack
270+
Add-BuildTask Publish Test, TagSource, DotNetPack, DotNetPush
270271
}
271272

272273

0 commit comments

Comments
 (0)