Skip to content

Instantly share code, notes, and snippets.

@baronfel
Created October 9, 2022 23:31
Show Gist options
  • Save baronfel/27f3cb04d2a1cfc398a250bb74080be4 to your computer and use it in GitHub Desktop.
Save baronfel/27f3cb04d2a1cfc398a250bb74080be4 to your computer and use it in GitHub Desktop.
MSBuild target to write license expressions to an assembly attribute
<Project>
<!-- Create a target that will assign the license information for a project to an attribute -->
<Target Name="AssignLicenseAttribute">
<!-- Only do this if the user is allowing assemblyinfo generation -->
<ItemGroup Condition="'$(GenerateAssemblyInfo)' == 'true'">
<!-- Only generate if the license is present and the user has requested this attribute -->
<AssemblyAttribute
Include="System.Reflection.AssemblyMetadataAttribute"
Condition="'$(GenerateLicenseExpressionAttribute)' == 'true' and '$(PackageLicenseExpression)' != ''">
<_Parameter1>LicenseExpression</_Parameter1>
<_Parameter2>$(PackageLicenseExpression)</_Parameter2>
</AssemblyAttribute>
</ItemGroup>
</Target>
<PropertyGroup>
<!-- Default the attribute to 'on' but allow for user overrides -->
<GenerateLicenseExpressionAttribute Condition="'$(GenerateLicenseExpressionAttribute)' == ''">true</GenerateLicenseExpressionAttribute>
<!-- Tell the build to run our target before the build command (so we generate the attribute before compilation) -->
<PrepareForBuildDependsOn>
AssignLicenseAttribute;
$(PrepareForBuildDependsOn)
</PrepareForBuildDependsOn>
</PropertyGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment