using System.Collections.Generic; using System.Linq; using ICD.Common.Properties; using ICD.Common.Utils.Extensions; using ICD.Common.Utils.IO; namespace ICD.Common.Utils { /// /// Provides util methods for working with file/directory paths. /// public static class PathUtils { #region Properties /// /// Gets the path to the root directory of the processor. /// [PublicAPI] public static string RootPath { get { if (IcdEnvironment.RuntimeEnvironment == IcdEnvironment.eRuntimeEnvironment.SimplSharpProMono) return IcdDirectory.GetApplicationRootDirectory(); return IcdDirectory.GetDirectoryRoot(IcdPath.DirectorySeparatorChar.ToString()); } } /// /// Gets the path to the program directory /// [PublicAPI] public static string ProgramPath { get { return IcdDirectory.GetApplicationDirectory(); } } /// /// Gets the path to the root config directory, /// which contains common and program-specific config directories. /// [PublicAPI] public static string RootConfigPath { get { #if SIMPLSHARP return Join(RootPath, "User"); #elif LINUX return Join(RootPath, "opt", "ICD.Connect"); #else return Join(RootPath, "ProgramData", "ICD.Connect"); #endif } } /// /// Returns the absolute path to the configuration directory. /// /// [PublicAPI] public static string ProgramConfigPath { get { if (IcdEnvironment.RuntimeEnvironment == IcdEnvironment.eRuntimeEnvironment.SimplSharpProMono) return Join(RootConfigPath, "ProgramConfig"); string directoryName = string.Format("Program{0:D2}Config", ProgramUtils.ProgramNumber); return Join(RootConfigPath, directoryName); } } /// /// Returns the absolute path to the common configuration directory. /// [PublicAPI] public static string CommonConfigPath { get { return Join(RootConfigPath, "CommonConfig"); } } /// /// Returns the absolute path to the common config library directory. /// [PublicAPI] public static string CommonLibPath { get { return Join(CommonConfigPath, "Lib"); } } /// /// Returns the absolute path to the program config library directory. /// [PublicAPI] public static string ProgramLibPath { get { return Join(ProgramConfigPath, "Lib"); } } #endregion #region Methods /// /// Creates a path from the given path nodes. /// /// /// public static string Join(params string[] items) { return items.Length > 1 ? items.Skip(1).Aggregate(items.First(), IcdPath.Combine) : items.FirstOrDefault(string.Empty); } /// /// Gets the full path for the given path. /// /// /// public static string GetFullPath(string path) { return Join(IcdDirectory.GetApplicationDirectory(), path); } /// /// Replaces the filename while leaving the directory and extension intact. /// /// /// /// public static string ChangeFilenameWithoutExt(string path, string newName) { string dir = IcdPath.GetDirectoryName(path); string ext = IcdPath.GetExtension(path); return Join(dir, newName + ext); } /// /// Removes the extension from the given path. /// /// /// public static string GetPathWithoutExtension(string path) { string dir = IcdPath.GetDirectoryName(path); string filename = IcdPath.GetFileNameWithoutExtension(path); return Join(dir, filename); } /// /// Recurses over the file paths at the given directory. /// /// /// public static IEnumerable RecurseFilePaths(string path) { if (!IcdDirectory.Exists(path)) return Enumerable.Empty(); return RecursionUtils.BreadthFirstSearch(path, IcdDirectory.GetDirectories) .SelectMany(p => IcdDirectory.GetFiles(p)); } /// /// Searches the program config path, common config path, and application path to /// find the first config that exists with the given local path. /// /// /// [PublicAPI] public static string GetDefaultConfigPath(params string[] localPath) { string local = Join(localPath); // Program slot configuration string programPath = GetProgramConfigPath(localPath); if (PathExists(programPath)) return programPath; // Common program configuration string commonPath = GetCommonConfigPath(local); if (PathExists(commonPath)) return commonPath; return Join(IcdDirectory.GetApplicationDirectory(), local); // Installation defaults } /// /// Appends the local path to the common config path. /// /// public static string GetCommonConfigPath(params string[] localPath) { string local = Join(localPath); return Join(CommonConfigPath, local); } /// /// Appends the local path to the program config path. /// /// public static string GetProgramConfigPath(params string[] localPath) { string local = Join(localPath); return Join(ProgramConfigPath, local); } /// /// Returns true if the given path exists. /// /// /// [PublicAPI] public static bool PathExists(string path) { return IcdFile.Exists(path) || IcdDirectory.Exists(path); } /// /// Returns the path if the given path is already a directory or has a trailing slash. /// Otherwise returns the parent directory name. /// /// /// [PublicAPI] public static string GetDirectoryNameFromPath(string path) { if (IcdDirectory.Exists(path)) return path; if (path.EndsWith(IcdPath.DirectorySeparatorChar) || path.EndsWith(IcdPath.AltDirectorySeparatorChar)) return path; return IcdPath.GetDirectoryName(path); } #endregion } }