Reading the Source Code of Microsoft.NET.Sdk, Writing the Creative Extension of Compiling

栏目: 编程工具 · 发布时间: 6年前

内容简介:We’ll try to read the source code ofThis post is written in

Project node starts to support the Sdk attribute since MSBuild release the 15.0 version which is embedded in Visual Studio 2017. For the Sdk attribute, the C# project file whose file extension is csproj becomes much more powerful and extensible.

We’ll try to read the source code of Microsoft.NET.Sdk which is the default Sdk value of C#.NET project and try to write some creative extension of compiling.

This post is written in multiple languages . Please select yours:

Where to find the source code of Microsoft.NET.Sdk

Search Microsoft.NET.Sdk using Everything or Wox , I find that multiple versions are installed in my computer. As I’ve installed the .NET Core 2.1, the location of my latest version is C:\Program Files\dotnet\sdk\2.1.201\Sdks . The official document How to: Reference an MSBuild Project SDK says that if you implement your own Sdk, you can also push it to .

Reading the Source Code of Microsoft.NET.Sdk, Writing the Creative Extension of Compiling ▲ Search Microsoft.NET.Sdk

Reading the Source Code of Microsoft.NET.Sdk, Writing the Creative Extension of Compiling ▲ The Sdk folder on my computer

The NuGet part of Microsoft.NET.Sdk is on GitHub:

The folder structure of Microsoft.NET.Sdk

When clicking into the Microsoft.NET.Sdk folder, we can find that the folder structure is very similar to the NuGet folder structure.

Reading the Source Code of Microsoft.NET.Sdk, Writing the Creative Extension of Compiling

I’ve written some posts talking about the NuGet folder structure but unfortunately they are all not in English:

Microsoft have some official document talking about the NuGet folder structure How to create a NuGet package from a convention-based working directory .

Butthere exists an extra Sdk folder for the Sdk kind of NuGet package.

Reading the Source Code of Microsoft.NET.Sdk, Writing the Creative Extension of Compiling

The Sdk.props file and the Sdj.targets file will be imported by default and Microsoft’s official document also mentions it here: How to: Reference an MSBuild Project SDK - Visual Studio . It says that the two code blocks are actually the same:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net46</TargetFramework>
    </PropertyGroup>
</Project>
<Project>
    <!-- Implicit top import -->
    <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />

    <PropertyGroup>
        <TargetFramework>net46</TargetFramework>
    </PropertyGroup>

    <!-- Implicit bottom import -->
    <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>

Because of the default importation behavior, Sdk can do variaty of tasks when MSBuild or Roslyn build the .NET projects. The default Sdk Microsoft.NET.Sdk is very extensible so that we can easily use it to customize out compiling behavior and I’ve mentioned these in the two non-English posts above.

The major targets of Microsoft.NET.Sdk

I try to search Target node in the whole Sdk folder and find out 174 Target s. Don’t worry about the huge Target amount because most of them are private by indicating the name with a _ prefix and some of them have the same name to override and wait to be overridden.

So the core compiling Target is not so many and I pick up some core Target here:

  • CollectPackageReferences : Collect the PackageReference items to resolve the package dependencies of the project.
  • CoreCompile The core compiling Target .

  • GenerateAssemblyInfo : Generate the AssemblyInfo.cs file which is usually written by developers manually before .NET Core published.
  • Pack : Pack current project into a NuGet package file whose extension is nupkg.
  • GenerateNuspec : Generate the nuspec file which is the meta info of the NuGet package.

Write creative extensions of compiling

I also find some creative Target that inspires me:

<Target Name="DontRestore" BeforeTargets="Restore">
  <Error Text="This project should not be restored" />
 </Target>

▲ If a Restore target exists, then report a compiling error.

<Target Name="ReferenceStaticLegacyPackage" BeforeTargets="CollectPackageReferences">
  <ItemGroup>
    <PackageReference Remove="LiteDB" />
    <PackageReference Include="LiteDB" Version="2.0.2" />
  </ItemGroup>
</Target>

▲ This is written by me to prevent a specific package named LiteDB to be upgrade. I post this method in my another non-English post .

References

本文会经常更新,请阅读原文: https://walterlv.github.io/post/read-microsoft-net-sdk-en.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

Reading the Source Code of Microsoft.NET.Sdk, Writing the Creative Extension of Compiling 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://walterlv.github.io ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

移动应用的设计与开发

移动应用的设计与开发

[美] 弗林 (Brian Fling) / 马晶慧 / 电子工业出版社 / 2010-5 / 59.80元

本书全面介绍了如何在移动设备上设计和开发应用程序。书中从介绍移动产业的生态环境和移动媒体开始,阐述产品策划的方法、产品架构、视觉设计和产品类型的选择,并详细描述了产品实现过程中所用到的一些技术、工具和概念,最后还简单介绍了如何获得利润和降低成本,肯定了iPhone在移动设备发展史上起到的巨大推动作用。本书不仅能让读者了解到移动设计和开发的知识,更重要的是,它揭示了移动开发的代价高昂、标准混乱的根本......一起来看看 《移动应用的设计与开发》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具