2022-02-11 21:39:34 +00:00
|
|
|
|
using CommandLine;
|
2022-01-25 20:57:22 +00:00
|
|
|
|
using Image.Files;
|
2022-02-12 19:45:59 +00:00
|
|
|
|
using Image.Tasks;
|
2022-01-22 17:06:10 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-01-25 20:27:39 +00:00
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
2022-01-22 17:06:10 +00:00
|
|
|
|
|
2022-01-23 13:12:17 +00:00
|
|
|
|
// ReSharper disable MemberCanBePrivate.Global
|
|
|
|
|
// ReSharper disable ClassNeverInstantiated.Global
|
|
|
|
|
// ReSharper disable UnusedAutoPropertyAccessor.Global
|
|
|
|
|
|
2022-01-22 17:06:10 +00:00
|
|
|
|
namespace ConsoleInterface
|
|
|
|
|
{
|
2022-02-11 21:39:34 +00:00
|
|
|
|
public static class Program
|
2022-01-22 17:06:10 +00:00
|
|
|
|
{
|
2022-02-11 21:39:34 +00:00
|
|
|
|
private static ILoggerFactory _loggerFactory;
|
|
|
|
|
public static readonly ILogger Logger = NullLogger.Instance;
|
2022-01-25 20:27:39 +00:00
|
|
|
|
|
2022-01-23 17:30:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The console interface for the project and the main entrypoint.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args">Command line provided args.</param>
|
2022-01-27 21:22:10 +00:00
|
|
|
|
// ReSharper disable once UnusedMember.Local
|
2022-02-11 21:39:34 +00:00
|
|
|
|
public static void Main(string[] args)
|
2022-01-23 13:12:17 +00:00
|
|
|
|
{
|
|
|
|
|
Parser.Default.ParseArguments<Options>(args).WithParsed(RunOptions);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-23 17:30:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// RunOptions will be called after the command-line arguments were successfully parsed.
|
|
|
|
|
/// </summary>
|
2022-01-23 13:12:17 +00:00
|
|
|
|
private static void RunOptions(Options options)
|
2022-01-22 17:06:10 +00:00
|
|
|
|
{
|
2022-02-11 21:39:34 +00:00
|
|
|
|
_loggerFactory = LoggerFactory.Create(b => b.AddConsole());
|
|
|
|
|
TaskExecutor.Logger = _loggerFactory.CreateLogger(nameof(TaskExecutor));
|
|
|
|
|
LocalSystemFilesRetriever.Logger = _loggerFactory.CreateLogger(nameof(LocalSystemFilesRetriever));
|
2022-01-25 20:57:22 +00:00
|
|
|
|
KeepFilenameFormatter.Logger =
|
2022-02-11 21:39:34 +00:00
|
|
|
|
_loggerFactory.CreateLogger(nameof(KeepFilenameFormatter));
|
2022-01-23 13:12:17 +00:00
|
|
|
|
|
2022-01-25 20:27:39 +00:00
|
|
|
|
CreateDestinationDirectory(options.DestinationDirectory);
|
2022-01-25 20:57:22 +00:00
|
|
|
|
var outputFormatter = KeepFilenameFormatter.Create(options.DestinationDirectory);
|
2022-01-23 13:12:17 +00:00
|
|
|
|
var executor = TaskExecutor.Create(new TaskExecutorOptions
|
2022-01-22 17:06:10 +00:00
|
|
|
|
{
|
2022-01-23 13:12:17 +00:00
|
|
|
|
EnableCompression = options.CompressFiles is true,
|
2022-01-25 20:57:22 +00:00
|
|
|
|
FileOutputFormatter = outputFormatter
|
2022-01-23 13:12:17 +00:00
|
|
|
|
});
|
2022-01-23 17:30:05 +00:00
|
|
|
|
var filesRetriever = LocalSystemFilesRetriever.Create();
|
2022-01-23 13:12:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
executor.ParallelCleanImages(filesRetriever.GetFilenamesFromPath(options.SourceDirectory));
|
|
|
|
|
}
|
2022-02-11 21:39:34 +00:00
|
|
|
|
|
2022-01-25 20:27:39 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the directory if it doesn't exist.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="destinationDirectory">The destination directory.</param>
|
|
|
|
|
private static void CreateDestinationDirectory(string destinationDirectory)
|
|
|
|
|
{
|
2022-02-11 21:39:34 +00:00
|
|
|
|
if (FileSystemHelpers.Logger == null)
|
|
|
|
|
{
|
|
|
|
|
FileSystemHelpers.Logger = _loggerFactory.CreateLogger(nameof(FileSystemHelpers));
|
|
|
|
|
}
|
|
|
|
|
FileSystemHelpers.CreateDestinationDirectory(destinationDirectory);
|
2022-01-25 20:27:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-23 17:30:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Options is a class defining command line options supported by this program.
|
|
|
|
|
/// </summary>
|
2022-01-23 13:12:17 +00:00
|
|
|
|
public class Options
|
|
|
|
|
{
|
2022-01-23 17:30:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// CompressFiles indicates whether files should be compressed after being cleaned.
|
|
|
|
|
/// </summary>
|
2022-01-23 13:12:17 +00:00
|
|
|
|
[Option('c', "compress", Required = false, HelpText = "Compress images after cleaning.", Default = true)]
|
|
|
|
|
public bool? CompressFiles { get; set; }
|
|
|
|
|
|
2022-01-23 17:30:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// DestinationDirectory represents the destination directory for the cleaned images.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Option('d', "dest", Required = false, HelpText = "The destination directory for the cleaned images.",
|
|
|
|
|
Default = "./cleaned")]
|
2022-01-23 13:12:17 +00:00
|
|
|
|
public string DestinationDirectory { get; set; }
|
|
|
|
|
|
2022-01-23 17:30:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// SourceDirectory represents the source directory of images.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Value(0, MetaName = "source", HelpText = "The source directory of images.", Default = ".")]
|
2022-01-23 13:12:17 +00:00
|
|
|
|
public string SourceDirectory { get; set; }
|
2022-01-22 17:06:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|