I have a project which has the following PostBuildEvent
:
<PostBuildEvent>
if exist Diagnostic.nuspec
if exist "$(SolutionDir)\packages\NuGet.CommandLine.2.8.2\tools\NuGet.exe"
"$(SolutionDir)\packages\NuGet.CommandLine.2.8.2\tools\NuGet.exe"
pack Diagnostic.nuspec
-OutputDirectory "C:\Users\jeroen\Google Drive\NugetLocal\VSDiagnostics"
</PostBuildEvent>
Here, the OutPutDirectory
is obviously local to my own installation. The default value is .
.
This configuration is stored in the project's .csproj
file which also keeps track of things like references to other assemblies and the files in the project.
For this reason I can't simply ignore the file in git or none of my other changes to .csproj
would go through.
Is there any way I can keep my localized PostBuildEvent
without imposing it on other collaborators?
I solved it through setting a user-account level environment variable and depending on that, use it or use the default.
I simply swapped the original PostBuildEvent
and surrounding PropertyGroup
with this:
<Choose>
<When Condition=" $(NUGETLOCAL) != '' ">
<PropertyGroup>
<PostBuildEvent>if exist Diagnostic.nuspec if exist "$(SolutionDir)\packages\NuGet.CommandLine.2.8.2\tools\NuGet.exe" "$(SolutionDir)\packages\NuGet.CommandLine.2.8.2\tools\NuGet.exe" pack Diagnostic.nuspec -OutputDirectory $(NUGETLOCAL)</PostBuildEvent>
<RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<PostBuildEvent>if exist Diagnostic.nuspec if exist "$(SolutionDir)\packages\NuGet.CommandLine.2.8.2\tools\NuGet.exe" "$(SolutionDir)\packages\NuGet.CommandLine.2.8.2\tools\NuGet.exe" pack Diagnostic.nuspec -OutputDirectory .</PostBuildEvent>
<RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
</PropertyGroup>
</Otherwise>
</Choose>
The environment variable is set as NUGETLOCAL
with value "C:\Users\jeroen\Google Drive\NugetLocal\VSDiagnostics"
(including accents). If the variable isn't set it will use the default directory and the user has the option to define his own at anytime he wants.
The code is probably not as pretty as it can be so let me know if you see an improvement.
I elaborated on this here.
As I mentioned in the comments, I think this should only be done for Release
builds. The action could be skipped entirely unless it's being built in the Release
configuration by leveraging the ConfigurationName
Macro.
if $(ConfigurationName) == Release (
if exist Diagnostic.nuspec
if exist "$(SolutionDir)\packages\NuGet.CommandLine.2.8.2\tools\NuGet.exe"
"$(SolutionDir)\packages\NuGet.CommandLine.2.8.2\tools\NuGet.exe"
pack Diagnostic.nuspec
-OutputDirectory "C:\Users\jeroen\Google Drive\NugetLocal\VSDiagnostics"
)
Here's the list of all the available Macros. I also understand that all Environment variables are imported as macros, so you might be able to come up with something a little more elegant based on the user profile and the existence of the subdirectory.
Upon further thought, you could use a standard environment variable, USERPROFILE
and just check to see if the path exists.
if exist "$(USERPROFILE)\Google Drive\NugetLocal\VSDiagnostics"(
if exist Diagnostic.nuspec
if exist "$(SolutionDir)\packages\NuGet.CommandLine.2.8.2\tools\NuGet.exe"
"$(SolutionDir)\packages\NuGet.CommandLine.2.8.2\tools\NuGet.exe"
pack Diagnostic.nuspec
-OutputDirectory "$(USERPROFILE)\Google Drive\NugetLocal\VSDiagnostics"
)
This does have some limitations though. It will only work on Windows. Linux uses a different variable for the user profile path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With