Skip to content

Commit

Permalink
Merge pull request #3 from yuv4ik/project_json
Browse files Browse the repository at this point in the history
Project json
  • Loading branch information
yuv4ik authored Apr 15, 2018
2 parents 2af4410 + 208f85f commit f8fd651
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 69 deletions.
54 changes: 16 additions & 38 deletions Mutatio/ConvertProjectToNetStandardHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,38 +31,41 @@ protected async override void Run()

try
{
// We don't want to miss any log messages after reloading the solution
var logSummary = new StringBuilder();
var proj = ProjectOperations.CurrentSelectedItem as DotNetProject;

monitor.Log.WriteLine($"Converting {proj.Name}{proj.GetProjFileExtension()} to NET Standard 2.0 format ..");
monitor.Log.WriteLine();
logSummary.AppendLine($"Converting {proj.Name}{proj.GetProjFileExtension()} to NET Standard 2.0 format ..");

var projFilePath = proj.GetProjFilePath();

monitor.Log.WriteLine($"Generating new {proj.GetProjFileExtension()}");
monitor.Log.WriteLine();
logSummary.AppendLine($"Generating new {proj.GetProjFileExtension()}");

var netStandardProjContent = new NetStandardProjFileGenerator(proj).GenerateProjForNetStandard();
var projectTemplate = ProjectTemplateFactory.GetTemplate(proj);
logSummary.AppendLine($"Project template detected: {projectTemplate}");
var netStandardProjContent = new NetStandardProjFileGenerator().GenerateProjForNetStandard(projectTemplate.GetPackages);

monitor.Log.WriteLine($"Creating backup");
monitor.Log.WriteLine();
logSummary.AppendLine("Creating backup");

BackupOldFormatFiles(proj);
projectTemplate.BackupOldFormatFiles();

monitor.Log.WriteLine($"Deleting old files");
monitor.Log.WriteLine();
logSummary.AppendLine("Deleting old files");

CleanUpOldFormatFiles(proj);
projectTemplate.CleanUpOldFormatFiles();

// Create a new .xproj
File.WriteAllText($"{projFilePath}", netStandardProjContent, Encoding.UTF8);

// TODO: Programmatically reload the project instead of re-opening.
monitor.Log.WriteLine($"Re-opening the project");
monitor.Log.WriteLine();
logSummary.AppendLine("Re-opening the project");

await IdeApp.Workspace.Close(true);
await IdeApp.Workspace.OpenWorkspaceItem(proj.ParentSolution.FileName);

monitor.Log.WriteLine(logSummary);

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)
Expand All @@ -75,30 +77,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
Expand Down
9 changes: 7 additions & 2 deletions Mutatio/Extensions/DotNetProjectExtenions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}
}
5 changes: 5 additions & 0 deletions Mutatio/Mutatio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
<ItemGroup>
<PackageReference Include="MonoDevelop.Addins" Version="0.4.4" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Extensions\" />
<Folder Include="ProjectTemplates\" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Parsers\IParser.cs" />
</ItemGroup>
</Project>
20 changes: 6 additions & 14 deletions Mutatio/NetStandardProjFileGenerator.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -17,18 +17,10 @@ public class NetStandardProjFileGenerator
</ItemGroup>
</Project>";

readonly DotNetProject project;

public NetStandardProjFileGenerator(DotNetProject project)
{
this.project = project;
}

public string GenerateProjForNetStandard()

public string GenerateProjForNetStandard(Func<IEnumerable<(string name, string version)>> 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)
Expand Down
15 changes: 0 additions & 15 deletions Mutatio/PackagesFileParser.cs

This file was deleted.

11 changes: 11 additions & 0 deletions Mutatio/ProjectTemplates/IProjectTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace Mutatio
{
public interface IProjectTemplate
{
void BackupOldFormatFiles();
void CleanUpOldFormatFiles();
IEnumerable<(string name, string version)> GetPackages();
}
}
16 changes: 16 additions & 0 deletions Mutatio/ProjectTemplates/ProjectTemplateFactory.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
50 changes: 50 additions & 0 deletions Mutatio/ProjectTemplates/ProjectWithPackagesJson.cs
Original file line number Diff line number Diff line change
@@ -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";
}
}
66 changes: 66 additions & 0 deletions Mutatio/ProjectTemplates/ProjectWithProjectJson.cs
Original file line number Diff line number Diff line change
@@ -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<ProjJson>(projJsonContents);
return projJson.Dependencies.Select(d => (d.Key, d.Value));
}

public override string ToString() => "project with project.json";

class ProjJson
{
[JsonProperty("dependencies")]
public Dictionary<string, string> Dependencies { get; set; }
}
}
}

0 comments on commit f8fd651

Please sign in to comment.