microsoft / MSBuildSdks

MSBuild project SDKs
MIT License
459 stars 81 forks source link

Traversal - Per-project Properties #418

Closed rcollina closed 1 year ago

rcollina commented 1 year ago

Hello, Given the following proj file:

<Project Sdk="Microsoft.Build.Traversal/3.0.3">
    <ItemGroup>
        <ProjectReference
            Include="/src/alpha/alpha.csproj"/>
        <ProjectReference Include=“src/beta/beta.csproj"/>
    </ItemGroup>
</Project>

Is there any way to define a set of properties for each referenced project?

In pseudocode:

<Project Sdk="Microsoft.Build.Traversal/3.0.3">
    <ItemGroup>
        <ProjectReference
            Include="/src/alpha/alpha.csproj">
           <PropertyGroup>
                <VersionSuffix>prerelease.1</VersionSuffix>
            </PropertyGroup>
        </ProjectReference>
        <ProjectReference
            Include="/src/beta/beta.csproj">
            <PropertyGroup>
              <VersionSuffix>prerelease.2</VersionSuffix>
            </PropertyGroup>
        </ProjectReference>
    </ItemGroup>
</Project>

What are my options here? Thank you

jeffkl commented 1 year ago

ProjectReference items support the AdditionalProperties metadata which is passed as a global property during build:

<Project Sdk="Microsoft.Build.Traversal/3.0.3">
    <ItemGroup>
        <ProjectReference Include="/src/alpha/alpha.csproj"
                          AdditionalProperties="VersionSuffix=prerelease.1" />

        <ProjectReference Include="/src/beta/beta.csproj"
                          AdditionalProperties="VersionSuffix=prerelease.2" />
</Project>

I wouldn't recommend this though as you'll get different build results if you build the individual project vs this traversal project. Unless that's what you're going for, you would want to just set these properties in the project themselves.

rcollina commented 1 year ago

@jeffkl thank you, much appreciated.

I am going to use this in a CI pipeline. I have an admittedly complicated bunch of powershell scripts that act upon changes in a monorepo; they build, test and publish either nugets or container images. I'm appending a prerelease suffix depending on the commit branch.