Create and use ParentablePath

This commit is contained in:
Matt Nadareski
2020-07-26 23:39:33 -07:00
parent 3edd40b238
commit 5838c6f5c3
8 changed files with 135 additions and 90 deletions

View File

@@ -62,21 +62,24 @@ namespace SabreTools.Library.Tools
/// Retrieve a list of just directories from inputs
/// </summary>
/// <param name="inputs">List of strings representing directories and files</param>
/// <param name="appendparent">True if the parent name should be appended after the special character "¬", false otherwise (default)</param>
/// <param name="appendparent">True if the parent name should be included in the ParentablePath, false otherwise (default)</param>
/// <returns>List of strings representing just directories from the inputs</returns>
public static List<string> GetDirectoriesOnly(List<string> inputs, bool appendparent = false)
public static List<ParentablePath> GetDirectoriesOnly(List<string> inputs, bool appendparent = false)
{
List<string> outputs = new List<string>();
List<ParentablePath> outputs = new List<ParentablePath>();
foreach (string input in inputs)
{
if (Directory.Exists(input))
{
// Get the parent path in case of appending
string parentPath = Path.GetFullPath(input);
List<string> directories = GetDirectoriesOrdered(input);
foreach (string dir in directories)
{
try
{
outputs.Add(Path.GetFullPath(dir) + (appendparent ? $"¬{Path.GetFullPath(input)}" : string.Empty));
outputs.Add(new ParentablePath(Path.GetFullPath(dir), appendparent ? parentPath : string.Empty));
}
catch (PathTooLongException)
{
@@ -130,13 +133,16 @@ namespace SabreTools.Library.Tools
/// Retrieve a list of just files from inputs
/// </summary>
/// <param name="inputs">List of strings representing directories and files</param>
/// <param name="appendparent">True if the parent name should be appended after the special character "¬", false otherwise (default)</param>
/// <param name="appendparent">True if the parent name should be be included in the ParentablePath, false otherwise (default)</param>
/// <returns>List of strings representing just files from the inputs</returns>
public static List<string> GetFilesOnly(List<string> inputs, bool appendparent = false)
public static List<ParentablePath> GetFilesOnly(List<string> inputs, bool appendparent = false)
{
List<string> outputs = new List<string>();
List<ParentablePath> outputs = new List<ParentablePath>();
foreach (string input in inputs)
{
// Get the parent path in case of appending
string parentPath = Path.GetFullPath(input);
if (Directory.Exists(input))
{
List<string> files = GetFilesOrdered(input);
@@ -144,7 +150,7 @@ namespace SabreTools.Library.Tools
{
try
{
outputs.Add(Path.GetFullPath(file) + (appendparent ? $"¬{Path.GetFullPath(input)}" : string.Empty));
outputs.Add(new ParentablePath(Path.GetFullPath(file), appendparent ? parentPath : string.Empty));
}
catch (PathTooLongException)
{
@@ -160,7 +166,7 @@ namespace SabreTools.Library.Tools
{
try
{
outputs.Add(Path.GetFullPath(input) + (appendparent ? $"¬{Path.GetFullPath(input)}" : string.Empty));
outputs.Add(new ParentablePath(Path.GetFullPath(input), appendparent ? parentPath : string.Empty));
}
catch (PathTooLongException)
{

View File

@@ -0,0 +1,17 @@
namespace SabreTools.Library.Tools
{
/// <summary>
/// A path that optionally contains a parent root
/// </summary>
public class ParentablePath
{
public string CurrentPath { get; set; }
public string ParentPath { get; set; }
public ParentablePath(string currentPath, string parentPath = null)
{
CurrentPath = currentPath;
ParentPath = parentPath;
}
}
}

View File

@@ -40,36 +40,32 @@ namespace SabreTools.Library.Tools
/// <param name="path">Input combined path to use</param>
/// <param name="sanitize">True if path separators should be converted to '-', false otherwise</param>
/// <returns>Subpath for the file</returns>
public static string GetNormalizedFileName(string path, bool sanitize)
public static string GetNormalizedFileName(ParentablePath path, bool sanitize)
{
// Check that we have a combined path first
if (!path.Contains("¬"))
if (string.IsNullOrWhiteSpace(path.ParentPath))
{
string filename = Path.GetFileName(path);
string filename = Path.GetFileName(path.CurrentPath);
if (sanitize)
filename = filename.Replace(Path.DirectorySeparatorChar, '-').Replace(Path.AltDirectorySeparatorChar, '-');
return filename;
}
// First separate out the parts
string child = path.Split('¬')[0];
string parent = path.Split('¬')[1];
// If the parts are the same, return the filename from the first part
if (string.Equals(child, parent, StringComparison.Ordinal))
if (string.Equals(path.CurrentPath, path.ParentPath, StringComparison.Ordinal))
{
string filename = Path.GetFileName(child);
string filename = Path.GetFileName(path.CurrentPath);
if (sanitize)
filename = filename.Replace(Path.DirectorySeparatorChar, '-').Replace(Path.AltDirectorySeparatorChar, '-');
return filename;
}
// Otherwise, remove the parent from the child and return the remainder
// Otherwise, remove the path.ParentPath from the path.CurrentPath and return the remainder
else
{
string filename = child.Remove(0, parent.Length + 1);
string filename = path.CurrentPath.Remove(0, path.ParentPath.Length + 1);
if (sanitize)
filename = filename.Replace(Path.DirectorySeparatorChar, '-').Replace(Path.AltDirectorySeparatorChar, '-');
@@ -84,36 +80,34 @@ namespace SabreTools.Library.Tools
/// <param name="inputpath">Input path to create output for</param>
/// <param name="inplace">True if the output file should go to the same input folder, false otherwise</param>
/// <returns>Complete output path</returns>
public static string GetOutputPath(string outDir, string inputpath, bool inplace)
public static string GetOutputPath(string outDir, ParentablePath inputPath, bool inplace)
{
// First, we need to ensure the output directory
outDir = DirectoryExtensions.Ensure(outDir);
// Check if we have a split path or not
bool splitpath = inputpath.Contains("¬");
bool splitpath = !string.IsNullOrWhiteSpace(inputPath.ParentPath);
// If we have a split path, we need to treat the input separately
if (splitpath)
{
string[] split = inputpath.Split('¬');
// If we have an inplace output, use the directory name from the input path
if (inplace)
{
outDir = Path.GetDirectoryName(split[0]);
outDir = Path.GetDirectoryName(inputPath.CurrentPath);
}
// TODO: Should this be the default? Always create a subfolder if a folder is found?
// If we are processing a path that is coming from a directory and we are outputting to the current directory, we want to get the subfolder to write to
else if (split[0].Length != split[1].Length && outDir == Environment.CurrentDirectory)
else if (inputPath.CurrentPath.Length != inputPath.ParentPath.Length && outDir == Environment.CurrentDirectory)
{
outDir = Path.GetDirectoryName(Path.Combine(outDir, split[0].Remove(0, Path.GetDirectoryName(split[1]).Length + 1)));
outDir = Path.GetDirectoryName(Path.Combine(outDir, inputPath.CurrentPath.Remove(0, Path.GetDirectoryName(inputPath.ParentPath).Length + 1)));
}
// If we are processing a path that is coming from a directory, we want to get the subfolder to write to
else if (split[0].Length != split[1].Length)
else if (inputPath.CurrentPath.Length != inputPath.ParentPath.Length)
{
outDir = Path.GetDirectoryName(Path.Combine(outDir, split[0].Remove(0, split[1].Length + 1)));
outDir = Path.GetDirectoryName(Path.Combine(outDir, inputPath.CurrentPath.Remove(0, inputPath.ParentPath.Length + 1)));
}
// If we are processing a single file from the root of a directory, we just use the output directory
@@ -128,7 +122,7 @@ namespace SabreTools.Library.Tools
// If we have an inplace output, use the directory name from the input path
if (inplace)
{
outDir = Path.GetDirectoryName(inputpath);
outDir = Path.GetDirectoryName(inputPath.CurrentPath);
}
// Otherwise, just use the supplied output directory