示例#1
0
        /// <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);
        }
示例#2
0
 /// <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);
 }
示例#3
0
        /// <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));
        }
示例#4
0
        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);
        }
示例#5
0
        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);
        }
示例#6
0
        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);
        }
示例#7
0
        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);
        }
示例#8
0
 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);
 }
示例#9
0
 /// <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;
 }
示例#10
0
 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;
 }
示例#11
0
        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);
        }
示例#12
0
        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);
        }