Skip to content

Instantly share code, notes, and snippets.

@Thermionix
Created October 2, 2013 22:59
Show Gist options
  • Save Thermionix/6801791 to your computer and use it in GitHub Desktop.
Save Thermionix/6801791 to your computer and use it in GitHub Desktop.
NAnt functions
<assemblyfileset id="sys.assemblies">
<include name="System.dll" />
<include name="System.Net.dll" />
</assemblyfileset>
<script language="C#">
<references refid="sys.assemblies" />
<imports>
<import namespace="System.Net" />
<import namespace="System.Net.Sockets" />
</imports>
<code><![CDATA[
[Function("LocalIPAddress")]
public string LocalIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}
]]></code>
</script>
<script language="C#">
<code><![CDATA[
[Function("version-greater-or-equal")]
public bool VersionGreaterOrEqual(string version1, string version2)
{
Version v1 = new Version(version1);
Version v2 = new Version(version2);
return v1 >= v2;
}
]]></code>
</script>
<script language="C#">
<references refid="sys.assemblies" />
<imports>
<import namespace="System.Text.RegularExpressions" />
</imports>
<code><![CDATA[
[Function("regex-replace-in-file")]
public static string RegexReplaceInFile(string filename, string pattern, string replacement)
{
if (!File.Exists(filename))
throw new FileNotFoundException("Could not find: " + filename);
var fileContent = File.ReadAllText(filename, Encoding.Default);
if (Regex.Match(fileContent, pattern).Success)
{
File.WriteAllText(filename, Regex.Replace(fileContent, pattern, replacement, RegexOptions.IgnoreCase), Encoding.Default);
return "Replaced pattern in: " + filename;
}
return "Failed to replace in: " + filename;
}
]]></code>
</script>
<script language="C#">
<references refid="sys.assemblies" />
<imports>
<import namespace="System.Text.RegularExpressions" />
<import namespace="System.Diagnostics" />
</imports>
<code><![CDATA[
[Function("current-svn-revision")]
public static string CurrentSvnRevision(string path)
{
if (Directory.Exists(path))
{
using (Process svnProcess = new Process())
{
svnProcess.StartInfo.WorkingDirectory = path;
svnProcess.StartInfo.FileName = "svn.exe";
svnProcess.StartInfo.Arguments = "info -r BASE";
svnProcess.StartInfo.UseShellExecute = false;
svnProcess.StartInfo.RedirectStandardOutput = true;
svnProcess.Start();
using (StreamReader reader = svnProcess.StandardOutput)
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (Regex.Match(line, "Revision:").Success)
{
return Regex.Match(line, @"([\d]+)").Value;
}
}
}
}
}
return "0";
}
]]></code>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment