使用 XSLT 重定向 Visual Studio 解决方案文件

马丁·奥托

我目前正在与 MSBuild 作斗争,它不会在 Github 上找到的库中构建解决方案文件。问题似乎是出于某种莫名其妙的原因,Microsoft 决定需要在 XML 文件中设置目标 SDK,并且指定的版本当然与 VM 中安装的版本不匹配。

所以我想要一个简单的方法来更新它。我决定使用 xsltproc 来做到这一点。我创建了以下 XSLT 文件:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="WindowsTargetPlatformVersion"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="WindowsTargetPlatformVersion/text()[.='X']">
        <xsl:value-of select="$WindowsTargetPlatformVersion"/>
    </xsl:template>
</xsl:stylesheet>

但由于某种原因,这不适用于解决方案文件。我试图修改的解决方案文件的一个例子是:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <PropertyGroup Label="Globals">
    <ProjectGuid>{1A234565-926D-49B2-83E4-D56E0C38C9F2}</ProjectGuid>
    <RootNamespace>libconfig</RootNamespace>
    <WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
    <ConfigurationType>DynamicLibrary</ConfigurationType>
    <PlatformToolset>v141</PlatformToolset>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    <ConfigurationType>DynamicLibrary</ConfigurationType>
    <PlatformToolset>v141</PlatformToolset>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ImportGroup Label="ExtensionSettings">
  </ImportGroup>
  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup>
    <_ProjectFileVersion>15.0.26919.1</_ProjectFileVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <OutDir>$(SolutionDir)$(Configuration)\</OutDir>
    <IntDir>$(ProjectName).$(Configuration)\</IntDir>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <OutDir>$(SolutionDir)$(Configuration)\</OutDir>
    <IntDir>$(ProjectName).$(Configuration)\</IntDir>
  </PropertyGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <ClCompile>
      <PreprocessorDefinitions>LIBCONFIG_EXPORTS;YY_NO_UNISTD_H;YY_USE_CONST;WIN32;_DEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
      <CompileAs>CompileAsC</CompileAs>
    </ClCompile>
    <Link>
      <AdditionalDependencies>Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <OutputFile>$(OutDir)$(ProjectName)_d.dll</OutputFile>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <GenerateMapFile>true</GenerateMapFile>
      <MapExports>true</MapExports>
      <RandomizedBaseAddress>false</RandomizedBaseAddress>
      <DataExecutionPrevention />
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <ClCompile>
      <PreprocessorDefinitions>LIBCONFIG_EXPORTS;YY_NO_UNISTD_H;YY_USE_CONST;_CRT_SECURE_NO_DEPRECATE;_STDLIB_H;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
      <CompileAs>CompileAsC</CompileAs>
    </ClCompile>
    <Link>
      <AdditionalDependencies>Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <RandomizedBaseAddress>false</RandomizedBaseAddress>
      <DataExecutionPrevention />
    </Link>
  </ItemDefinitionGroup>
  <ItemGroup>
    <ClCompile Include="grammar.c" />
    <ClCompile Include="libconfig.c" />
    <ClCompile Include="scanctx.c" />
    <ClCompile Include="scanner.c" />
    <ClCompile Include="strbuf.c" />
    <ClCompile Include="strvec.c" />
    <ClCompile Include="util.c" />
    <ClCompile Include="wincompat.c" />
  </ItemGroup>
  <ItemGroup>
    <ClInclude Include="..\ac_config.h" />
    <ClInclude Include="grammar.h" />
    <ClInclude Include="libconfig.h" />
    <ClInclude Include="parsectx.h" />
    <ClInclude Include="private.h" />
    <ClInclude Include="scanctx.h" />
    <ClInclude Include="scanner.h" />
    <ClInclude Include="strbuf.h" />
    <ClInclude Include="strvec.h" />
    <ClInclude Include="util.h" />
    <ClInclude Include="win32\stdint.h" />
    <ClInclude Include="wincompat.h" />
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
  </ImportGroup>
</Project>

我不明白为什么 xsltproc 拒绝替换文本。我像这样从 bash 运行它:

xsltproc --stringparam WindowsTargetPlatformVersion $SDKVERSION retarget.xslt $SOLUTIONFILE

如果我创建一个带有标记的简单 xml 文件,它会按预期工作。解决方案文件不起作用。

C队

Visual Studio 解决方案文件有一个默认命名空间(这里的 xlmns...)

 <Project DefaultTargets="Build" ToolsVersion="15.0" 
          xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

但是您没有在 XSLT 中考虑到这一点。当您的模板与“WindowsTargetPlatformVersion”匹配时,它试图匹配没有命名空间的元素

您只需要在 XSLT 中声明命名空间,绑定到前缀,并在模板匹配中使用它。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ms="http://schemas.microsoft.com/developer/msbuild/2003">
    <xsl:param name="WindowsTargetPlatformVersion"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ms:WindowsTargetPlatformVersion/text()[.='X']">
        <xsl:value-of select="$WindowsTargetPlatformVersion"/>
    </xsl:template>
</xsl:stylesheet>

如果你可以使用 XSLT 2.0(我猜不是,如果你在微软的世界里)你可以使用xpath-default-namespace,所以你不需要绑定到前缀

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xpath-default-namespace="http://schemas.microsoft.com/developer/msbuild/2003">
    <xsl:param name="WindowsTargetPlatformVersion"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="WindowsTargetPlatformVersion/text()[.='X']">
        <xsl:value-of select="$WindowsTargetPlatformVersion"/>
    </xsl:template>
</xsl:stylesheet>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我可以同时使用Visual Studio for Mac打开两个解决方案吗?

如何在Visual Studio解决方案中使用class属性来检查C#?

如何使Visual Studio使用FAKE构建我的解决方案?

Visual Studio 2015-命令行重定向解决方案

Visual Studio Code是否可以与混合语言.NET Core解决方案一起使用?

使用cmake在Mac上生成Visual Studio解决方案

Visual Studio解决方案中使用的.vs文件夹是什么?

使用Mac的Visual Studio在解决方案资源管理器中复制客户端文件

如何为Visual Studio中的解决方案动态创建解决方案文件夹?

是否可以使用Visual Studio for Mac清除解决方案中的bin和obj目录?

如何在Visual Studio解决方案的部署中使用nuget包

Visual Studio构建解决方案

Visual Studio解决方案参考

Visual Studio 2019-使用PowerShell打开文件而不是解决方案资源管理器

使用解决方案以及特定的文件和行打开Visual Studio

使用解决方案以及特定的文件和行打开Visual Studio

Visual Studio 2019开放解决方案文件不兼容

Visual Studio不能创建解决方案文件吗?

使用CMake在同一文件夹中生成两个Visual Studio解决方案

如何取消使用AnkhSVN提交的Visual Studio解决方案版本

无法打开Visual Studio解决方案

我应该如何使用新的类库nuget包设计我的Visual Studio解决方案

在Visual Studio中构建解决方案时使用哪个编译器?

使用不属于解决方案的源文件的Visual Studio自动完成失败

CMake Visual Studio 解决方案设置

使用 Visual Studio 2017 打开解决方案

Visual Studio 解决方案文件中的环境变量

Visual Studio 总是使用 Paket 重建解决方案

有没有办法在 Visual Studio 2017 中查看解决方案/项目构建使用的每个文件?