Ejemplo n.º 1
0
        private IEnumerable <string> AlternativeModuleScan(PsRequest request)
        {
            var psModulePath = Environment.GetEnvironmentVariable("PSModulePath") ?? "";

            IEnumerable <string> paths = psModulePath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            // add assumed paths just in case the environment variable isn't really set.
            try {
                paths = paths.ConcatSingleItem(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"WindowsPowerShell\v1.0\Modules"));
            } catch {
                // skip the folder if it's not valid
            }
            try {
                paths = paths.ConcatSingleItem(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"WindowsPowerShell\Modules"));
            }
            catch {
                // skip the folder if it's not valid
            }

            if (!string.IsNullOrWhiteSpace(BaseFolder) && BaseFolder.DirectoryExists())
            {
                paths = paths.ConcatSingleItem(BaseFolder);
            }

            paths = paths.Distinct(new PathEqualityComparer(PathCompareOption.Full)).ToArray();

            var moduleFolders = paths.Where(each => each.DirectoryExists()).SelectMany(each => Directory.EnumerateDirectories(each).Where(dir => !_exclusionList.Contains(Path.GetFileName(dir))));

            var modules = moduleFolders.Select(module => {
                var moduleManifest = Path.Combine(module, Path.GetFileName(module) + ".psd1");
                if (File.Exists(moduleManifest))
                {
                    return(moduleManifest);
                }

                var versions = Directory.EnumerateDirectories(module).Select(dir => new { folder = dir, ver = (FourPartVersion)Path.GetFileName(dir) }).Where(each => each.ver > 0L).OrderByDescending(each => each).Select(each => each.folder);

                return(versions.Select(version => Path.Combine(version, Path.GetFileName(Path.GetFileName(module)) + ".psd1")).FirstOrDefault(File.Exists));
            }).WhereNotNull();

            return(modules.Where(moduleName => File.ReadAllText(moduleName).IndexOf("PackageManagementProviders", StringComparison.OrdinalIgnoreCase) > -1));
        }
Ejemplo n.º 2
0
        private IEnumerable <string> AlternativeModuleScan(PsRequest request)
        {
            var psModulePath = Environment.GetEnvironmentVariable("PSModulePath") ?? "";

            IEnumerable <string> paths = psModulePath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            var sysRoot     = Environment.GetEnvironmentVariable("systemroot");
            var userProfile = Environment.GetEnvironmentVariable("userprofile");

            // add assumed paths just in case the environment variable isn't really set.
            paths = paths.ConcatSingleItem(Path.Combine(sysRoot, @"system32\WindowsPowerShell\v1.0\Modules"));
            paths = paths.ConcatSingleItem(Path.Combine(userProfile, @"Documents\WindowsPowerShell\Modules"));

            if (!string.IsNullOrWhiteSpace(BaseFolder) && BaseFolder.DirectoryExists())
            {
                paths = paths.ConcatSingleItem(BaseFolder);
            }

            paths = paths.Distinct().ToArray();

            return(paths.Where(each => each.DirectoryExists()).SelectMany(each => Directory.EnumerateDirectories(each).Where(dir => !_exclusionList.Contains(Path.GetFileName(dir))), (p, child) => Path.Combine(child, Path.GetFileName(child) + ".psd1")).Where(moduleName => File.Exists(moduleName) && File.ReadAllText(moduleName).IndexOf("OneGetProviders", StringComparison.OrdinalIgnoreCase) > -1));
        }
Ejemplo n.º 3
0
        private IEnumerable <string> AlternativeModuleScan(
            PsRequest request,
            Version requiredVersion,
            Version minimumVersion,
            Version maximumVersion,
            ProviderOption providerOption = ProviderOption.LatestVersion)
        {
            var psModulePath = Environment.GetEnvironmentVariable("PSModulePath") ?? "";

            IEnumerable <string> paths = psModulePath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            // add assumed paths just in case the environment variable isn't really set.
            try {
#if CORECLR
                paths = paths.ConcatSingleItem(Path.Combine(Environment.GetEnvironmentVariable("windir"), "system32", @"WindowsPowerShell\v1.0\Modules"));
#else
                paths = paths.ConcatSingleItem(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"WindowsPowerShell\v1.0\Modules"));
#endif
            } catch {
                // skip the folder if it's not valid
            }
            try {
#if CORECLR
                paths = paths.ConcatSingleItem(Path.Combine(Environment.GetEnvironmentVariable("userprofile"), "documents", @"WindowsPowerShell\Modules"));
#else
                paths = paths.ConcatSingleItem(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"WindowsPowerShell\Modules"));
#endif
            } catch {
                // skip the folder if it's not valid
            }

            if (!string.IsNullOrWhiteSpace(BaseFolder) && BaseFolder.DirectoryExists())
            {
                paths = paths.ConcatSingleItem(BaseFolder);
            }

            var moduleFolders = paths.Distinct(new PathEqualityComparer(PathCompareOption.Full)).Where(each => each.DirectoryExists()).SelectMany(each => Directory.EnumerateDirectories(each).Where(dir => !_exclusionList.Contains(Path.GetFileName(dir))));

            foreach (var module in moduleFolders)
            {
                var moduleManifest = Path.Combine(module, Path.GetFileName(module) + ".psd1");
                if (File.Exists(moduleManifest))
                {
                    //The version check is defered in the GetPackageManagementModules() because we do not know the version without looking into the content.
                    yield return(moduleManifest);
                }

                //The following are the cases where there are multiple modules installed on the system. The file folder is the module version.
                var selectedVersions = Directory.EnumerateDirectories(module).Select(dir => new {
                    folder = dir,
                    ver    = (FourPartVersion)Path.GetFileName(dir)
                }).Where(each => each.ver > 0L);

                if (requiredVersion != null)
                {
                    var version = selectedVersions.Where(each => each.ver == (FourPartVersion)requiredVersion).Select(each => each.folder).FirstOrDefault();
                    if (version != null)
                    {
                        var file = Path.Combine(version, Path.GetFileName(Path.GetFileName(module)) + ".psd1");
                        if (File.Exists(file))
                        {
                            yield return(file);
                        }
                    }
                }

                if (minimumVersion != null)
                {
                    selectedVersions = selectedVersions.Where(each => each.ver >= (FourPartVersion)minimumVersion);
                }
                if (maximumVersion != null)
                {
                    selectedVersions = selectedVersions.Where(each => each.ver <= (FourPartVersion)maximumVersion);
                }

                var results = (providerOption == PackageManagementService.ProviderOption.AllProvider) ?
                              selectedVersions.Select(version => Path.Combine(version.folder, Path.GetFileName(Path.GetFileName(module)) + ".psd1")).Where(File.Exists) :
                              new[] {
                    selectedVersions.OrderByDescending(each => each.ver)
                    .Select(version => Path.Combine(version.folder, Path.GetFileName(Path.GetFileName(module)) + ".psd1"))
                    .FirstOrDefault(File.Exists)
                };

                foreach (var result in results.WhereNotNull())
                {
                    yield return(result);
                }
            }
        }