コード例 #1
0
        public bool DirectoryExists(string directory)
        {
            if (!IsPathInCone(directory, out string processedPath))
            {
                return(_basis.DirectoryExists(directory));
            }

            directory = processedPath;
            string rel = directory.Substring(_root.FullPath.Length).Trim('/', '\\');

            if (string.IsNullOrEmpty(rel))
            {
                return(true);
            }

            string[]            parts      = rel.Split('/', '\\');
            FileSystemDirectory currentDir = _root;

            for (int i = 0; i < parts.Length; ++i)
            {
                FileSystemDirectory dir;
                if (!currentDir.Directories.TryGetValue(parts[i], out dir))
                {
                    return(false);
                }

                currentDir = dir;
            }

            return(true);
        }
コード例 #2
0
        public override IEnumerable <IFileSystemInfo> EnumerateFileSystemInfos(string pattern, SearchOption searchOption)
        {
            return(_paths.EnumerateFileSystemEntries(_physicalPath, pattern, searchOption).Select(x =>
            {
                if (MountPoint is not FileSystemMountPoint fileSystemMountPoint)
                {
                    throw new NotSupportedException($"{nameof(FileSystemDirectory)} may only exist in {nameof(FileSystemMountPoint)} mount point.");
                }

                string baseName = x.Substring(fileSystemMountPoint.MountPointRootPath.Length).Replace(Path.DirectorySeparatorChar, '/');

                if (baseName.Length == 0)
                {
                    baseName = "/";
                }

                if (baseName[0] != '/')
                {
                    baseName = "/" + baseName;
                }

                if (_fileSystem.DirectoryExists(x) && baseName[baseName.Length - 1] != '/')
                {
                    baseName = baseName + "/";
                }

                return fileSystemMountPoint.FileSystemInfo(baseName);
            }));
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: virzak/templating
        private static bool CheckDirectoryExists(IPhysicalFileSystem fs, JObject config, string outputPath)
        {
            string path = Path.Combine(outputPath, config["path"].ToString());
            if (fs.DirectoryExists(path))
            {
                return true;
            }

            Console.Error.WriteLine($"Expected a directory {path} to exist but it did not");
            return false;
        }
コード例 #4
0
        internal IReadOnlyList <string> FindProjFileAtOrAbovePath(IPhysicalFileSystem fileSystem, string startPath, HashSet <string> extensionLimiters)
        {
            string directory;

            if (fileSystem.DirectoryExists(startPath))
            {
                directory = startPath;
            }
            else
            {
                directory = Path.GetDirectoryName(startPath);
            }

            do
            {
                List <string> filesInDir = fileSystem.EnumerateFileSystemEntries(directory, "*.*proj", SearchOption.TopDirectoryOnly).ToList();
                List <string> matches    = new List <string>();

                if (extensionLimiters.Count == 0)
                {
                    matches = filesInDir;
                }
                else
                {
                    foreach (string filename in filesInDir)
                    {
                        string extension = Path.GetExtension(filename);
                        if (extensionLimiters.Contains(extension))
                        {
                            matches.Add(filename);
                        }
                    }
                }

                if (matches.Count > 0)
                {
                    return(matches);
                }

                if (Path.GetPathRoot(directory) != directory)
                {
                    directory = Directory.GetParent(directory).FullName;
                }
                else
                {
                    directory = null;
                }
            } while (directory != null);

            return(new List <string>());
        }
コード例 #5
0
        private static string ResolveExecutableFilePath(IPhysicalFileSystem fileSystem, string executableFileName, string outputBasePath)
        {
            if (!string.IsNullOrEmpty(outputBasePath) && fileSystem.DirectoryExists(outputBasePath))
            {
                string executableCombinedFileName = Path.Combine(Path.GetFullPath(outputBasePath), executableFileName);
                if (fileSystem.FileExists(executableCombinedFileName))
                {
                    return(executableCombinedFileName);
                }
            }

            // The executable has not been found in the template folder, thus do not use the full path to the file.
            // The executable will be further searched in the directories from the PATH environment variable.
            return(executableFileName);
        }
コード例 #6
0
        // Walks up the directory path looking for files that match the matchPattern and the secondary filter (if provided)
        // Returns all the matching files in the first directory that has any matched files.
        public static IReadOnlyList <string> FindFilesAtOrAbovePath(IPhysicalFileSystem fileSystem, string startPath, string matchPattern, Func <string, bool> secondaryFilter = null)
        {
            string directory;

            if (fileSystem.DirectoryExists(startPath))
            {
                directory = startPath;
            }
            else
            {
                directory = Path.GetDirectoryName(startPath);
            }

            do
            {
                List <string> filesInDir = fileSystem.EnumerateFileSystemEntries(directory, matchPattern, SearchOption.TopDirectoryOnly).ToList();
                List <string> matches    = new List <string>();

                if (secondaryFilter == null)
                {
                    matches = filesInDir;
                }
                else
                {
                    matches = filesInDir.Where(x => secondaryFilter(x)).ToList();
                }

                if (matches.Count > 0)
                {
                    return(matches);
                }

                if (Path.GetPathRoot(directory) != directory)
                {
                    directory = Directory.GetParent(directory).FullName;
                }
                else
                {
                    directory = null;
                }
            } while (directory != null);

            return(new List <string>());
        }
コード例 #7
0
        public override IEnumerable <IFileSystemInfo> EnumerateFileSystemInfos(string pattern, SearchOption searchOption)
        {
            return(_paths.EnumerateFileSystemEntries(_physicalPath, pattern, searchOption).Select(x =>
            {
                string baseName = x.Substring(((FileSystemMountPoint)MountPoint).MountPointRootPath.Length).Replace(Path.DirectorySeparatorChar, '/');

                if (baseName.Length == 0)
                {
                    baseName = "/";
                }

                if (baseName[0] != '/')
                {
                    baseName = "/" + baseName;
                }

                if (_fileSystem.DirectoryExists(x) && baseName[baseName.Length - 1] != '/')
                {
                    baseName = baseName + "/";
                }

                return MountPoint.FileSystemInfo(baseName);
            }));
        }
コード例 #8
0
ファイル: Orchestrator.cs プロジェクト: dotnet/templating
        private IReadOnlyList <IFileChange2> GetFileChangesInternal(IDirectory sourceDir, string targetDir, IGlobalRunSpec spec)
        {
            EngineConfig cfg      = new EngineConfig(_logger, EngineConfig.DefaultWhitespaces, EngineConfig.DefaultLineEndings, spec.RootVariableCollection);
            IProcessor   fallback = Processor.Create(cfg, spec.Operations);

            List <IFileChange2> changes = new List <IFileChange2>();
            List <KeyValuePair <IPathMatcher, IProcessor> > fileGlobProcessors = CreateFileGlobProcessors(_logger, spec);

            foreach (IFile file in sourceDir.EnumerateFiles("*", SearchOption.AllDirectories))
            {
                string sourceRel = file.PathRelativeTo(sourceDir);
                string fileName  = Path.GetFileName(sourceRel);
                bool   checkingDirWithPlaceholderFile = false;

                if (spec.IgnoreFileNames.Contains(fileName))
                {
                    // The placeholder file should never get copied / created / processed. It just causes the dir to get created if needed.
                    // The change checking / reporting is different, setting this variable tracks it.
                    checkingDirWithPlaceholderFile = true;
                }

                foreach (IPathMatcher include in spec.Include)
                {
                    if (include.IsMatch(sourceRel))
                    {
                        bool excluded = false;
                        foreach (IPathMatcher exclude in spec.Exclude)
                        {
                            if (exclude.IsMatch(sourceRel))
                            {
                                excluded = true;
                                break;
                            }
                        }

                        if (!excluded)
                        {
                            if (!spec.TryGetTargetRelPath(sourceRel, out string targetRel))
                            {
                                targetRel = sourceRel;
                            }

                            string targetPath = Path.Combine(targetDir, targetRel);

                            if (checkingDirWithPlaceholderFile)
                            {
                                targetPath = Path.GetDirectoryName(targetPath);
                                targetRel  = Path.GetDirectoryName(targetRel);

                                if (_fileSystem.DirectoryExists(targetPath))
                                {
                                    changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Overwrite));
                                }
                                else
                                {
                                    changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Create));
                                }
                            }
                            else if (_fileSystem.FileExists(targetPath))
                            {
                                changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Overwrite));
                            }
                            else
                            {
                                changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Create));
                            }
                        }

                        break;
                    }
                }
            }

            return(changes);
        }