.Net Core msbuild build.proj, эквивалентный AssemblyInfo - PullRequest
0 голосов
/ 17 октября 2019

Я обновляю проект с AspNet Mvc 4 до AspNet Core Mvc 2.2;Я пытаюсь перенести файл msbuild build.proj, чтобы установить версию и другие атрибуты для проектов, которые создают библиотеки DLL;все работает, кроме задачи GenerateAssemblyInfo. Есть ли новый способ сделать это в netcoreapp2.2?

    <?xml version="1.0" encoding="utf-8"?>
       <Project ToolsVersion="15.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="GetRevisionInfo" BeforeTargets="Build">
    <GitPendingChanges ContinueOnError="WarnAndContinue">
      <Output TaskParameter="HasPendingChanges" PropertyName="HasPendingChanges" />
    </GitPendingChanges>
    <!--This will throw git Error 128 if there are no Tags -->
    <GitDescribe SoftErrorMode="true" Lightweight="true" ContinueOnError="WarnAndContinue">
      <Output TaskParameter="Tag" PropertyName="Tag" />
      <Output TaskParameter="CommitCount" PropertyName="CommitCount" />
      <Output TaskParameter="CommitHash" PropertyName="CommitHash" />
    </GitDescribe>
    <GitBranch ContinueOnError="WarnAndContinue">
      <Output TaskParameter="Branch" PropertyName="Branch" />
    </GitBranch>
    <PropertyGroup>
      <ShortCommitHash Condition="'$(CommitHash)' != ''">$(CommitHash.Substring(0,6))</ShortCommitHash>
      <ReleaseType Condition="'$(CommitCount)' != '' AND '$(CommitCount)' != '0'">Beta</ReleaseType>
      <ReleaseType Condition="'$(Branch)' != '' AND '$(Branch)' != 'master'">Alpha</ReleaseType>
    </PropertyGroup>
  </Target>

  <!-- Error out if this is a release and our working copy has uncommitted changes -->
  <Target Name="CheckRelease" AfterTargets="GetRevisionInfo" Condition="'$(Configuration)' == 'Release'">
    <Error Text="Cannot build a Release Version when there are uncommitted changes, commit or revert all changes." Condition="'$(CommitHash)' != '' AND '$(HasPendingChanges)' == 'True'" />
  </Target>

  <!-- Generates AssemblyInfo file using Git Describe -->
  <Target Name="GenerateAssemblyInfo" AfterTargets="CheckRelease" Condition="'$(CommitHash)' != ''">
    <Time>
      <Output TaskParameter="Year" PropertyName="Year" />
    </Time>
    <AssemblyInfo
      CodeLanguage="CS"
      OutputFile="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs"
      AssemblyProduct="$(MSBuildProjectName) $(ReleaseType)"
      AssemblyCompany="xxx xxx xxx, LLC"
      AssemblyCopyright="Copyright © $(Year) xxx xxx xxx, LLC. All rights reserved."
      AssemblyConfiguration="$(Configuration)-$(Platform)"
      AssemblyVersion="$(Tag).$(CommitCount)"
      AssemblyFileVersion="$(Tag).$(CommitCount)"
        AssemblyInformationalVersion ="$(Tag)-$(CommitCount)-$(ShortCommitHash) $(ReleaseType)"
        AssemblyTitle="$(Tag)-$(CommitCount)-$(CommitHash)"/>
  </Target>

  <!-- copy framework files to libraries -->
  <Target Name="CopyLibraries" Condition="'$(MSBuildProjectName)' == 'BaseApplication'" AfterTargets="Build">
    <CreateItem Include="$(TargetDir)xxx.*">
      <Output TaskParameter="Include" PropertyName="CopyFiles"  />
    </CreateItem>
    <Copy SourceFiles="$(CopyFiles)" DestinationFolder="$(MSBuildProjectDirectory)\..\libraries\xxx\$(Configuration)"/>
  </Target>
  <Target Name="CopyKendoUI" AfterTargets="AfterBuild">
    <ItemGroup>
      <KendoFiles Include="
                    $(TargetDir)kendo.mvc.*;
                    $(TargetDir)\**\Kendo.Mvc.resources.*;" />
    </ItemGroup>
    <Copy SourceFiles="@(KendoFiles)" DestinationFolder="$(MSBuildProjectDirectory)\..\libraries\kendoui\$(Configuration)\%(RecursiveDir)"/>
  </Target>
</Project>

1 Ответ

0 голосов
/ 18 октября 2019

