Skip to content

Commit

Permalink
Merge pull request #13 from dassjosh/develop
Browse files Browse the repository at this point in the history
V1.0.5
  • Loading branch information
dassjosh authored Oct 18, 2023
2 parents 4e7f9f4 + 5ca48a5 commit d26d9aa
Show file tree
Hide file tree
Showing 36 changed files with 874 additions and 622 deletions.
28 changes: 28 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

[*]

# Microsoft .NET properties
csharp_new_line_before_members_in_object_initializers = false
csharp_preserve_single_line_blocks = true

# ReSharper properties
resharper_blank_lines_after_block_statements = 0
resharper_blank_lines_after_using_list = 1
resharper_blank_lines_around_block_case_section = 1
resharper_csharp_blank_lines_around_field = 0
resharper_csharp_blank_lines_around_invocable = 0
resharper_csharp_blank_lines_around_namespace = 0
resharper_csharp_blank_lines_around_single_line_invocable = 1
resharper_csharp_blank_lines_inside_region = 0
resharper_csharp_max_line_length = 600
resharper_keep_existing_attribute_arrangement = true
resharper_keep_existing_enum_arrangement = false
resharper_max_attribute_length_for_same_line = 70
resharper_place_expr_method_on_single_line = true
resharper_place_expr_property_on_single_line = true
resharper_place_simple_embedded_statement_on_same_line = false
resharper_place_simple_initializer_on_single_line = false
resharper_remove_blank_lines_near_braces_in_code = false
resharper_space_within_single_line_array_initializer_braces = false
resharper_wrap_object_and_collection_initializer_style = chop_always
configure_await_analysis_mode = library
24 changes: 19 additions & 5 deletions src/PluginMerge/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@

namespace PluginMerge.Commands;

public class BaseCommand : ICommand
public abstract class BaseCommand<T> : ICommand
{
[Option('d', "debug", Required = false, HelpText = "Enable debug log output")]
[Option('d', "debug", Required = false, HelpText = "Enable debug log output", Hidden = true)]
public bool Debug { get; set; }

[Option('l', "log", Required = false, HelpText = "Log Level for output")]
public LogLevel? LoggerLevel { get; set; }

public int CloseCode { get; protected set; } = Constants.CloseCodes.NoError;

protected ILogger Logger;
protected ILogger Logger { get; private set; }

public virtual Task Execute()
{
LogHandler.InitLogger(Debug ? LogLevel.Debug : LogLevel.Information);
Logger = this.GetLogger();
LogLevel level = LogLevel.Information;
if (Debug)
{
level = LogLevel.Debug;
}

if (LoggerLevel.HasValue)
{
level = LoggerLevel.Value;
}

LogBuilder.InitLogger(level);
Logger = LogBuilder.GetLogger<T>();
return Task.CompletedTask;
}
}
6 changes: 3 additions & 3 deletions src/PluginMerge/Commands/InitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace PluginMerge.Commands;

