Example #1
0
        public Task Invoke(IDictionary <string, object> environment)
        {
            if (!environment.ContainsKey("owin.RequestPath"))
            {
                throw new ApplicationException("Invalid OWIN request. Expected owin.RequestPath, but not present.");
            }

            var path = Uri.UnescapeDataString((string)environment["owin.RequestPath"]);

            if (statelessAuthOptions != null)
            {
                foreach (var ignorePath in statelessAuthOptions.IgnorePaths)
                {
                    var mm = new Minimatcher(ignorePath, new Options()
                    {
                        IgnoreCase = true
                    });

                    if (mm.IsMatch(path))
                    {
                        return(nextFunc(environment));
                    }
                }
            }

            var requestHeaders = (IDictionary <string, string[]>)environment["owin.RequestHeaders"];

            if (!requestHeaders.ContainsKey("Authorization"))
            {
                SetResponseStatusCodeAndHeader(environment);
                return(ReturnCompletedTask());
            }

            var token = requestHeaders["Authorization"].FirstOrDefault();

            if (string.IsNullOrWhiteSpace(token))
            {
                SetResponseStatusCodeAndHeader(environment);
                return(ReturnCompletedTask());
            }

            var validatedUser = tokenValidator.ValidateUser(token);

            if (validatedUser == null)
            {
                SetResponseStatusCodeAndHeader(environment);
                return(ReturnCompletedTask());
            }

            if (environment.ContainsKey(ServerUser))
            {
                environment[ServerUser] = validatedUser;
            }
            else
            {
                environment.Add(ServerUser, validatedUser);
            }

            return(nextFunc(environment));
        }
