Skip to content

Commit

Permalink
Support project.json
Browse files Browse the repository at this point in the history
  • Loading branch information
yuv4ik committed Apr 15, 2018
1 parent ca321b4 commit d9c3788
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 59 deletions.
35 changes: 7 additions & 28 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 @@ -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);
Expand All @@ -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)
Expand All @@ -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
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 d9c3788

Please sign in to comment.