Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 945
Kobi Avatar asked Apr 13 '17 13:04

Kobi


People also ask

Does .NET have compiler?

Typically, . NET apps are compiled to intermediate language (IL). At run time, the just-in-time (JIT) compiler translates the IL to native code.

What is Microsoft Net compilers toolset for?

NET Compilers Toolset Package. Referencing this package will cause the project to be built using the C# and Visual Basic compilers contained in the package, as opposed to the version installed with MSBuild. This package is primarily intended as a method for rapidly shipping hotfixes to customers.

Which compiler is used in asp net?

NET Compiler Platform, also known by its codename Roslyn, is a set of open-source compilers and code analysis APIs for C# and Visual Basic (VB.NET) languages from Microsoft.

What is .NET compiler platform SDK?

The . NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich code analysis APIs. You can build code analysis tools with the same APIs that Microsoft is using to implement Visual Studio! Also includes the Syntax Visualizer, a Visual Studio ... Download.


2 Answers

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 (animation is from the NuGet blog post linked above)

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>
like image 104
Martin Ullrich Avatar answered Oct 23 '22 05:10

Martin Ullrich


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 : https://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.

like image 36
satibel Avatar answered Oct 23 '22 04:10

satibel