Skip to content

Commit

Permalink
Merge pull request #67 from christianhelle/netstandard-common-lib
Browse files Browse the repository at this point in the history
Create .NET Standard Library as a preparation for creating cross platform CLI tool
  • Loading branch information
christianhelle authored Nov 9, 2019
2 parents 4df2128 + e41729d commit f2c303b
Show file tree
Hide file tree
Showing 124 changed files with 911 additions and 1,817 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,4 @@ __pycache__/
# Cake Build
src/tools/
src/.ionide/
src/MigrationBackup/
39 changes: 39 additions & 0 deletions src/ApiClientCodeGen.Core/ApiClientCodeGen.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Commands\**" />
<EmbeddedResource Remove="Commands\**" />
<None Remove="Commands\**" />
</ItemGroup>

<ItemGroup>
<Folder Include="NuGet\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ICSharpCode.CodeConverter" Version="7.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="NSwag.CodeGeneration.CSharp" Version="13.1.3" />
</ItemGroup>

<ItemGroup>
<Compile Update="Resource.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resource.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resource.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading.Tasks;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Converters
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Converters
{
public interface ILanguageConverter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Generators;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core
{
Expand All @@ -20,30 +20,26 @@ public static void InstallAutoRest()
{
npmCommand = Path.Combine(programFiles64, "nodejs\\npm.cmd");
if (!File.Exists(npmCommand))
throw new NotInstalledException("Unable to find NPM. Please install Node.js");
throw new InvalidOperationException("Unable to find NPM. Please install Node.js");
}

ProcessHelper.StartProcess(npmCommand, "install -g autorest");
new ProcessLauncher().Start(npmCommand, "install -g autorest");
Trace.WriteLine("AutoRest installed successfully through NPM");
}

public static string InstallOpenApiGenerator(string path = null)
{
const string md5 = "45F0AEDA24983AC998878C4D8516CF48";
const string url = "http://central.maven.org/maven2/org/openapitools/openapi-generator-cli/4.1.2/openapi-generator-cli-4.1.2.jar";
const string jar = "openapi-generator-cli.jar";

return InstallJarFile(path, jar, md5, url);
}
=> InstallJarFile(
path,
"openapi-generator-cli.jar",
Resource.OpenApiGenerator_MD5,
Resource.OpenApiGenerator_DownloadUrl);

public static string InstallSwaggerCodegenCli(string path = null)
{
const string md5 = "89E1C5F578CC0B7A5D430CDF8210AC44";
const string url = "https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.11/swagger-codegen-cli-3.0.11.jar";
const string jar = "swagger-codegen-cli.jar";

return InstallJarFile(path, jar, md5, url);
}
=> InstallJarFile(
path,
"swagger-codegen-cli.jar",
Resource.SwaggerCodegenCli_MD5,
Resource.SwaggerCodegenCli_DownloadUrl);

private static string InstallJarFile(string path, string jar, string md5, string url)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using NSwag;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Extensions
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Extensions
{
public static class OpenApiDocumentExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Extensions
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Extensions
{
public static class StringExtension
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.IO;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Options.AutoRest;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Options.General;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Options.AutoRest;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Options.General;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Generators.AutoRest
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators.AutoRest
{
public class AutoRestCSharpCodeGenerator : CodeGenerator
{
Expand All @@ -13,8 +12,9 @@ public class AutoRestCSharpCodeGenerator : CodeGenerator
public AutoRestCSharpCodeGenerator(
string swaggerFile,
string defaultNamespace,
IAutoRestOptions options)
: base(swaggerFile, defaultNamespace)
IAutoRestOptions options,
IProcessLauncher processLauncher)
: base(swaggerFile, defaultNamespace, processLauncher)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Linq;
using System.Text;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Generators
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators
{
public static class CSharpFileMerger
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
using System;
using System.IO;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Extensions;
using Microsoft.VisualStudio.Shell.Interop;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Generators
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators
{
public interface ICodeGenerator
{
string GenerateCode(IVsGeneratorProgress pGenerateProgress);
string GenerateCode(IProgressReporter pGenerateProgress);
}

public abstract class CodeGenerator : ICodeGenerator
{
protected readonly string DefaultNamespace;
protected readonly string SwaggerFile;
private readonly IProcessLauncher processLauncher;

protected CodeGenerator(string swaggerFile, string defaultNamespace)
protected CodeGenerator(string swaggerFile, string defaultNamespace, IProcessLauncher processLauncher)
{
SwaggerFile = swaggerFile ?? throw new ArgumentNullException(nameof(swaggerFile));
DefaultNamespace = defaultNamespace ?? throw new ArgumentNullException(nameof(defaultNamespace));
this.processLauncher = processLauncher ?? throw new ArgumentNullException(nameof(processLauncher));
}

public virtual string GenerateCode(IVsGeneratorProgress pGenerateProgress)
public virtual string GenerateCode(IProgressReporter pGenerateProgress)
{
try
{
Expand All @@ -35,7 +35,7 @@ public virtual string GenerateCode(IVsGeneratorProgress pGenerateProgress)
var arguments = GetArguments(outputFile);
pGenerateProgress.Progress(30);

ProcessHelper.StartProcess(command, arguments);
processLauncher.Start(command, arguments);
pGenerateProgress.Progress(80);

return FileHelper.ReadThenDelete(outputFile);
Expand Down
12 changes: 12 additions & 0 deletions src/ApiClientCodeGen.Core/Generators/CodeGeneratorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators
{
public interface ICodeGeneratorFactory
{
ICodeGenerator Create(
string defaultNamespace,
string inputFileContents,
string inputFilePath,
SupportedLanguage language,
SupportedCodeGenerator generator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.IO;
using System.Security.Cryptography;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Generators
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators
{
public static class FileHelper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Extensions;
using Microsoft.VisualStudio.Shell.Interop;
using NSwag.CodeGeneration.CSharp;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Generators.NSwag
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators.NSwag
{
public class NSwagCSharpCodeGenerator : ICodeGenerator
{
Expand All @@ -22,7 +20,7 @@ public NSwagCSharpCodeGenerator(
throw new ArgumentNullException(nameof(generatorSettingsFactory));
}

public string GenerateCode(IVsGeneratorProgress pGenerateProgress)
public string GenerateCode(IProgressReporter pGenerateProgress)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Extensions;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Options.NSwag;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Extensions;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Options.NSwag;
using NSwag;
using NSwag.CodeGeneration.CSharp;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Generators.NSwag
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators.NSwag
{
public interface INSwagCodeGeneratorSettingsFactory
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using NSwag;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators.NSwag
{
public interface IOpenApiDocumentFactory
{
OpenApiDocument GetDocument(string swaggerFile);
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
using System;
using System.Diagnostics;
using System.IO;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Extensions;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Options.General;
using Microsoft.VisualStudio.Shell.Interop;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Options.General;
using Newtonsoft.Json;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Generators.NSwagStudio
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators.NSwagStudio
{
public class NSwagStudioCodeGenerator : ICodeGenerator
{
private readonly string nswagStudioFile;
private readonly CustomPathOptions options;
private readonly IGeneralOptions options;
private readonly IProcessLauncher processLauncher;

public NSwagStudioCodeGenerator(string nswagStudioFile, IGeneralOptions options)
public NSwagStudioCodeGenerator(string nswagStudioFile, IGeneralOptions options, IProcessLauncher processLauncher)
{
this.nswagStudioFile = nswagStudioFile ?? throw new ArgumentNullException(nameof(nswagStudioFile));
this.options = new CustomPathOptions(options ?? throw new ArgumentNullException(nameof(options)));
this.options = options ?? throw new ArgumentNullException(nameof(options));
this.processLauncher = processLauncher ?? throw new ArgumentNullException(nameof(processLauncher));
}

public string GenerateCode(IVsGeneratorProgress pGenerateProgress)
public string GenerateCode(IProgressReporter pGenerateProgress)
{
pGenerateProgress?.Progress(10);

Expand All @@ -30,11 +29,11 @@ public string GenerateCode(IVsGeneratorProgress pGenerateProgress)
Trace.WriteLine(command + " does not exist! Retrying with default NSwag.exe path");
command = PathProvider.GetNSwagPath();
if (!File.Exists(command))
throw new NotInstalledException("NSwag not installed. Please install NSwagStudio");
throw new InvalidOperationException("NSwag not installed. Please install NSwagStudio");
}

TryRemoveSwaggerJsonSpec(nswagStudioFile);
ProcessHelper.StartProcess(command, $"run \"{nswagStudioFile}\"");
processLauncher.Start(command, $"run \"{nswagStudioFile}\"");
pGenerateProgress?.Progress(90);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
using System;
using System.Diagnostics;
using System.IO;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Extensions;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Options.General;
using Microsoft.VisualStudio.Shell.Interop;
using ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Options.General;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Generators.OpenApi
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators.OpenApi
{
public class OpenApiCSharpCodeGenerator : ICodeGenerator
{
private readonly string defaultNamespace;
private readonly JavaPathProvider javaPathProvider;
private readonly CustomPathOptions options;
private readonly IGeneralOptions options;
private readonly IProcessLauncher processLauncher;
private readonly string swaggerFile;

public OpenApiCSharpCodeGenerator(string swaggerFile, string defaultNamespace, IGeneralOptions options)
public OpenApiCSharpCodeGenerator(string swaggerFile, string defaultNamespace, IGeneralOptions options, IProcessLauncher processLauncher)
{
this.swaggerFile = swaggerFile ?? throw new ArgumentNullException(nameof(swaggerFile));
this.defaultNamespace = defaultNamespace ?? throw new ArgumentNullException(nameof(defaultNamespace));
this.options = new CustomPathOptions(options ?? throw new ArgumentNullException(nameof(options)));
javaPathProvider = new JavaPathProvider(options);
this.options = options ?? throw new ArgumentNullException(nameof(options));
this.processLauncher = processLauncher ?? throw new ArgumentNullException(nameof(processLauncher));
javaPathProvider = new JavaPathProvider(options, processLauncher);
}

public string GenerateCode(IVsGeneratorProgress pGenerateProgress)
public string GenerateCode(IProgressReporter pGenerateProgress)
{
try
{
Expand Down Expand Up @@ -54,7 +53,7 @@ public string GenerateCode(IVsGeneratorProgress pGenerateProgress)
$"-DpackageName={defaultNamespace} " +
"--skip-overwrite ";

ProcessHelper.StartProcess(javaPathProvider.GetJavaExePath(), arguments);
processLauncher.Start(javaPathProvider.GetJavaExePath(), arguments);
pGenerateProgress.Progress(80);

return CSharpFileMerger.MergeFilesAndDeleteSource(output);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.Diagnostics;

namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Generators
namespace ChristianHelle.DeveloperTools.CodeGenerators.ApiClient.Core.Generators
{
public static class ProcessHelper
public interface IProcessLauncher
{
public static void StartProcess(string command, string arguments)
void Start(string command, string arguments);
}

public class ProcessLauncher : IProcessLauncher
{
public void Start(string command, string arguments)
{
Trace.WriteLine("Executing:");
Trace.WriteLine($"{command} {arguments}");
Expand Down
Loading

0 comments on commit f2c303b

Please sign in to comment.