[Verb("init", HelpText = "Creates a new merge.json configuration in the current directory")]
public class InitCommand : BaseCommand
public class InitCommand : BaseCommand<InitCommand>
{
[Option('p', "path", Required = false, HelpText = "Path to create the merge.json configuration file in", Default = "./")]
public string FilePath { get; set; } = "./";
Expand All @@ -14,12 +14,12 @@ public class InitCommand : BaseCommand

public override async Task Execute()
{
await base.Execute();
await base.Execute().ConfigureAwait(false);

try
{
string path = Path.Combine(FilePath, FileName).ToFullPath();
await PluginMergeConfigHandler.Instance.Create(path);
await PluginMergeConfigHandler.Instance.Create(path).ConfigureAwait(false);
}
catch (Exception ex)
{
Expand Down
95 changes: 62 additions & 33 deletions src/PluginMerge/Commands/MergeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace PluginMerge.Commands;

[Verb("merge", true, HelpText = "Merges multiple .cs files into a single plugin/framework file.")]
public class MergeCommand : BaseCommand
public class MergeCommand : BaseCommand<MergeCommand>
{
[Option('p', "path", Required = false, HelpText = "Path to the merge.json configuration file", Default = "./merge.yml")]
public string ConfigPath { get; set; } = "./merge.yml";
Expand All @@ -17,22 +17,59 @@ public class MergeCommand : BaseCommand

[Option('o', "output", HelpText = "Additional output paths for the generated code file")]
public IEnumerable<string> OutputPaths { get; set; }

public override async Task Execute()
{
await base.Execute();
await base.Execute().ConfigureAwait(false);

PluginMergeConfig config = await LoadConfig().ConfigureAwait(false);
if (config is null)
{
return;
}

PluginMergeConfig config;
bool success = await RunMerge(config).ConfigureAwait(false);
if (!success)
{
return;
}

await RunCompile(config).ConfigureAwait(false);
}

private async Task<bool> RunMerge(PluginMergeConfig config)
{
if (!Merge)
{
return true;
}

try
{
MergeHandler handler = new(config);
await handler.Run().ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogCritical(ex, "An error occured merging files");
CloseCode = Constants.CloseCodes.MergeFilesError;
return false;
}

return true;
}

private async Task<PluginMergeConfig> LoadConfig()
{
try
{
string configFile = ConfigPath.ToFullPath();
Logger.LogInformation("Loading Plugin Merge Config At {File}", configFile);
config = await PluginMergeConfigHandler.Instance.Load(configFile);
if (config == null)
PluginMergeConfig config = await PluginMergeConfigHandler.Instance.Load(configFile).ConfigureAwait(false);
if (config is null)
{
CloseCode = Constants.CloseCodes.MergeConfigNotFoundError;
return;
return null;
}

string path = Path.GetDirectoryName(configFile);
Expand All @@ -41,48 +78,40 @@ public override async Task Execute()
Directory.SetCurrentDirectory(path);
}

if (OutputPaths != null)
if (OutputPaths is not null)
{
config.Merge.OutputPaths.AddRange(OutputPaths);
}

return config;
}
catch (Exception ex)
{
Logger.LogCritical(ex, "An error occured loading the config file");
CloseCode = Constants.CloseCodes.MergeConfigError;
return;
return null;
}
}

if (Merge)
private async Task RunCompile(PluginMergeConfig config)
{
if (!Compile)
{
try
{
MergeHandler handler = new(config);
await handler.Run();
}
catch (Exception ex)
{
Logger.LogCritical(ex, "An error occured merging files");
CloseCode = Constants.CloseCodes.MergeFilesError;
return;
}
return;
}

if (Compile)
try
{
try
{
CompileHandler compile = new(config.Merge.FinalFiles.FirstOrDefault(), config);
await compile.Run();
}
catch (Exception ex)
{
Logger.LogCritical(ex, "An error compiling merged file");
CloseCode = Constants.CloseCodes.CompileFilesError;
}
CompileHandler compile = new(config.Merge.FinalFiles.FirstOrDefault(), config);
await compile.Run().ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.LogCritical(ex, "An error compiling merged file");
CloseCode = Constants.CloseCodes.CompileFilesError;
}
}

[Usage(ApplicationAlias = "plugin.merge")]
public static IEnumerable<Example> Examples => new List<Example>
{
Expand Down
22 changes: 22 additions & 0 deletions src/PluginMerge/Commands/RenameCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CommandLine;
using PluginMerge.Rename;

namespace PluginMerge.Commands;

[Verb("rename", HelpText = "Renames a framework class name to match the plugin class name")]
public class RenameCommand : BaseCommand<RenameCommand>
{
[Option('f', "file", Required = true, HelpText = "Path to the framework to rename")]
public string FileName { get; set; }

[Option('n', "name", Required = true, HelpText = "Name to change the framework to")]
public string PluginName { get; set; }

public override async Task Execute()
{
await base.Execute().ConfigureAwait(false);

RenameHandler handler = new(FileName, PluginName);
CloseCode = await handler.Run().ConfigureAwait(false);
}
}
13 changes: 6 additions & 7 deletions src/PluginMerge/Compile/CompileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using PluginMerge.Scanner;

namespace PluginMerge.Compile;

Expand All @@ -15,7 +14,7 @@ public class CompileHandler

public CompileHandler(string fileName, PluginMergeConfig config)
{
_logger = this.GetLogger();
_logger = LogBuilder.GetLogger<CompileHandler>();
_fileName = fileName;
_config = config;
_compile = config.Compile;
Expand All @@ -24,24 +23,24 @@ public CompileHandler(string fileName, PluginMergeConfig config)
public async Task Run()
{
Stopwatch sw = Stopwatch.StartNew();
_logger.LogInformation("Starting Merged File Compilation");
_logger.LogInformation("Starting Merged File Compilation Version: {Version}", typeof(Program).Assembly.GetName().Version);

if (!File.Exists(_fileName))
{
_logger.LogError("File does not exist at path: {Path}", _fileName);
return;
}

string code = await File.ReadAllTextAsync(_fileName);
string code = await File.ReadAllTextAsync(_fileName).ConfigureAwait(false);
SyntaxTree tree = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(_config.PlatformSettings.Lang));

FileScanner scanner = new(_compile.AssemblyPaths, "*.dll", _compile.IgnorePaths, _compile.IgnoreFiles);

List<MetadataReference> references = new();
foreach ((string fileName, string _) in scanner.ScanFiles())
foreach (ScannedFile file in scanner.ScanFiles())
{
_logger.LogDebug("Added Assembly Reference: {File}", fileName);
references.Add(MetadataReference.CreateFromFile(fileName));
_logger.LogDebug("Added Assembly Reference: {File}", file.FileName);
references.Add(MetadataReference.CreateFromFile(file.FileName));
}

await using MemoryStream stream = new();
Expand Down
2 changes: 1 addition & 1 deletion src/PluginMerge/Configuration/CodeStyleConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CodeStyleConfig
[YamlMember(Alias = "Write The Relative File Path In Region", Description = "Adds the code file path in a region")]
public bool WriteFileRegion { get; set; } = true;

[JsonPropertyName("Keep Comments Comments")]
[JsonPropertyName("Keep Code Comments")]
[YamlMember(Alias = "Keep Code Comments", Description = "Adds the code file path in a region")]
public bool KeepComments { get; set; } = true;

Expand Down
14 changes: 8 additions & 6 deletions src/PluginMerge/Configuration/MergeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public class MergeConfig
[JsonPropertyName("Plugin Name")]
[YamlMember(Alias = "Plugin Name", Description = "Outputted plugin name")]
public string PluginName { get; set; }

[JsonPropertyName("Plugin Base Class")]
[YamlMember(Alias = "Plugin Base Class", Description = "Outputted plugin base class")]
public string BaseClass { get; set; }


[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonPropertyName("Creator Mode")]
[YamlMember(Alias = "Creator Mode", Description = "Which type of file to output (Plugin, Framework, or MergeFramework)")]
public CreatorMode CreatorMode { get; set; }

[JsonPropertyName("Namespace Override")]
[YamlMember(Alias = "Namespace Override", Description = "Overrides the default namespace")]
public string NamespaceOverride { get; set; }

[JsonPropertyName("Plugin Input Paths")]
[YamlMember(Alias = "Plugin Input Paths", Description = "Paths to use when reading in source code relative to the merge config")]
Expand Down Expand Up @@ -54,10 +54,12 @@ public class MergeConfig
[YamlIgnore]
public IEnumerable<string> FinalFiles => OutputPaths.Select(p => Path.Combine(p, $"{PluginName}.cs").ToFullPath());

private bool ShouldSerializeNamespaceOverride() => CreatorMode == CreatorMode.MergeFramework;

public void Initialize()
{
PluginName ??= "MyPluginName";
BaseClass ??= "CovalencePlugin";
NamespaceOverride ??= string.Empty;
InputPaths ??= new List<string> { "./" };
OutputPaths ??= new List<string> {"./build"};
Defines ??= new List<string> { "DEBUG" };
Expand Down
10 changes: 10 additions & 0 deletions src/PluginMerge/Configuration/PlatformSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,14 @@ public static PlatformSettings GetPlatformSettings(Platform platform)
_ => null
};
}

public static PlatformSettings GetCustomPlatformSettings(Platform platform, string @namespace)
{
return platform switch
{
Platform.Oxide => new PlatformSettings(@namespace, Oxide.Lang),
Platform.uMod => new PlatformSettings(@namespace, uMod.Lang),
_ => null
};
}
}
6 changes: 3 additions & 3 deletions src/PluginMerge/Configuration/PluginMergeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public class PluginMergeConfig

[JsonIgnore]
[YamlIgnore]
public PlatformSettings PlatformSettings;
public PlatformSettings PlatformSettings { get; set; }

public void Initialize()
{
PlatformSettings = PlatformSettings.GetPlatformSettings(Platform);

Merge ??= new MergeConfig();
Merge.Initialize();

Compile ??= new CompileConfig();
Compile.Initialize();

PlatformSettings = string.IsNullOrEmpty(Merge.NamespaceOverride) ? PlatformSettings.GetPlatformSettings(Platform) : PlatformSettings.GetCustomPlatformSettings(Platform, Merge.NamespaceOverride);
}
}
Loading

0 comments on commit d26d9aa

Please sign in to comment.