/// <summary> /// Factory method to create a new <see cref="PurePath"/> instance /// based upon the current operating system. /// </summary> /// <param name="options"></param> /// <param name="path"></param> /// <param name="result"></param> /// <returns></returns> public bool TryCreate(PathFactoryOptions options, string path, out IPath result) { result = null; switch (PlatformChooser.GetPlatform()) { case Platform.Posix: PurePosixPath purePosixPath; if (PurePosixPath.TryParse(path, out purePosixPath)) { result = new PosixPath(purePosixPath); break; } return(false); case Platform.Windows: PureWindowsPath pureWindowsPath; if (PureWindowsPath.TryParse(path, out pureWindowsPath)) { result = new WindowsPath(path); break; } return(false); } result = ApplyOptions(result, options); return(true); }
/// <summary> /// Factory method to create a new <see cref="PurePath"/> instance /// based upon the current operating system. /// </summary> /// <param name="path"></param> /// <param name="options"></param> /// <returns></returns> public IPath Create(string path, PathFactoryOptions options) { IPath ret = null; switch (PlatformChooser.GetPlatform()) { case Platform.Posix: ret = new PosixPath(path); break; case Platform.Windows: ret = new WindowsPath(path); break; } return ApplyOptions(ret, options); }
/// <summary> /// Factory method to create a new <see cref="PurePath"/> instance /// based upon the current operating system. /// </summary> /// <param name="path"></param> /// <param name="options"></param> /// <returns></returns> public IPath Create(string path, PathFactoryOptions options) { IPath ret = null; switch (PlatformChooser.GetPlatform()) { case Platform.Posix: ret = new PosixPath(path); break; case Platform.Windows: ret = new WindowsPath(path); break; } return(ApplyOptions(ret, options)); }
public void Create_WithExpandEnvVarsOption_CreatesPathExpandsVars() { Environment.SetEnvironmentVariable("MYCUSTOMBIN", "bin"); const string expectedDir = @"C:\bin\myprogram.exe"; const string unexpandedDir = @"C:\%MYCUSTOMBIN%\myprogram.exe"; var factory = new PathFactory(); var expected = (IsWindows ? (IPath) new WindowsPath(expectedDir) : new PosixPath(expectedDir)).NormCase(); var options = new PathFactoryOptions { AutoExpandEnvironmentVariables = true }; var actual = factory.Create(unexpandedDir, options); Assert.AreEqual(expected, actual); }
public void Create_WithExpandUserOptionAndExplicitUserDirectory_CreatesPathAndExpandsUser() { const string expectedDir = @"c:\users\fake\tmp"; const string unexpandedDir = @"~\tmp"; var factory = new PathFactory(); var expected = (IsWindows ? (IPath) new WindowsPath(expectedDir) : new PosixPath(expectedDir)).NormCase(); var options = new PathFactoryOptions { AutoExpandUserDirectory = true, UserDirectory = factory.Create(@"C:\users\fake") }; var actual = factory.Create(unexpandedDir, options); Assert.AreEqual(expected, actual); }
public void Create_WithExpandUserOption_CreatesPathAndExpandsUser() { var userDir = Environment.GetEnvironmentVariable("USERPROFILE"); var expectedDir = userDir + @"\tmp"; const string unexpandedDir = @"~\tmp"; var factory = new PathFactory(); var expected = (IsWindows ? (IPath) new WindowsPath(expectedDir) : new PosixPath(expectedDir)).NormCase(); var options = new PathFactoryOptions { AutoExpandUserDirectory = true }; var actual = factory.Create(unexpandedDir, options); Assert.AreEqual(expected, actual); }
public void Create_WithExpandEnvVarsOption_CreatesPathExpandsVars() { Environment.SetEnvironmentVariable("MYCUSTOMBIN", "bin"); const string expectedDir = @"C:\bin\myprogram.exe"; const string unexpandedDir = @"C:\%MYCUSTOMBIN%\myprogram.exe"; var factory = new PathFactory(); var expected = (IsWindows ? (IPath)new WindowsPath(expectedDir) : new PosixPath(expectedDir)).NormCase(); var options = new PathFactoryOptions { AutoExpandEnvironmentVariables = true }; var actual = factory.Create(unexpandedDir, options); Assert.AreEqual(expected, actual); }
private static IPath ApplyOptions(IPath path, PathFactoryOptions options) { if (options.AutoNormalizeCase) { path = options.Culture != null ? path.NormCase(options.Culture) : path.NormCase(); } if (options.AutoExpandEnvironmentVariables) { path = path.ExpandEnvironmentVars(); } if (options.AutoExpandUserDirectory) { path = options.UserDirectory != null ? path.ExpandUser(options.UserDirectory) : path.ExpandUser(); } return(path); }
/// <summary> /// Factory method to create a new <see cref="PurePath"/> instance /// based upon the current operating system. /// </summary> /// <param name="options"></param> /// <param name="path"></param> /// <param name="result"></param> /// <returns></returns> public bool TryCreate(PathFactoryOptions options, string path, out IPath result) { result = null; switch (PlatformChooser.GetPlatform()) { case Platform.Posix: PurePosixPath purePosixPath; if (PurePosixPath.TryParse(path, out purePosixPath)) { result = new PosixPath(purePosixPath); break; } return false; case Platform.Windows: PureWindowsPath pureWindowsPath; if (PureWindowsPath.TryParse(path, out pureWindowsPath)) { result = new WindowsPath(path); break; } return false; } result = ApplyOptions(result, options); return true; }
private static IPath ApplyOptions(IPath path, PathFactoryOptions options) { if (options.AutoNormalizeCase) { path = options.Culture != null ? path.NormCase(options.Culture) : path.NormCase(); } if (options.AutoExpandEnvironmentVariables) { path = path.ExpandEnvironmentVars(); } if (options.AutoExpandUserDirectory) { path = options.UserDirectory != null ? path.ExpandUser(options.UserDirectory) : path.ExpandUser(); } return path; }
public void Create_WithExpandUserOptionAndExplicitUserDirectory_CreatesPathAndExpandsUser() { const string expectedDir = @"c:\users\fake\tmp"; const string unexpandedDir = @"~\tmp"; var factory = new PathFactory(); var expected = (IsWindows ? (IPath)new WindowsPath(expectedDir) : new PosixPath(expectedDir)).NormCase(); var options = new PathFactoryOptions { AutoExpandUserDirectory = true, UserDirectory = factory.Create(@"C:\users\fake") }; var actual = factory.Create(unexpandedDir, options); Assert.AreEqual(expected, actual); }
public void Create_WithExpandUserOption_CreatesPathAndExpandsUser() { var userDir = Environment.GetEnvironmentVariable("USERPROFILE"); var expectedDir = userDir + @"\tmp"; const string unexpandedDir = @"~\tmp"; var factory = new PathFactory(); var expected = (IsWindows ? (IPath)new WindowsPath(expectedDir) : new PosixPath(expectedDir)).NormCase(); var options = new PathFactoryOptions { AutoExpandUserDirectory = true }; var actual = factory.Create(unexpandedDir, options); Assert.AreEqual(expected, actual); }