Skip to content

Commit

Permalink
Add OperationUtils.IO for handling working paths
Browse files Browse the repository at this point in the history
The idea is essentially to add shortcut functions which tend to be common for projects using this library, so why not embed them.
Added a demo to showcase the current implementation
  • Loading branch information
NotCoffee418 committed Jun 24, 2023
1 parent 343ffca commit d223a5d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CSharpScriptOperations/CSharpScriptOperations.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>Library for streamlining the process of executing custom operations via the console.</Description>
<Authors>Stijn Raeymaekers</Authors>
<Version>1.4.1</Version>
<Version>1.4.2</Version>
<LangVersion>latest</LangVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down
53 changes: 53 additions & 0 deletions CSharpScriptOperations/OperationUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.IO;

namespace CSharpScriptOperations;

public static partial class OperationUtils
{
public static class IO
{
static string _workingDirBase = AppDomain.CurrentDomain.BaseDirectory;

/// <summary>
/// Change the working directory base throughout the application.
/// Be default, this is the directory of the executable.
/// </summary>
/// <param name="path">Absolute path</param>
public static void SetWorkingDirBase(params string[] path)
{
_workingDirBase = Path.Combine(path);
}

/// <summary>
/// Prepare the directory tree for a path and return whether the file or directory exists.
/// </summary>
/// <param name="pathIsDir"></param>
/// <param name="makeMissingDir"></param>
/// <returns>True when the path exists as a file or directory based on pathIsDir</returns>
public static bool EnsureValidPath(string path, bool pathIsDir, bool makeMissingDir = true)
{
string dir = pathIsDir ? path : Path.GetDirectoryName(path);
if (makeMissingDir && !Directory.Exists(dir))
Directory.CreateDirectory(dir);

// Check if the file already exists
return pathIsDir ? Directory.Exists(path) : File.Exists(path);
}

/// <summary>
/// Returns a path relative to the working directory.
/// Working directory can be changed with SetWorkingDirBase() on startup.
/// </summary>
/// <param name="parts"></param>
/// <returns></returns>
public static string GetWorkingPath(params string[] parts)
{
string[] finalParts = new string[parts.Length + 1];
finalParts[0] = _workingDirBase;
for (int i = 0; i < parts.Length; i++)
finalParts[i + 1] = parts[i];
return Path.Combine(finalParts);
}
}

}
25 changes: 25 additions & 0 deletions DemoApp/Operations/OperationUtilsIoDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CSharpScriptOperations;

namespace DemoApp.Operations;

[OperationDescription("OperationUtils.IO Demo", 6)]
internal class OperationUtilsIoDemo : IOperation
{
public async Task RunAsync()
{
// Change working directory base
// OperationUtils.IO.SetWorkingDirBase("C:", "Users", "Public", "Documents", "CSharpScriptOperations");

// Try out GetWorkingPath()
Console.WriteLine("Enter a relative path to get the absolute path");
string relativePathInput = Console.ReadLine();
string finalPath = OperationUtils.IO.GetWorkingPath(relativePathInput);
Console.WriteLine("Working path: " + finalPath);

// Try out EnsureValidPath()
bool isDir = UserInput.PoseBoolQuestion("Is the path a directory?");
bool makeMissingDir = UserInput.PoseBoolQuestion("Make missing directories?");
bool pathExists = OperationUtils.IO.EnsureValidPath(finalPath, isDir, makeMissingDir);
Console.WriteLine("Directory tree was prepared if needed and path " + (pathExists ? "exists." : "does not exist."));
}
}

0 comments on commit d223a5d

Please sign in to comment.