Skip to content

Commit d223a5d

Browse files
committed
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
1 parent 343ffca commit d223a5d

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

CSharpScriptOperations/CSharpScriptOperations.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1111
<Description>Library for streamlining the process of executing custom operations via the console.</Description>
1212
<Authors>Stijn Raeymaekers</Authors>
13-
<Version>1.4.1</Version>
13+
<Version>1.4.2</Version>
1414
<LangVersion>latest</LangVersion>
1515
<PackageReadmeFile>README.md</PackageReadmeFile>
1616
</PropertyGroup>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.IO;
2+
3+
namespace CSharpScriptOperations;
4+
5+
public static partial class OperationUtils
6+
{
7+
public static class IO
8+
{
9+
static string _workingDirBase = AppDomain.CurrentDomain.BaseDirectory;
10+
11+
/// <summary>
12+
/// Change the working directory base throughout the application.
13+
/// Be default, this is the directory of the executable.
14+
/// </summary>
15+
/// <param name="path">Absolute path</param>
16+
public static void SetWorkingDirBase(params string[] path)
17+
{
18+
_workingDirBase = Path.Combine(path);
19+
}
20+
21+
/// <summary>
22+
/// Prepare the directory tree for a path and return whether the file or directory exists.
23+
/// </summary>
24+
/// <param name="pathIsDir"></param>
25+
/// <param name="makeMissingDir"></param>
26+
/// <returns>True when the path exists as a file or directory based on pathIsDir</returns>
27+
public static bool EnsureValidPath(string path, bool pathIsDir, bool makeMissingDir = true)
28+
{
29+
string dir = pathIsDir ? path : Path.GetDirectoryName(path);
30+
if (makeMissingDir && !Directory.Exists(dir))
31+
Directory.CreateDirectory(dir);
32+
33+
// Check if the file already exists
34+
return pathIsDir ? Directory.Exists(path) : File.Exists(path);
35+
}
36+
37+
/// <summary>
38+
/// Returns a path relative to the working directory.
39+
/// Working directory can be changed with SetWorkingDirBase() on startup.
40+
/// </summary>
41+
/// <param name="parts"></param>
42+
/// <returns></returns>
43+
public static string GetWorkingPath(params string[] parts)
44+
{
45+
string[] finalParts = new string[parts.Length + 1];
46+
finalParts[0] = _workingDirBase;
47+
for (int i = 0; i < parts.Length; i++)
48+
finalParts[i + 1] = parts[i];
49+
return Path.Combine(finalParts);
50+
}
51+
}
52+
53+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using CSharpScriptOperations;
2+
3+
namespace DemoApp.Operations;
4+
5+
[OperationDescription("OperationUtils.IO Demo", 6)]
6+
internal class OperationUtilsIoDemo : IOperation
7+
{
8+
public async Task RunAsync()
9+
{
10+
// Change working directory base
11+
// OperationUtils.IO.SetWorkingDirBase("C:", "Users", "Public", "Documents", "CSharpScriptOperations");
12+
13+
// Try out GetWorkingPath()
14+
Console.WriteLine("Enter a relative path to get the absolute path");
15+
string relativePathInput = Console.ReadLine();
16+
string finalPath = OperationUtils.IO.GetWorkingPath(relativePathInput);
17+
Console.WriteLine("Working path: " + finalPath);
18+
19+
// Try out EnsureValidPath()
20+
bool isDir = UserInput.PoseBoolQuestion("Is the path a directory?");
21+
bool makeMissingDir = UserInput.PoseBoolQuestion("Make missing directories?");
22+
bool pathExists = OperationUtils.IO.EnsureValidPath(finalPath, isDir, makeMissingDir);
23+
Console.WriteLine("Directory tree was prepared if needed and path " + (pathExists ? "exists." : "does not exist."));
24+
}
25+
}

0 commit comments

Comments
 (0)