首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MSBuild -如何动态包含生成的*.razor文件

MSBuild -如何动态包含生成的*.razor文件
EN

Stack Overflow用户
提问于 2020-12-19 06:15:57
回答 1查看 177关注 0票数 0

下面的csproj似乎包含了我生成的.razor文件,因为如果我运行两次,它会报告重复。它没有做的是生成g.cs文件或将它们编译到项目中。如果我重新构建,它们会包含在内,但我的要求是在一个步骤中生成和构建。生成器是一个命令行生成器MudBlazor.Docs.Compiler

我已经试着解决这个问题3天了,所以我得到了很多帮助。

代码语言:javascript
复制
<!--https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-reference -->
<!--Use: dotnet msbuild -preprocess:<fileName>.xml to evaluate this project-->
<Project Sdk="Microsoft.NET.Sdk.Razor">

  <!--Make sure this projects build targets run before other referenced projects-->
  <PropertyGroup>
    <BuildDependsOn>
      CompileDocs;
      $(BuildDependsOn)
    </BuildDependsOn> 
  </PropertyGroup>

  <!--Outside Visual Studio SolutionDir is not available-->  
  <PropertyGroup>
    <SolutionDir Condition=" '$(SolutionDir)' == '' ">$(MSBuildThisFileDirectory)..\</SolutionDir>
  </PropertyGroup>

  <!--Look for a compiled version of this project for performance-->
  <Choose>
    <When Condition="Exists('$(SolutionDir)MudBlazor.Docs.Compiler/bin/Debug/netcoreapp3.1/MudBlazor.Docs.Compiler.dll')">
      <PropertyGroup>
        <RunCommand>dotnet "$(SolutionDir)MudBlazor.Docs.Compiler/bin/Debug/netcoreapp3.1/MudBlazor.Docs.Compiler.dll"</RunCommand>
      </PropertyGroup>
    </When>
    <When Condition="Exists('$(SolutionDir)MudBlazor.Docs.Compiler/bin/Debug/net5.0/MudBlazor.Docs.Compiler.dll')">
      <PropertyGroup>
        <RunCommand>dotnet "$(SolutionDir)MudBlazor.Docs.Compiler/bin/Debug/net5.0/MudBlazor.Docs.Compiler.dll"</RunCommand>
      </PropertyGroup>
    </When>
    <Otherwise>
      <PropertyGroup>
        <RunCommand>dotnet run --configuration release --project "$(SolutionDir)MudBlazor.Docs.Compiler/MudBlazor.Docs.Compiler.csproj"</RunCommand>
      </PropertyGroup>     
    </Otherwise>
  </Choose>

  <!--Execute the code generator-->
  <Target Name="CompileDocs" BeforeTargets="BeforeBuild;BeforeRebuild" DependsOnTargets="IncludeGeneratedFiles">
    <Message Text="Generating Docs and Tests using $(RunCommand)" Importance="high"/>
    <Exec Command='$(RunCommand)' />
  </Target>

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <RazorLangVersion>3.0</RazorLangVersion>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DefineConstants>DEBUG;TRACE</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DefineConstants>TRACE;LIVESHARP_DISABLE</DefineConstants>
  </PropertyGroup>

  <!--Because we have a dynamic content .cs and .razor files comming from the code generator 
  we need to start from no content and recreate MSBuild rules so evreything is included.
  If we dont do this the compiler takes 2 passes to compile all the content-->  

  <Target Name="IncludeGeneratedFiles" BeforeTargets="BeforeBuild;BeforeRebuild" >
    <ItemGroup>
      <Compile Include="Models\Snippets.generated.cs" Condition="!Exists('Models\Snippets.generated.cs')" />
      <Compile Include="Models\DocStrings.generated.cs" Condition="!Exists('Models\DocStrings.generated.cs')" />
      <RazorComponent Include="**/*ExampleCode.razor" />
    </ItemGroup>
  </Target>

  <!--https://docs.microsoft.com/en-gb/aspnet/core/razor-pages/sdk?view=aspnetcore-5.0 -->
  <Target Name="ExampleCode" BeforeTargets="PrepareForRazorComponentGenerate" >
    <ItemGroup>
      <RazorComponent Include="**/*ExampleCode.razor" Condition="!Exists('Models\DocStrings.generated.cs')" />
      <Content Update="$(_RazorComponentInclude)">
        <Generator>MSBuild:RazorGenerateComponentDeclarationDesignTime</Generator>
        <CopyToPublishDirectory>Never</CopyToPublishDirectory>
      </Content>
    </ItemGroup>
  </Target>
 

  <ItemGroup>
    <EmbeddedResource Include="Data\Elements.json" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.10" />
    <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.10" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="Toolbelt.Blazor.HeadElement" Version="5.0.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\MudBlazor\MudBlazor.csproj" />
  </ItemGroup>

