在 C# 中工作的正则表达式在 MSBuild 中不工作
Regex that is working in C# is not working in MSBuild
我想验证使用 dotnet build /p:VERSION=1.2.3
和 GeneratePackageOnBuild
打包一些 nuget 包时使用的版本。我的正则表达式正在使用 C# 在 LINQPad 6 中工作:
Regex.Match("1.2.3", @"^\d{1,3}\.\d{1,3}\.\d{1,6}(-(beta|rc)(\d{1,1})?)?$")
但是在我的 default.props
中,所有 csproj 文件(使用新的 sdk 样式,如果相关的话)正在导入我的 default.props
我有这个,但它根本不起作用:
<Target Name="ValidateVersion" BeforeTargets="BeforeBuild">
<PropertyGroup>
<VersionRegex>^\d{1,3}\.\d{1,3}\.\d{1,6}(-(beta|rc)(\d{1,1})?)?$</VersionRegex>
<VersionTest>1.2.3</VersionTest> <!-- Just to make it easier during testing -->
</PropertyGroup>
<Error
Text="Version is not following the correct format: $(VersionRegex)"
Condition=" $([System.Text.RegularExpressions.Regex]::IsMatch('$(VersionTest)', `$(VersionRegex)`)) " />
</Target>
内联VersionRegex
和VersionTest
也没关系,还是不行。知道为什么它在 C# 中工作但在 MSBuild 中不工作吗?
解决方法
我还没有找到真正的问题,但解决方法是,如 ,使用 CDATA:
Targets/ValidateVersioning.targets
:
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask
TaskName="ValidateVersion"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<Version ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Text.RegularExpressions"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
if (!Regex.Match(Version, @"^\d{1,3}\.\d{1,3}\.\d{1,6}(-(beta|rc)(\d{1,1})?)?$").Success)
{
Log.LogError("Version has wrong format: {0}", Version);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
</Project>
default.props
:
<Project>
...
<Import Project="Targets\ValidateVersioning.targets" />
<Target Condition="$(VERSION) != ''" Name="ValidateVersion" BeforeTargets="BeforeBuild">
<ValidateVersion Version="$(VERSION)" />
</Target>
...
</Project>
> dotnet build --no-restore -p:Version=1.3.4
可以,但是
> dotnet build --no-restore -p:Version=1.3
不会构建
根据你原来的例子,你应该能够把 VersionRegex
属性 的内容放在 CDATA
:
中
<Target Name="ValidateVersion" BeforeTargets="BeforeBuild">
<PropertyGroup>
<VersionRegex><![CDATA[^\d{1,3}\.\d{1,3}\.\d{1,6}(-(beta|rc)(\d{1,1})?)?$]]></VersionRegex>
<VersionTest>1.2.3</VersionTest>
</PropertyGroup>
<Error
Text="Version is not following the correct format: $(VersionRegex)"
Condition="!$([System.Text.RegularExpressions.Regex]::IsMatch('$(VersionTest)', '$(VersionRegex)'))" />
</Target>
我想验证使用 dotnet build /p:VERSION=1.2.3
和 GeneratePackageOnBuild
打包一些 nuget 包时使用的版本。我的正则表达式正在使用 C# 在 LINQPad 6 中工作:
Regex.Match("1.2.3", @"^\d{1,3}\.\d{1,3}\.\d{1,6}(-(beta|rc)(\d{1,1})?)?$")
但是在我的 default.props
中,所有 csproj 文件(使用新的 sdk 样式,如果相关的话)正在导入我的 default.props
我有这个,但它根本不起作用:
<Target Name="ValidateVersion" BeforeTargets="BeforeBuild">
<PropertyGroup>
<VersionRegex>^\d{1,3}\.\d{1,3}\.\d{1,6}(-(beta|rc)(\d{1,1})?)?$</VersionRegex>
<VersionTest>1.2.3</VersionTest> <!-- Just to make it easier during testing -->
</PropertyGroup>
<Error
Text="Version is not following the correct format: $(VersionRegex)"
Condition=" $([System.Text.RegularExpressions.Regex]::IsMatch('$(VersionTest)', `$(VersionRegex)`)) " />
</Target>
内联VersionRegex
和VersionTest
也没关系,还是不行。知道为什么它在 C# 中工作但在 MSBuild 中不工作吗?
解决方法
我还没有找到真正的问题,但解决方法是,如
Targets/ValidateVersioning.targets
:
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask
TaskName="ValidateVersion"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<Version ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Text.RegularExpressions"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
if (!Regex.Match(Version, @"^\d{1,3}\.\d{1,3}\.\d{1,6}(-(beta|rc)(\d{1,1})?)?$").Success)
{
Log.LogError("Version has wrong format: {0}", Version);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
</Project>
default.props
:
<Project>
...
<Import Project="Targets\ValidateVersioning.targets" />
<Target Condition="$(VERSION) != ''" Name="ValidateVersion" BeforeTargets="BeforeBuild">
<ValidateVersion Version="$(VERSION)" />
</Target>
...
</Project>
> dotnet build --no-restore -p:Version=1.3.4
可以,但是
> dotnet build --no-restore -p:Version=1.3
不会构建
根据你原来的例子,你应该能够把 VersionRegex
属性 的内容放在 CDATA
:
<Target Name="ValidateVersion" BeforeTargets="BeforeBuild">
<PropertyGroup>
<VersionRegex><![CDATA[^\d{1,3}\.\d{1,3}\.\d{1,6}(-(beta|rc)(\d{1,1})?)?$]]></VersionRegex>
<VersionTest>1.2.3</VersionTest>
</PropertyGroup>
<Error
Text="Version is not following the correct format: $(VersionRegex)"
Condition="!$([System.Text.RegularExpressions.Regex]::IsMatch('$(VersionTest)', '$(VersionRegex)'))" />
</Target>