[tasque/xbuild] [xbuild] Add NormalizePath task and adjust other tasks accordingly



commit d13e3192f30f8ef74b8d9dd0bf7abad284d20f66
Author: Antonius Riha <antoniusriha gmail com>
Date:   Sun Sep 30 23:58:18 2012 +0200

    [xbuild] Add NormalizePath task and adjust other tasks accordingly

 build/GetAbsSrcDir.cs   |    2 +-
 build/GetRelPath.cs     |   55 +++++++++++++++++-----------------------
 build/GetSrcDirStrip.cs |    4 +-
 build/NormalizePath.cs  |   64 +++++++++++++++++++++++++++++++++++++++++++++++
 build/build.csproj      |    1 +
 5 files changed, 91 insertions(+), 35 deletions(-)
---
diff --git a/build/GetAbsSrcDir.cs b/build/GetAbsSrcDir.cs
index 7a2b9a7..2b77a01 100644
--- a/build/GetAbsSrcDir.cs
+++ b/build/GetAbsSrcDir.cs
@@ -48,7 +48,7 @@ namespace Tasque.Build
 					throw new Exception ("AbsTopSrcDir must be an absolute path.");
 				
 				// normalize paths
-				var topSrcPath = Path.GetFullPath (AbsTopSrcDir).TrimEnd (Path.DirectorySeparatorChar);
+				var topSrcPath = NormalizePath.InternalNormalizePath (AbsTopSrcDir);
 
 				AbsSrcDir = Path.Combine (topSrcPath, SrcDirStrip);
 			} catch (Exception ex) {
diff --git a/build/GetRelPath.cs b/build/GetRelPath.cs
index 23ab9d5..473b638 100644
--- a/build/GetRelPath.cs
+++ b/build/GetRelPath.cs
@@ -45,9 +45,7 @@ namespace Tasque.Build
 		public override bool Execute ()
 		{
 			try {
-				var fromPath = Path.GetFullPath (FromPath);
-				var toPath = Path.GetFullPath (ToPath);
-				RelativePath = MakeRelPath (fromPath, toPath);
+				RelativePath = InternalGetRelPath (FromPath, ToPath);
 			} catch (Exception ex) {
 				Log.LogErrorFromException (ex, true);
 				return false;
@@ -55,42 +53,35 @@ namespace Tasque.Build
 			return true;
 		}
 
-        string MakeRelPath (string fromPath, string toPath)
+        internal static string InternalGetRelPath (string fromPath, string toPath)
 		{
-			if (!Path.IsPathRooted (fromPath) || !Path.IsPathRooted (toPath))
-				throw new Exception ("All paths must be absolute.");
+			if (fromPath == null)
+				throw new ArgumentNullException ("fromPath");
+			if (toPath == null)
+				throw new ArgumentNullException ("toPath");
 
 			// normalize
-			fromPath = Path.GetFullPath (fromPath).TrimEnd (Path.DirectorySeparatorChar);
-			toPath = Path.GetFullPath (toPath).TrimEnd (Path.DirectorySeparatorChar);
-			
-			var fromPieces = fromPath.Split (Path.DirectorySeparatorChar);
-			var toPieces = toPath.Split (Path.DirectorySeparatorChar);
+			fromPath = NormalizePath.InternalNormalizePath (Path.GetFullPath (fromPath));
+			toPath = NormalizePath.InternalNormalizePath (Path.GetFullPath (toPath));
 
-			var fromBranch = new StringBuilder ();
-			var toBranch = new StringBuilder ();
-			var root = new StringBuilder ();
+			var basePathPieces = fromPath.Split (Path.DirectorySeparatorChar);
+			var targetPathPieces = toPath.Split (Path.DirectorySeparatorChar);
 
-			var longerPieces = fromPieces.Length > toPieces.Length ? fromPieces : toPieces;
+			var max = Math.Min (basePathPieces.Length, targetPathPieces.Length);
+			int i = 0;
+			for (; i < max; i++)
+				if (basePathPieces [i] != targetPathPieces [i])
+					break;
 
-			var isRootFinished = false;
-			for (int i = 0; i < longerPieces.Length; i++) {
-				if ((i < fromPieces.Length && i < toPieces.Length) && !isRootFinished
-				    && fromPieces [i] == toPieces [i])
-					root.Append (longerPieces [i] + Path.DirectorySeparatorChar);
-				else {
-					isRootFinished = true;
+			StringBuilder fromBranch = new StringBuilder (), toBranch = new StringBuilder ();
+			for (int j = i; j < basePathPieces.Length; j++)
+				fromBranch.Append (".." + Path.DirectorySeparatorChar.ToString ());
+			for (int j = i; j < targetPathPieces.Length; j++)
+				toBranch.Append (targetPathPieces [j] + Path.DirectorySeparatorChar.ToString ());
 
-					if (i < fromPieces.Length)
-						fromBranch.Append (".." + Path.DirectorySeparatorChar.ToString ());
-
-					if (i < toPieces.Length)
-						toBranch.Append (toPieces [i] + (i == toPieces.Length - 1 ? ""
-							: Path.DirectorySeparatorChar.ToString ()));
-				}
-			}
-
-			return fromBranch.ToString () + toBranch.ToString ();
+			var toBranchString = toBranch.ToString ();
+			var result = fromBranch.Append (toBranchString).ToString ();
+			return result.TrimEnd (Path.DirectorySeparatorChar);
         }
 	}
 }
