Skip to content

Commit

Permalink
Merge pull request #2 from yuv4ik/usability_improvements
Browse files Browse the repository at this point in the history
Strict ConvertToNetStandardHandler to support only C# portable libraries
  • Loading branch information
yuv4ik committed Mar 24, 2018
2 parents d6f293b + 3604014 commit ca321b4
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 28 deletions.
8 changes: 8 additions & 0 deletions Mutatio/Consts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;
namespace Mutatio
{
public static class Consts
{
public const string ProjectUrl = "https://github.com/yuv4ik/mutatio";
}
}
39 changes: 24 additions & 15 deletions Mutatio/ConvertProjectToNetStandardHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ public class ConvertProjectToNetStandardHandler : CommandHandler

protected override void Update(CommandInfo info)
{
info.Enabled = IsWorkspaceOpen() && ProjectIsNotBuildingOrRunning() && UserSelectedItemIsProject() && UserSelectedProjectIsNotNetStandard20();
info.Enabled =
IsWorkspaceOpen()
&& ProjectIsNotBuildingOrRunning()
&& UserSelectedItemIsProject()
&& UserSelectedProjectIsPortableLibrary()
&& UserSelectedProjectIsCSharpProject()
&& UserSelectedProjectIsNotNetStandard20();
}

protected async override void Run()
Expand All @@ -28,12 +34,12 @@ protected async override void Run()
{
var proj = ProjectOperations.CurrentSelectedItem as DotNetProject;

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

var csProjFilePath = proj.GetCsProjFilePath();
var projFilePath = proj.GetProjFilePath();

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

var netStandardProjContent = new NetStandardProjFileGenerator(proj).GenerateProjForNetStandard();
Expand All @@ -48,11 +54,10 @@ protected async override void Run()

CleanUpOldFormatFiles(proj);

// Create a new .csproj
File.WriteAllText($"{csProjFilePath}", netStandardProjContent, Encoding.UTF8);
// 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();

Expand All @@ -63,7 +68,7 @@ protected async override void Run()
}
catch (Exception ex)
{
monitor.ReportError("Convertion failed. Please create an issue on github.", ex);
monitor.ReportError($"Convertion failed. Please create an issue on github: {Consts.ProjectUrl}", ex);
}
finally
{
Expand All @@ -77,21 +82,23 @@ void BackupOldFormatFiles(DotNetProject proj)
var backupFolderPath = $"{proj.BaseDirectory.FullPath.ParentDirectory}/mutatio_backup";
// Create backup directory
FileService.CreateDirectory(backupFolderPath);
// Backup current .csproj
var csProjFilePath = proj.GetCsProjFilePath();
FileService.CopyFile(csProjFilePath, $"{backupFolderPath}/{proj.Name}.csproj");
// 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 /Properties dir

// 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.GetCsProjFilePath());
FileService.DeleteFile(proj.GetProjFilePath());
}

// Shoud be enabled only when the workspace is opened
Expand All @@ -106,7 +113,9 @@ bool ProjectIsNotBuildingOrRunning()
&& IdeApp.ProjectOperations.CurrentRunOperation.IsCompleted;
}

bool UserSelectedItemIsProject() => ProjectOperations.CurrentSelectedItem is DotNetProject;
bool UserSelectedProjectIsNotNetStandard20() => !(ProjectOperations.CurrentSelectedItem as DotNetProject).TargetFramework.Name.Equals(".NETStandard 2.0", System.StringComparison.OrdinalIgnoreCase);
bool UserSelectedItemIsProject() => ProjectOperations.CurrentSelectedItem is DotNetProject;
bool UserSelectedProjectIsCSharpProject() => (ProjectOperations.CurrentSelectedItem as DotNetProject).IsCSharpProject();
bool UserSelectedProjectIsNotNetStandard20() => !(ProjectOperations.CurrentSelectedItem as DotNetProject).TargetFramework.Name.Equals(".NETStandard 2.0", StringComparison.OrdinalIgnoreCase);
bool UserSelectedProjectIsPortableLibrary() => (ProjectOperations.CurrentSelectedItem as DotNetProject).IsPortableLibrary;
}
}
24 changes: 22 additions & 2 deletions Mutatio/Extensions/DotNetProjectExtenions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
using MonoDevelop.Projects;
using System;
using MonoDevelop.Projects;

