-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPublish.ps1
executable file
·122 lines (100 loc) · 3.87 KB
/
Publish.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
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
#!/usr/bin/env pwsh
param (
[string]$ProjectPath = ".", # Path to the .NET project or solution
[string]$NuGetServerUrl = "http://localhost:5555/v3/index.json", # URL of the locally hosted NuGet server
[string]$OutputDirectory = "bin/Release", # Output directory for the package (relative to project path)
[string]$ApiKey = "your-api-key-here" # API key for your local NuGet server (if required)
)
# Convert the path specified to absolute
function Convert-ToAbsolutePath {
param (
[string]$PathToCheck
)
# Check if the path is already absolute
if ([System.IO.Path]::IsPathRooted($PathToCheck)) {
# If it's already absolute, return it as is
return $PathToCheck
} else {
# If it's relative, resolve it to an absolute path
$absolutePath = Resolve-Path $PathToCheck
return $absolutePath
}
}
# Function to check if .NET CLI is installed
function Check-Dotnet {
$dotnetInstalled = (Get-Command dotnet -ErrorAction SilentlyContinue)
if (-not $dotnetInstalled) {
Write-Host ".NET CLI is not installed. Please install .NET SDK to proceed." -ForegroundColor Red
exit 1
} else {
Write-Host ".NET CLI is installed." -ForegroundColor Green
}
}
# Function to build the project using dotnet
function Build-Project {
param (
[string]$ProjectPath,
[string]$OutputDirectory
)
Write-Host "Building project at: $ProjectPath" -ForegroundColor Yellow
dotnet build $ProjectPath --configuration Release
if ($LASTEXITCODE -eq 0) {
Write-Host "Project built successfully." -ForegroundColor Green
} else {
Write-Host "Build failed." -ForegroundColor Red
exit 1
}
}
# Function to pack the project into a NuGet package
function Pack-Project {
param (
[string]$ProjectPath,
[string]$OutputDirectory
)
Write-Host "Packing project into a NuGet package..." -ForegroundColor Yellow
dotnet pack $ProjectPath --configuration Release --output $OutputDirectory
if ($LASTEXITCODE -eq 0) {
Write-Host "Project packaged successfully." -ForegroundColor Green
} else {
Write-Host "Packaging failed." -ForegroundColor Red
exit 1
}
}
# Function to push the NuGet package to the server
function Push-NuGetPackage {
param (
[string]$PackagePath,
[string]$NuGetServerUrl,
[string]$ApiKey
)
Write-Host "Pushing package to NuGet server: $NuGetServerUrl" -ForegroundColor Yellow
dotnet nuget push $PackagePath --api-key $ApiKey --source $NuGetServerUrl
if ($LASTEXITCODE -eq 0) {
Write-Host "Package pushed to NuGet server successfully." -ForegroundColor Green
} else {
Write-Host "Failed to push package." -ForegroundColor Red
exit 1
}
}
# Main Script
Write-Host "Starting the publish process..." -ForegroundColor Cyan
# Ensure .NET CLI is installed
Check-Dotnet
# Convert project path to an absolute path
$absoluteProjectPath = Resolve-Path $ProjectPath
Write-Host "Resolved project path: $absoluteProjectPath" -ForegroundColor Cyan
# Build the project
Build-Project -ProjectPath $absoluteProjectPath -OutputDirectory $OutputDirectory
# Pack the project
Pack-Project -ProjectPath $absoluteProjectPath -OutputDirectory $OutputDirectory
# Find the NuGet package in the output directory
$package = Get-ChildItem -Path $OutputDirectory -Filter "*.nupkg" | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if (-not $package) {
Write-Host "No NuGet package found in the output directory." -ForegroundColor Red
exit 1
}
$packagePath = $package.FullName
Write-Host "Found package: $packagePath" -ForegroundColor Green
# Push the package to the NuGet server
Push-NuGetPackage -PackagePath $packagePath -NuGetServerUrl $NuGetServerUrl -ApiKey $ApiKey
Write-Host "Publish process completed." -ForegroundColor Cyan