Skip to content

Commit

Permalink
Fixing issue with msbuild assuming net6.0 (#776)
Browse files Browse the repository at this point in the history
* Possible fix for msbuild requiring net6

closes #773

* Figuring out better way to test, plus adding a way to decide which framework csharpier.msbuild will use

* revert version change and update doc

* some cleanup
  • Loading branch information
belav authored Dec 26, 2022
1 parent d1ccb6c commit 3791a63
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 13 deletions.
13 changes: 11 additions & 2 deletions Docs/MSBuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ title: MsBuild Package
hide_table_of_contents: true
---

CSharpier can be run when a package is built by installing the CSharpier.MSBuild nuget package
CSharpier can be run when a project is built by installing the CSharpier.MSBuild nuget package
```console
Install-Package CSharpier.MSBuild
```

As of 0.15.0 This requires .NET6 to be installed. Previous versions require .NET5
## Target Frameworks
By default CSharpier.MsBuild will run CSharpier using the target framework of the project it is running on.
This can be controlled with the following property. This property is required if the csproj is targeting < net6.0 (netstandard2.0, net48, etc)
```xml
<PropertyGroup>
<CSharpier_FrameworkVersion>net6.0</CSharpier_FrameworkVersion>
</PropertyGroup>
```



By default this will
- In Debug - on build will run `dotnet csharpier` in the project folder to format all files in the project.
Expand Down
18 changes: 18 additions & 0 deletions Src/CSharpier.MsBuild.Test/CSharpier.MsBuild.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
<OutputType>library</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.19.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Src/CSharpier.MsBuild.Test/Class.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public class Class { }
8 changes: 8 additions & 0 deletions Src/CSharpier.MsBuild.Test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS base

COPY nupkg ./nupkg
COPY Src/CSharpier.MsBuild.Test ./Src/CSharpier.MsBuild.Test

WORKDIR Src/CSharpier.MsBuild.Test

RUN dotnet build -c Release
7 changes: 7 additions & 0 deletions Src/CSharpier.MsBuild.Test/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="Test Source" value="../../nupkg" />
</packageSources>
</configuration>
10 changes: 3 additions & 7 deletions Src/CSharpier.MsBuild/CSharpier.MsBuild.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../../CSharpier.Build.props"/>
<PropertyGroup>
<!--
targeting more than one framework leads to occasional build errors
and CSharpier.MSBuild just calls csharpier via command line so
targeting other frameworks seems unnecessary
-->
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<!-- See https://github.com/belav/csharpier/issues/481 and https://github.com/belav/csharpier/issues/773 -->
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<PackageId>CSharpier.MsBuild</PackageId>
<DevelopmentDependency>true</DevelopmentDependency>
<CSharpierOutputDir>../CSharpier.Cli/bin/$(Configuration)/net6.0</CSharpierOutputDir>
<CSharpierOutputDir>../CSharpier.Cli/bin/$(Configuration)/$(TargetFramework)</CSharpierOutputDir>
</PropertyGroup>

<!--
Expand Down
8 changes: 6 additions & 2 deletions Src/CSharpier.MsBuild/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
You can test this by running the script at `[RepositoryRoot]/Scripts/BuildPackages.ps1`
Then add a nuget source pointing to `[RepositoryRoot]/nupkg` and install the nuget package into a project
This can be tested by

- Making any changes you want
- Running the script at `[RepositoryRoot]/Scripts/BuildPackages.ps1`
- Running this from the root `docker build . -f /Src/CSharpier.MsBuild.Test/Dockerfile`

1 change: 0 additions & 1 deletion Src/CSharpier.MsBuild/build/CSharpier.MsBuild.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<PropertyGroup>
<!-- Setting default value here, so it can be overwritten by caller -->
<CSharpier_Check>false</CSharpier_Check>
<CSharpier_Cache>false</CSharpier_Cache>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
Expand Down
3 changes: 2 additions & 1 deletion Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<PropertyGroup>
<CSharpierDllPath>$(MSBuildThisFileDirectory)../tools/csharpier/dotnet-csharpier.dll</CSharpierDllPath>
<CSharpier_FrameworkVersion Condition="'$(CSharpier_FrameworkVersion)' == ''">$(TargetFramework)</CSharpier_FrameworkVersion>
<CSharpierDllPath>$(MSBuildThisFileDirectory)../tools/csharpier/$(CSharpier_FrameworkVersion)/dotnet-csharpier.dll</CSharpierDllPath>
<CSharpierArgs Condition="$(CSharpier_Check)">$(CSharpierArgs) --check</CSharpierArgs>
</PropertyGroup>

Expand Down

0 comments on commit 3791a63

Please sign in to comment.