From bf101259feb1bd0669b554b5a668a3753c094dde Mon Sep 17 00:00:00 2001 From: Glatzemann Date: Tue, 23 Oct 2012 09:23:41 +0000 Subject: [PATCH] Added output path switch to ProjectConverter: we are now able to select where to create the converted project --- Tools/ProjectConverter/Converter.cs | 26 +++++----- Tools/ProjectConverter/Program.cs | 49 ++++++++++++------- Tools/ProjectConverter/ProjectPath.cs | 42 +++++++++++----- .../Properties/AssemblyInfo.cs | 4 +- 4 files changed, 79 insertions(+), 42 deletions(-) diff --git a/Tools/ProjectConverter/Converter.cs b/Tools/ProjectConverter/Converter.cs index 10ee2808..05367a59 100644 --- a/Tools/ProjectConverter/Converter.cs +++ b/Tools/ProjectConverter/Converter.cs @@ -31,9 +31,9 @@ namespace ProjectConverter public abstract string Name { get; } #region ConvertAllProjects - public void ConvertAllProjects(string solutionFilepath) + public void ConvertAllProjects(string solutionFilepath, string destinationPath) { - ProjectPath[] allProjects = CollectAllProjects(solutionFilepath); + ProjectPath[] allProjects = CollectAllProjects(solutionFilepath, destinationPath); for (int index = 0; index < allProjects.Length; index++) { @@ -45,9 +45,9 @@ namespace ProjectConverter #endregion #region ConvertProject - public void ConvertProject(string projectFilePath) + public void ConvertProject(string projectFilePath, string destinationPath) { - ProjectPath projectPath = new ProjectPath(this, projectFilePath, "."); + ProjectPath projectPath = new ProjectPath(this, projectFilePath, ".", destinationPath); ConvertProject(projectPath); } @@ -220,7 +220,11 @@ namespace ProjectConverter string referencePath = includeAttribute.Value; if (referencePath.EndsWith(".csproj")) { - referencePath = referencePath.Replace(".csproj", "_" + Postfix + ".csproj"); + if (!string.IsNullOrEmpty(Postfix)) + { + referencePath = referencePath.Replace(".csproj", "_" + Postfix + ".csproj"); + } + string basePath = Path.GetDirectoryName(CurrentProject.FullSourcePath); string fullReferencePath = Path.Combine(basePath, referencePath); if (File.Exists(fullReferencePath)) @@ -233,7 +237,7 @@ namespace ProjectConverter #endregion #region CollectAllProjects - private ProjectPath[] CollectAllProjects(string solutionFilepath) + private ProjectPath[] CollectAllProjects(string solutionFilepath, string destinationPath) { var solution = VsSolution.Load(solutionFilepath); var result = new List(); @@ -242,7 +246,7 @@ namespace ProjectConverter foreach (var project in solution.Projects) if (project.IsCsProject && project.RelativePath.Contains("Tools") == false) - result.Add(new ProjectPath(this, project.RelativePath, basePath)); + result.Add(new ProjectPath(this, project.RelativePath, basePath, destinationPath)); return result.ToArray(); } @@ -282,7 +286,7 @@ namespace ProjectConverter { const string filepath = @"D:\code\csharp\ANX.Framework\ANX.Framework.sln"; var converter = new MetroConverter(); - ProjectPath[] result = converter.CollectAllProjects(filepath); + ProjectPath[] result = converter.CollectAllProjects(filepath, string.Empty); Assert.Greater(result.Length, 0); } @@ -294,7 +298,7 @@ namespace ProjectConverter { const string filepath = @"D:\code\csharp\ANX.Framework\ANX.Framework.sln"; var converter = new MetroConverter(); - converter.ConvertAllProjects(filepath); + converter.ConvertAllProjects(filepath, string.Empty); } #endregion @@ -304,7 +308,7 @@ namespace ProjectConverter { const string filepath = @"D:\code\csharp\ANX.Framework\ANX.Framework.sln"; var converter = new PsVitaConverter(); - converter.ConvertAllProjects(filepath); + converter.ConvertAllProjects(filepath, string.Empty); } #endregion @@ -314,7 +318,7 @@ namespace ProjectConverter { const string filepath = @"D:\code\csharp\ANX.Framework\ANX.Framework.sln"; var converter = new LinuxConverter(); - converter.ConvertAllProjects(filepath); + converter.ConvertAllProjects(filepath, string.Empty); } #endregion } diff --git a/Tools/ProjectConverter/Program.cs b/Tools/ProjectConverter/Program.cs index fb07d9b4..cff2a78e 100644 --- a/Tools/ProjectConverter/Program.cs +++ b/Tools/ProjectConverter/Program.cs @@ -23,6 +23,10 @@ namespace ProjectConverter new XnaConverter(), }; + private static readonly List switches = new List(); + private static readonly Dictionary keyValueParameters = new Dictionary(); + private static readonly List files = new List(); + [STAThread] static void Main(string[] args) { @@ -30,29 +34,27 @@ namespace ProjectConverter // To restore "old" behaviour use: // ProjectConverter /linux /psvita /windowsmetro ../../ANX.Framework.sln // - // For testing only - //args = new[] { "/linux", "/psvita", "/windowsmetro", "../../ANX.Framework.sln" }; Directory.SetCurrentDirectory(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)); - var switches = new List(); - var keyValueParameters = new Dictionary(); - var files = new List(); - foreach (string arg in args) { if (arg.StartsWith("/") || arg.StartsWith("-")) { - if (arg.Contains("=")) - { - string[] parts = arg.Split('='); - keyValueParameters[parts[0].Trim().ToLowerInvariant()] = parts[1].Trim().ToLowerInvariant(); - } - else - switches.Add(arg.Substring(1).Trim().ToLowerInvariant()); + if (arg.Contains("=")) + { + string[] parts = arg.Split('='); + keyValueParameters[parts[0].Trim().ToLowerInvariant()] = parts[1].Trim().ToLowerInvariant(); + } + else + { + switches.Add(arg.Substring(1).Trim().ToLowerInvariant()); + } } - else if (File.Exists(arg)) - files.Add(arg); + else if (File.Exists(arg)) + { + files.Add(arg); + } } foreach (string file in files) @@ -65,10 +67,10 @@ namespace ProjectConverter switch (fileExt) { case ".sln": - converter.ConvertAllProjects(file); + converter.ConvertAllProjects(file, TryGetDestinationPath()); break; case ".csproj": - converter.ConvertProject(file); + converter.ConvertProject(file, TryGetDestinationPath()); break; default: throw new NotImplementedException("unsupported file type '" + fileExt + "'"); @@ -77,5 +79,18 @@ namespace ProjectConverter } } } + + private static string TryGetDestinationPath() + { + foreach (KeyValuePair kvp in keyValueParameters) + { + if (string.Equals(kvp.Key, "/O", StringComparison.InvariantCultureIgnoreCase)) + { + return kvp.Value; + } + } + + return string.Empty; + } } } diff --git a/Tools/ProjectConverter/ProjectPath.cs b/Tools/ProjectConverter/ProjectPath.cs index 5d51db0e..8036d596 100644 --- a/Tools/ProjectConverter/ProjectPath.cs +++ b/Tools/ProjectConverter/ProjectPath.cs @@ -58,21 +58,36 @@ namespace ProjectConverter } } - public ProjectPath(Converter converter, string relativeSourcePath, string basePath) + public ProjectPath(Converter converter, string relativeSourcePath, string basePath, string destinationPath) { RelativeSourcePath = relativeSourcePath; FullSourcePath = Path.Combine(basePath, relativeSourcePath); - RelativeDestinationPath = BuildTargetFilepath(converter); + if (string.IsNullOrEmpty(destinationPath)) + { + RelativeDestinationPath = BuildTargetFilepath(converter); + } + else + { + if (!Directory.Exists(destinationPath)) + { + Directory.CreateDirectory(destinationPath); + } + + RelativeDestinationPath = Path.Combine(destinationPath, Path.GetFileName(relativeSourcePath)); + } + FullDestinationPath = Path.Combine(basePath, RelativeDestinationPath); - try - { + //TODO: never ever ignore all exceptions without proper handling + + //try + //{ LoadProjectFile(); - } - catch - { - } + //} + //catch + //{ + //} } #region Save @@ -92,7 +107,10 @@ namespace ProjectConverter filename = filename.Substring(0, filename.IndexOf('_')); } - filename += "_" + converter.Postfix; + if (!string.IsNullOrEmpty(converter.Postfix)) + { + filename += "_" + converter.Postfix; + } return Path.Combine(basePath, filename + ".csproj"); } @@ -121,18 +139,18 @@ namespace ProjectConverter string testRelativeSourcePath = "ANX.Framework.csproj"; var projPath = new ProjectPath(new PsVitaConverter(), - testRelativeSourcePath, testBasePath); + testRelativeSourcePath, testBasePath, string.Empty); Assert.AreEqual(projPath.RelativeDestinationPath, "ANX.Framework_PSVita.csproj"); projPath = new ProjectPath(new LinuxConverter(), - "ANX.Framework_IOS.csproj", testBasePath); + "ANX.Framework_IOS.csproj", testBasePath, string.Empty); Assert.AreEqual(projPath.RelativeDestinationPath, "ANX.Framework_Linux.csproj"); projPath = new ProjectPath(new MetroConverter(), - "ANX.Framework_IOS_Android_WindowsXNA.csproj", testBasePath); + "ANX.Framework_IOS_Android_WindowsXNA.csproj", testBasePath, string.Empty); Assert.AreEqual(projPath.RelativeDestinationPath, "ANX.Framework_WindowsMetro.csproj"); } diff --git a/Tools/ProjectConverter/Properties/AssemblyInfo.cs b/Tools/ProjectConverter/Properties/AssemblyInfo.cs index 9ad90467..92368932 100644 --- a/Tools/ProjectConverter/Properties/AssemblyInfo.cs +++ b/Tools/ProjectConverter/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern // übernehmen, indem Sie "*" eingeben: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.2.*")] -[assembly: AssemblyFileVersion("1.2.2.0")] +[assembly: AssemblyVersion("1.2.3.*")] +[assembly: AssemblyFileVersion("1.2.3.0")]