public async Task <IEnumerable <IPackageViewModel> > GetInstalledPackages(bool force = false) { // Ensure that we only retrieve the packages one at a time to refresh the Cache. using (await this.GetInstalledLock.LockAsync()) { ICollection <IPackageViewModel> packages; if (!force) { packages = BasePackageService.CachedPackages; if (packages != null) { return(packages); } } this.StartProgressDialog("Chocolatey Service", "Retrieving installed packages..."); var result = await ProcessEx.RunAsync(this.chocoExePath, "list -lo"); var chocoPackageList = result.StandardOutput.Where(p => PackageRegex.IsMatch(p.ToString(CultureInfo.InvariantCulture))) .Select(p => PackageRegex.Match(p.ToString(CultureInfo.InvariantCulture))) .ToDictionary(m => m.Groups["Name"].Value, m => new SemanticVersion(m.Groups["VersionString"].Value)); var libPath = Path.Combine(this.ChocolateyConfigurationProvider.ChocolateyInstall, "lib"); packages = new List <IPackageViewModel>(); await this.EnumerateLocalPackagesAndSetCache(packages, chocoPackageList, libPath); await this.ProgressService.StopLoading(); return(packages); } }
/// <summary> /// Retrieves the currently installed packages. /// If the package list is cached, retrieve it from there. /// Else, scan the file system for packages and pull the appropriate information from there. /// </summary> /// <param name="force">Forces a cache reset.</param> /// <returns>List of currently installed packages.</returns> public async Task <IEnumerable <IPackageViewModel> > GetInstalledPackages(bool force = false) { // Ensure that we only retrieve the packages one at a to refresh the Cache. using (await this.GetInstalledLock.LockAsync()) { ICollection <IPackageViewModel> packages; if (!force) { packages = BasePackageService.CachedPackages; if (packages != null) { return(packages); } } await this.ProgressService.StartLoading("Chocolatey Service"); this.ProgressService.WriteMessage("Retrieving installed packages..."); var chocoPath = this.ChocolateyConfigurationProvider.ChocolateyInstall; if (string.IsNullOrWhiteSpace(chocoPath) || !Directory.Exists(chocoPath)) { throw new InvalidDataException( "Invalid Chocolatey Path. Check that chocolateyInstall is correct in the app.config."); } var libPath = Path.Combine(chocoPath, "lib"); var chocoPackageList = (await this.RunIndirectChocolateyCommand("list -lo", false)) .Where(p => PackageRegex.IsMatch(p.ToString())) .Select(p => PackageRegex.Match(p.ToString())) .ToDictionary(m => m.Groups["Name"].Value, m => new SemanticVersion(m.Groups["VersionString"].Value)); packages = new List <IPackageViewModel>(); await this.EnumerateLocalPackagesAndSetCache(packages, chocoPackageList, libPath); await this.ProgressService.StopLoading(); return(packages); } }