diff --git a/build/GetSrcDirStrip.cs b/build/GetSrcDirStrip.cs
index 792057d..5c48143 100644
--- a/build/GetSrcDirStrip.cs
+++ b/build/GetSrcDirStrip.cs
@@ -48,8 +48,8 @@ namespace Tasque.Build
 					throw new Exception ("MSBuildProjectDir and AbsTopBuildDir paths must be absolute.");
 				
 				// normalize paths
-				var projPath = Path.GetFullPath (MSBuildProjectDir).TrimEnd (Path.DirectorySeparatorChar);
-				var topBuildPath = Path.GetFullPath (AbsTopBuildDir).TrimEnd (Path.DirectorySeparatorChar);
+				var projPath = NormalizePath.InternalNormalizePath (MSBuildProjectDir);
+				var topBuildPath = NormalizePath.InternalNormalizePath (AbsTopBuildDir);
 				
 				if (!projPath.Contains (topBuildPath))
 					throw new Exception ("AbsTopBuildDir must be a subpath of MSBuildProjectDir.");
diff --git a/build/NormalizePath.cs b/build/NormalizePath.cs
new file mode 100644
index 0000000..e3480e8
--- /dev/null
+++ b/build/NormalizePath.cs
@@ -0,0 +1,64 @@
+// 
+// NormalizePath.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System;
+using IO = System.IO;
+using System.Text;
+using Microsoft.Build.Framework;
+using Microsoft.Build.Utilities;
+
+namespace Tasque.Build
+{
+	public class NormalizePath : Task
+	{
+		[Required]
+		[Output]
+		public string Path { get; set; }
+
+		public override bool Execute ()
+		{
+			try {
+				Path = InternalNormalizePath (Path);
+			} catch (Exception ex) {
+				Log.LogErrorFromException (ex, true);
+				return false;
+			}
+			return true;
+		}
+
+		internal static string InternalNormalizePath (string path)
+		{
+			if (path == null)
+				throw new ArgumentNullException ("path");
+
+			if (IO.Path.IsPathRooted (path))
+				return IO.Path.GetFullPath (path).TrimEnd (IO.Path.DirectorySeparatorChar);
+
+			var basePath = Environment.CurrentDirectory;
+			var targetPath = IO.Path.GetFullPath (path);
+			return GetRelPath.InternalGetRelPath (basePath, targetPath);
+		}
+	}
+}
diff --git a/build/build.csproj b/build/build.csproj
index 4120e0b..fc141ca 100644
--- a/build/build.csproj
+++ b/build/build.csproj
@@ -36,6 +36,7 @@
     <Compile Include="GetRelPath.cs" />
     <Compile Include="GetAbsSrcDir.cs" />
     <Compile Include="GetSrcDirStrip.cs" />
+    <Compile Include="NormalizePath.cs" />
   </ItemGroup>
   <ItemGroup>
     <Proj Include="..\src\Gtk.Tasque\Gtk.Tasque.csproj">



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]