2

My goal: To run dotnet gitversion /updateprojectfiles as part of the build.

My limitations: not all machines have gitversion installed, so I need to install it on those without. However, Microsoft's dotnet tool install will return the code 1 when attempting to install a tool that is already installed. So I need to first check if it is installed, and only if it isn't, install it.

My first solution: Write a batch file and call it from the prebuild script.

The complication: Some of the machines building the code are linux, and do not recognize batch files.

My question: How can I write my logic as part of the pre-build commands, in a way that will work on both windows and linux?

The code: gitversion-up-to-date.cmd:

FOR /F "tokens=* USEBACKQ" %%F IN (`dotnet tool list -g ^| findstr gitversion.tool`) DO (
SET "grep=TRUE"
)

if %grep%==TRUE goto setVersion

dotnet tool install -g GitVersion.Tool
setVersion

:setVersion
dotnet gitversion /updateprojectfiles

and then from the csproj prebuild:

$(SolutionDir)gitversion-up-to-date.cmd

How can I write this logic, preferably directly in the prebuild, in a way that will work on both windows and linux?

My biggest issue atm is understanding how to write the logic for finding whether the tool is already installed.

TIA!

PMO1948
  • 2,210
  • 11
  • 34
  • Would you be open to having powershell as a dependency? If yes that would allow you to have a scripting system existing on both OSes. – Irwene Mar 10 '21 at 08:36
  • powershell cannot run on linux either – PMO1948 Mar 10 '21 at 10:46
  • Check this: https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7.1 or alternatively it can be installed as a global tool using the `dotnet` command – Irwene Mar 10 '21 at 13:08
  • 2
    This: `SET grep = TRUE` is the incorrect syntax. Have created a variable name with a trailing space and assigned the value of the variable with a leading space. Please use this best practice. `SET "grep=TRUE"` This protects special characters and keeps any trailing spaces from getting assigned to the variable. – Squashman Mar 10 '21 at 14:09
  • @Squashman thanks! I'm new to batch, so thanks for the tip :-) – PMO1948 Mar 11 '21 at 08:31
  • Thanks! I am not currently interested in improving the batch file - I need to use something else, that works in linux as well. My question is not about the code, so much as how to write the same logic in a way that would work from the prebuild command ;-) – PMO1948 Mar 11 '21 at 10:05
  • 1
    @PMO1948 we are pointing out possible failure points in your code and trying to help you understand the best practices to prevent future failures of your code. We can only lead the horse to the water. – Squashman Mar 11 '21 at 13:02
  • Thanks - and I appreciate the tips. The code itself works just fine on Windows, but I need to be able to run the preBuild on linux too. Linux does not support batch files, so I need another way to write the same logic using something that will run on both windows and linux – PMO1948 Mar 11 '21 at 13:14
  • Did you have the time to review the information I gave you about powershell on linux ? – Irwene Mar 12 '21 at 08:01
  • hey, I spoke to the project leader - he is not interested in adding an additional dependency. The linux aspect of the work is with jenkins, so including powershell would require a special image (or something - I am not an expert on the Jenkins aspect) So the solution would need to be able to work on both platforms out of the box, so to speak – PMO1948 Mar 14 '21 at 08:21

0 Answers0