Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Balanukhin committed Jan 20, 2022
1 parent 1af9375 commit 633c15d
Show file tree
Hide file tree
Showing 33 changed files with 1,544 additions and 1,560 deletions.
222 changes: 117 additions & 105 deletions FFmpeg.AutoGen.CppSharpUnsafeGenerator/CliOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,118 +2,130 @@
using System.IO;
using CommandLine;

namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator;

/// <summary>
/// Command line options.
/// </summary>
public class CliOptions
{
[Option('n',
"namespace",
Default = "FFmpeg.AutoGen",
HelpText = "The namespace that will contain the generated symbols.")]
public string Namespace { get; set; }

[Option('c',
"class",
Default = "ffmpeg",
HelpText = "The name of the class that contains the FFmpeg unmanaged method calls.")]
public string ClassName { get; set; }

/// <summary>
/// Command line options.
/// See http:https://ybeernet.blogspot.ro/2011/03/techniques-of-calling-unmanaged-code.html.
/// </summary>
public class CliOptions
[Option('f',
"SuppressUnmanagedCodeSecurity",
HelpText = "Add the [SuppressUnmanagedCodeSecurity] attribute to unmanaged method calls " +
"(faster invocation).")]
public bool SuppressUnmanagedCodeSecurity { get; set; }

[Option('i',
"input",
Required = false,
HelpText = "The path to the directory that contains the FFmpeg header and binary files " +
"(must have the default structure).")]
public string FFmpegDir { get; set; }

[Option('h',
"headers",
Required = false,
HelpText = "The path to the directory that contains the FFmpeg header files.")]
public string FFmpegIncludesDir { get; set; }

[Option('b',
"bin",
Required = false,
HelpText = "The path to the directory that contains the FFmpeg binaries.")]
public string FFmpegBinDir { get; set; }

[Option('o',
"output",
Required = false,
HelpText = "The path to the directory where to output the generated files.")]
public string OutputDir { get; set; }

[Option('v',
HelpText = "Print details during execution.")]
public bool Verbose { get; set; }

public static CliOptions ParseArgumentsStrict(string[] args)
{
var result = Parser.Default.ParseArguments<CliOptions>(args);
var options = result.MapResult(x => x, x => new CliOptions());
options.Normalize();
return options;
}

private void Normalize()
{
[Option('n', "namespace", Default = "FFmpeg.AutoGen",
HelpText = "The namespace that will contain the generated symbols.")]
public string Namespace { get; set; }

[Option('c', "class", Default = "ffmpeg",
HelpText = "The name of the class that contains the FFmpeg unmanaged method calls.")]
public string ClassName { get; set; }

/// <summary>
/// See http:https://ybeernet.blogspot.ro/2011/03/techniques-of-calling-unmanaged-code.html.
/// </summary>
[Option('f', "SuppressUnmanagedCodeSecurity",
HelpText = "Add the [SuppressUnmanagedCodeSecurity] attribute to unmanaged method calls " +
"(faster invocation).")]
public bool SuppressUnmanagedCodeSecurity { get; set; }

[Option('i', "input", Required = false,
HelpText = "The path to the directory that contains the FFmpeg header and binary files " +
"(must have the default structure).")]
public string FFmpegDir { get; set; }

[Option('h', "headers", Required = false,
HelpText = "The path to the directory that contains the FFmpeg header files.")]
public string FFmpegIncludesDir { get; set; }

[Option('b', "bin", Required = false,
HelpText = "The path to the directory that contains the FFmpeg binaries.")]
public string FFmpegBinDir { get; set; }

[Option('o', "output", Required = false,
HelpText = "The path to the directory where to output the generated files.")]
public string OutputDir { get; set; }

[Option('v',
HelpText = "Print details during execution.")]
public bool Verbose { get; set; }

public static CliOptions ParseArgumentsStrict(string[] args)
// Support for the original path setup
var solutionDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../../");

if (string.IsNullOrWhiteSpace(FFmpegDir) &&
string.IsNullOrWhiteSpace(FFmpegIncludesDir) &&
string.IsNullOrWhiteSpace(FFmpegBinDir))
FFmpegDir = Path.Combine(solutionDir, "FFmpeg");

if (string.IsNullOrWhiteSpace(OutputDir)) OutputDir = Path.Combine(solutionDir, "FFmpeg.AutoGen/");

// If the FFmpegDir option is specified, it will take precedence
if (!string.IsNullOrWhiteSpace(FFmpegDir))
{
FFmpegIncludesDir = Path.Combine(FFmpegDir, "include");
FFmpegBinDir = Path.Combine(FFmpegDir, "bin/x64");
FFmpegDir = null;
}

// Fail if required options are not specified
if (string.IsNullOrWhiteSpace(FFmpegBinDir))
{
var result = Parser.Default.ParseArguments<CliOptions>(args);
var options = result.MapResult(x => x, x => new CliOptions());
options.Normalize();
return options;
Console.WriteLine("The path to the directory that contains " +
"the FFmpeg binaries is missing (specify it using -b or --bin).");
Environment.Exit(1);
}

private void Normalize()
if (string.IsNullOrWhiteSpace(FFmpegIncludesDir))
{
// Support for the original path setup
string solutionDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../../");

if (string.IsNullOrWhiteSpace(FFmpegDir) &&
string.IsNullOrWhiteSpace(FFmpegIncludesDir) &&
string.IsNullOrWhiteSpace(FFmpegBinDir))
FFmpegDir = Path.Combine(solutionDir, "FFmpeg");

if (string.IsNullOrWhiteSpace(OutputDir)) OutputDir = Path.Combine(solutionDir, "FFmpeg.AutoGen/");

// If the FFmpegDir option is specified, it will take precedence
if (!string.IsNullOrWhiteSpace(FFmpegDir))
{
FFmpegIncludesDir = Path.Combine(FFmpegDir, "include");
FFmpegBinDir = Path.Combine(FFmpegDir, "bin/x64");
FFmpegDir = null;
}

// Fail if required options are not specified
if (string.IsNullOrWhiteSpace(FFmpegBinDir))
{
Console.WriteLine("The path to the directory that contains " +
"the FFmpeg binaries is missing (specify it using -b or --bin).");
Environment.Exit(1);
}

if (string.IsNullOrWhiteSpace(FFmpegIncludesDir))
{
Console.WriteLine("The path to the directory that contains " +
"the FFmpeg headers is missing (specify it using -h or --headers).");
Environment.Exit(1);
}

// Check paths exist
if (!Directory.Exists(FFmpegBinDir))
{
Console.WriteLine("The path to the directory that contains " +
"the FFmpeg binaries does not exist.");
Environment.Exit(1);
}

if (!Directory.Exists(FFmpegIncludesDir))
{
Console.WriteLine("The path to the directory that contains " +
"the FFmpeg headers does not exist.");
Environment.Exit(1);
}

if (!Directory.Exists(OutputDir))
{
Console.WriteLine("The output directory does not exist.");
Environment.Exit(1);
}

// Resolve paths
FFmpegIncludesDir = Path.GetFullPath(FFmpegIncludesDir);
FFmpegBinDir = Path.GetFullPath(FFmpegBinDir);
OutputDir = Path.GetFullPath(OutputDir);
Console.WriteLine("The path to the directory that contains " +
"the FFmpeg headers is missing (specify it using -h or --headers).");
Environment.Exit(1);
}

// Check paths exist
if (!Directory.Exists(FFmpegBinDir))
{
Console.WriteLine("The path to the directory that contains " +
"the FFmpeg binaries does not exist.");
Environment.Exit(1);
}

if (!Directory.Exists(FFmpegIncludesDir))
{
Console.WriteLine("The path to the directory that contains " +
"the FFmpeg headers does not exist.");
Environment.Exit(1);
}

if (!Directory.Exists(OutputDir))
{
Console.WriteLine("The output directory does not exist.");
Environment.Exit(1);
}

// Resolve paths
FFmpegIncludesDir = Path.GetFullPath(FFmpegIncludesDir);
FFmpegBinDir = Path.GetFullPath(FFmpegBinDir);
OutputDir = Path.GetFullPath(OutputDir);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions;

internal class DelegateDefinition : TypeDefinition, ICanGenerateXmlDoc
{
internal class DelegateDefinition : TypeDefinition, ICanGenerateXmlDoc
{
public string FunctionName { get; init; }
public TypeDefinition ReturnType { get; init; }
public FunctionParameter[] Parameters { get; init; }
public string Content { get; set; }
}
public string FunctionName { get; init; }
public TypeDefinition ReturnType { get; init; }
public FunctionParameter[] Parameters { get; init; }
public string Content { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions;

internal class EnumerationDefinition : NamedDefinition, IDefinition
{
internal class EnumerationDefinition : NamedDefinition, IDefinition
{
public EnumerationItem[] Items { get; set; }
}
public EnumerationItem[] Items { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions;

internal class EnumerationItem : ICanGenerateXmlDoc
{
internal class EnumerationItem : ICanGenerateXmlDoc
{
public string Name { get; init; }
public string Value { get; init; }
public string Content { get; set; }
}
public string Name { get; init; }
public string Value { get; init; }
public string Content { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions;

internal class ExportFunctionDefinition : FunctionDefinitionBase
{
internal class ExportFunctionDefinition : FunctionDefinitionBase
{
public string LibraryName { get; set; }
public int LibraryVersion { get; set; }
}
public string LibraryName { get; set; }
public int LibraryVersion { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions;

internal class FixedArrayDefinition : TypeDefinition
{
internal class FixedArrayDefinition : TypeDefinition
{
public TypeDefinition ElementType { get; init; }
public int Size { get; init; }
public bool IsPrimitive { get; init; }
}
public TypeDefinition ElementType { get; init; }
public int Size { get; init; }
public bool IsPrimitive { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions;

internal class FunctionDefinitionBase : IDefinition, ICanGenerateXmlDoc, IObsoletionAware
{
internal class FunctionDefinitionBase : IDefinition, ICanGenerateXmlDoc, IObsoletionAware
{
public TypeDefinition ReturnType { get; set; }
public FunctionParameter[] Parameters { get; set; }
public string ReturnComment { get; set; }
public string Content { get; set; }
public string Name { get; set; }
public Obsoletion Obsoletion { get; set; }
}
public TypeDefinition ReturnType { get; set; }
public FunctionParameter[] Parameters { get; set; }
public string ReturnComment { get; set; }
public string Content { get; set; }
public string Name { get; set; }
public Obsoletion Obsoletion { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions;

internal class FunctionParameter : ICanGenerateXmlDoc
{
internal class FunctionParameter : ICanGenerateXmlDoc
{
public string Name { get; init; }
public TypeDefinition Type { get; init; }
public string Content { get; set; }
}
public string Name { get; init; }
public TypeDefinition Type { get; init; }
public string Content { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions;

internal interface ICanGenerateXmlDoc
{
internal interface ICanGenerateXmlDoc
{
string Content { get; }
}
string Content { get; }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions;

internal interface IDefinition
{
internal interface IDefinition
{
string Name { get; }
}
string Name { get; }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions
namespace FFmpeg.AutoGen.CppSharpUnsafeGenerator.Definitions;

internal interface IObsoletionAware
{
internal interface IObsoletionAware
{
Obsoletion Obsoletion { get; }
}
Obsoletion Obsoletion { get; }
}
Loading

0 comments on commit 633c15d

Please sign in to comment.