[tasque/xbuild] [xbuild] Added tasks for paths handling



commit d4429018f229bef5f33b8cc62915a799d213d567
Author: Antonius Riha <antoniusriha gmail com>
Date:   Tue Sep 18 13:09:02 2012 +0200

    [xbuild] Added tasks for paths handling
    
    * Added new initial target to project: _GetSrcDir. This target
    calculates the srcdir of the current project via the tasks
    	* GetAbsSrcDir
    	* GetRelPath and
    	* GetSrcDirStrip
    
    * Setup a prefix property, which is derived from the current (absolute)
    project directory + a RelPrefix property, which each child project has
    to provide.
    
    * also: removed obsolete _tmpInstall... property
    * also: added previously removed BuildingSolutionFile property

 build/GetAbsSrcDir.cs   |   61 ++++++++++++++++++++++++++++++
 build/GetRelPath.cs     |   96 +++++++++++++++++++++++++++++++++++++++++++++++
 build/GetSrcDirStrip.cs |   69 +++++++++++++++++++++++++++++++++
 build/Tasque.targets    |   31 ++++++++++++++-
 build/build.csproj      |    3 +
 5 files changed, 258 insertions(+), 2 deletions(-)
---
diff --git a/build/GetAbsSrcDir.cs b/build/GetAbsSrcDir.cs
new file mode 100644
index 0000000..7a2b9a7
--- /dev/null
+++ b/build/GetAbsSrcDir.cs
@@ -0,0 +1,61 @@
+// 
+// GetAbsSrcDir.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 System.IO;
+using Microsoft.Build.Framework;
+using Microsoft.Build.Utilities;
+
+namespace Tasque.Build
+{
+	public class GetAbsSrcDir : Task
+	{
+		[Required]
+		public string AbsTopSrcDir { get; set; }
+		
+		[Required]
+		public string SrcDirStrip { get; set; }
+		
+		[Output]
+		public string AbsSrcDir { get; set; }
+		
+		public override bool Execute ()
+		{
+			try {
+				if (!Path.IsPathRooted (AbsTopSrcDir))
+					throw new Exception ("AbsTopSrcDir must be an absolute path.");
+				
+				// normalize paths
+				var topSrcPath = Path.GetFullPath (AbsTopSrcDir).TrimEnd (Path.DirectorySeparatorChar);
+
+				AbsSrcDir = Path.Combine (topSrcPath, SrcDirStrip);
+			} catch (Exception ex) {
+				Log.LogErrorFromException (ex, true);
+				return false;
+			}
+			return true;
+		}
+	}
+}
diff --git a/build/GetRelPath.cs b/build/GetRelPath.cs
new file mode 100644
index 0000000..23ab9d5
--- /dev/null
+++ b/build/GetRelPath.cs
@@ -0,0 +1,96 @@
+// 
+// GetRelPath.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 System.IO;
+using System.Text;
+using Microsoft.Build.Framework;
+using Microsoft.Build.Utilities;
+
+namespace Tasque.Build
+{
+	public class GetRelPath : Task
+	{
+		[Required]
+		public string FromPath { get; set; }
+
+		[Required]
+		public string ToPath { get; set; }
+
+		[Output]
+		public string RelativePath { get; set; }
+
+		public override bool Execute ()
+		{
+			try {
+				var fromPath = Path.GetFullPath (FromPath);
+				var toPath = Path.GetFullPath (ToPath);
+				RelativePath = MakeRelPath (fromPath, toPath);
+			} catch (Exception ex) {
+				Log.LogErrorFromException (ex, true);
+				return false;
+			}
+			return true;
+		}
+
+        string MakeRelPath (string fromPath, string toPath)
+		{
+			if (!Path.IsPathRooted (fromPath) || !Path.IsPathRooted (toPath))
+				throw new Exception ("All paths must be absolute.");
+
+			// 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);
+
+			var fromBranch = new StringBuilder ();
+			var toBranch = new StringBuilder ();
+			var root = new StringBuilder ();
+
+			var longerPieces = fromPieces.Length > toPieces.Length ? fromPieces : toPieces;
+
+			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;
+
+					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 ();
+        }
+	}
+}
diff --git a/build/GetSrcDirStrip.cs b/build/GetSrcDirStrip.cs
new file mode 100644
index 0000000..792057d
--- /dev/null
+++ b/build/GetSrcDirStrip.cs
@@ -0,0 +1,69 @@
+// 
+// GetSrcDirStrip.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 System.IO;
+using Microsoft.Build.Framework;
+using Microsoft.Build.Utilities;
+
+namespace Tasque.Build
+{
+	public class GetSrcDirStrip : Task
+	{
+		[Required]
+		public string MSBuildProjectDir { get; set; }
+		
+		[Required]
+		public string AbsTopBuildDir { get; set; }
+		
+		[Output]
+		public string SrcDirStrip { get; set; }
+		
+		public override bool Execute ()
+		{
+			try {
+				if (!Path.IsPathRooted (MSBuildProjectDir) || !Path.IsPathRooted (AbsTopBuildDir))
+					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);
+				
+				if (!projPath.Contains (topBuildPath))
+					throw new Exception ("AbsTopBuildDir must be a subpath of MSBuildProjectDir.");
+				
+				var srcDirBranch = string.Empty;
+				if (projPath != topBuildPath)
+					srcDirBranch = projPath.Substring (topBuildPath.Length + 1);
+				
+				SrcDirStrip = srcDirBranch;
+			} catch (Exception ex) {
+				Log.LogErrorFromException (ex, true);
+				return false;
+			}
+			return true;
+		}
+	}
+}
diff --git a/build/Tasque.targets b/build/Tasque.targets
index c31ddea..4bd041e 100644
--- a/build/Tasque.targets
+++ b/build/Tasque.targets
@@ -1,8 +1,11 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project DefaultTargets="Build" InitialTargets="_CheckProperties" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003";>
+<Project DefaultTargets="Build" InitialTargets="_CheckProperties;_GetSrcDir" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003";>
   <PropertyGroup>
