[tasque/transition: 140/213] Rewrite some build tasks and imporve build



commit fdda0e2e9844b34700aff331ca462f78fb0aac2e
Author: Antonius Riha <antoniusriha gmail com>
Date:   Sat Aug 18 15:46:50 2012 +0200

    Rewrite some build tasks and imporve build
    
    * SetDataAtBuildTime.cs: Dropped in favor of SetValues.cs
    * SetValues: A generic build task that sets values for variables in files
    before the actual build. Much like during autotool's configure pass.
    * PrepareAuthorsFile.cs: Formats the AUTHORS file's content at the repo
    root to be properly processed by SetValues task.
    * CopyrightInfo.txt: Provides copyright info for processing by SetValues
    task.
    * BuildBinScript.cs: Dropped. Not needed anymore.
    
    * Tasque.targets: defines the tasks used in build.csproj.
    * build.csproj: uses the new tasks now and imports Tasque.targets.

 build/CopyrightInfo.txt                            |    1 +
 build/{BuildBinScript.cs => PrepareAuthorsFile.cs} |   37 ++--
 build/SetDataAtBuildTime.cs                        |  215 --------------------
 build/{BuildBinScript.cs => SetValues.cs}          |   51 ++++-
 build/Tasque.targets                               |   10 +
 build/build.csproj                                 |   34 ++--
 6 files changed, 91 insertions(+), 257 deletions(-)
---
diff --git a/build/CopyrightInfo.txt b/build/CopyrightInfo.txt
new file mode 100644
index 0000000..51dfdf1
--- /dev/null
+++ b/build/CopyrightInfo.txt
@@ -0,0 +1 @@
+Copyright (c) 2008 Novell, Inc.
diff --git a/build/BuildBinScript.cs b/build/PrepareAuthorsFile.cs
similarity index 67%
copy from build/BuildBinScript.cs
copy to build/PrepareAuthorsFile.cs
index 7d27e60..40b1f22 100644
--- a/build/BuildBinScript.cs
+++ b/build/PrepareAuthorsFile.cs
@@ -1,5 +1,5 @@
 // 
-// BuildBinScript.cs
+// PrepareAuthorsFile.cs
 //  
 // Author:
 //       Antonius Riha <antoniusriha gmail com>
@@ -25,33 +25,42 @@
 // THE SOFTWARE.
 using System;
 using System.IO;
