From dd257ee29db77edbd1f9765968e83586c4691040 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sat, 2 Oct 2010 22:15:19 +0000 Subject: [PATCH] Added Build.Tasks project that contains custom MSBuild tasks for the compilation process: - DateStamp, which generates a version number based on the current date. - DelTree which mimics RemoveDir but can delete non-empty directories on xbuild 2.6.x (which only supports empty directories). - Run which mimics Exec but also captures stderr/stdout. --- Source/Build.Tasks/Build.Tasks.csproj | 78 +++++++++++++++ Source/Build.Tasks/Common.xml | 14 +++ Source/Build.Tasks/DateStamp.cs | 39 ++++++++ Source/Build.Tasks/DelTree.cs | 50 ++++++++++ Source/Build.Tasks/Properties/AssemblyInfo.cs | 36 +++++++ Source/Build.Tasks/Run.cs | 94 +++++++++++++++++++ 6 files changed, 311 insertions(+) create mode 100644 Source/Build.Tasks/Build.Tasks.csproj create mode 100644 Source/Build.Tasks/Common.xml create mode 100644 Source/Build.Tasks/DateStamp.cs create mode 100644 Source/Build.Tasks/DelTree.cs create mode 100644 Source/Build.Tasks/Properties/AssemblyInfo.cs create mode 100644 Source/Build.Tasks/Run.cs diff --git a/Source/Build.Tasks/Build.Tasks.csproj b/Source/Build.Tasks/Build.Tasks.csproj new file mode 100644 index 00000000..6465eb39 --- /dev/null +++ b/Source/Build.Tasks/Build.Tasks.csproj @@ -0,0 +1,78 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {CCE26215-7591-4CC3-8E39-9A08F8BF35E2} + Library + Properties + Build.Tasks + Build.Tasks + v2.0 + 512 + + + + true + full + false + ..\..\Binaries\OpenTK\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + ..\..\Binaries\OpenTK\Release\ + TRACE + prompt + 4 + + + ..\..\Binaries\OpenTK\Release\ + TRACE + true + + + ..\..\Binaries\OpenTK\Release\ + TRACE + true + pdbonly + AnyCPU + ..\..\Binaries\OpenTK\Release\Build.Tasks.dll.CodeAnalysisLog.xml + true + GlobalSuppressions.cs + prompt + MinimumRecommendedRules.ruleset + ;C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets + ;C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules + false + + + + + + + + + + + + + + + Designer + + + + + \ No newline at end of file diff --git a/Source/Build.Tasks/Common.xml b/Source/Build.Tasks/Common.xml new file mode 100644 index 00000000..59b62f56 --- /dev/null +++ b/Source/Build.Tasks/Common.xml @@ -0,0 +1,14 @@ + + + + + Release + ..\..\Binaries\OpenTK\Release + $(BuildTasksPath)\Build.Tasks.dll + + + + + + + diff --git a/Source/Build.Tasks/DateStamp.cs b/Source/Build.Tasks/DateStamp.cs new file mode 100644 index 00000000..9b46e1f9 --- /dev/null +++ b/Source/Build.Tasks/DateStamp.cs @@ -0,0 +1,39 @@ +using System; +using System.Globalization; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Build.Tasks +{ + /// + /// Returns a date stamp in the form yyMMdd. + /// + public class DateStamp : Task + { + string date; + + /// + /// Gets a represting the date stamp. + /// + [Output] + public string Date + { + get { return date; } + private set { date = value; } + } + + public override bool Execute() + { + try + { + Date = DateTime.Now.ToString("yyMMdd", CultureInfo.InvariantCulture); + } + catch (Exception e) + { + Log.LogErrorFromException(e); + return false; + } + return true; + } + } +} diff --git a/Source/Build.Tasks/DelTree.cs b/Source/Build.Tasks/DelTree.cs new file mode 100644 index 00000000..37c4570b --- /dev/null +++ b/Source/Build.Tasks/DelTree.cs @@ -0,0 +1,50 @@ +using System; +using System.Globalization; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Build.Tasks +{ + /// + /// Deletes directory and all of its contents. + /// Replaces RemoveDir task which exhibits different behavior + /// on xbuild compared to msbuild: the first requires an empty + /// directory, while the latter does not. + /// + public class DelTree : Task + { + string path; + + /// + /// The filesystem path to delete. + /// + [Required] + public string Path + { + get { return path; } + set { path = value; } + } + + public override bool Execute() + { + try + { + if (String.IsNullOrEmpty(Path) || + System.IO.Directory.Exists(Path)) + { + return false; + } + else + { + System.IO.Directory.Delete(Path, true); + } + } + catch (Exception e) + { + Log.LogErrorFromException(e); + return false; + } + return true; + } + } +} diff --git a/Source/Build.Tasks/Properties/AssemblyInfo.cs b/Source/Build.Tasks/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..a9f4d287 --- /dev/null +++ b/Source/Build.Tasks/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Build.Time")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Build.Time")] +[assembly: AssemblyCopyright("Copyright © 2010")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("f1d4ac4c-e931-44f4-ac34-966f3ec505e3")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Source/Build.Tasks/Run.cs b/Source/Build.Tasks/Run.cs new file mode 100644 index 00000000..2dfee512 --- /dev/null +++ b/Source/Build.Tasks/Run.cs @@ -0,0 +1,94 @@ +using System; +using System.Diagnostics; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Build.Tasks +{ + /// + /// Executes specified process, capturing its stdout/stderr output. + /// Replaces Exec task which does not capture output. + /// + public class Run : Task + { + string command, args, wdir; + + /// + /// The command to execute. + /// + [Required] + public string Command + { + get { return command; } + set { command = value; } + } + + /// + /// The working directory for the command. + /// + public string WorkingDirectory + { + get { return wdir; } + set { wdir = value; } + } + + public override bool Execute() + { + try + { + if (String.IsNullOrEmpty(command)) + { + return false; + } + + // Split arguments from command: + int arg_end = command.IndexOf(' '); + ProcessStartInfo psi = null; + if (arg_end > 0) + { + psi = new ProcessStartInfo(command.Substring(0, arg_end), command.Substring(arg_end + 1)); + } + else + { + psi = new ProcessStartInfo(command); + } + + psi.UseShellExecute = false; + if (!String.IsNullOrEmpty(wdir)) + { + psi.WorkingDirectory = wdir; + } + + Process p = new Process(); + p.ErrorDataReceived += LogErrors; + p.OutputDataReceived += LogOutput; + p.StartInfo = psi; + + Log.LogMessage("Running {0} {1} on directory {2}", + psi.FileName, + psi.Arguments, + String.IsNullOrEmpty(psi.WorkingDirectory) ? + Environment.CurrentDirectory : psi.WorkingDirectory); + + if (p.Start()) + p.WaitForExit(); + return p.ExitCode == 0; + } + catch (Exception e) + { + Log.LogErrorFromException(e); + return false; + } + } + + void LogErrors(object sender, DataReceivedEventArgs e) + { + Log.LogError(e.Data); + } + + void LogOutput(object sender, DataReceivedEventArgs e) + { + Log.LogMessage(e.Data); + } + } +}