/// <summary>
        /// Gets the extended search paths for about_topics help. To be able to get about_topics help from unloaded modules,
        /// we will add $pshome and the folders under PS module paths to the collection of paths to search.
        /// </summary>
        /// <returns>A collection of string representing locations.</returns>
        internal Collection <string> GetExtendedSearchPaths()
        {
            Collection <string> searchPaths = GetSearchPaths();

            // Add $pshome at the top of the list
            string defaultShellSearchPath = GetDefaultShellSearchPath();

            int index = searchPaths.IndexOf(defaultShellSearchPath);

            if (index != 0)
            {
                if (index > 0)
                {
                    searchPaths.RemoveAt(index);
                }

                searchPaths.Insert(0, defaultShellSearchPath);
            }

            // Add the CurrentUser help path.
            searchPaths.Add(HelpUtils.GetUserHomeHelpSearchPath());

            // Add modules that are not loaded. Since using 'get-module -listavailable' is very expensive,
            // we load all the directories (which are not empty) under the module path.
            foreach (string psModulePath in ModuleIntrinsics.GetModulePath(false, this.HelpSystem.ExecutionContext))
            {
                if (Directory.Exists(psModulePath))
                {
                    try
                    {
                        // Get all the directories under the module path
                        // * and SearchOption.AllDirectories gets all the version directories.
                        string[] directories = Directory.GetDirectories(psModulePath, "*", SearchOption.AllDirectories);

                        var possibleModuleDirectories = directories.Where(static directory => !ModuleUtils.IsPossibleResourceDirectory(directory));
Exemplo n.º 2
0
        /// <summary>
        /// Gets the extended search paths for about_topics help. To be able to get about_topics help from unloaded modules,
        /// we will add $pshome and the folders under PS module paths to the collection of paths to search.
        /// </summary>
        /// <returns>A collection of string representing locations.</returns>
        internal Collection <string> GetExtendedSearchPaths()
        {
            Collection <string> searchPaths = GetSearchPaths();

            // Add $pshome at the top of the list
            string defaultShellSearchPath = GetDefaultShellSearchPath();

            int index = searchPaths.IndexOf(defaultShellSearchPath);

            if (index != 0)
            {
                if (index > 0)
                {
                    searchPaths.RemoveAt(index);
                }

                searchPaths.Insert(0, defaultShellSearchPath);
            }

            // Add the CurrentUser help path.
            searchPaths.Add(HelpUtils.GetUserHomeHelpSearchPath());

            // Add modules that are not loaded. Since using 'get-module -listavailable' is very expensive,
            // we load all the directories (which are not empty) under the module path.
            foreach (string psModulePath in ModuleIntrinsics.GetModulePath(false, this.HelpSystem.ExecutionContext))
            {
                if (Directory.Exists(psModulePath))
                {
                    try
                    {
                        // Get all the directories under the module path
                        // * and SearchOption.AllDirectories gets all the version directories.
                        string[] directories = Directory.GetDirectories(psModulePath, "*", SearchOption.AllDirectories);

                        var possibleModuleDirectories = directories.Where(directory => !ModuleUtils.IsPossibleResourceDirectory(directory));

                        foreach (string directory in possibleModuleDirectories)
                        {
                            // Add only directories that are not empty
                            if (Directory.EnumerateFiles(directory).Any())
                            {
                                if (!searchPaths.Contains(directory))
                                {
                                    searchPaths.Add(directory);
                                }
                            }
                        }
                    }
                    // Absorb any exception related to enumerating directories
                    catch (System.ArgumentException) { }
                    catch (System.IO.IOException) { }
                    catch (System.UnauthorizedAccessException) { }
                    catch (System.Security.SecurityException) { }
                }
            }

            return(searchPaths);
        }
Exemplo n.º 3
0
        private Collection <string> FilterToLatestModuleVersion(Collection <string> filesMatched)
        {
            Collection <string> matchedFilesToRemove = new Collection <string>();

            if (filesMatched.Count > 1)
            {
                //Dictionary<<ModuleName,fileName>, <Version, helpFileFullName>>
                Dictionary <Tuple <string, string>, Tuple <string, Version> > modulesAndVersion = new Dictionary <Tuple <string, string>, Tuple <string, Version> >();
                HashSet <string> filesProcessed = new HashSet <string>();

                var allPSModulePaths = ModuleIntrinsics.GetModulePath(false, this.HelpSystem.ExecutionContext);

                foreach (string fileFullName in filesMatched)
                {
                    // Use the filename as a check if we need to process further.
                    // Single module can have multiple .help.txt files.
                    var fileName = Path.GetFileName(fileFullName);

                    foreach (string psModulePath in allPSModulePaths)
                    {
                        Version moduleVersionFromPath = null;
                        string  moduleName            = null;
                        GetModuleNameAndVersion(psModulePath, fileFullName, out moduleName, out moduleVersionFromPath);

                        //Skip modules whose root we cannot determine or which do not have versions.
                        if (moduleVersionFromPath != null && moduleName != null)
                        {
                            Tuple <string, Version> moduleVersion = null;
                            Tuple <string, string>  key           = new Tuple <string, string>(moduleName, fileName);
                            if (modulesAndVersion.TryGetValue(key, out moduleVersion))
                            {
                                //Consider for further processing only if the help file name is same.
                                if (filesProcessed.Contains(fileName))
                                {
                                    if (moduleVersionFromPath > moduleVersion.Item2)
                                    {
                                        modulesAndVersion[key] = new Tuple <string, Version>(fileFullName, moduleVersionFromPath);

                                        //Remove the old file since we found a newer version.
                                        matchedFilesToRemove.Add(moduleVersion.Item1);
                                    }
                                    else
                                    {
                                        //Remove the new file as higher version item is already in dictionary.
                                        matchedFilesToRemove.Add(fileFullName);
                                    }
                                }
                            }
                            else
                            {
                                //Add the module to the dictionary as it was not processes earlier.
                                modulesAndVersion.Add(new Tuple <string, string>(moduleName, fileName),
                                                      new Tuple <string, Version>(fileFullName, moduleVersionFromPath));
                            }
                        }
                    }

                    filesProcessed.Add(fileName);
                }
            }

            return(matchedFilesToRemove);
        }