mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-06 08:16:19 +00:00
Add path to program directory
This commit is contained in:
@@ -6,179 +6,192 @@ using ICD.Common.Utils.IO;
|
||||
|
||||
namespace ICD.Common.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides util methods for working with file/directory paths.
|
||||
/// </summary>
|
||||
public static class PathUtils
|
||||
{
|
||||
#region Properties
|
||||
/// <summary>
|
||||
/// Provides util methods for working with file/directory paths.
|
||||
/// </summary>
|
||||
public static class PathUtils
|
||||
{
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path to the root directory of the processor.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static string RootPath { get { return IcdDirectory.GetDirectoryRoot("\\"); } }
|
||||
/// <summary>
|
||||
/// Gets the path to the root directory of the processor.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static string RootPath { get { return IcdDirectory.GetDirectoryRoot("\\"); } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path to the NVRAM directory.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static string NvramPath { get { return Join(RootPath, "NVRAM"); } }
|
||||
/// <summary>
|
||||
/// Gets the path to the program directory
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static string ProgramPath
|
||||
{
|
||||
get
|
||||
{
|
||||
string directoryName = string.Format("Program{0}", ProgramUtils.ProgramNumber);
|
||||
return Join(RootPath, directoryName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the absolute path to the configuration directory.
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
[PublicAPI]
|
||||
public static string ProgramConfigPath
|
||||
{
|
||||
get
|
||||
{
|
||||
string directoryName = string.Format("Program{0:D2}Config", ProgramUtils.ProgramNumber);
|
||||
return Join(NvramPath, directoryName);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets the path to the NVRAM directory.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static string NvramPath { get { return Join(RootPath, "NVRAM"); } }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the absolute path to the common configuration directory.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static string CommonConfigPath { get { return Join(NvramPath, "CommonConfig"); } }
|
||||
/// <summary>
|
||||
/// Returns the absolute path to the configuration directory.
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
[PublicAPI]
|
||||
public static string ProgramConfigPath
|
||||
{
|
||||
get
|
||||
{
|
||||
string directoryName = string.Format("Program{0:D2}Config", ProgramUtils.ProgramNumber);
|
||||
return Join(NvramPath, directoryName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the absolute path to the common config library directory.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static string CommonLibPath { get { return Join(CommonConfigPath, "Lib"); } }
|
||||
/// <summary>
|
||||
/// Returns the absolute path to the common configuration directory.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static string CommonConfigPath { get { return Join(NvramPath, "CommonConfig"); } }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the absolute path to the program config library directory.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static string ProgramLibPath { get { return Join(ProgramConfigPath, "Lib"); } }
|
||||
/// <summary>
|
||||
/// Returns the absolute path to the common config library directory.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static string CommonLibPath { get { return Join(CommonConfigPath, "Lib"); } }
|
||||
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// Returns the absolute path to the program config library directory.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static string ProgramLibPath { get { return Join(ProgramConfigPath, "Lib"); } }
|
||||
|
||||
#region Methods
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Creates a path from the given path nodes.
|
||||
/// </summary>
|
||||
/// <param name="items"></param>
|
||||
/// <returns></returns>
|
||||
public static string Join(params string[] items)
|
||||
{
|
||||
return items.Length > 1
|
||||
? items.Skip(1).Aggregate(items.First(), IcdPath.Combine)
|
||||
: items.FirstOrDefault(string.Empty);
|
||||
}
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full path for the given path.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetFullPath(string path)
|
||||
{
|
||||
return Join(IcdDirectory.GetApplicationDirectory(), path);
|
||||
}
|
||||
/// <summary>
|
||||
/// Creates a path from the given path nodes.
|
||||
/// </summary>
|
||||
/// <param name="items"></param>
|
||||
/// <returns></returns>
|
||||
public static string Join(params string[] items)
|
||||
{
|
||||
return items.Length > 1
|
||||
? items.Skip(1).Aggregate(items.First(), IcdPath.Combine)
|
||||
: items.FirstOrDefault(string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the filename while leaving the directory and extension intact.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="newName"></param>
|
||||
/// <returns></returns>
|
||||
public static string ChangeFilenameWithoutExt(string path, string newName)
|
||||
{
|
||||
string dir = IcdPath.GetDirectoryName(path);
|
||||
string ext = IcdPath.GetExtension(path);
|
||||
/// <summary>
|
||||
/// Gets the full path for the given path.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetFullPath(string path)
|
||||
{
|
||||
return Join(IcdDirectory.GetApplicationDirectory(), path);
|
||||
}
|
||||
|
||||
return Join(dir, newName + ext);
|
||||
}
|
||||
/// <summary>
|
||||
/// Replaces the filename while leaving the directory and extension intact.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="newName"></param>
|
||||
/// <returns></returns>
|
||||
public static string ChangeFilenameWithoutExt(string path, string newName)
|
||||
{
|
||||
string dir = IcdPath.GetDirectoryName(path);
|
||||
string ext = IcdPath.GetExtension(path);
|
||||
|
||||
/// <summary>
|
||||
/// Removes the extension from the given path.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetPathWithoutExtension(string path)
|
||||
{
|
||||
string dir = IcdPath.GetDirectoryName(path);
|
||||
string filename = IcdPath.GetFileNameWithoutExtension(path);
|
||||
return Join(dir, newName + ext);
|
||||
}
|
||||
|
||||
return Join(dir, filename);
|
||||
}
|
||||
/// <summary>
|
||||
/// Removes the extension from the given path.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetPathWithoutExtension(string path)
|
||||
{
|
||||
string dir = IcdPath.GetDirectoryName(path);
|
||||
string filename = IcdPath.GetFileNameWithoutExtension(path);
|
||||
|
||||
/// <summary>
|
||||
/// Recurses over the file paths at the given directory.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<string> RecurseFilePaths(string path)
|
||||
{
|
||||
if (!IcdDirectory.Exists(path))
|
||||
return Enumerable.Empty<string>();
|
||||
return Join(dir, filename);
|
||||
}
|
||||
|
||||
return RecursionUtils.BreadthFirstSearch(path, IcdDirectory.GetDirectories)
|
||||
.SelectMany(p => IcdDirectory.GetFiles(p));
|
||||
}
|
||||
/// <summary>
|
||||
/// Recurses over the file paths at the given directory.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<string> RecurseFilePaths(string path)
|
||||
{
|
||||
if (!IcdDirectory.Exists(path))
|
||||
return Enumerable.Empty<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Searches the program config path, common config path, and application path to
|
||||
/// find the first config that exists with the given local path.
|
||||
/// </summary>
|
||||
/// <param name="localPath"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static string GetDefaultConfigPath(params string[] localPath)
|
||||
{
|
||||
string local = Join(localPath);
|
||||
return RecursionUtils.BreadthFirstSearch(path, IcdDirectory.GetDirectories)
|
||||
.SelectMany(p => IcdDirectory.GetFiles(p));
|
||||
}
|
||||
|
||||
// Program slot configuration
|
||||
string programPath = GetProgramConfigPath(localPath);
|
||||
if (PathExists(programPath))
|
||||
return programPath;
|
||||
/// <summary>
|
||||
/// Searches the program config path, common config path, and application path to
|
||||
/// find the first config that exists with the given local path.
|
||||
/// </summary>
|
||||
/// <param name="localPath"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static string GetDefaultConfigPath(params string[] localPath)
|
||||
{
|
||||
string local = Join(localPath);
|
||||
|
||||
// Common program configuration
|
||||
string commonPath = GetCommonConfigPath(local);
|
||||
if (PathExists(commonPath))
|
||||
return commonPath;
|
||||
// Program slot configuration
|
||||
string programPath = GetProgramConfigPath(localPath);
|
||||
if (PathExists(programPath))
|
||||
return programPath;
|
||||
|
||||
return Join(IcdDirectory.GetApplicationDirectory(), local); // Installation defaults
|
||||
}
|
||||
// Common program configuration
|
||||
string commonPath = GetCommonConfigPath(local);
|
||||
if (PathExists(commonPath))
|
||||
return commonPath;
|
||||
|
||||
/// <summary>
|
||||
/// Appends the local path to the common config path.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetCommonConfigPath(params string[] localPath)
|
||||
{
|
||||
string local = Join(localPath);
|
||||
return Join(CommonConfigPath, local);
|
||||
}
|
||||
return Join(IcdDirectory.GetApplicationDirectory(), local); // Installation defaults
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends the local path to the program config path.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetProgramConfigPath(params string[] localPath)
|
||||
{
|
||||
string local = Join(localPath);
|
||||
return Join(ProgramConfigPath, local);
|
||||
}
|
||||
/// <summary>
|
||||
/// Appends the local path to the common config path.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetCommonConfigPath(params string[] localPath)
|
||||
{
|
||||
string local = Join(localPath);
|
||||
return Join(CommonConfigPath, local);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given path exists.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool PathExists(string path)
|
||||
{
|
||||
return IcdFile.Exists(path) || IcdDirectory.Exists(path);
|
||||
}
|
||||
/// <summary>
|
||||
/// Appends the local path to the program config path.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetProgramConfigPath(params string[] localPath)
|
||||
{
|
||||
string local = Join(localPath);
|
||||
return Join(ProgramConfigPath, local);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns true if the given path exists.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool PathExists(string path)
|
||||
{
|
||||
return IcdFile.Exists(path) || IcdDirectory.Exists(path);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user