-  	<_tmpInstallFileNames>obj\Tasque.RelInstallFileNames.txt</_tmpInstallFileNames>
+    <BuildingSolutionFile>True</BuildingSolutionFile>
+
+    <!-- Global dirs -->
     <OutputPath>.</OutputPath>
+    <Prefix Condition=" '$(Prefix)' == '' ">$(MSBuildProjectDirectory)\$(RelPrefix)</Prefix>
     <SrcDir Condition=" '$(SrcDir)' == '' ">.</SrcDir>
     <!-- TopBuildDir is usually TopSrcDir, hence default to TopSrcDir -->
     <TopBuildDir Condition=" '$(TopBuildDir)' == '' ">$(TopSrcDir)</TopBuildDir>
@@ -58,6 +61,9 @@
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <UsingTask TaskName="Tasque.Build.Substitute" AssemblyFile="build.dll" />
+  <UsingTask TaskName="Tasque.Build.GetRelPath" AssemblyFile="build.dll" />
+  <UsingTask TaskName="Tasque.Build.GetSrcDirStrip" AssemblyFile="build.dll" />
+  <UsingTask TaskName="Tasque.Build.GetAbsSrcDir" AssemblyFile="build.dll" />
 	
   <!-- Substitute -->
   <Target Name="Substitute" DependsOnTargets="BeforeSubstitute;CoreSubstitute;AfterSubstitute" />
@@ -192,6 +198,27 @@
     <Error Condition=" '$(RelPrefix)' == '' " Text="RelPrefix is not set." />
     <Message Text="Prefix=$(Prefix)" />
   </Target>
+
+  <Target Name="_GetSrcDirStrip">
+    <GetSrcDirStrip MSBuildProjectDir="$(MSBuildProjectDirectory)" AbsTopBuildDir="$(AbsTopBuildDir)">
+      <Output TaskParameter="SrcDirStrip" PropertyName="SrcDirStrip" />
+    </GetSrcDirStrip>
+    <Message Text="SrcDirStrip=$(SrcDirStrip)" />
+  </Target>
+
+  <Target Name="_GetSrcDir" Condition=" '$(AbsTopSrcDir)' != '$(AbsTopBuildDir)' "
+      DependsOnTargets="_GetSrcDirStrip">
+    <Message Text="AbsTopSrcDir=$(AbsTopSrcDir)" />
+    <Message Text="AbsTopBuildDir=$(AbsTopBuildDir)" />
+    <Message Text="MSBuildProjectDirectory=$(MSBuildProjectDirectory)" />
+    <GetAbsSrcDir AbsTopSrcDir="$(AbsTopSrcDir)" SrcDirStrip="$(SrcDirStrip)">
+      <Output TaskParameter="AbsSrcDir" PropertyName="AbsSrcDir" />
+    </GetAbsSrcDir>
+    <GetRelPath FromPath="$(MSBuildProjectDirectory)" ToPath="$(AbsSrcDir)">
+      <Output TaskParameter="RelativePath" PropertyName="SrcDir" />
+    </GetRelPath>
+    <Message Text="SrcDir=$(SrcDir)" />
+  </Target>
   
   <Target Name="_SetupBinInstallFile">
     <Message Text="$(TargetFileName)" />
diff --git a/build/build.csproj b/build/build.csproj
index 5337ac4..822035a 100644
--- a/build/build.csproj
+++ b/build/build.csproj
@@ -33,6 +33,9 @@
   <ItemGroup>
     <Compile Include="AssemblyInfo.cs" />
     <Compile Include="Substitute.cs" />
+    <Compile Include="GetRelPath.cs" />
+    <Compile Include="GetAbsSrcDir.cs" />
+    <Compile Include="GetSrcDirStrip.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="build.csproj" />



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