我使用Nant构建来更新C#AssemblyInfo.cs文件中的日期(很多(。每个文件都包含一行,如。。。
[assembly: AssemblyCopyright("Copyright Whoever 2020-2021")]
或
[assembly: AssemblyCopyright("Copyright Whoever 2021")]
我更新为
[assembly: AssemblyCopyright("Copyright Whoever 2020-2022")]
或
[assembly: AssemblyCopyright("Copyright Whoever 2021-2022")]
我有一个属性versionFilePath
,其中包含文件名,我正在执行。。。
<loadfile file="${versionFilePath}" property="versionFileContent"/>
<regex pattern="^(?'prefix'[assembly:s+AssemblyCopyright(".+?)(?'fromdate'dddd)(?'todate'-dddd)?(?'suffix'.*")])" input="${versionFileContent}" options="Multiline" />
<loadfile file="${versionFilePath}" property="versionFileContent">
<filterchain>
<replacestring from="${prefix}${fromdate}${todate}${suffix}" to="${prefix}${fromdate}-${datetime::get-year(datetime::now())}${suffix}" />
</filterchain>
</loadfile>
<echo file="${versionFilePath}">${versionFileContent}</echo>
这是基本上工作,但是它正在写入的文件与它加载的文件是不同的编码;我们使用的版本控制系统不太喜欢这样。
如何在不更改编码的情况下进行替换?我可以在加载文件时捕获编码,以便在写入文件时使用相同的值吗?或者有更好的方法可以做到这一点,我可以直接在文件上进行Regex替换?
我不知道有什么解决方案可以保留原始编码,但您可能会将echo任务强制为定义的编码(VCS接受的编码(,因为echo
具有encoding
属性(至少从0.92版本开始(
<echo file="${versionFilePath}" encoding="iso-8859-1">${versionFileContent}</echo>
更新:让它更清楚一点:从源文本文件中没有很好的方法来判断它的编码是什么。你可以做出很好的猜测(看看Python模块chardet(,但大多数时候一切都取决于元信息。我的建议是:
- 将源
CommonAssemblyInfo.cs
转换为共享编码,例如带有BOM的UTF-8 - 这是非常实验性的:您可以对
loadfile
和echo
任务使用相同的encoding
属性。如果你看一下NAnt源代码loadfile
编码属性默认为encoding.Default(这很简单,根据文档的意思是Unless an encoding is specified, the encoding associated with the system's current ANSI code page is used
(。根据NAnt来源,echo
任务默认为UTF-8。如果两者都选择ASCII编码,则大多数8位编码可能是安全的,因为您将用ANSI代码<仅128