Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting incorrect when using build events and variables #8

Open
psylenced opened this issue Apr 26, 2017 · 2 comments
Open

Sorting incorrect when using build events and variables #8

psylenced opened this issue Apr 26, 2017 · 2 comments

Comments

@psylenced
Copy link

I was trying to create a build event that sorts the csproj files pre-build.

And have the following code:

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
    <PreBuildEvent>
        call "$(SolutionDir)\BuildScripts\sort-csproj.cmd" "$(ProjectPath)"
    </PreBuildEvent>
  </PropertyGroup>

What I found was that when using the option CombineRootElements, the <PreBuildEvent> element gets combined and moved up to the top <PropertyGroup> parent.

The issue with this is the $(ProjectPath) needs to be after the <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> line otherwise the variable is blank.

This means when running the first time, it works. But running it a second time, the sorting event fails due to passing a blank parameter.

The fix is to make sure the <PreBuildEvent> (and other event elements) are excluded from the combine and also make sure they are always placed after the <Import> target elements.

@psylenced
Copy link
Author

psylenced commented Apr 26, 2017

Code for sort-csproj.cmd for fullness:

@echo off

for %%A in ("%~dp0\..") do set "root_parent=%%~fA"

echo param: %0 %1

IF NOT EXIST "%root_parent%\BuildScripts\CsProjArrange.exe" (
	echo ERROR: Cannot find %root_parent%\BuildScripts\CsProjArrange.exe sorting utility.
	exit 1
)

echo Sorting: %~1
copy /y "%root_parent%\BuildScripts\empty.txt" "%~1.sorted" > nul
"%root_parent%\BuildScripts\CsProjArrange.exe" -i "%~1" -o "%~1.sorted" >> %%f.sorted

FC  "%~1" "%~1.sorted" > nul 
IF ERRORLEVEL 1 (
	echo csproj changed after sorting - replacing
	move /y "%~1.sorted" "%~1" > nul
) ELSE ( 
	echo csproj same - no action
	del "%~1.sorted"
)

empty.txt is just an empty text file with the 3 character BOM / UTF-8 header (EF BB BF).

@schmalls
Copy link
Contributor

@Chris-GH Take a look at the Option Sections. If you have certain sections that have to appear in a specific order, you can use them to make that happen. Another benefit to this is that you can specify different options for each section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants