Tuesday, May 2, 2017

How can I use Microsoft.Net.Compilers at solution level?

Leave a Comment

I want to start using Microsoft.Net.Compilers to simplify work with our build server. However, I could only get it to work at a per-project level, by adding the package to all projects.
This is problematic because the package would have to be added to each new created project. This could lead to a case where the code compiles on the developer's machine (which has the latest compiler), but would fail on the build server. We have many projects (over 100), so this is relatively common.

Is there a way of using Microsoft.Net.Compilers at solution level?

If there is no supported way, is there a command line tool I would not have to install on the build server? Alternatively, is this not an intended usage of these tools?

2 Answers

Answers 1

If in VS 2017 (update 1, build numbers >= 15.1.*) you can use the MSBuild integrated PackageReference mechanism instead of packages.config which was previously only available for .net core and .net standard project types. See the PackageReference documentation as well as the NuGet blog post announcing the support, especially the section "What about other project types that are not .NET Core?".

The idea is to switch from installing a package and adding it to packages.config for restore to just specifying an MSBuild items in the csproj file. This can be set up for new projects in VS: Switch from packages.config to ProjectReference

A new feature of MSBuild 15 is that it supports automatically including files in the directory hierarchy that have special names. Those are Directory.Build.props and Directory.Build.targets which will get included before (props) and after (targets) your project file's content (there is a bug with the .targets version for multi targeting projects for which a fix is about to be released).

If you create a Directory.Build.props file with the following content at the solution level, all projects in the directory hierarchy below it will inherit it's content and you can force a NuGet dependency onto each project:

<Project>     <ItemGroup>         <PackageReference Include="Microsoft.Net.Compilers" Version="2.1.0"/>     </ItemGroup> <Project> 

Answers 2

For existing projects in the solution:

Right-click your solution > Manage NuGet Packages for Solution...

... Or:

Tools > Library Package Manager > Manage NuGet Packages for Solution...

Then add it to all the projects by browsing for your package, then checking the top checkbox, and clicking install.

Source : http://stackoverflow.com/a/8653312/7007466


For the new projects:

Create a template mimicking the original one for each type of project you need (Console, Library etc...) and adding the package to it.

  • Create a project.

    Note: Use only valid identifier characters when naming a project that will be the source for a template. A template exported from a project named with invalid characters can cause compilation errors in future projects based on the template.

  • Edit the project until it is ready to be exported as a template.
  • As appropriate, edit the code files to indicate where parameter replacement should take place.
  • On the File menu, click Export Template. The Export Template wizard opens.
  • Click Project Template.
  • If you have more than one project in your current solution, select the projects you want to export to a template.
  • Click Next.
  • Select an icon and a preview image for your template. These will appear in the New Project dialog box.
  • Enter a template name and description.
  • Click Finish. Your project is exported into a .zip file and placed in the specified output location, and, if selected, imported into Visual Studio.

If you have the Visual Studio SDK installed, you can wrap the finished template in a .vsix file for deployment by using the VSIX Project template.

Source : https://msdn.microsoft.com/en-us/library/xkh1wxd8.aspx

If someone has an easier way than creating templates, I'll gladly take it.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment