Beispiel #1
0
        /// <summary>
        /// Finds a tool in the file system
        /// </summary>
        /// <param name="filename">The name of the tool</param>
        /// <param name="executableType">Platform or assembly info</param>
        /// <param name="minimumVersion">Minimum required version</param>
        /// <param name="excludeFilters">A list of tool names which should be excluded from the scan</param>
        /// <param name="includeFilters">A list of tool names which should be included in the scan</param>
        /// <param name="rememberMissingFile">Disables searching for files which are known to be missing.</param>
        /// <param name="tagWithCosmeticVersion"></param>
        /// <returns></returns>
        public string ScanForFile(string filename, ExecutableInfo executableType = ExecutableInfo.none, string minimumVersion = "0.0",
                                  IEnumerable <string> excludeFilters            = null, IEnumerable <string> includeFilters  = null, bool rememberMissingFile = false, string tagWithCosmeticVersion = null)
        {
            if (!IgnoreCache)
            {
                var result = GetCachedPath(filename, executableType, tagWithCosmeticVersion ?? minimumVersion);

                if ("NOT-FOUND".Equals(result))
                {
                    if (rememberMissingFile)
                    {
                        return(null); // we've asked to remember that we haven't found this
                    }
                    result = null;    // we've not asked to remember that, so we'll let it try to find it again.
                }

                if (!string.IsNullOrEmpty(result))
                {
                    return(result);
                }
            }

            Notify("[One moment.. Scanning for utility({0}/{1}/{2})]", filename, executableType.ToString(), tagWithCosmeticVersion ?? minimumVersion);

            var ver = VersionStringToUInt64(minimumVersion);

            var files = _commonSearchLocations.Union(_searchLocations).SelectMany(
                directory => directory.DirectoryEnumerateFilesSmarter("**\\" + filename, SearchOption.TopDirectoryOnly))
                        .Union(
                _recursiveSearchLocations.AsParallel().SelectMany(
                    directory => directory.DirectoryEnumerateFilesSmarter("**\\" + filename, SearchOption.AllDirectories)));



            if (executableType != ExecutableInfo.none || ver != 0)
            {
                files =
                    files.Where(file =>
                                (PEInfo.Scan(file).ExecutableInfo & executableType) == executableType &&
                                PEInfo.Scan(file).FileVersionLong >= ver);
            }

            if (includeFilters != null)
            {
                files = includeFilters.Aggregate(files, (current, filter) => (from eachFile in current
                                                                              where eachFile.ToLower().NewIsWildcardMatch(filter.ToLower())
                                                                              select eachFile));
            }


            if (excludeFilters != null)
            {
                files = excludeFilters.Aggregate(files, (current, filter) => (from eachFile in current
                                                                              where !eachFile.ToLower().NewIsWildcardMatch(filter.ToLower())
                                                                              select eachFile));
            }



            var filePath = files.MaxElement(each => PEInfo.Scan(each).FileVersionLong);

            if (!string.IsNullOrEmpty(filePath) || rememberMissingFile)
            {
                SetCachedPath(filename, string.IsNullOrEmpty(filePath) ? "NOT-FOUND" : filePath, executableType, tagWithCosmeticVersion ?? minimumVersion);
                try {
                    SetCachedPath(filename, string.IsNullOrEmpty(filePath) ? "NOT-FOUND" : filePath, executableType,
                                  PEInfo.Scan(filePath).FileVersion);
                } catch {
                }
            }

            return(filePath);
        }