123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using ArcToolkitCLI.Commands.Options;
- using ArcToolkitCLI.Util;
- using CommandLine;
- namespace ArcToolkitCLI.Commands.Converters
- {
- public class BaseConverter : IInputOptions, IOutputOptions
- {
- protected Dictionary<string, Func<string, int>> Converters { get; set; }
- protected BaseConverter() { }
-
- public int Run()
- {
- var verb = GetType().GetCustomAttributes(typeof(VerbAttribute), true).Cast<VerbAttribute>().FirstOrDefault();
-
- if(verb == null)
- throw new NotImplementedException("The converter is not properly implemented yet, sorry!");
- var convertCommand = verb.Name;
-
- var files = Glob.EnumerateFiles(Input).ToArray();
- Directory.CreateDirectory(Output);
- if (files.Length == 0)
- {
- Console.WriteLine($"No files specified. Run `convert help {convertCommand}` for help.");
- return 0;
- }
- foreach (var file in files)
- {
- if (Converters.TryGetValue(file.Extension.ToLowerInvariant(), out var converter))
- {
- var result = 0;
- if ((result = converter(file.FullName)) != 0)
- return result;
- }
- else
- Console.WriteLine($"File {file.FullName} is none of the supported file formats ({string.Join(", ", Converters.Keys)}). Skipping...");
- }
- return 0;
- }
- [Value(0, HelpText = "Input files to convert")]
- public IEnumerable<string> Input { get; set; }
- public string Output { get; set; }
- }
- }
|