-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OperationUtils.IO for handling working paths
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
1 parent
343ffca
commit d223a5d
Showing
3 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")); | ||
} | ||
} |