Implemented new Attributes in ChangeDownload

This commit is contained in:
erri120 2020-04-06 14:57:37 +02:00
parent 3bcab0db51
commit 48332d67f1
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135
3 changed files with 99 additions and 18 deletions

View File

@ -8,19 +8,52 @@ using Wabbajack.Common;
namespace Wabbajack.CLI
{
/// <summary>
/// Abstract class to mark attributes which need validating
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
internal class IsFileAttribute : Attribute { }
internal abstract class AValidateAttribute : Attribute
{
/// <summary>
/// Exit the application if validation failed. Will just log if set to false
/// </summary>
public bool Exit { get; set; }
[AttributeUsage(AttributeTargets.Property)]
internal class IsDirectoryAttribute : Attribute { }
/// <summary>
/// Custom message if validation failed. Use placeholder %1 to insert the value
/// </summary>
public string? CustomMessage { get; set; }
}
/// <summary>
/// Validating if the file exists
/// </summary>
internal class IsFileAttribute : AValidateAttribute { }
/// <summary>
/// Validating if the directory exists
/// </summary>
internal class IsDirectoryAttribute : AValidateAttribute
{
/// <summary>
/// Create the directory if it does not exists
/// </summary>
public bool Create { get; set; }
}
internal static class CLIUtils
{
/// <summary>
/// Validates all Attributes of type <see cref="AValidateAttribute"/>
/// </summary>
/// <param name="verb">The verb to validate</param>
/// <returns></returns>
internal static bool HasValidArguments(AVerb verb)
{
var props = verb.GetType().GetProperties().Where(p =>
{
var hasAttr = p.HasAttribute(typeof(OptionAttribute));
var hasAttr = p.HasAttribute(typeof(OptionAttribute))
&& p.HasAttribute(typeof(AValidateAttribute));
if (!hasAttr)
return false;
@ -32,7 +65,7 @@ namespace Wabbajack.CLI
return false;
var stringValue = (string)value;
return string.IsNullOrWhiteSpace(stringValue);
return !string.IsNullOrWhiteSpace(stringValue);
});
var valid = true;
@ -49,21 +82,68 @@ namespace Wabbajack.CLI
return;
var value = (string)valueObject;
var attribute = (AValidateAttribute)p.GetAttribute(typeof(AValidateAttribute));
var isFile = false;
if (p.HasAttribute(typeof(IsFileAttribute)))
{
isFile = true;
valid = File.Exists(value);
}
if (p.HasAttribute(typeof(IsDirectoryAttribute)))
{
valid = Directory.Exists(value);
var dirAttribute = (IsDirectoryAttribute)attribute;
var exists = Directory.Exists(value);
if (!exists)
{
if (dirAttribute.Create)
{
Log($"Directory {value} does not exist and will be created");
Directory.CreateDirectory(value);
}
else
valid = false;
}
}
if (valid)
return;
var message = string.IsNullOrWhiteSpace(attribute.CustomMessage)
? isFile
? $"The file {value} does not exist!"
: $"The folder {value} does not exist!"
: attribute.CustomMessage.Replace("%1", value);
if (attribute.Exit)
Exit(message, -1);
else
Log(message);
});
return valid;
}
/// <summary>
/// Gets an attribute of a specific type
/// </summary>
/// <param name="member"></param>
/// <param name="attribute"></param>
/// <returns></returns>
internal static Attribute GetAttribute(this MemberInfo member, Type attribute)
{
var attributes = member.GetCustomAttributes(attribute);
return attributes.ElementAt(0);
}
/// <summary>
/// Checks if a <see cref="MemberInfo"/> has a custom attribute
/// </summary>
/// <param name="member"></param>
/// <param name="attribute"></param>
/// <returns></returns>
internal static bool HasAttribute(this MemberInfo member, Type attribute)
{
var attributes = member.GetCustomAttributes(attribute);

View File

@ -0,0 +1,8 @@
{
"profiles": {
"Wabbajack.CLI": {
"commandName": "Project",
"commandLineArgs": "change-download --input=\"S:\\Modding FAST\\Wabbajack\\downloads\" --output=\"S:\\Modding FAST\\test\" --modlist=\"S:\\Modding FAST\\Wabbajack\\profiles\\erri120's Dope Test Modlist\\modlist.txt\""
}
}
}

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
@ -18,12 +17,15 @@ namespace Wabbajack.CLI.Verbs
[Verb("change-download", HelpText = "Move or Copy all used Downloads from a Modlist to another directory")]
public class ChangeDownload : AVerb
{
[IsDirectory(Exit = true, CustomMessage = "Downloads folder %1 does not exist!")]
[Option("input", Required = true, HelpText = "Input folder containing the downloads you want to move")]
public string? Input { get; set; }
[IsDirectory(Create = true)]
[Option("output", Required = true, HelpText = "Output folder the downloads should be transferred to")]
public string? Output { get; set; }
[IsFile(Exit = true, CustomMessage = "Modlist file %1 does not exist!")]
[Option("modlist", Required = true, HelpText = "The Modlist, can either be a .wabbajack or a modlist.txt file")]
public string? Modlist { get; set; }
@ -58,17 +60,8 @@ namespace Wabbajack.CLI.Verbs
protected override async Task<int> Run()
{
if (!File.Exists(Modlist))
return CLIUtils.Exit($"The file {Modlist} does not exist!", -1);
if (!Directory.Exists(Input))
return CLIUtils.Exit($"The input directory {Input} does not exist!", -1);
if (!Directory.Exists(Output))
{
CLIUtils.Log($"The output directory {Output} does not exist, it will be created.");
Directory.CreateDirectory(Output);
}
if (!CLIUtils.HasValidArguments(this))
CLIUtils.Exit("Arguments are not valid!", -1);
if (Modlist != null && (!Modlist.EndsWith(Consts.ModListExtension) && !Modlist.EndsWith("modlist.txt")))
return CLIUtils.Exit($"The file {Modlist} is not a valid modlist file!", -1);