Было несколько вещей, мешающих этому работать в Visual Studio 2019 с использованием .Net Core;

  1. Имя цели не может быть «GenerateAssemblyInfo», если это имя цели, оно просто игнорируется, без ошибок, предупреждений или сообщений, цель просто не запускается. ..
  2. В файле csproj для проекта, в который импортируется проект msbuild, вам нужно добавить false, я добавил это прямо под
  3. . Вы должны вручную создать папку Properties, есливы устанавливаете OutputFile в эту папку.

Ниже приведен рабочий файл build.proj

    <?xml version="1.0" encoding="utf-8"?>
   <Project ToolsVersion="15.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     <Target Name="GetRevisionInfo" BeforeTargets="Build">
       <GitPendingChanges ContinueOnError="WarnAndContinue">
         <Output TaskParameter="HasPendingChanges" PropertyName="HasPendingChanges" />
       </GitPendingChanges>
       <!--This will throw git Error 128 if there are no Tags -->
       <GitDescribe SoftErrorMode="true" Lightweight="true" ContinueOnError="WarnAndContinue">
         <Output TaskParameter="Tag" PropertyName="Tag" />
         <Output TaskParameter="CommitCount" PropertyName="CommitCount" />
         <Output TaskParameter="CommitHash" PropertyName="CommitHash" />
       </GitDescribe>
       <GitBranch ContinueOnError="WarnAndContinue">
         <Output TaskParameter="Branch" PropertyName="Branch" />
       </GitBranch>
       <PropertyGroup>
         <ShortCommitHash Condition="'$(CommitHash)' != ''">$(CommitHash.Substring(0,6))</ShortCommitHash>
         <ReleaseType Condition="'$(CommitCount)' != '' AND '$(CommitCount)' != '0'">Beta</ReleaseType>
         <ReleaseType Condition="'$(Branch)' != '' AND '$(Branch)' != 'master'">Alpha</ReleaseType>
       </PropertyGroup>
     </Target>

  <!-- Error out if this is a release and our working copy has uncommitted changes -->
  <Target Name="CheckRelease" AfterTargets="GetRevisionInfo" Condition="'$(Configuration)' == 'Release'">
    <Error Text="Cannot build a Release Version when there are uncommitted changes, commit or revert all changes." Condition="'$(CommitHash)' != '' AND '$(HasPendingChanges)' == 'True'" />
  </Target>

     <!-- Generates AssemblyInfo file using Git Describe -->
     <Target Name="GenerateAssemblyInfo" AfterTargets="CheckRelease" Condition="'$(CommitHash)' != ''">
       <Time>
         <Output TaskParameter="Year" PropertyName="Year" />
       </Time>
       <AssemblyInfo
         CodeLanguage="CS"
         OutputFile="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs"
         AssemblyProduct="$(MSBuildProjectName) $(ReleaseType)"
         AssemblyCompany="xxx xxx xxx, LLC"
         AssemblyCopyright="Copyright © $(Year) xxx xxx xxx, LLC. All rights reserved."
         AssemblyConfiguration="$(Configuration)-$(Platform)"
         AssemblyVersion="$(Tag).$(CommitCount)"
         AssemblyFileVersion="$(Tag).$(CommitCount)"
         AssemblyInformationalVersion ="$(Tag)-$(CommitCount)-$(ShortCommitHash) $(ReleaseType)"
         AssemblyTitle="$(Tag)-$(CommitCount)-$(CommitHash)"/>
      </Target>

     <!-- copy framework files to libraries -->
     <Target Name="CopyLibraries" Condition="'$(MSBuildProjectName)' == 'BaseApplication'" AfterTargets="Build">
       <CreateItem Include="$(TargetDir)xxx.*">
         <Output TaskParameter="Include" PropertyName="CopyFiles"  />
       </CreateItem>
       <Copy SourceFiles="$(CopyFiles)" DestinationFolder="$(MSBuildProjectDirectory)\..\libraries\xxx\$(Configuration)"/>
     </Target>
     <Target Name="CopyKendoUI" AfterTargets="AfterBuild">
       <ItemGroup>
         <KendoFiles Include="
                       $(TargetDir)kendo.mvc.*;
                       $(TargetDir)\**\Kendo.Mvc.resources.*;" />
       </ItemGroup>
       <Copy SourceFiles="@(KendoFiles)" DestinationFolder="$(MSBuildProjectDirectory)\..\libraries\kendoui\$(Configuration)\%(RecursiveDir)"/>
     </Target>
   </Project>

и файл MyProject.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MSBuildTasks" Version="1.5.0.235">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="Microsoft.AspNetCore.Http.Abstractions">
      <HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.http.abstractions\2.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
    </Reference>
  </ItemGroup>

  <Import Project="../.build/build.proj" />
</Project
...