0

I'm using a Nant build to update the dates in C# AssemblyInfo.cs files (lots of them). Each file contains a line like...

[assembly: AssemblyCopyright("Copyright Whoever 2020-2021")]

or

[assembly: AssemblyCopyright("Copyright Whoever 2021")]

and I'm updating that to say

[assembly: AssemblyCopyright("Copyright Whoever 2020-2022")]

or

[assembly: AssemblyCopyright("Copyright Whoever 2021-2022")]

I have a property versionFilePath which contains the name of the file, and I'm doing...

  <loadfile file="${versionFilePath}" property="versionFileContent"/>
  <regex pattern="^(?'prefix'\[assembly:\s+AssemblyCopyright\(&quot;.+?)(?'fromdate'\d\d\d\d)(?'todate'\-\d\d\d\d)?(?'suffix'.*&quot;\)\])" 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>

And this is basically working, however the file that it is writing is a different encoding than the one which it loaded; and the version control system we're using doesn't like that very much.

How can I make it do the replacement without altering the encoding? Can I capture the encoding when the file is being loaded so that I can use the same value when the file is being written? Or is there a better way of doing this, where I can just do the Regex replace directly on the file?

1 Answer 1

1

I'm not aware of a solution which keeps the original encoding, but you might force the echo task to a defined encoding (one that is accepted by your VCS) since echo has an encoding attribute (as least as of version 0.92).

  <echo file="${versionFilePath}" encoding="iso-8859-1">${versionFileContent}</echo>

Update: To make it a bit clearer: There's no good way of telling from a source text file what its encoding is. You can make a good guess (take a look at Python module chardet), but most of the time everything depends on meta-information. My advice is:

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. The only piece of the puzzle left is how to get the encoding from loadfile (or some other way), to pass into that encoding attribute of echo.
OK. I refined my answer a bit
Upvote for being helpful; but this answer basically proposes to set all of the files to a particular encoding, and as explained in the question, if I do that, it causes the issue to the VCS for the ones which were different. Given that "tweaking files" is a common thing that builds need to do, it seems an odd oversight that there isn't a way of doing this. In the absence of other answers, I guess I'll just write my own task to do a regex file replace, while retaining the encoding, since its not that hard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.