-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Refactor module code related to 'Get-Module -ListAvailable' #7145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ff97ae2
[Feature] Refactor module code related to 'Get-Module -ListAvailable'
daxian-dbw 1ca7e12
[Feature] Add tests for 'Get-Module -List -All'
daxian-dbw 406dfc2
[Feature] Address feedback about the 'string[]' parameter
daxian-dbw 4211401
Update a comment based on feedback
daxian-dbw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -869,37 +869,19 @@ internal List<PSModuleInfo> GetModule(string[] names, bool all, bool refresh) | |
| moduleNames.Add(n); | ||
| } | ||
| } | ||
| modulesToReturn.AddRange(GetModuleForRootedPaths(modulePaths.ToArray(), all, refresh)); | ||
| modulesToReturn.AddRange(GetModuleForRootedPaths(modulePaths, all, refresh)); | ||
| } | ||
|
|
||
| // If no names were passed to this function, then this API will return list of all available modules | ||
| if (names == null || moduleNames.Count > 0) | ||
| { | ||
| modulesToReturn.AddRange(GetModuleForNonRootedPaths(moduleNames.ToArray(), all, refresh)); | ||
| modulesToReturn.AddRange(GetModuleForNames(moduleNames, all, refresh)); | ||
| } | ||
|
|
||
| return modulesToReturn; | ||
| } | ||
|
|
||
| private IEnumerable<PSModuleInfo> GetModuleForNonRootedPaths(string[] names, bool all, bool refresh) | ||
| { | ||
| const WildcardOptions wildcardOptions = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant; | ||
| IEnumerable<WildcardPattern> patternList = SessionStateUtilities.CreateWildcardsFromStrings(names, wildcardOptions); | ||
|
|
||
| Dictionary<String, List<PSModuleInfo>> availableModules = GetAvailableLocallyModulesCore(names, all, refresh); | ||
| foreach (var entry in availableModules) | ||
| { | ||
| foreach (PSModuleInfo module in entry.Value) | ||
| { | ||
| if (SessionStateUtilities.MatchesAnyWildcardPattern(module.Name, patternList, true)) | ||
| { | ||
| yield return module; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(string[] modulePaths, bool all, bool refresh) | ||
| private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(List<string> modulePaths, bool all, bool refresh) | ||
| { | ||
| // This is to filter out duplicate modules | ||
| var modules = new HashSet<string>(StringComparer.OrdinalIgnoreCase); | ||
|
|
@@ -950,7 +932,7 @@ private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(string[] modulePaths, | |
|
|
||
| var availableModuleFiles = all | ||
| ? ModuleUtils.GetAllAvailableModuleFiles(resolvedModulePath) | ||
| : ModuleUtils.GetModuleVersionsFromAbsolutePath(resolvedModulePath); | ||
| : ModuleUtils.GetModuleFilesFromAbsolutePath(resolvedModulePath); | ||
|
|
||
| bool foundModule = false; | ||
| foreach (string file in availableModuleFiles) | ||
|
|
@@ -998,38 +980,31 @@ private ErrorRecord CreateModuleNotFoundError(string modulePath) | |
| return er; | ||
| } | ||
|
|
||
| private Dictionary<String, List<PSModuleInfo>> GetAvailableLocallyModulesCore(string[] names, bool all, bool refresh) | ||
| private IEnumerable<PSModuleInfo> GetModuleForNames(List<string> names, bool all, bool refresh) | ||
| { | ||
| var modules = new Dictionary<string, List<PSModuleInfo>>(StringComparer.OrdinalIgnoreCase); | ||
| var modulePaths = ModuleIntrinsics.GetModulePath(false, Context); | ||
|
|
||
| IEnumerable<PSModuleInfo> allModules = null; | ||
| HashSet<string> modulePathSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase); | ||
| bool cleanupModuleAnalysisAppDomain = Context.TakeResponsibilityForModuleAnalysisAppDomain(); | ||
|
|
||
| try | ||
| { | ||
| foreach (string path in ModuleIntrinsics.GetModulePath(false, Context)) | ||
| { | ||
| try | ||
| { | ||
| var availableModules = all | ||
| ? GetAllAvailableModules(path, refresh) | ||
| : GetDefaultAvailableModules(names, path, refresh); | ||
| string uniquePath = path.TrimEnd(Utils.Separators.Directory); | ||
|
|
||
| // Add the path in $env:PSModulePath as the keys of the dictionary | ||
| // If the paths are repeated, ignore the repetitions | ||
| string uniquePath = path.TrimEnd(Utils.Separators.Directory); | ||
| if (!modules.ContainsKey(uniquePath)) | ||
| { | ||
| modules.Add(uniquePath, availableModules.OrderBy(m => m.Name).ToList()); | ||
| } | ||
| } | ||
| catch (IOException) | ||
| // Ignore repeated module path. | ||
| if (!modulePathSet.Add(uniquePath)) { continue; } | ||
|
|
||
| try | ||
| { | ||
| continue; // ignore directories that can't be accessed | ||
| IEnumerable<PSModuleInfo> modulesFound = GetModulesFromOneModulePath( | ||
| names, uniquePath, all, refresh).OrderBy(m => m.Name); | ||
| allModules = allModules == null ? modulesFound : allModules.Concat(modulesFound); | ||
| } | ||
| catch (UnauthorizedAccessException) | ||
| catch (Exception e) when (e is IOException || e is UnauthorizedAccessException) | ||
| { | ||
| continue; // ignore directories that can't be accessed | ||
| // ignore directories that can't be accessed | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1041,64 +1016,43 @@ private Dictionary<String, List<PSModuleInfo>> GetAvailableLocallyModulesCore(st | |
| } | ||
| } | ||
|
|
||
| return modules; | ||
| // Make sure we always return a non-null collection. | ||
| return allModules ?? Utils.EmptyArray<PSModuleInfo>(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get a list of all modules | ||
| /// which can be imported just by specifying a non rooted file name of the module | ||
| /// (Import-Module foo\bar.psm1; but not Import-Module .\foo\bar.psm1) | ||
| /// Get modules based on the given names and module files. | ||
| /// </summary> | ||
| private IEnumerable<PSModuleInfo> GetAllAvailableModules(string directory, bool refresh) | ||
| private IEnumerable<PSModuleInfo> GetModulesFromOneModulePath(List<string> names, string modulePath, bool all, bool refresh) | ||
| { | ||
| var availableModuleFiles = ModuleUtils.GetAllAvailableModuleFiles(directory); | ||
|
|
||
| foreach (string file in availableModuleFiles) | ||
| const WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant; | ||
| IEnumerable<WildcardPattern> namePatterns = null; | ||
| if (names != null && names.Count > 0) | ||
| { | ||
| PSModuleInfo module = CreateModuleInfoForGetModule(file, refresh); | ||
| if (module != null) | ||
| { | ||
| yield return module; | ||
| } | ||
| namePatterns = SessionStateUtilities.CreateWildcardsFromStrings(names, options); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get a list of the available modules | ||
| /// which can be imported just by specifying a non rooted directory name of the module | ||
| /// (Import-Module foo\bar; but not Import-Module .\foo\bar or Import-Module .\foo\bar.psm1) | ||
| /// </summary> | ||
| private List<PSModuleInfo> GetDefaultAvailableModules(string[] name, string directory, bool refresh) | ||
| { | ||
| List<PSModuleInfo> availableModules = new List<PSModuleInfo>(); | ||
|
|
||
| const WildcardOptions wildcardOptions = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant; | ||
| IEnumerable<WildcardPattern> patternList = SessionStateUtilities.CreateWildcardsFromStrings(name, wildcardOptions); | ||
| IEnumerable<string> moduleFiles = all | ||
| ? ModuleUtils.GetAllAvailableModuleFiles(modulePath) | ||
| : ModuleUtils.GetDefaultAvailableModuleFiles(modulePath); | ||
|
|
||
| var availableModuleFiles = ModuleUtils.GetDefaultAvailableModuleFiles(directory); | ||
|
|
||
| foreach (string file in availableModuleFiles) | ||
| foreach (string file in moduleFiles) | ||
| { | ||
| string actualModuleName = System.IO.Path.GetFileNameWithoutExtension(file); | ||
| if (SessionStateUtilities.MatchesAnyWildcardPattern(actualModuleName, patternList, true)) | ||
| if (namePatterns == null || | ||
| SessionStateUtilities.MatchesAnyWildcardPattern( | ||
| Path.GetFileNameWithoutExtension(file), namePatterns, defaultValue: true)) | ||
| { | ||
| PSModuleInfo module = CreateModuleInfoForGetModule(file, refresh); | ||
| if (module == null) { continue; } | ||
|
|
||
| if (module != null) | ||
| if (all || !ModuleUtils.IsModuleInVersionSubdirectory(file, out Version directoryVersion) || directoryVersion == module.Version) | ||
| { | ||
| Version directoryVersion; | ||
| if (!ModuleUtils.IsModuleInVersionSubdirectory(file, out directoryVersion) | ||
| || directoryVersion == module.Version) | ||
| { | ||
| availableModules.Add(module); | ||
| } | ||
| yield return module; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ClearAnalysisCaches(); | ||
|
|
||
| return availableModules; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -1191,24 +1145,24 @@ private PSModuleInfo CreateModuleInfoForGetModule(string file, bool refresh) | |
| moduleInfo = LoadModuleManifest( | ||
| scriptInfo, | ||
| flags /* - don't write errors, don't load elements */, | ||
| null, | ||
| null, | ||
| null, | ||
| null); | ||
| minimumVersion: null, | ||
|
||
| maximumVersion: null, | ||
| requiredVersion: null, | ||
| requiredModuleGuid: null); | ||
| } | ||
| else | ||
| { | ||
| // It's not a module manifest, process the individual module | ||
| ImportModuleOptions options = new ImportModuleOptions(); | ||
| bool found = false; | ||
|
|
||
| moduleInfo = LoadModule(file, null, String.Empty, null, ref options, flags, out found); | ||
| moduleInfo = LoadModule(file, moduleBase: null, prefix: String.Empty, ss: null, ref options, flags, out found); | ||
| } | ||
|
|
||
| // return fake PSModuleInfo if can't read the file for any reason | ||
| if (moduleInfo == null) | ||
| { | ||
| moduleInfo = new PSModuleInfo(file, null, null); | ||
| moduleInfo = new PSModuleInfo(file, context: null, sessionState: null); | ||
| moduleInfo.HadErrorsLoading = true; // Prevent analysis cache from caching a bad module. | ||
| } | ||
|
|
||
|
|
@@ -2677,9 +2631,13 @@ internal PSModuleInfo LoadModuleManifest( | |
| } | ||
| } | ||
|
|
||
| var needToAnalyzeScriptModules = usedWildcard; | ||
| // We have to further analyze the module if any wildcard characters are used. | ||
| bool needToAnalyzeScriptModules = usedWildcard; | ||
|
|
||
| if (!needToAnalyzeScriptModules) | ||
| // We can skip further analysis if 'FunctionsToExport', 'CmdletsToExport' and 'AliasesToExport' | ||
| // are all declared and no wildcard character is used for them. But if any of 'FunctionsToExport', | ||
| // 'CmdletsToExport' or 'AliasesToExport' were not given, we must check to see if more analysis is needed. | ||
| if (!needToAnalyzeScriptModules && (!sawExportedCmdlets || !sawExportedFunctions || !sawExportedAliases)) | ||
| { | ||
| foreach (var nestedModule in nestedModules) | ||
| { | ||
|
|
@@ -3173,9 +3131,9 @@ internal PSModuleInfo LoadModuleManifest( | |
| newManifestInfo.DeclaredCmdletExports = manifestInfo.DeclaredCmdletExports; | ||
| } | ||
|
|
||
| if (manifestInfo._detectedCmdletExports != null) | ||
| if (manifestInfo.DetectedCmdletExports != null) | ||
| { | ||
| foreach (string detectedExport in manifestInfo._detectedCmdletExports) | ||
| foreach (string detectedExport in manifestInfo.DetectedCmdletExports) | ||
| { | ||
| newManifestInfo.AddDetectedCmdletExport(detectedExport); | ||
| } | ||
|
|
@@ -3187,9 +3145,9 @@ internal PSModuleInfo LoadModuleManifest( | |
| newManifestInfo.DeclaredFunctionExports = manifestInfo.DeclaredFunctionExports; | ||
| } | ||
|
|
||
| if (manifestInfo._detectedFunctionExports != null) | ||
| if (manifestInfo.DetectedFunctionExports != null) | ||
| { | ||
| foreach (string detectedExport in manifestInfo._detectedFunctionExports) | ||
| foreach (string detectedExport in manifestInfo.DetectedFunctionExports) | ||
| { | ||
| newManifestInfo.AddDetectedFunctionExport(detectedExport); | ||
| } | ||
|
|
@@ -3200,9 +3158,9 @@ internal PSModuleInfo LoadModuleManifest( | |
| newManifestInfo.DeclaredAliasExports = manifestInfo.DeclaredAliasExports; | ||
| } | ||
|
|
||
| if (manifestInfo._detectedAliasExports != null) | ||
| if (manifestInfo.DetectedAliasExports != null) | ||
| { | ||
| foreach (var pair in manifestInfo._detectedAliasExports) | ||
| foreach (var pair in manifestInfo.DetectedAliasExports) | ||
| { | ||
| newManifestInfo.AddDetectedAliasExport(pair.Key, pair.Value); | ||
| } | ||
|
|
@@ -3269,15 +3227,7 @@ internal PSModuleInfo LoadModuleManifest( | |
| } | ||
| else | ||
| { | ||
| Collection<string> v = new Collection<string>(); | ||
| if (manifestInfo.DeclaredFunctionExports != null) | ||
| { | ||
| foreach (var f in manifestInfo.DeclaredFunctionExports) | ||
| { | ||
| v.Add(f); | ||
| } | ||
| } | ||
| UpdateCommandCollection(v, exportedFunctions); | ||
| UpdateCommandCollection(manifestInfo.DeclaredFunctionExports, exportedFunctions); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hooray for this and the one above!