+using System.Text;
 using Microsoft.Build.Framework;
 using Microsoft.Build.Utilities;
 
 namespace Tasque.Build
 {
-	public class BuildBinScript : Task
+	public class PrepareAuthorsFile : Task
 	{
 		[Required]
-		public ITaskItem Prefix { get; set; }
+		public ITaskItem AuthorsFile { get; set; }
 		
-		[Required]
-		public ITaskItem BinScriptIn { get; set; }
+		[Output]
+		public ITaskItem Output { get; set; }
 		
 		public override bool Execute ()
 		{
 			try {
-				var text = File.ReadAllText (BinScriptIn.GetMetadata ("FullPath"));
-				text = text.Replace ("@prefix@", Prefix.ItemSpec);
-				
-				var dirPath = Path.Combine (Prefix.ItemSpec, "bin");
-				if (!Directory.Exists (dirPath))
-					Directory.CreateDirectory (dirPath);
+				string authors;
+				using (var fs = new FileStream (AuthorsFile.GetMetadata ("FullPath"), FileMode.Open)) {
+					using (var sr = new StreamReader (fs)) {
+
+						var authorsStrBuilder = new StringBuilder ();
+
+						while (!sr.EndOfStream) {
+							var line = sr.ReadLine ().Trim ();
+							if (!string.IsNullOrWhiteSpace (line))
+								authorsStrBuilder.Append ("\"" + line + "\", ");
+						}
+
+						authors = authorsStrBuilder.ToString ();
+					}
+				}
 				
-				var path = Path.Combine (dirPath, "tasque");
-				File.WriteAllText (path, text);
+				Output = new TaskItem (authors);
 			} catch (Exception ex) {
-				Log.LogErrorFromException (ex, true);
+				Log.LogErrorFromException (ex);
 				return false;
 			}
 			return true;
diff --git a/build/BuildBinScript.cs b/build/SetValues.cs
similarity index 50%
rename from build/BuildBinScript.cs
rename to build/SetValues.cs
index 7d27e60..bc21aef 100644
--- a/build/BuildBinScript.cs
+++ b/build/SetValues.cs
@@ -1,5 +1,5 @@
 // 
-// BuildBinScript.cs
+// SetValues.cs
 //  
 // Author:
 //       Antonius Riha <antoniusriha gmail com>
@@ -30,31 +30,60 @@ using Microsoft.Build.Utilities;
 
 namespace Tasque.Build
 {
-	public class BuildBinScript : Task
+	public class SetValues : Task
 	{
 		[Required]
-		public ITaskItem Prefix { get; set; }
+		public ITaskItem[] SourceFiles { get; set; }
 		
 		[Required]
-		public ITaskItem BinScriptIn { get; set; }
+		public ITaskItem[] TargetFiles { get; set; }
+		
+		[Required]
+		public ITaskItem[] Patterns { get; set; }
+		
+		[Required]
+		public ITaskItem[] Values { get; set; }
 		
 		public override bool Execute ()
 		{
 			try {
-				var text = File.ReadAllText (BinScriptIn.GetMetadata ("FullPath"));
-				text = text.Replace ("@prefix@", Prefix.ItemSpec);
+				if (Patterns.Length != Values.Length)
+					throw new Exception ("The number of provided patterns must be equal to" +
+						" the number of provided values."
+					);
 				
-				var dirPath = Path.Combine (Prefix.ItemSpec, "bin");
-				if (!Directory.Exists (dirPath))
-					Directory.CreateDirectory (dirPath);
+				if (SourceFiles.Length != TargetFiles.Length)
+					throw new Exception ("The number of provided source files must be equal to" +
+						" the number of provided target files."
+					);
 				
-				var path = Path.Combine (dirPath, "tasque");
-				File.WriteAllText (path, text);
+				for (int i = 0; i < SourceFiles.Length; i++) {
+					fileText = File.ReadAllText (SourceFiles [i].GetMetadata ("FullPath"));
+					
+					for (int j = 0; j < Values.Length; j++) {
+						// if the input value is actually a path, take file content as new value
+						string newVal;
+						var path = Values [j].GetMetadata ("FullPath");
+						if (File.Exists (path)) {
+							newVal = File.ReadAllText (path);
+							newVal = newVal.Replace ("\"", "\"\"");
+						} else
+							newVal = Values [j].ItemSpec;
+						fileText = fileText.Replace (Patterns [j].ItemSpec, newVal);
+					}
+					var newPath = TargetFiles [i].GetMetadata ("FullPath");
+					var dirNewPath = Path.GetDirectoryName (newPath);
+					if (!Directory.Exists (dirNewPath))
+						Directory.CreateDirectory (dirNewPath);
+					File.WriteAllText (newPath, fileText);
+				}
 			} catch (Exception ex) {
 				Log.LogErrorFromException (ex, true);
 				return false;
 			}
 			return true;
 		}
+		
+		string fileText;
 	}
 }
diff --git a/build/Tasque.targets b/build/Tasque.targets
new file mode 100644
index 0000000..1b53dc4
--- /dev/null
+++ b/build/Tasque.targets
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003";>
+	<PropertyGroup>
+		<AssemblyFile Condition=" '$(AssemblyFile)' == '' ">bin\Tasque.Build.dll</AssemblyFile>
+	</PropertyGroup>
+	<UsingTask AssemblyFile="$(AssemblyFile)" TaskName="Tasque.Build.SetValues" />
+	<UsingTask AssemblyFile="$(AssemblyFile)" TaskName="Tasque.Build.CheckPrefix" />
+	<UsingTask AssemblyFile="$(AssemblyFile)" TaskName="Tasque.Build.PrepareAuthorsFile" />
+	<UsingTask AssemblyFile="$(AssemblyFile)" TaskName="Tasque.Build.LoadGitSubmodules" />
+</Project>
diff --git a/build/build.csproj b/build/build.csproj
index 7519c45..450124b 100644
--- a/build/build.csproj
+++ b/build/build.csproj
@@ -29,10 +29,7 @@
   </PropertyGroup>
   <!-- Project and tasks imports -->
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
-  <UsingTask AssemblyFile="bin\Tasque.Build.dll" TaskName="Tasque.Build.SetDataAtBuildTime" />
-  <UsingTask AssemblyFile="bin\Tasque.Build.dll" TaskName="Tasque.Build.LoadGitSubmodules" />
-  <UsingTask AssemblyFile="bin\Tasque.Build.dll" TaskName="Tasque.Build.BuildBinScript" />
-  <UsingTask AssemblyFile="bin\Tasque.Build.dll" TaskName="Tasque.Build.CheckPrefix" />
+  <Import Project="Tasque.targets" />
   <ItemGroup>
     <None Include="build.csproj" />
     <None Include="..\tasque.sln">
@@ -96,16 +93,21 @@
     </None>
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="SetDataAtBuildTime.cs" />
     <Compile Include="LoadGitSubmodules.cs" />
     <Compile Include="CompileTranslations.cs" />
     <Compile Include="DeleteCompiledTranslations.cs" />
-    <Compile Include="BuildBinScript.cs" />
     <Compile Include="CheckPrefix.cs" />
+    <Compile Include="SetValues.cs" />
+    <Compile Include="PrepareAuthorsFile.cs" />
   </ItemGroup>
   <ItemGroup>
-    <GlobalDefines Include="GlobalDefines.cs" />
-    <GlobalDefinesIn Include="GlobalDefines.cs.in" />
+    <SetValueSourceFile Include="GlobalDefines.cs.in" />
+    <SetValueSourceFile Include="Solution.properties.in" />
+    <SetValueSourceFile Include="tasque.in" />
+  </ItemGroup>
+  <ItemGroup>
+    <SetValueTargetFile Include="GlobalDefines.cs" />
+    <SetValueTargetFile Include="Solution.properties" />
   </ItemGroup>
   <ItemGroup>
     <License Include="..\LICENSE">
@@ -114,9 +116,7 @@
     <Authors Include="..\AUTHORS">
       <Link>AUTHORS</Link>
     </Authors>
-  </ItemGroup>
-  <ItemGroup>
-    <BinScriptIn Include="tasque.in" />
+    <CopyrightInfo Include="CopyrightInfo.txt" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Microsoft.Build.Framework" />
@@ -127,15 +127,15 @@
     <Message Text="Checking prefix..." />
     <CheckPrefix Prefix="$(Prefix)" />
     <Message Text="Finished checking prefix." />
-    <Message Text="Setting up global definitions..." />
-    <SetDataAtBuildTime AuthorsFile="@(Authors)" CopyingFile="@(License)" Prefix="$(Prefix)" GlobalDefinesFile="@(GlobalDefines)" GlobalDefinesFileIn="@(GlobalDefinesIn)" Version="$(ReleaseVersion)" Website="$(Website)" />
-    <Message Text="Finished setting up global definitions." />
+    <Message Text="Setting up global variable definitions..." />
+    <PrepareAuthorsFile AuthorsFile="@(Authors)">
+      <Output ItemName="AuthorsPrepared" TaskParameter="Output" />
+    </PrepareAuthorsFile>
+    <SetValues SourceFiles="@(SetValueSourceFile)" TargetFiles="@(SetValueTargetFile);$(Prefix)\bin\tasque" Patterns="@version@;@prefix@;@website@;@authors@;@copyrightinfo@;@license@" Values="$(ReleaseVersion);$(Prefix);$(Website);@(AuthorsPrepared);@(CopyrightInfo);@(License)" />
+    <Message Text="Finished setting up global variable definitions." />
     <Message Text="Load submodules..." />
     <LoadGitSubmodules SolutionDirectory="$(MSBuildProjectDirectory)\.." />
     <Message Text="Finished loading submodules." />
-    <Message Text="Installing bin script..." />
-    <BuildBinScript Prefix="$(Prefix)" BinScriptIn="@(BinScriptIn)" />
-    <Message Text="Finished installing bin script." />
   </Target>
   <ProjectExtensions>
     <MonoDevelop>



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