我想在我的ASP.NET核心2.2项目中这样做:
git log -1 --format="Git commit %h committed on %cd by %cn" --date=iso但是,作为预构建步骤,我将它包含在csproj中,如下所示:
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="git log -1 --format="Git commit %25%25h committed on %25%25cd by %25%25cn" --date=iso > "$(ProjectDir)/version.txt"" />
</Target>这在Windows上是有效的(如果我正确理解%25是MSBuild术语中的百分比,而double %是命令行转义,那么我们就有了%25%25)。它给了我这种version.txt
Git commit abcdef12345 committed on 2019-01-25 14:48:20 +0100 by Jeroen Heijmans但是如果我在Ubuntu18.04上使用dotnet build执行上面的操作,那么我就可以在我的version.txt中得到这个
Git commit %h committed on %cd by %cn如何重新构造Exec元素,使其同时运行在Windows (Visual、Rider或dotnet )和Linux (Rider或dotnet )上?
发布于 2021-10-13 19:06:14
为了避免成为"DenverCoder9“,这里是最后的工作解决方案:
有两个选项,它们都使用Condition属性的功能。
选项1:
复制PreBuild Exec元素,每个元素具有Unix类OS的条件和非Unix类OS的条件。
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Condition="!$([MSBuild]::IsOSUnixLike())" Command="git log -1 --format="Git commit %25%25h committed on %25%25cd by %25%25cn" --date=iso > "$(ProjectDir)/version.txt"" />
<Exec Condition="$([MSBuild]::IsOSUnixLike())" Command="git log -1 --format="Git commit %25h committed on %25cd by %25cn" --date=iso > "$(ProjectDir)/version.txt"" />
</Target>备选方案2:
在应用程序的根级向Directory.Build.props文件添加一个属性组,并在PreBuild Exec命令中使用它。
<!-- file: Directory.Build.props -->
<Project>
<!-- Adds batch file escape character for targets using Exec command when run on Windows -->
<PropertyGroup Condition="!$([MSBuild]::IsOSUnixLike())">
<AddEscapeIfWin>%25</AddEscapeIfWin>
</PropertyGroup>
</Project> <!-- Use in *.csproj -->
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="git log -1 --format="Git commit $(AddEscapeIfWin)%25h committed on $(AddEscapeIfWin)%25cd by $(AddEscapeIfWin)%25cn" --date=iso > "$(ProjectDir)/Resources/version.txt"" />
</Target>https://stackoverflow.com/questions/54368280
复制相似问题