示例#1
0
        private void Init(IEnumerable <string> commandPatterns, IEnumerable <string> searchPath, ExecutionContext context)
        {
            // Note, discovery must be set before resolving the current directory

            _context  = context;
            _patterns = commandPatterns;

            _lookupPaths = new LookupPathCollection(searchPath);
            ResolveCurrentDirectoryInLookupPaths();

            this.Reset();
        }
        internal CommandPathSearch(
            string commandName,
            LookupPathCollection lookupPaths,
            ExecutionContext context,
            Collection <string>?acceptableCommandNames,
            bool useFuzzyMatch)
        {
            _useFuzzyMatch = useFuzzyMatch;
            string[] commandPatterns;
            if (acceptableCommandNames != null)
            {
                // The name passed in is not a pattern. To minimize enumerating the file system, we
                // turn the command name into a pattern and then match against extensions in PATHEXT.
                // The old code would enumerate the file system many more times, once per possible extension.
                if (Platform.IsWindows)
                {
                    commandPatterns = new[] { commandName + ".*" };
                }
                else
                {
                    // Porting note: on non-Windows platforms, we want to always allow just 'commandName'
                    // as an acceptable command name. However, we also want to allow commands to be
                    // called with the .ps1 extension, so that 'script.ps1' can be called by 'script'.
                    commandPatterns = new[] { commandName, commandName + ".ps1" };
                }

                _postProcessEnumeratedFiles = CheckAgainstAcceptableCommandNames;
                _acceptableCommandNames     = acceptableCommandNames;
            }
            else
            {
                commandPatterns             = new[] { commandName };
                _postProcessEnumeratedFiles = JustCheckExtensions;
            }

            // Note, discovery must be set before resolving the current directory
            _context     = context;
            _patterns    = commandPatterns;
            _lookupPaths = lookupPaths;
            ResolveCurrentDirectoryInLookupPaths();

            _orderedPathExt = CommandDiscovery.PathExtensionsWithPs1Prepended;

            // The same as in this.Reset()
            _lookupPathsEnumerator             = _lookupPaths.GetEnumerator();
            _patternEnumerator                 = _patterns.GetEnumerator();
            _currentDirectoryResults           = Array.Empty <string>();
            _currentDirectoryResultsEnumerator = _currentDirectoryResults.GetEnumerator();
            _justReset = true;
        }
示例#3
0
 internal CommandPathSearch(
     IEnumerable <string> patterns,
     IEnumerable <string> lookupPaths,
     ExecutionContext context)
 {
     if (patterns == null)
     {
         throw CommandPathSearch.tracer.NewArgumentNullException(nameof(patterns));
     }
     if (lookupPaths == null)
     {
         throw CommandPathSearch.tracer.NewArgumentNullException(nameof(lookupPaths));
     }
     this._context    = context != null ? context : throw CommandPathSearch.tracer.NewArgumentNullException(nameof(context));
     this.patterns    = patterns;
     this.lookupPaths = new LookupPathCollection(lookupPaths);
     this.ResolveCurrentDirectoryInLookupPaths();
     this.Reset();
 }
示例#4
0
 internal CommandPathSearch(IEnumerable <string> patterns, IEnumerable <string> lookupPaths, HashSet <string> allowedExtensions, bool allowAnyExtension, ExecutionContext context)
 {
     if (patterns == null)
     {
         throw PSTraceSource.NewArgumentNullException("patterns");
     }
     if (lookupPaths == null)
     {
         throw PSTraceSource.NewArgumentNullException("lookupPaths");
     }
     if (context == null)
     {
         throw PSTraceSource.NewArgumentNullException("context");
     }
     this._context    = context;
     this.patterns    = patterns;
     this.lookupPaths = new LookupPathCollection(lookupPaths);
     this.ResolveCurrentDirectoryInLookupPaths();
     this.Reset();
     this.allowAnyExtension = allowAnyExtension;
     this.allowedExtensions = allowedExtensions;
 }
示例#5
0
        internal IEnumerable <string> GetLookupDirectoryPaths()
        {
            LookupPathCollection lookupPathCollection = new LookupPathCollection();
            string environmentVariable = Environment.GetEnvironmentVariable("PATH");

            CommandDiscovery.discoveryTracer.WriteLine("PATH: {0}", (object)environmentVariable);
            if (environmentVariable == null || !string.Equals(this.pathCacheKey, environmentVariable, StringComparison.OrdinalIgnoreCase) || this.cachedPath == null)
            {
                this.cachedLookupPaths = (LookupPathCollection)null;
                this.pathCacheKey      = environmentVariable;
                if (this.pathCacheKey != null)
                {
                    string[] strArray = this.pathCacheKey.Split(new char[1]
                    {
                        ';'
                    }, StringSplitOptions.RemoveEmptyEntries);
                    if (strArray != null)
                    {
                        this.cachedPath = new Collection <string>();
                        foreach (string str1 in strArray)
                        {
                            string str2 = str1.TrimStart();
                            this.cachedPath.Add(str2);
                            lookupPathCollection.Add(str2);
                        }
                    }
                }
            }
            else
            {
                lookupPathCollection.AddRange((ICollection <string>) this.cachedPath);
            }
            if (this.cachedLookupPaths == null)
            {
                this.cachedLookupPaths = lookupPathCollection;
            }
            return((IEnumerable <string>) this.cachedLookupPaths);
        }