Example #2
0
        private static bool IsSourceFile(JObject obj, string cwd, string fileName)
        {
            IEnumerable <string> files    = obj["files"]?.Values <string>() ?? Enumerable.Empty <string>();
            IEnumerable <string> includes = obj["include"]?.Values <string>() ?? Enumerable.Empty <string>();

            var options = new Options {
                AllowWindowsPaths = true
            };
            string relative = fileName.Substring(cwd.Length).Trim('\\');

            foreach (string file in files)
            {
                bool isMatch = Minimatcher.Check(relative, file, options);
                if (isMatch)
                {
                    return(true);
                }
            }

            foreach (string pattern in includes)
            {
                bool isMatch = Minimatcher.Check(relative, pattern, options);
                if (isMatch)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #3
0
        private bool IsImageOnProbingPath(string file, out Optimization optimization)
        {
            optimization = null;

            foreach (var opti in _config.Optimizations)
            {
                optimization = opti;

                bool isIncluded = opti.Includes.Any(pattern => Minimatcher.Check(file, pattern, _matcherOptions));

                if (!isIncluded)
                {
                    continue;
                }

                bool isExcluded = opti.Excludes.Any(pattern => Minimatcher.Check(file, pattern, _matcherOptions));

                if (isExcluded)
                {
                    continue;
                }

                return(true);
            }

            return(false);
        }
Example #4
0
        private IReadOnlyCollection <IProject> GetNotExcludedProjects(IReadOnlyCollection <string> exclusionPatterns)
        {
            if (exclusionPatterns == null || exclusionPatterns.Count < 1)
            {
                return(_projects);
            }

            var notExcludedProjects = new List <IProject>();

            foreach (var project in _projects)
            {
                var excluded = false;
                foreach (var excludedProject in exclusionPatterns)
                {
                    if (Minimatcher.Check(project.FilePath, excludedProject, new Options {
                        AllowWindowsPaths = true, NoCase = true
                    }))
                    {
                        _logger.LogDebug($"Ignoring project '{project.FilePath}' as it matches the exclusion pattern {excludedProject}");
                        excluded = true;
                        break;
                    }
                }

                if (excluded)
                {
                    continue;
                }

                notExcludedProjects.Add(project);
            }

            return(notExcludedProjects);
        }
        public string GetSchemaFor(string fileLocation)
        {
            foreach (string url in _schemas.Keys)
            {
                try
                {
                    if (_schemas[url] == null)
                    {
                        continue;
                    }
                    foreach (string file in _schemas[url])
                    {
                        var pattern = "**/" + file.TrimStart('*', '/');

                        var matche = new Minimatcher(pattern, _options);
                        if (matche.IsMatch(fileLocation))
                        {
                            return(url);
                        }
                    }
                }
                catch
                {
                    Logger.Log("JSON Schema: Couldn't parse " + _schemas[url] + " as a valid regex.");
                }
            }

            return(null);
        }
Example #6
0
        public static Boolean IsSetVariableAllowed(this TaskRestrictions restrictions, String variable)
        {
            ArgUtil.NotNull(variable, nameof(variable));

            var allowedList = restrictions.SettableVariables?.Allowed;

            if (allowedList == null)
            {
                return(true);
            }

            var opts = new Options()
            {
                IgnoreCase = true
            };

            foreach (String pattern in allowedList)
            {
                if (Minimatcher.Check(variable, pattern, opts))
                {
                    return(true);
                }
            }

            return(false);
        }
Example #7
0
        /// <summary>
        /// Expands braces in patterns if they are exist
        /// </summary>
        /// <param name="pattern">String of pattern</param>
        /// <param name="matchOptions">Download parameters from minimatcherFuncs</param>
        /// <returns></returns>
        private string[] ExpandBraces(string pattern, Options matchOptions)
        {
            // Convert slashes on Windows before calling braceExpand(). Unfortunately this means braces cannot
            // be escaped on Windows, this limitation is consistent with current limitations of minimatch (3.0.3).
            tracer.Info($"Expanding braces.");
            string convertedPattern = pattern.Replace("\\", "/");

            return(Minimatcher.BraceExpand(convertedPattern, matchOptions).ToArray());
        }
        private IEnumerable <DbvItemBase> FilterDeleted(IEnumerable <DbvItemBase> source, string itemMask)
        {
            var matcher = new Minimatcher(string.IsNullOrEmpty(itemMask) ? "**" : itemMask, new Options {
                IgnoreCase = true, MatchBase = true
            });

            // code duplication with the above
            return(source.Where(item => matcher.IsMatch(item.Name))
                   .Where(item => !IsDeleted(item)));
        }
        public IEnumerable <DbvItemBase> History(string itemMask)
        {
            var matcher = new Minimatcher(string.IsNullOrEmpty(itemMask) ? "**" : itemMask, new Options {
                IgnoreCase = true, MatchBase = true
            });

            return(_revisionStore.List(0)
                   .Where(item => matcher.IsMatch(item.Name))
                   .OrderBy(k => k.Revision));
        }
Example #10
0
        public static bool CheckGlobbing(string path, string pattern)
        {
            string p = pattern?.TrimEnd('/');

            if (!string.IsNullOrWhiteSpace(p))
            {
                return(Minimatcher.Check(path, p, _options));
            }

            return(false);
        }
Example #11
0
        public void VerifyMinimatcherSearchPatternMatch()
        {
            var matcher = new Minimatcher("/test/*", new Minimatch.Options {
                IgnoreCase = true
            });


            Assert.IsTrue(matcher.IsMatch("/test/dummy.csv"));
            Assert.IsTrue(matcher.IsMatch("/TEST/dummy.csv"));
            Assert.IsFalse(matcher.IsMatch("/second/dummy.csv"));
        }
        internal static Func <string, bool> CreateMinimatchFilter(AgentTaskPluginExecutionContext context, string rule, bool invert)
        {
            Func <string, bool> filter       = Minimatcher.CreateFilter(rule, minimatchOptions);
            Func <string, bool> tracedFilter = (path) => {
                bool filterResult = filter(path);
                context.Verbose($"Path `{path}` is {(filterResult ? "" : "not ")}{(invert ? "excluded" : "included")} because of pattern `{(invert ? "!" : "")}{rule}`.");
                return(invert ^ filterResult);
            };

            return(tracedFilter);
        }
Example #13
0
            ///<summary>Filters a list of inputs against a single pattern.</summary>
            ///<remarks>This function reparses this input on each invocation.  For performance, avoid this function and reuse a Minimatcher instance instead.</remarks>
            public static IEnumerable <string> Filter(IEnumerable <string> list, string pattern, Options options = null)
            {
                var mm = new Minimatcher(pattern, options);

                list = list.Where(mm.IsMatch);
                if (options != null && options.NoNull)
                {
                    list = list.DefaultIfEmpty(pattern);
                }
                return(list);
            }
Example #14
0
        public void TestIssue3()
        {
            var filterOptions = new Options
            {
                AllowWindowsPaths = true,
                IgnoreCase = true
            };

            var minimatcher = new Minimatcher("**/Thumbs.db", filterOptions);

            Assert.IsFalse(minimatcher.IsMatch(@"NewFolder\"));
        }
        static void TestCase(string pattern, IList <string> expected, Options options = null, IEnumerable <string> input = null)
        {
            input ??= files;

            Assert.Equal(
                string.Join(Environment.NewLine, expected.OrderBy(s => s)),
                string.Join(Environment.NewLine, Minimatcher.Filter(input, pattern, options).OrderBy(s => s))
                );

            var regex = Minimatcher.CreateRegex(pattern, options);

            actualRegexes.Add(Tuple.Create(pattern, regex == null ? "false" : "/" + regex.ToString() + "/" + (regex.Options == RegexOptions.IgnoreCase ? "i" : "")));
        }
Example #16
0
        public void IncrementAndUpdateAll()
        {
            b.Verbose.Log("IncrementAndUpdateAll called");
            ValidateForUpdate();
            LoadVersioningComponent();
            b.Verbose.Log("Versioning Loaded ");
            ver.Increment();
            b.Verbose.Log("Saving");
            SaveVersioningComponent();
            b.Verbose.Log($"Searching {BaseSearchDir} there are {pendingUpdates.Count} pends.");

            var  enumer         = Directory.EnumerateFiles(BaseSearchDir, "*.*", SearchOption.AllDirectories).GetEnumerator();
            bool shouldContinue = true;

            while (shouldContinue)
            {
                try {
                    shouldContinue = enumer.MoveNext();
                    if (shouldContinue)
                    {
                        var v = enumer.Current;

                        // Check every file that we have returned.
                        foreach (var chk in pendingUpdates.Keys)
                        {
                            var mm = new Minimatcher(chk, new Options {
                                AllowWindowsPaths = true, IgnoreCase = true
                            });
                            b.Verbose.Log($"Checking {chk} against {v}");
                            if (mm.IsMatch(v))
                            {
                                b.Info.Log("Match...");
                                // TODO Cache this and make it less loopey
                                VersionFileUpdater sut = new VersionFileUpdater(ver);
                                foreach (var updateType in pendingUpdates[chk])
                                {
                                    b.Verbose.Log($"Perform update {v}");
                                    sut.PerformUpdate(v, updateType);
                                }
                            }
                        }
                    }
                } catch (System.UnauthorizedAccessException) {
                    // If you run through all the filles in a directory you can hit areas of the filesystem
                    // that you dont have access to - this skips those files and then continues.
                    b.Verbose.Log("Unauthorised area of the filesystem, skipping");
                }
            }

            VersionString = ver.GetVersionString();
        }
        private static void CheckTheseMatches(List <Tuple <string, bool> > mtchs, string againstThis)
        {
            var mm = new Minimatcher(againstThis, new Options {
                AllowWindowsPaths = true, IgnoreCase = true
            });
            int i = 0;

            foreach (var v in mtchs)
            {
                i++;
                bool isMatch = mm.IsMatch(v.Item1);
                Assert.Equal(v.Item2, isMatch); //, "Mismatch " + i.ToString());
            }
        }
Example #18
0
        public Vehicle[] Search(SearchCtx ctx)
        {
            Minimatcher mm        = GetMinimatcher(ctx);
            var         vehicles  = JoinVehicles();
            var         typesById = TypesById();
            var         found     = vehicles
                                    .Where(a => ctx.SelectedType == "1" ||
                                           a.Typename == typesById[ctx.SelectedType])
                                    .Where(a => !ctx.OnlyToday ||
                                           DateTime.Now.Day == a.checkInDate.Day)
                                    .Where(a => mm == null ||
                                           (a.RegNr != null && mm.IsMatch(a.RegNr)) ||
                                           (a.OwnerName != null && mm.IsMatch(a.OwnerName)));

            return(found.ToArray());
        }
Example #19
0
            ///<summary>Creates a filter function that tests input against a pattern.</summary>
            public static Func <string, bool> CreateFilter(string pattern, Options options = null)
            {
                if (pattern == null)
                {
                    throw new ArgumentNullException("pattern");
                }
                // "" only matches ""
                if (String.IsNullOrWhiteSpace(pattern))
                {
                    return(String.IsNullOrEmpty);
                }

                var m = new Minimatcher(pattern, options);

                return(m.IsMatch);
            }
        public void MiniMatchSyntax_FindAssemblyInfo()
        {
            var mtchs = new List <Tuple <string, bool> >();

            mtchs.Add(new Tuple <string, bool>(@"C:\temp\te st\properties\assemblyinfo.cs", true));
            mtchs.Add(new Tuple <string, bool>(@"C:\te mp\test\assemblyinfo.cs", false));
            mtchs.Add(new Tuple <string, bool>(@"C:\te mp\t e s t\properties\notassemblyinfo.cs", false));
            mtchs.Add(new Tuple <string, bool>(@"C:\temp\test\properties\assemblyinfo.cs.txt", false));
            mtchs.Add(new Tuple <string, bool>(@"C:\a\1\s\PliskyLibrary\PliskyLib\Properties\AssemblyInfo.cs", true));
            string againstThis = @"**\properties\assemblyinfo.cs";

            CheckTheseMatches(mtchs, againstThis);

            var mm2 = new Minimatcher(@"C:\temp\test\testfile.tst", new Options {
                AllowWindowsPaths = true
            });

            Assert.True(mm2.IsMatch(@"C:\temp\test\testfile.tst"), "Cant match on full filename");
        }
Example #21
0
        private bool CheckAndUpdate(string fl, Minimatcher assemblyMM, string versionValue, Action <string, string> p)
        {
            b.Assert.True(p != null, "The action used cant be null");

            b.Verbose.Log("Checking file :" + fl);

            bool result = assemblyMM.IsMatch(fl);

            if ((result) && (File.Exists(fl)))
            {
                b.Info.Log($"Updating VersioningFile File ({fl}) to ({versionValue})");

                hook?.PreUpdateFileAction(fl); // PreUpdateAllAction?.Invoke(fl);
                //PreUpdateAction?.Invoke(fl);

                p(fl, versionValue);

                hook?.PostUpdateFileAction(fl);
            }
            return(result);
        }
        public string[] ListTestAssemblyPaths(string directory, string[] minimatchPatterns)
        {
            var files = ListAllAccessibleFilesInDirectory(directory);

            var matchingFilesToBeIncluded = new List <string>();

            foreach (var minimatchPattern in minimatchPatterns.Where(minimatchPattern => !minimatchPattern.StartsWith("!")))
            {
                var mm = new Minimatcher(minimatchPattern, new Minimatch.Options()
                {
                    AllowWindowsPaths = true, IgnoreCase = true
                });
                matchingFilesToBeIncluded.AddRange(mm.Filter(files));
            }

            var noMatchingFilesFound = matchingFilesToBeIncluded.Count == 0;

            if (noMatchingFilesFound)
            {
                return(null);
            }

            var matchingFilesToBeExcluded = new List <string>();

            foreach (var minimatchPattern in minimatchPatterns.Where(minimatchPattern => minimatchPattern.StartsWith("!")))
            {
                var actualMinimatchPattern = minimatchPattern.TrimStart('!');
                var mm = new Minimatcher(actualMinimatchPattern, new Minimatch.Options()
                {
                    AllowWindowsPaths = true, IgnoreCase = true
                });
                matchingFilesToBeExcluded.AddRange(mm.Filter(files));
            }

            matchingFilesToBeIncluded.RemoveAll(x => matchingFilesToBeExcluded.Contains(x));

            return(matchingFilesToBeIncluded.ToArray());
        }
        /// <inheritdoc/>
        public override async Task<FtpResponse> Process(FtpCommand command, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(command.Argument))
            {
                var taskStates = Server.GetBackgroundTaskStates();
                var statusMessage = new StringBuilder();
                statusMessage.AppendFormat("Server functional, {0} open connections", Server.Statistics.ActiveConnections);
                if (taskStates.Count != 0)
                    statusMessage.AppendFormat(", {0} active background transfers", taskStates.Count);
                return new FtpResponse(211, statusMessage.ToString());
            }

            var mask = command.Argument;
            if (!mask.EndsWith("*"))
                mask += "*";

            var mmOptions = new Options()
            {
                IgnoreCase = Data.FileSystem.FileSystemEntryComparer.Equals("a", "A"),
                NoGlobStar = true,
                Dot = true,
            };

            var mm = new Minimatcher(mask, mmOptions);

            var formatter = new LongListFormatter();
            await Connection.WriteAsync($"211-STAT {command.Argument}", cancellationToken);

            foreach (var entry in (await Data.FileSystem.GetEntriesAsync(Data.CurrentDirectory, cancellationToken)).Where(x => mm.IsMatch(x.Name)))
            {
                var line = formatter.Format(entry, entry.Name);
                Connection.Log?.Debug(line);
                await Connection.WriteAsync($" {line}", cancellationToken);
            }

            return new FtpResponse(211, "STAT");
        }
Example #24
0
        public void IncrementAndUpdateAll()
        {
            Bilge.VerboseLog("IncrementAndUpdateAll called");
            ValidateForUpdate();
            LoadVersioningComponent();

            Bilge.VerboseLog("Versioning Loaded Ver at " + ver.ToString());
            ver.PerformIncrement();
            Bilge.VerboseLog("Saving Ver At " + ver.ToString());
            SaveVersioningComponent();
            Bilge.VerboseLog($"Searching {BaseSearchDir} there are {pendingUpdates.Count} pends.");
            foreach (var v in Directory.EnumerateFiles(BaseSearchDir, "*.*", SearchOption.AllDirectories))
            {
                // Check every file that we have returned.
                foreach (var chk in pendingUpdates.Keys)
                {
                    var mm = new Minimatcher(chk, new Options {
                        AllowWindowsPaths = true, IgnoreCase = true
                    });

                    // Dump out every file considered. Bilge.VerboseLog($"Checking {chk} against {v}");

                    if (mm.IsMatch(v))
                    {
                        Bilge.Log($"Match...{chk}", v);
                        // TODO Cache this and make it less loopey
                        VersionFileUpdater vfu = new VersionFileUpdater(ver);
                        foreach (var updateType in pendingUpdates[chk])
                        {
                            Bilge.VerboseLog($"Perform update {v} using {updateType.ToString()} as {(int)updateType}");
                            vfu.PerformUpdate(v, updateType);
                        }
                    }
                }
            }
        }
Example #25
0
        public void Start()
        {
            var filesMinimatched = new List <string>();
            var baseFolderLength = Arguments.BaseFolder.Length;
            var relativeFiles    = Directory.EnumerateFiles(Arguments.BaseFolder, "*.*", SearchOption.AllDirectories)
                                   .Select(f => f.Substring(baseFolderLength));

            // Search files matching the Pattern
            foreach (string file in relativeFiles)
            {
                if (Minimatcher.IsMatch(file))
                {
                    filesMinimatched.Add(file);
                }
            }

            // Read and replace
            foreach (var relativeFile in filesMinimatched)
            {
                var filename = Arguments.BaseFolder + relativeFile;
                var file     = new File(filename);

                string newContent = Regex.Replace(file.Content, Arguments.Find, Arguments.Replace, RegexOptions.IgnoreCase);

                if (newContent != file.Content)
                {
                    if (!Arguments.IsDemoMode)
                    {
                        file.WriteAllText(newContent);
                    }

                    this.FilesMatched.Add(relativeFile);
                    Logger?.Invoke(relativeFile, newContent);
                }
            }
        }
Example #26
0
        public static IEnumerable <string> ExpandFileGlobs(IEnumerable <string> potentialGlobs, IEnumerable <string> libraryFiles)
        {
            var finalSetOfFiles = new HashSet <string>();
            var negatedOptions  = new Minimatch.Options {
                FlipNegate = true
            };

            foreach (string potentialGlob in potentialGlobs)
            {
                // only process globs where we find them, otherwise it can get expensive
                if (potentialGlob.StartsWith("!", StringComparison.Ordinal))
                {
                    // Remove matches from the files list
                    var filesToRemove = finalSetOfFiles.Where(f => Minimatcher.Check(f, potentialGlob, negatedOptions)).ToList();
                    foreach (string file in filesToRemove)
                    {
                        finalSetOfFiles.Remove(file);
                    }
                }
                else if (potentialGlob.IndexOfAny(GlobIndicatorCharacters) >= 0)
                {
                    IEnumerable <string> filterResult = libraryFiles.Where(f => Minimatcher.Check(f, potentialGlob));
                    if (filterResult.Any())
                    {
                        finalSetOfFiles.UnionWith(filterResult);
                    }
                }
                else
                {
                    // not a glob pattern, so just include the file literally
                    finalSetOfFiles.Add(potentialGlob);
                }
            }

            return(finalSetOfFiles);
        }
Example #27
0
        internal List <string> GetAbsoluteInputFiles()
        {
            List <string> files   = new List <string>();
            string        folder  = new DirectoryInfo(Path.GetDirectoryName(FileName)).FullName;
            string        ext     = Path.GetExtension(InputFiles.First());
            Options       options = new Options {
                AllowWindowsPaths = true
            };

            foreach (string inputFile in InputFiles.Where(f => !f.StartsWith("!", StringComparison.Ordinal)))
            {
                int globIndex = inputFile.IndexOf('*');

                if (globIndex > -1)
                {
                    string relative = string.Empty;
                    int    last     = inputFile.LastIndexOf('/', globIndex);

                    if (last > -1)
                    {
                        relative = inputFile.Substring(0, last + 1);
                    }

                    var output    = GetAbsoluteOutputFile();
                    var outputMin = BundleFileProcessor.GetMinFileName(output);

                    string searchDir = new FileInfo(Path.Combine(folder, relative).Replace("/", "\\")).FullName;
                    var    allFiles  = Directory.EnumerateFiles(searchDir, "*" + ext, SearchOption.AllDirectories).Select(f => f.Replace(folder + "\\", ""));

                    var matches = Minimatcher.Filter(allFiles, inputFile, options).Select(f => Path.Combine(folder, f));
                    matches = matches.Where(match => match != output && match != outputMin);
                    files.AddRange(matches.Where(f => !files.Contains(f)));
                }
                else
                {
                    string fullPath = Path.Combine(folder, inputFile).Replace("/", "\\");

                    if (Directory.Exists(fullPath))
                    {
                        DirectoryInfo dir      = new DirectoryInfo(fullPath);
                        SearchOption  search   = SearchOption.TopDirectoryOnly;
                        var           dirFiles = dir.GetFiles("*" + Path.GetExtension(OutputFileName), search);
                        files.AddRange(dirFiles.Select(f => f.FullName).Where(f => !files.Contains(f)));
                    }
                    else
                    {
                        files.Add(fullPath);
                    }
                }
            }

            // Remove files starting with a !
            foreach (string inputFile in InputFiles)
            {
                int globIndex = inputFile.IndexOf('!');

                if (globIndex == 0)
                {
                    var allFiles = files.Select(f => f.Replace(folder + "\\", ""));
                    var matches  = Minimatcher.Filter(allFiles, inputFile, options).Select(f => Path.Combine(folder, f));
                    files = matches.ToList();
                }
            }

            return(files);
        }
Example #28
0
        /// <summary>
        /// Returns a list of absolute file paths of all matching input files.
        /// </summary>
        /// <param name="notifyOnPatternMiss">Writes to the Console if any input file is missing on disk.</param>
        public List <string> GetAbsoluteInputFiles(bool notifyOnPatternMiss = false)
        {
            List <string> files = new List <string>();

            if (!InputFiles.Any())
            {
                return(files);
            }

            string  folder  = new DirectoryInfo(Path.GetDirectoryName(FileName)).FullName;
            string  ext     = Path.GetExtension(InputFiles.First());
            Options options = new Options {
                AllowWindowsPaths = true
            };

            foreach (string inputFile in InputFiles.Where(f => !f.StartsWith("!", StringComparison.Ordinal)))
            {
                int globIndex = inputFile.IndexOf('*');

                if (globIndex > -1)
                {
                    string relative = string.Empty;
                    int    last     = inputFile.LastIndexOf('/', globIndex);

                    if (last > -1)
                    {
                        relative = inputFile.Substring(0, last + 1);
                    }

                    var output    = GetAbsoluteOutputFile();
                    var outputMin = BundleMinifier.GetMinFileName(output);

                    string searchDir = new FileInfo(Path.Combine(folder, relative).NormalizePath()).FullName;
                    var    allFiles  = Directory.EnumerateFiles(searchDir, "*" + ext, SearchOption.AllDirectories).Select(f => f.Replace(folder + FileHelpers.PathSeparatorChar, ""));

                    var matches = Minimatcher.Filter(allFiles, inputFile, options).Select(f => Path.Combine(folder, f));
                    matches = matches.Where(match => match != output && match != outputMin).ToList();

                    if (notifyOnPatternMiss && !matches.Any())
                    {
                        Console.WriteLine($"  No files matched the pattern {inputFile}".Orange().Bright());
                    }

                    files.AddRange(matches.Where(f => !files.Contains(f)));
                }
                else
                {
                    string fullPath = Path.Combine(folder, inputFile.NormalizePath());

                    if (Directory.Exists(fullPath))
                    {
                        DirectoryInfo dir       = new DirectoryInfo(fullPath);
                        SearchOption  search    = SearchOption.TopDirectoryOnly;
                        var           dirFiles  = dir.GetFiles("*" + Path.GetExtension(OutputFileName), search);
                        var           collected = dirFiles.Select(f => f.FullName).Where(f => !files.Contains(f)).ToList();

                        if (notifyOnPatternMiss && collected.Count == 0)
                        {
                            Console.WriteLine($"  No files were found in {inputFile}".Orange().Bright());
                        }

                        files.AddRange(collected);
                    }
                    else
                    {
                        files.Add(fullPath);

                        if (notifyOnPatternMiss && !File.Exists(fullPath))
                        {
                            Console.WriteLine($"  {inputFile} was not found".Orange().Bright());
                        }
                    }
                }
            }

            // Remove files starting with a !
            foreach (string inputFile in InputFiles)
            {
                int globIndex = inputFile.IndexOf('!');

                if (globIndex == 0)
                {
                    var allFiles = files.Select(f => f.Replace(folder + FileHelpers.PathSeparatorChar, ""));
                    var matches  = Minimatcher.Filter(allFiles, inputFile, options).Select(f => Path.Combine(folder, f));
                    files = matches.ToList();
                }
            }

            return(files);
        }
Example #29
0
 ///<summary>Filters a list of inputs against a single pattern.</summary>
 ///<remarks>This function reparses this input on each invocation.  For performance, avoid this function and reuse a Minimatcher instance instead.</remarks>
 public static IEnumerable<string> Filter(IEnumerable<string> list, string pattern, Options options = null)
 {
     var mm = new Minimatcher(pattern, options);
     list = list.Where(mm.IsMatch);
     if (options != null && options.NoNull)
         list = list.DefaultIfEmpty(pattern);
     return list;
 }
Example #30
0
            ///<summary>Creates a filter function that tests input against a pattern.</summary>
            public static Func<string, bool> CreateFilter(string pattern, Options options = null)
            {
                if (pattern == null) throw new ArgumentNullException("pattern");
                // "" only matches ""
                if (String.IsNullOrWhiteSpace(pattern)) return String.IsNullOrEmpty;

                var m = new Minimatcher(pattern, options);
                return m.IsMatch;
            }
Example #31
0
        public static List <string> GetProjectFiles(this ICollection <string> patterns, bool recursive = false)
        {
            var result = new List <string>();

            foreach (var p in patterns)
            {
                var pattern = p ?? "";

                if (recursive)
                {
                    pattern = pattern.NativeToUnix().Trailing('/') + "**";
                }

                if (pattern.IsValidPath() && (
                        File.Exists(pattern) ||
                        Directory.Exists(pattern)
                        ))
                {
                    result.Add(GetProjectFile(pattern));
                    continue;
                }

                if (pattern.IndexOf('*') == -1)
                {
                    throw new FileNotFoundException("No such file or directory: " + pattern);
                }

                if (pattern.IndexOf("..", StringComparison.Ordinal) != -1)
                {
                    throw new Exception("'..' is not supported in glob pattern: " + p);
                }

                while (pattern.StartsWith("./") || pattern.StartsWith(".\\"))
                {
                    pattern = pattern.Substring(2);
                }

                var glob = new Minimatcher(pattern, new Options {
                    IgnoreCase = true, AllowWindowsPaths = true
                });
                var root = Directory.GetCurrentDirectory();

                foreach (var e in Directory.EnumerateFiles(
                             root, "*.unoproj",
                             pattern.Contains("**")
                                ? SearchOption.AllDirectories
                                : SearchOption.TopDirectoryOnly))
                {
                    if (glob.IsMatch(e.Substring(root.Length + 1)))
                    {
                        result.Add(e);
                    }
                }
            }

            if (result.Count == 0)
            {
                throw new FileNotFoundException("No project".Plural(patterns) + " matching: " + string.Join(", ", patterns));
            }

            return(result);
        }
 public GlobbingPattern(string pattern)
 {
     _matcher = new Minimatcher(pattern);
 }
Example #33
0
        /// <inheritdoc/>
        public override async Task <FtpResponse> Process(FtpCommand command, CancellationToken cancellationToken)
        {
            await Connection.WriteAsync(new FtpResponse(150, "Opening data connection."), cancellationToken);

            ITcpSocketClient responseSocket;

            try
            {
                responseSocket = await Connection.CreateResponseSocket();
            }
            catch (Exception)
            {
                return(new FtpResponse(425, "Can't open data connection."));
            }
            try
            {
                // Parse arguments in a way that's compatible with broken FTP clients
                var argument   = new ListArguments(command.Argument);
                var showHidden = argument.All;

                // Instantiate the formatter
                IListFormatter formatter;
                if (string.Equals(command.Name, "NLST", StringComparison.OrdinalIgnoreCase))
                {
                    formatter = new ShortListFormatter();
                }
                else if (string.Equals(command.Name, "LS", StringComparison.OrdinalIgnoreCase))
                {
                    formatter = new LongListFormatter();
                }
                else
                {
                    formatter = new LongListFormatter();
                }

                // Parse the given path to determine the mask (e.g. when information about a file was requested)
                var directoriesToProcess = new Queue <DirectoryQueueItem>();

                // Use braces to avoid the definition of mask and path in the following parts
                // of this function.
                {
                    var mask = "*";
                    var path = Data.Path.Clone();

                    if (!string.IsNullOrEmpty(argument.Path))
                    {
                        var foundEntry = await Data.FileSystem.SearchEntryAsync(path, argument.Path, cancellationToken);

                        if (foundEntry?.Directory == null)
                        {
                            return(new FtpResponse(550, "File system entry not found."));
                        }
                        var dirEntry = foundEntry.Entry as IUnixDirectoryEntry;
                        if (dirEntry == null)
                        {
                            mask = foundEntry.FileName;
                        }
                        else if (!dirEntry.IsRoot)
                        {
                            path.Push(dirEntry);
                        }
                    }
                    directoriesToProcess.Enqueue(new DirectoryQueueItem(path, mask));
                }

                var encoding = Data.NlstEncoding ?? Connection.Encoding;

                using (var stream = await Connection.CreateEncryptedStream(responseSocket.WriteStream))
                {
                    using (var writer = new StreamWriter(stream, encoding, 4096, true)
                    {
                        NewLine = "\r\n",
                    })
                    {
                        while (directoriesToProcess.Count != 0)
                        {
                            var queueItem = directoriesToProcess.Dequeue();

                            var currentPath     = queueItem.Path;
                            var mask            = queueItem.Mask;
                            var currentDirEntry = currentPath.Count != 0 ? currentPath.Peek() : Data.FileSystem.Root;

                            if (argument.Recursive)
                            {
                                var line = currentPath.ToDisplayString() + ":";
                                Connection.Log?.Debug(line);
                                await writer.WriteLineAsync(line);
                            }

                            var mmOptions = new Options()
                            {
                                IgnoreCase = Data.FileSystem.FileSystemEntryComparer.Equals("a", "A"),
                                NoGlobStar = true,
                                Dot        = true,
                            };

                            var mm = new Minimatcher(mask, mmOptions);

                            var entries = await Data.FileSystem.GetEntriesAsync(currentDirEntry, cancellationToken);

                            var enumerator = new DirectoryListingEnumerator(entries, Data.FileSystem, currentPath, true);
                            while (enumerator.MoveNext())
                            {
                                var name = enumerator.Name;
                                if (!enumerator.IsDotEntry)
                                {
                                    if (!mm.IsMatch(name))
                                    {
                                        continue;
                                    }
                                    if (name.StartsWith(".") && !showHidden)
                                    {
                                        continue;
                                    }
                                }

                                var entry = enumerator.Entry;

                                if (argument.Recursive && !enumerator.IsDotEntry)
                                {
                                    var dirEntry = entry as IUnixDirectoryEntry;
                                    if (dirEntry != null)
                                    {
                                        var subDirPath = currentPath.Clone();
                                        subDirPath.Push(dirEntry);
                                        directoriesToProcess.Enqueue(new DirectoryQueueItem(subDirPath, "*"));
                                    }
                                }

                                var line = formatter.Format(entry, name);
                                Connection.Log?.Debug(line);
                                await writer.WriteLineAsync(line);
                            }
                        }
                    }
                }
            }
            finally
            {
                responseSocket.Dispose();
            }

            // Use 250 when the connection stays open.
            return(new FtpResponse(250, "Closing data connection."));
        }
        public void VerifyMinimatcherSearchPatternMatch()
        {
            var matcher = new Minimatcher("/test/*", new Minimatch.Options {IgnoreCase = true});

            Assert.IsTrue(matcher.IsMatch("/test/dummy.csv"));
            Assert.IsTrue(matcher.IsMatch("/TEST/dummy.csv"));
            Assert.IsFalse(matcher.IsMatch("/second/dummy.csv"));
        }