Use MatchPattern property of FindMatchingFiles workflow activity

后端 未结 3 567
心在旅途
心在旅途 2021-01-02 09:02

I\'m customizing the default workflow of build process template using TFS 2010 Team Build. There is an activity named FindMatchingFiles allows to search for spe

3条回答
  •  难免孤独
    2021-01-02 09:22

    FindMatchingFiles has some strange search pattern. Here is the code (decompiled using ILSpy) so you can test your search patterns without having to kick off a new build.

    It contains a hardcoded root dir at the following location (Z:)

    List matchingDirectories = GetMatchingDirectories(@"Z:", matchPattern.Substring(0, num), 0);
    

    Code:

    using System;
    using System.Collections.Generic;
    using System.IO;
    
    namespace DirectoryGetFiles
    {
    
        ////    Use the FindMatchingFiles activity to find files. Specify the search criteria in the MatchPattern (String) property.
        ////    In this property, you can specify an argument that includes the following elements: 
        ////        Syntax that is supported by the searchPattern argument of the Directory GetFiles(String, String) method.
        ////
        ////        ** to specify a recursive search. For example:
        ////            To search the sources directory for text files, you could specify something that resembles the following
        ////            value for the MatchPattern property: String.Format("{0}\**\*.txt", SourcesDirectory).
        ////            
        ////            To search the sources directory for text files in one or more subdirectories that are called txtfiles, 
        ////            you could specify something that resembles the following value for the MatchPattern property: 
        ////            String.Format("{0}\**\txtfiles\*.txt", SourcesDirectory).
    
        class Program
        {
            static void Main(string[] args)
            {
                string searchPattern = @"_PublishedWebsites\Web\Scripts\jasmine-specs**\*.js";
                var results = Execute(searchPattern);
    
                foreach (var i in results)
                {
                    Console.WriteLine("found: {0}", i);
                }
    
                Console.WriteLine("Done...");
                Console.ReadLine();
            }
    
            private static IEnumerable Execute(string pattern)
            {
                string text = pattern;
                text = text.Replace(";;", "\0");
                var hashSet = new HashSet(StringComparer.OrdinalIgnoreCase);
                string[] array = text.Split(new char[]
        {
            ';'
        }, StringSplitOptions.RemoveEmptyEntries);
    
                for (int i = 0; i < array.Length; i++)
                {
                    string text2 = array[i];
                    string text3 = text2.Replace("\0", ";");
                    if (IsValidPattern(text3))
                    {
                        List list = ComputeMatchingPaths(text3);
                        if (list.Count > 0)
                        {
                            using (List.Enumerator enumerator = list.GetEnumerator())
                            {
                                while (enumerator.MoveNext())
                                {
                                    string current = enumerator.Current;
                                    hashSet.Add(current);
                                }
                                goto IL_15C;
                            }
                        }
                        ////Message = ActivitiesResources.Format("NoMatchesForSearchPattern", new object[]
                    }
                    else
                    {
                        ////       Message = ActivitiesResources.Format("InvalidSearchPattern", new object[]
                    }
    
                IL_15C: ;
                }
    
                return hashSet;//.OrderBy((string x) => x, FileSpec.TopDownComparer);
            }
    
            private static bool IsValidPattern(string pattern)
            {
                string text = "**" + Path.DirectorySeparatorChar;
                int num = pattern.IndexOf(text, StringComparison.Ordinal);
                return (num < 0 || (pattern.IndexOf(text, num + text.Length, StringComparison.OrdinalIgnoreCase) <= 0 && pattern.IndexOf(Path.DirectorySeparatorChar, num + text.Length) <= 0)) && pattern[pattern.Length - 1] != Path.DirectorySeparatorChar;
            }
    
            private static List ComputeMatchingPaths(string matchPattern)
            {
                List list = new List();
                string text = "**" + Path.DirectorySeparatorChar;
                int num = matchPattern.IndexOf(text, 0, StringComparison.OrdinalIgnoreCase);
                if (num >= 0)
                {
                    List matchingDirectories = GetMatchingDirectories(@"Z:", matchPattern.Substring(0, num), 0);
                    string searchPattern = matchPattern.Substring(num + text.Length);
                    using (List.Enumerator enumerator = matchingDirectories.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            string current = enumerator.Current;
                            list.AddRange(Directory.GetFiles(current, searchPattern, SearchOption.AllDirectories));
                        }
                        return list;
                    }
                }
                int num2 = matchPattern.LastIndexOf(Path.DirectorySeparatorChar);
                if (num2 >= 0)
                {
                    List matchingDirectories2 = GetMatchingDirectories(string.Empty, matchPattern.Substring(0, num2 + 1), 0);
                    string searchPattern2 = matchPattern.Substring(num2 + 1);
                    using (List.Enumerator enumerator2 = matchingDirectories2.GetEnumerator())
                    {
                        while (enumerator2.MoveNext())
                        {
                            string current2 = enumerator2.Current;
                            try
                            {
                                list.AddRange(Directory.GetFiles(current2, searchPattern2, SearchOption.TopDirectoryOnly));
                            }
                            catch
                            {
                            }
                        }
                        return list;
                    }
                }
                try
                {
                    list.AddRange(Directory.GetFiles(Directory.GetCurrentDirectory(), matchPattern, SearchOption.TopDirectoryOnly));
                }
                catch
                {
                }
                return list;
            }
    
            private static List GetMatchingDirectories(string rootDir, string pattern, int level)
            {
                if (level > 129)
                {
                    return new List();
                }
                List list = new List();
                int num = pattern.IndexOf('*');
                if (num >= 0)
                {
                    int num2 = pattern.Substring(0, num).LastIndexOf(Path.DirectorySeparatorChar);
                    string text = (num2 >= 0) ? Path.Combine(rootDir, pattern.Substring(0, num2 + 1)) : rootDir;
                    if (text.Equals(string.Empty))
                    {
                        text = Directory.GetCurrentDirectory();
                    }
                    int num3 = pattern.IndexOf(Path.DirectorySeparatorChar, num);
                    if (num3 < 0)
                    {
                        num3 = pattern.Length;
                    }
                    string searchPattern = pattern.Substring(num2 + 1, num3 - num2 - 1);
                    try
                    {
                        string[] directories = Directory.GetDirectories(text, searchPattern, SearchOption.TopDirectoryOnly);
                        if (num3 < pattern.Length - 1)
                        {
                            string pattern2 = pattern.Substring(num3 + 1);
                            string[] array = directories;
                            for (int i = 0; i < array.Length; i++)
                            {
                                string rootDir2 = array[i];
                                list.AddRange(GetMatchingDirectories(rootDir2, pattern2, level + 1));
                            }
                        }
                        else
                        {
                            list.AddRange(directories);
                        }
                        return list;
                    }
                    catch
                    {
                        return list;
                    }
                }
                string text2 = Path.Combine(rootDir, pattern);
                if (text2.Equals(string.Empty))
                {
                    list.Add(Directory.GetCurrentDirectory());
                }
                else
                {
                    if (Directory.Exists(text2))
                    {
                        list.Add(Path.GetFullPath(text2));
                    }
                }
                return list;
            }
        }
    }
    

提交回复
热议问题