using System.Collections.Generic;
using System.IO;
using CommandLine;
using Image.Files;
using Image.Output;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
namespace ConsoleInterface
{
internal static class Program
{
private static readonly ILogger Logger = NullLogger.Instance;
///
/// The console interface for the project and the main entrypoint.
///
/// Command line provided args.
// ReSharper disable once UnusedMember.Local
private static void Main(IEnumerable args)
{
Parser.Default.ParseArguments(args).WithParsed(RunOptions);
}
///
/// RunOptions will be called after the command-line arguments were successfully parsed.
///
private static void RunOptions(Options options)
{
var loggerFactory = LoggerFactory.Create(b => b.AddConsole());
TaskExecutor.Logger = loggerFactory.CreateLogger(nameof(TaskExecutor));
LocalSystemFilesRetriever.Logger = loggerFactory.CreateLogger(nameof(LocalSystemFilesRetriever));
KeepFilenameFormatter.Logger =
loggerFactory.CreateLogger(nameof(KeepFilenameFormatter));
CreateDestinationDirectory(options.DestinationDirectory);
var outputFormatter = KeepFilenameFormatter.Create(options.DestinationDirectory);
var executor = TaskExecutor.Create(new TaskExecutorOptions
{
EnableCompression = options.CompressFiles is true,
FileOutputFormatter = outputFormatter
});
var filesRetriever = LocalSystemFilesRetriever.Create();
executor.ParallelCleanImages(filesRetriever.GetFilenamesFromPath(options.SourceDirectory));
}
///
/// Creates the directory if it doesn't exist.
///
/// The destination directory.
private static void CreateDestinationDirectory(string destinationDirectory)
{
if (Directory.Exists(destinationDirectory)) return;
Logger.LogWarning("Output directory does not exist. Creating.");
Directory.CreateDirectory(destinationDirectory);
}
///
/// Options is a class defining command line options supported by this program.
///
public class Options
{
///
/// CompressFiles indicates whether files should be compressed after being cleaned.
///
[Option('c', "compress", Required = false, HelpText = "Compress images after cleaning.", Default = true)]
public bool? CompressFiles { get; set; }
///
/// DestinationDirectory represents the destination directory for the cleaned images.
///
[Option('d', "dest", Required = false, HelpText = "The destination directory for the cleaned images.",
Default = "./cleaned")]
public string DestinationDirectory { get; set; }
///
/// SourceDirectory represents the source directory of images.
///
[Value(0, MetaName = "source", HelpText = "The source directory of images.", Default = ".")]
public string SourceDirectory { get; set; }
}
}
}