Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a localized PostBuildEvent in an environment with multiple collaborators?

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?

like image 909
Jeroen Vannevel Avatar asked May 29 '15 12:05

Jeroen Vannevel


2 Answers

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.

like image 116
Jeroen Vannevel Avatar answered Nov 17 '22 22:11

Jeroen Vannevel


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.

like image 2
RubberDuck Avatar answered Nov 17 '22 20:11

RubberDuck