</Project>
EN

回答 1

Stack Overflow用户

发布于 2020-12-22 07:07:55

这就是我最终要做的

最重要的一点是

<RazorComponent Include=

代码语言:javascript
复制
<!--https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-reference -->
<!--Use: dotnet msbuild -preprocess:<fileName>.xml to evaluate this project-->
<Project Sdk="Microsoft.NET.Sdk.Razor">

  <!--Make sure this projects build targets run before other referenced projects-->
  <PropertyGroup>
    <BuildDependsOn>
      CompileDocs;
      $(BuildDependsOn)
    </BuildDependsOn>
  </PropertyGroup>

  <!--Outside Visual Studio SolutionDir is not available-->  
  <PropertyGroup>
    <SolutionDir Condition=" '$(SolutionDir)' == '' ">$(MSBuildThisFileDirectory)..\</SolutionDir>
  </PropertyGroup>

  <PropertyGroup>
    <RunCommand>dotnet run --configuration release --project "$(SolutionDir)MudBlazor.Docs.Compiler/MudBlazor.Docs.Compiler.csproj"</RunCommand>
  </PropertyGroup>   

  <!--Execute the code generator-->
  <Target Name="CompileDocs" BeforeTargets="BeforeBuild;BeforeRebuild">
    <Message Text="Generating Docs and Tests using $(RunCommand)" Importance="high" />
    <Exec Command="$(RunCommand)" />
  </Target>

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <RazorLangVersion>3.0</RazorLangVersion>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DefineConstants>DEBUG;TRACE</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DefineConstants>TRACE;LIVESHARP_DISABLE</DefineConstants>
  </PropertyGroup>

  <!--This file contains any ExampleCode that is new and needs including in the build -->
  <Target Name="ReadFromFile" DependsOnTargets="CompileDocs">
    <ItemGroup>
      <NewFiles Include="NewFilesToBuild.txt"/>
    </ItemGroup>
    <ReadLinesFromFile
      File="@(NewFiles)" >
      <Output
        TaskParameter="Lines"
        ItemName="NewExampleCodeToBuild"/>
    </ReadLinesFromFile>
  </Target>

  <!--Because we have a dynamic content .cs and .razor files comming from the code generator we need to add them -->  
  <Target Name="IncludeGeneratedFiles" BeforeTargets="BeforeBuild;BeforeRebuild" DependsOnTargets="CompileDocs;ReadFromFile">
    <Message Text="Found '@(NewExampleCodeToBuild->Count())' new ExampleCode files" Importance="high" />
    <ItemGroup>
      <Compile Include="Models/Snippets.generated.cs" Condition="!Exists('Models/Snippets.generated.cs')" />
      <Compile Include="Models/DocStrings.generated.cs" Condition="!Exists('Models/DocStrings.generated.cs')" />
      <RazorComponent Include="@(NewExampleCodeToBuild)" Condition="@(NewExampleCodeToBuild->Count()) != 0" />
    </ItemGroup>
  </Target>

  <ItemGroup>
    <EmbeddedResource Include="Data/Elements.json" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="BlazorInputFile" Version="0.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.10" />
    <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.10" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="Toolbelt.Blazor.HeadElement" Version="5.0.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="../MudBlazor/MudBlazor.csproj" />
  </ItemGroup>

</Project>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65364615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档