I was looking through the documentation and couldn't find a feature that we use at our company. We've got a Java/.NET environment and the corporate gods are trying to homogenize the two.
The Java programmers build up a config file using keywords in curly braces as placeholders for values. The values are then filled in during build with values from properties files. I extruded a couple things into the .NET world and ended up with this:
conf\app.config:
<configuration>
<add key="ServerURL" value="{SERVER_URL}"/>
<add key="Timeout" value="1000"/>
</configuration>
conf\qa.properties
SERVER_URL=http://qa.server.com/
conf\prod.properties
SERVER_URL=http://www.server.com
nant.build
<property name="env" value="qa"/>
...
<copy file="conf\app.config" tofile="bin\app.config" filter="conf\filters\${env}.properties"/>
The resulting filtered file would be filled in with the appropriate URL during the build.
I actually went through and coded this in NANT.Core\Tasks\CopyTask.cs in a nightly build of NAnt. Unfortunately I was working with a slightly old version (5/25/2004) and don't know if the file has changed since. The resulting file is attached, and additions are highlighted below. I think I followed coding conventions correctly.
I just want to know what to do to get this included so that I can keep getting new builds and ultimately the next release.
Thanks,
[CopyTask.cs Modifications]
Line 80 - Added class members:
private string _filterFile;
private Hashtable _filterMap = new Hashtable();
Line 167 - Added class property:
/// <summary>
/// The file containing the filters used when copying files.
/// </summary>
[TaskAttribute("filter")]
public string FilterFile {
get { return _filterFile; }
set { _filterFile = StringUtils.ConvertEmptyToNull(value); }
}
Line 252 - Added to void InitializeTask(XmlNode):
if (FilterFile != null) {
try {
FileStream Filters = new FileStream(FilterFile, FileMode.Open, FileAccess.Read);
StreamReader FilterReader = new StreamReader(Filters);
string FilterData = FilterReader.ReadToEnd();
Filters.Close();
System.Text.RegularExpressions.MatchCollection AllFilterPairs = System.Text.RegularExpressions.Regex.Matches(FilterData, @"(?<filter>.+)=(?<value>.+)");
foreach (System.Text.RegularExpressions.Match FilterPair in AllFilterPairs)
_filterMap.Add(FilterPair.Groups["filter"].Value.Trim(), FilterPair.Groups["value"].Value.Trim());
}
catch (Exception exx) {
throw new BuildException(exx.Message, Location);
}
}
Line 417 - Modified void DoFileOperations():
Was:
File.Copy(sourceFile, destinationFile, true);
Changed To:
if (_filterMap.Count == 0)
File.Copy(sourceFile, destinationFile, true);
else {
FileStream SourceFileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
StreamReader SourceReader = new StreamReader(SourceFileStream);
string FileData = SourceReader.ReadToEnd();
SourceFileStream.Close();
foreach (System.Collections.DictionaryEntry Entry in _filterMap)
FileData = FileData.Replace("{" + (string)Entry.Key + "}", (string)Entry.Value);
FileStream DestinationFileStream = new FileStream(destinationFile, FileMode.Create, FileAccess.Write);
StreamWriter DestinationWriter = new StreamWriter(DestinationFileStream);
DestinationWriter.WriteLine(FileData);
DestinationWriter.Flush();
DestinationWriter.Close();
}
Paul Hounshell
Programmer
Analyst
New York Mercantile Exchange
One North End Avenue
New York, NY 10282
(212) 299-2650
CONFIDENTIALITY NOTICE: This message and any attachments relate to the official business of the New York Mercantile Exchange, Inc. ("NYMEX") and are proprietary to NYMEX. This e-mail is intended for the above-named person(s) only and is confidential, proprietary and/or legally privileged. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on this information is strictly prohibited. If this message has come to you in error, please immediately notify the sender by telephone or return e-mail and delete the original transmission and its attachments without reading or saving in any manner. Thank you.
CopyTask.cs
Description: Binary data
