From d9c3788a60da001ea1279338b4cdcb4426bc19d0 Mon Sep 17 00:00:00 2001 From: Evgeny Zborovsky Date: Sun, 15 Apr 2018 11:26:29 +0300 Subject: [PATCH] Support project.json --- Mutatio/ConvertProjectToNetStandardHandler.cs | 35 ++-------- Mutatio/Extensions/DotNetProjectExtenions.cs | 9 ++- Mutatio/Mutatio.csproj | 5 ++ Mutatio/NetStandardProjFileGenerator.cs | 20 ++---- Mutatio/PackagesFileParser.cs | 15 ----- Mutatio/ProjectTemplates/IProjectTemplate.cs | 11 ++++ .../ProjectTemplateFactory.cs | 16 +++++ .../ProjectWithPackagesJson.cs | 50 ++++++++++++++ .../ProjectWithProjectJson.cs | 66 +++++++++++++++++++ 9 files changed, 168 insertions(+), 59 deletions(-) delete mode 100644 Mutatio/PackagesFileParser.cs create mode 100644 Mutatio/ProjectTemplates/IProjectTemplate.cs create mode 100644 Mutatio/ProjectTemplates/ProjectTemplateFactory.cs create mode 100644 Mutatio/ProjectTemplates/ProjectWithPackagesJson.cs create mode 100644 Mutatio/ProjectTemplates/ProjectWithProjectJson.cs diff --git a/Mutatio/ConvertProjectToNetStandardHandler.cs b/Mutatio/ConvertProjectToNetStandardHandler.cs index 1f60401..1bc83c9 100644 --- a/Mutatio/ConvertProjectToNetStandardHandler.cs +++ b/Mutatio/ConvertProjectToNetStandardHandler.cs @@ -2,7 +2,6 @@ using System.IO; using System.Text; using MonoDevelop.Components.Commands; -using MonoDevelop.Core; using MonoDevelop.Ide; using MonoDevelop.Projects; using Mutatio.Extensions; @@ -42,17 +41,19 @@ protected async override void Run() monitor.Log.WriteLine($"Generating new {proj.GetProjFileExtension()}"); monitor.Log.WriteLine(); - var netStandardProjContent = new NetStandardProjFileGenerator(proj).GenerateProjForNetStandard(); + var projectTemplate = ProjectTemplateFactory.GetTemplate(proj); + monitor.Log.WriteLine($"Project template detected: {projectTemplate}"); + var netStandardProjContent = new NetStandardProjFileGenerator().GenerateProjForNetStandard(projectTemplate.GetPackages); monitor.Log.WriteLine($"Creating backup"); monitor.Log.WriteLine(); - BackupOldFormatFiles(proj); + projectTemplate.BackupOldFormatFiles(); monitor.Log.WriteLine($"Deleting old files"); monitor.Log.WriteLine(); - CleanUpOldFormatFiles(proj); + projectTemplate.CleanUpOldFormatFiles(); // Create a new .xproj File.WriteAllText($"{projFilePath}", netStandardProjContent, Encoding.UTF8); @@ -64,6 +65,8 @@ protected async override void Run() await IdeApp.Workspace.Close(true); await IdeApp.Workspace.OpenWorkspaceItem(proj.ParentSolution.FileName); + monitor.Log.WriteLine("Please note that .NET Standard 2.0 is supported only from Xamarin.Forms 2.4+."); + monitor.Log.WriteLine("More information can be found here: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/internals/net-standard"); monitor.ReportSuccess("Convertion succeed."); } catch (Exception ex) @@ -75,30 +78,6 @@ protected async override void Run() monitor.EndTask(); } } - } - - void BackupOldFormatFiles(DotNetProject proj) - { - var backupFolderPath = $"{proj.BaseDirectory.FullPath.ParentDirectory}/mutatio_backup"; - // Create backup directory - FileService.CreateDirectory(backupFolderPath); - // Backup current .xproj - var projFilePath = proj.GetProjFilePath(); - FileService.CopyFile(projFilePath, $"{backupFolderPath}/{proj.Name}.{proj.GetProjFileExtension()}"); - // Backup packages.config - var packagesConfigFilePath = proj.GetPackagesFilePath(); - FileService.CopyFile(packagesConfigFilePath, $"{backupFolderPath}/packages.config"); - - // Backup AssemblyInfo.x - FileService.CopyDirectory(proj.GetPropertiesDirPath(), $"{backupFolderPath}/Properties"); - } - - void CleanUpOldFormatFiles(DotNetProject proj) - { - FileService.DeleteFile(proj.GetPackagesFilePath()); - // TODO: In F# there is no /Properties - FileService.DeleteDirectory(proj.GetPropertiesDirPath()); - FileService.DeleteFile(proj.GetProjFilePath()); } // Shoud be enabled only when the workspace is opened diff --git a/Mutatio/Extensions/DotNetProjectExtenions.cs b/Mutatio/Extensions/DotNetProjectExtenions.cs index 4f53433..682d60d 100644 --- a/Mutatio/Extensions/DotNetProjectExtenions.cs +++ b/Mutatio/Extensions/DotNetProjectExtenions.cs @@ -1,11 +1,12 @@ using System; +using System.IO; using MonoDevelop.Projects; namespace Mutatio.Extensions { public static class DotNetProjectExtenions { - public static string GetPackagesFilePath(this DotNetProject proj) => $"{proj.BaseDirectory.FullPath}/packages.config"; + public static string GetPackagesJsonFilePath(this DotNetProject proj) => $"{proj.BaseDirectory.FullPath}/packages.config"; public static string GetPropertiesDirPath(this DotNetProject proj) => $"{proj.BaseDirectory.FullPath}/Properties"; public static string GetProjFilePath(this DotNetProject proj) => proj.FileName.FullPath; @@ -26,6 +27,10 @@ public static string GetAssemblyInfoFilePath(this DotNetProject proj) } } - public static bool IsCSharpProject(this DotNetProject proj) => proj.LanguageName.Equals("c#", StringComparison.OrdinalIgnoreCase); + public static bool IsCSharpProject(this DotNetProject proj) => proj.LanguageName.Equals("c#", StringComparison.OrdinalIgnoreCase); + + public static string GetProjectJsonFilePath(this DotNetProject proj) => $"{proj.BaseDirectory.FullPath}/project.json"; + public static string GetProjectLockJsonFilePath(this DotNetProject proj) => $"{proj.BaseDirectory.FullPath}/project.lock.json"; + public static bool ContainsProjectJson(this DotNetProject proj) => File.Exists(GetProjectJsonFilePath(proj)); } } diff --git a/Mutatio/Mutatio.csproj b/Mutatio/Mutatio.csproj index b70efbb..80e3196 100644 --- a/Mutatio/Mutatio.csproj +++ b/Mutatio/Mutatio.csproj @@ -5,8 +5,13 @@ + + + + + diff --git a/Mutatio/NetStandardProjFileGenerator.cs b/Mutatio/NetStandardProjFileGenerator.cs index f55afc0..78540b4 100644 --- a/Mutatio/NetStandardProjFileGenerator.cs +++ b/Mutatio/NetStandardProjFileGenerator.cs @@ -1,7 +1,7 @@ -using System.Linq; +using System; +using System.Collections.Generic; +using System.Linq; using System.Text; -using MonoDevelop.Projects; -using Mutatio.Extensions; namespace Mutatio { @@ -17,18 +17,10 @@ public class NetStandardProjFileGenerator "; - - readonly DotNetProject project; - - public NetStandardProjFileGenerator(DotNetProject project) - { - this.project = project; - } - - public string GenerateProjForNetStandard() + + public string GenerateProjForNetStandard(Func> getPackages) { - var packagesFileParser = new PackagesFileParser(); - var packages = packagesFileParser.GetPackages(project.GetPackagesFilePath()); + var packages = getPackages(); var packagesText = new StringBuilder(packages.Count()); foreach (var package in packages) diff --git a/Mutatio/PackagesFileParser.cs b/Mutatio/PackagesFileParser.cs deleted file mode 100644 index bea7292..0000000 --- a/Mutatio/PackagesFileParser.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Xml.Linq; - -namespace Mutatio -{ - public class PackagesFileParser - { - public IEnumerable<(string name, string version)> GetPackages(string filePath) - { - var doc = XDocument.Load(filePath); - return doc.Elements("packages").Descendants().Select(p => (p.Attribute("id").Value, p.Attribute("version").Value)); - } - } -} diff --git a/Mutatio/ProjectTemplates/IProjectTemplate.cs b/Mutatio/ProjectTemplates/IProjectTemplate.cs new file mode 100644 index 0000000..45c7b44 --- /dev/null +++ b/Mutatio/ProjectTemplates/IProjectTemplate.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace Mutatio +{ + public interface IProjectTemplate + { + void BackupOldFormatFiles(); + void CleanUpOldFormatFiles(); + IEnumerable<(string name, string version)> GetPackages(); + } +} diff --git a/Mutatio/ProjectTemplates/ProjectTemplateFactory.cs b/Mutatio/ProjectTemplates/ProjectTemplateFactory.cs new file mode 100644 index 0000000..7accaed --- /dev/null +++ b/Mutatio/ProjectTemplates/ProjectTemplateFactory.cs @@ -0,0 +1,16 @@ +using MonoDevelop.Projects; +using Mutatio.Extensions; + +namespace Mutatio +{ + public static class ProjectTemplateFactory + { + public static IProjectTemplate GetTemplate(DotNetProject project) + { + if (project.ContainsProjectJson()) + return new ProjectWithProjectJson(project); + else + return new ProjectWithPackagesJson(project); + } + } +} diff --git a/Mutatio/ProjectTemplates/ProjectWithPackagesJson.cs b/Mutatio/ProjectTemplates/ProjectWithPackagesJson.cs new file mode 100644 index 0000000..4b55bce --- /dev/null +++ b/Mutatio/ProjectTemplates/ProjectWithPackagesJson.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using MonoDevelop.Core; +using MonoDevelop.Projects; +using Mutatio.Extensions; + +namespace Mutatio +{ + public class ProjectWithPackagesJson : IProjectTemplate + { + readonly DotNetProject proj; + + public ProjectWithPackagesJson(DotNetProject proj) + { + this.proj = proj; + } + + public void BackupOldFormatFiles() + { + var backupFolderPath = $"{proj.BaseDirectory.FullPath.ParentDirectory}/mutatio_backup"; + // Create backup directory + FileService.CreateDirectory(backupFolderPath); + // Backup current .xproj + var projFilePath = proj.GetProjFilePath(); + FileService.CopyFile(projFilePath, $"{backupFolderPath}/{proj.Name}.{proj.GetProjFileExtension()}"); + // Backup packages.config + var packagesConfigFilePath = proj.GetPackagesJsonFilePath(); + FileService.CopyFile(packagesConfigFilePath, $"{backupFolderPath}/packages.config"); + + // Backup AssemblyInfo.x + FileService.CopyDirectory(proj.GetPropertiesDirPath(), $"{backupFolderPath}/Properties"); + } + + public void CleanUpOldFormatFiles() + { + FileService.DeleteFile(proj.GetPackagesJsonFilePath()); + FileService.DeleteDirectory(proj.GetPropertiesDirPath()); + FileService.DeleteFile(proj.GetProjFilePath()); + } + + public IEnumerable<(string name, string version)> GetPackages() + { + var doc = XDocument.Load(proj.GetPackagesJsonFilePath()); + return doc.Elements("packages").Descendants().Select(p => (p.Attribute("id").Value, p.Attribute("version").Value)); + } + + public override string ToString() => "project with packages.json"; + } +} diff --git a/Mutatio/ProjectTemplates/ProjectWithProjectJson.cs b/Mutatio/ProjectTemplates/ProjectWithProjectJson.cs new file mode 100644 index 0000000..4ea73a9 --- /dev/null +++ b/Mutatio/ProjectTemplates/ProjectWithProjectJson.cs @@ -0,0 +1,66 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using MonoDevelop.Core; +using MonoDevelop.Projects; +using Mutatio.Extensions; +using Newtonsoft.Json; + +namespace Mutatio +{ + public class ProjectWithProjectJson : IProjectTemplate + { + readonly DotNetProject proj; + + public ProjectWithProjectJson(DotNetProject proj) + { + this.proj = proj; + } + + public void BackupOldFormatFiles() + { + var backupFolderPath = $"{proj.BaseDirectory.FullPath.ParentDirectory}/mutatio_backup"; + // Create backup directory + FileService.CreateDirectory(backupFolderPath); + // Backup current .xproj + var projFilePath = proj.GetProjFilePath(); + FileService.CopyFile(projFilePath, $"{backupFolderPath}/{proj.Name}.{proj.GetProjFileExtension()}"); + // Backup project.json + var projectJsonFilePath = proj.GetProjectJsonFilePath(); + FileService.CopyFile(projectJsonFilePath, $"{backupFolderPath}/project.json"); + // Backup project.lock.json + if(File.Exists(proj.GetProjectLockJsonFilePath())) + { + var projectLockJsonFilePath = proj.GetProjectLockJsonFilePath(); + FileService.CopyFile(projectLockJsonFilePath, $"{backupFolderPath}/project.lock.json"); + } + // Backup AssemblyInfo.x + FileService.CopyDirectory(proj.GetPropertiesDirPath(), $"{backupFolderPath}/Properties"); + } + + public void CleanUpOldFormatFiles() + { + FileService.DeleteFile(proj.GetProjectJsonFilePath()); + if (File.Exists(proj.GetProjectLockJsonFilePath())) + FileService.DeleteFile(proj.GetProjectLockJsonFilePath()); + FileService.DeleteDirectory(proj.GetPropertiesDirPath()); + FileService.DeleteFile(proj.GetProjFilePath()); + } + + public IEnumerable<(string name, string version)> GetPackages() + { + var projJsonContents = File.ReadAllText(proj.GetProjectJsonFilePath(), new UTF8Encoding(true)); + var projJson = JsonConvert.DeserializeObject(projJsonContents); + return projJson.Dependencies.Select(d => (d.Key, d.Value)); + } + + public override string ToString() => "project with project.json"; + + class ProjJson + { + [JsonProperty("dependencies")] + public Dictionary Dependencies { get; set; } + } + } +}