forked from davidfowl/NuGetPowerTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNuGetMSBuild.psm1
More file actions
186 lines (153 loc) · 5.49 KB
/
Copy pathNuGetMSBuild.psm1
File metadata and controls
186 lines (153 loc) · 5.49 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
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
function Resolve-ProjectName {
param(
[parameter(ValueFromPipelineByPropertyName = $true)]
[string[]]$ProjectName
)
if($ProjectName) {
$projects = Get-Project $ProjectName
}
else {
# All projects by default
$projects = Get-Project -All
}
$projects
}
function Get-InstallPath {
param(
$package
)
# Get the repository path
$componentModel = Get-VSComponentModel
$repositorySettings = $componentModel.GetService([NuGet.VisualStudio.IRepositorySettings])
$pathResolver = New-Object NuGet.DefaultPackagePathResolver($repositorySettings.RepositoryPath)
$pathResolver.GetInstallPath($package)
}
function Ensure-NuGetBuild {
# Install the nuget command line if it doesn't exist
$solutionDir = Get-SolutionDir
$nugetToolsPath = (Join-Path $solutionDir .nuget)
if(!(Test-Path $nugetToolsPath) -or !(Get-ChildItem $nugetToolsPath)) {
Write-Host "Tool path does not exist or could not find nuget.exe and msbuild scripts in the tool path."
$nugetBuildPackage = @(Get-Package -List NuGet.Build)[0]
$nugetExePackage = @(Get-Package -List NuGet.CommandLine)[0]
if($nugetBuildPackage -and $nugetExePackage) {
Install-Package NuGet.Build
}
else {
Write-Warning "Could not find NuGet.Build in normal sources, attempting default source."
Install-Package NuGet.Build -Source 'https://go.microsoft.com/fwlink/?LinkID=206669'
}
$nugetBuildPackage = @(Get-Package NuGet.Build)[0]
$nugetExePackage = @(Get-Package NuGet.CommandLine)[0]
if(!$nugetBuildPackage -and !$nugetExePackage) {
return $false
}
# Get the package path
$nugetBuildPath = Get-InstallPath $nugetBuildPackage
$nugetExePath = Get-InstallPath $nugetExePackage
if(!(Test-Path $nugetToolsPath)) {
mkdir $nugetToolsPath | Out-Null
}
Write-Host "Copying nuget.exe and msbuild scripts to $nugetToolsPath"
Copy-Item "$nugetBuildPath\tools\*.*" $nugetToolsPath -Force | Out-Null
Copy-Item "$nugetExePath\tools\*.*" $nugetToolsPath -Force | Out-Null
Uninstall-Package NuGet.Build -RemoveDependencies
Write-Host "Don't forget to commit the .nuget folder"
}
return $true
}
function Add-NuGetTargets {
param(
[parameter(ValueFromPipelineByPropertyName = $true)]
[string[]]$ProjectName
)
Process {
if($ProjectName) {
$projects = Get-Project $ProjectName
}
else {
# All projects by default
$projects = Get-Project -All
}
if(!$projects) {
Write-Error "Unable to locate project. Make sure it isn't unloaded."
return
}
$targetsPath = '$(SolutionDir)\.nuget\NuGet.targets'
$projects | %{
$project = $_
try {
if($project.Type -eq 'Web Site') {
Write-Warning "Skipping '$($project.Name)', Website projects are not supported"
return
}
if(!$initialized) {
# Make sure the nuget tools exists
$initialized = Ensure-NuGetBuild
}
$project | Add-SolutionDirProperty
$buildProject = $project | Get-MSBuildProject
if(!($buildProject.Xml.Imports | ?{ $_.Project -eq $targetsPath } )) {
$buildProject.Xml.AddImport($targetsPath) | Out-Null
$project.Save()
$buildProject.ReevaluateIfNecessary()
"Updated '$($project.Name)' to use 'NuGet.targets'"
}
}
catch {
Write-Warning "Failed to add import 'NuGet.targets' to $($project.Name) because $($_)"
}
}
}
}
function Enable-PackageRestore {
param(
[parameter(ValueFromPipelineByPropertyName = $true)]
[string[]]$ProjectName
)
# Add the nuget targets on demand
Add-NuGetTargets $ProjectName
(Resolve-ProjectName $ProjectName) | %{
$_ | Set-MSBuildProperty RestorePackages true
"Enabled package restore for $($_.Name)"
}
}
function Disable-PackageRestore {
param(
[parameter(ValueFromPipelineByPropertyName = $true)]
[string[]]$ProjectName
)
(Resolve-ProjectName $ProjectName) | %{
$_ | Set-MSBuildProperty RestorePackages false
"Disabled package restore for $($_.Name)"
}
}
function Enable-PackageBuild {
param(
[parameter(ValueFromPipelineByPropertyName = $true)]
[string[]]$ProjectName
)
# Add the nuget targets on demand
Add-NuGetTargets $ProjectName
(Resolve-ProjectName $ProjectName) | %{
$_ | Set-MSBuildProperty BuildPackage true
"Enabled package build for $($_.Name)"
}
}
function Disable-PackageBuild {
param(
[parameter(ValueFromPipelineByPropertyName = $true)]
[string[]]$ProjectName
)
(Resolve-ProjectName $ProjectName) | %{
$_ | Set-MSBuildProperty BuildPackage false
"Disabled package build for $($_.Name)"
}
}
# Statement completion for project names
'Enable-PackageRestore', 'Disable-PackageRestore', 'Enable-PackageBuild', 'Disable-PackageBuild' | %{
Register-TabExpansion $_ @{
ProjectName = { Get-Project -All | Select -ExpandProperty Name }
}
}
Export-ModuleMember Enable-PackageRestore, Disable-PackageRestore, Enable-PackageBuild, Disable-PackageBuild