namespace Mutatio.Extensions
{
public static class DotNetProjectExtenions
{
public static string GetCsProjFilePath(this DotNetProject proj) => $"{proj.BaseDirectory.FullPath}/{proj.Name}.csproj";
public static string GetPackagesFilePath(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;
public static string GetProjFileExtension(this DotNetProject proj) => proj.FileName.Extension;

public static string GetAssemblyInfoFilePath(this DotNetProject proj)
{
switch(proj.LanguageName)
{
case "C#":
return $"{proj.BaseDirectory.FullPath}/Properties/AssemblyInfo.cs";
case "F#":
return $"{proj.BaseDirectory.FullPath}/AssemblyInfo.fs";
case "VBNET":
return $"{proj.BaseDirectory.FullPath}/AssemblyInfo.vb";
default:
throw new NotSupportedException($"{proj.LanguageName} is currently unsupported. Please create an issue on github: {Consts.ProjectUrl}");
}
}

public static bool IsCSharpProject(this DotNetProject proj) => proj.LanguageName.Equals("c#", StringComparison.OrdinalIgnoreCase);
}
}
1 change: 1 addition & 0 deletions Mutatio/Mutatio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MonoDevelop.Addins" Version="0.4.4" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Extensions\" />
Expand Down
6 changes: 3 additions & 3 deletions Mutatio/NetStandardProjFileGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Mutatio
{
public class NetStandardProjFileGenerator
{
string netStandard20CsProjFormat => @"<Project Sdk=""Microsoft.NET.Sdk"">
string netStandard20ProjFormat => @"<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand All @@ -32,9 +32,9 @@ public string GenerateProjForNetStandard()

var packagesText = new StringBuilder(packages.Count());
foreach (var package in packages)
packagesText.AppendLine($"<PackageReference Include=\"{package.Item1}\" Version=\"{package.Item2}\"/>");
packagesText.AppendLine($"<PackageReference Include=\"{package.name}\" Version=\"{package.version}\"/>");

return string.Format(netStandard20CsProjFormat, packagesText);
return string.Format(netStandard20ProjFormat, packagesText);
}
}
}
7 changes: 3 additions & 4 deletions Mutatio/PackagesFileParser.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Mutatio
{
public class PackagesFileParser
{
public IEnumerable<Tuple<string, string>> GetPackages(string filePath)
public IEnumerable<(string name, string version)> GetPackages(string filePath)
{
var doc = XDocument.Load(filePath);
return doc.Elements("packages").Descendants().Select(p => new Tuple<string, string>(p.Attribute("id").Value, p.Attribute("version").Value));
return doc.Elements("packages").Descendants().Select(p => (p.Attribute("id").Value, p.Attribute("version").Value));
}
}
}
7 changes: 4 additions & 3 deletions Mutatio/Properties/AddinInfo.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using Mono.Addins;
using Mono.Addins.Description;
using Mutatio;

[assembly: Addin(
"Mutatio",
Namespace = "Mutatio",
Version = "1.0"
Version = "1.0.1"
)]

[assembly: AddinName("Mutatio")]
[assembly: AddinCategory("IDE extensions")]
[assembly: AddinDescription("Converts PCL to .NET Standard 2.0 automatically.")]
[assembly: AddinDescription("Automatically convert projects from PCL to .NET Standard 2.0.")]
[assembly: AddinAuthor("Evgeny Zborovsky")]
[assembly: AddinUrl("https://github.com/yuv4ik/mutatio")]
[assembly: AddinUrl(Consts.ProjectUrl)]
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ Visual Studio for Mac add-in/extension for converting old PCLs to .NET Standard
4. Choose the `.mpack` file you downloaded in step 1
5. When prompted, select Install

## Limitations

```Currently only C# projects supported.```<br/>
Due to behavior differences `F#` support is currently postponed, however, contributors are welcome!

## Usage

`Mutatio` can convert newly created or existing projects. Please keep in mind that there might be `NuGet` packages that does not support .NET Standard 2.0, in this case you may see `NuGet` related exceptions.

In order to convert a project, right click on it and select `Convert to NET Standard 2.0`.
In order to convert a project, right click on it and select `Convert to NET Standard 2.0`.<br/>For more details please check my [blog](https://smellyc0de.wordpress.com/2018/03/23/automatically-converting-pcl-to-net-standard-2-0-project/).

## Details

Expand Down
Binary file added Versions/Mutatio.Mutatio_1.0.1.mpack
Binary file not shown.

0 comments on commit ca321b4

Please sign in to comment.