예제 #1
0
        // this method parses the glob and extracts the fixed directory part in order to normalize it and make it absolute
        // without this normalization step, strings pointing outside the globbing cone would still match when they shouldn't
        // for example, we dont want "**/*.cs" to match "../Shared/Foo.cs"
        // todo: glob rooting partially duplicated with MSBuildGlob.Parse
        private static Regex CreateRegex(string unescapedFileSpec, string currentDirectory)
        {
            Regex  regex                 = null;
            string fixedDirPart          = null;
            string wildcardDirectoryPart = null;
            string filenamePart          = null;

            FileMatcher.SplitFileSpec(
                unescapedFileSpec,
                out fixedDirPart,
                out wildcardDirectoryPart,
                out filenamePart,
                FileMatcher.s_defaultGetFileSystemEntries);

            if (FileUtilities.PathIsInvalid(fixedDirPart))
            {
                return(null);
            }

            var absoluteFixedDirPart   = Path.Combine(currentDirectory, fixedDirPart);
            var normalizedFixedDirPart = string.IsNullOrEmpty(absoluteFixedDirPart)
                                         // currentDirectory is empty for some in-memory projects
                ? Directory.GetCurrentDirectory()
                : FileUtilities.GetFullPathNoThrow(absoluteFixedDirPart);

            normalizedFixedDirPart = FileUtilities.EnsureTrailingSlash(normalizedFixedDirPart);

            var recombinedFileSpec = string.Join("", normalizedFixedDirPart, wildcardDirectoryPart, filenamePart);

            bool isRecursive;
            bool isLegal;

            FileMatcher.GetFileSpecInfoWithRegexObject(
                recombinedFileSpec,
                out regex,
                out isRecursive,
                out isLegal,
                FileMatcher.s_defaultGetFileSystemEntries);

            return(isLegal ? regex : null);
        }