Skip to content

Commit b358e7a

Browse files
authored
Implement Progress Bar for Install (#552)
Add progress bar for install
1 parent 41dbb56 commit b358e7a

File tree

1 file changed

+59
-25
lines changed

1 file changed

+59
-25
lines changed

src/code/InstallHelper.cs

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ private void ProcessRepositories(
145145
bool skipDependencyCheck)
146146
{
147147
var listOfRepositories = RepositorySettings.Read(repository, out string[] _);
148-
List<string> pckgNamesToInstall = packageNames.ToList();
148+
List<string> pkgNamesToInstall = packageNames.ToList();
149149
var yesToAll = false;
150150
var noToAll = false;
151151

152152
var findHelper = new FindHelper(_cancellationToken, _cmdletPassedIn);
153153
foreach (var repo in listOfRepositories)
154154
{
155155
// If no more packages to install, then return
156-
if (!pckgNamesToInstall.Any()) return;
156+
if (!pkgNamesToInstall.Any()) return;
157157

158158
string repoName = repo.Name;
159159
_cmdletPassedIn.WriteVerbose(string.Format("Attempting to search for packages in '{0}'", repoName));
@@ -185,7 +185,7 @@ private void ProcessRepositories(
185185

186186
// Finds parent packages and dependencies
187187
IEnumerable<PSResourceInfo> pkgsFromRepoToInstall = findHelper.FindByResourceName(
188-
name: pckgNamesToInstall.ToArray(),
188+
name: pkgNamesToInstall.ToArray(),
189189
type: ResourceType.None,
190190
version: _versionRange != null ? _versionRange.OriginalString : null,
191191
prerelease: _prerelease,
@@ -225,14 +225,14 @@ private void ProcessRepositories(
225225

226226
List<string> pkgsInstalled = InstallPackage(
227227
pkgsFromRepoToInstall,
228-
repoName,
228+
pkgNamesToInstall,
229229
repo.Url.AbsoluteUri,
230230
credential,
231231
isLocalRepo);
232232

233233
foreach (string name in pkgsInstalled)
234234
{
235-
pckgNamesToInstall.Remove(name);
235+
pkgNamesToInstall.Remove(name);
236236
}
237237
}
238238
}
@@ -286,15 +286,26 @@ private IEnumerable<PSResourceInfo> FilterByInstalledPkgs(IEnumerable<PSResource
286286
}
287287

288288
private List<string> InstallPackage(
289-
IEnumerable<PSResourceInfo> pkgsToInstall,
290-
string repoName,
289+
IEnumerable<PSResourceInfo> pkgsToInstall, // those found to be required to be installed (includes Dependency packages as well)
290+
List<string> pkgNamesToInstall, // those requested by the user to be installed
291291
string repoUrl,
292292
PSCredential credential,
293293
bool isLocalRepo)
294294
{
295295
List<string> pkgsSuccessfullyInstalled = new List<string>();
296-
foreach (PSResourceInfo pkgInfo in pkgsToInstall)
296+
int totalPkgs = pkgsToInstall.Count();
297+
298+
// counters for tracking dependent package and current package out of total
299+
int totalPkgsCount = 0;
300+
int dependentPkgCount = 1;
301+
// by default this is 1, because if a parent package was already installed and only the dependent package
302+
// needs to be installed we don't want a default value of 0 which throws a division error.
303+
// if parent package isn't already installed we'll set this value properly in the below if condition anyways
304+
int currentPkgNumOfDependentPkgs = 1;
305+
306+
foreach (PSResourceInfo pkg in pkgsToInstall)
297307
{
308+
totalPkgsCount++;
298309
var tempInstallPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
299310
try
300311
{
@@ -307,24 +318,47 @@ private List<string> InstallPackage(
307318
// TODO: are there Linux accommodations we need to consider here?
308319
dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
309320

310-
_cmdletPassedIn.WriteVerbose(string.Format("Begin installing package: '{0}'", pkgInfo.Name));
321+
_cmdletPassedIn.WriteVerbose(string.Format("Begin installing package: '{0}'", pkg.Name));
322+
323+
int activityId = 0;
324+
string activity = "";
325+
string statusDescription = "";
326+
327+
if (pkgNamesToInstall.ToList().Contains(pkg.Name, StringComparer.InvariantCultureIgnoreCase))
328+
{
329+
// Installing parent package (one whose name was passed in to install)
330+
activityId = 0;
331+
activity = string.Format("Installing {0}...", pkg.Name);
332+
statusDescription = string.Format("{0}% Complete:", Math.Round(((double) totalPkgsCount/totalPkgs) * 100), 2);
333+
currentPkgNumOfDependentPkgs = pkg.Dependencies.Count();
334+
dependentPkgCount = 1;
335+
}
336+
else
337+
{
338+
// Installing dependent package
339+
activityId = 1;
340+
activity = string.Format("Installing dependent package {0}...", pkg.Name);
341+
statusDescription = string.Format("{0}% Complete:", Math.Round(((double) dependentPkgCount/currentPkgNumOfDependentPkgs) * 100), 2);
342+
dependentPkgCount++;
343+
}
311344

312-
// TODO: add progress bar here
313-
345+
var progressRecord = new ProgressRecord(activityId, activity, statusDescription);
346+
_cmdletPassedIn.WriteProgress(progressRecord);
347+
314348
// Create PackageIdentity in order to download
315-
string createFullVersion = pkgInfo.Version.ToString();
316-
if (pkgInfo.IsPrerelease)
349+
string createFullVersion = pkg.Version.ToString();
350+
if (pkg.IsPrerelease)
317351
{
318-
createFullVersion = pkgInfo.Version.ToString() + "-" + pkgInfo.PrereleaseLabel;
352+
createFullVersion = pkg.Version.ToString() + "-" + pkg.PrereleaseLabel;
319353
}
320354

321355
if (!NuGetVersion.TryParse(createFullVersion, out NuGetVersion pkgVersion))
322356
{
323357
_cmdletPassedIn.WriteVerbose(string.Format("Error parsing package '{0}' version '{1}' into a NuGetVersion",
324-
pkgInfo.Name, pkgInfo.Version.ToString()));
358+
pkg.Name, pkg.Version.ToString()));
325359
continue;
326360
}
327-
var pkgIdentity = new PackageIdentity(pkgInfo.Name, pkgVersion);
361+
var pkgIdentity = new PackageIdentity(pkg.Name, pkgVersion);
328362
var cacheContext = new SourceCacheContext();
329363

330364
if (isLocalRepo)
@@ -419,8 +453,8 @@ private List<string> InstallPackage(
419453
string tempDirNameVersion = isLocalRepo ? tempInstallPath : Path.Combine(tempInstallPath, pkgIdentity.Id.ToLower(), newVersion);
420454
var version4digitNoPrerelease = pkgIdentity.Version.Version.ToString();
421455
string moduleManifestVersion = string.Empty;
422-
var scriptPath = Path.Combine(tempDirNameVersion, pkgInfo.Name + ".ps1");
423-
var modulePath = Path.Combine(tempDirNameVersion, pkgInfo.Name + ".psd1");
456+
var scriptPath = Path.Combine(tempDirNameVersion, pkg.Name + ".ps1");
457+
var modulePath = Path.Combine(tempDirNameVersion, pkg.Name + ".psd1");
424458
// Check if the package is a module or a script
425459
var isModule = File.Exists(modulePath);
426460

@@ -446,13 +480,13 @@ private List<string> InstallPackage(
446480
moduleManifestVersion = parsedMetadataHashtable["ModuleVersion"] as string;
447481

448482
// Accept License verification
449-
if (!_savePkg && !CallAcceptLicense(pkgInfo, moduleManifest, tempInstallPath, newVersion))
483+
if (!_savePkg && !CallAcceptLicense(pkg, moduleManifest, tempInstallPath, newVersion))
450484
{
451485
continue;
452486
}
453487

454488
// If NoClobber is specified, ensure command clobbering does not happen
455-
if (_noClobber && !DetectClobber(pkgInfo.Name, parsedMetadataHashtable))
489+
if (_noClobber && !DetectClobber(pkg.Name, parsedMetadataHashtable))
456490
{
457491
continue;
458492
}
@@ -478,11 +512,11 @@ private List<string> InstallPackage(
478512

479513
if (_includeXML)
480514
{
481-
CreateMetadataXMLFile(tempDirNameVersion, installPath, pkgInfo, isModule);
515+
CreateMetadataXMLFile(tempDirNameVersion, installPath, pkg, isModule);
482516
}
483517

484518
MoveFilesIntoInstallPath(
485-
pkgInfo,
519+
pkg,
486520
isModule,
487521
isLocalRepo,
488522
tempDirNameVersion,
@@ -492,15 +526,15 @@ private List<string> InstallPackage(
492526
moduleManifestVersion,
493527
scriptPath);
494528

495-
_cmdletPassedIn.WriteVerbose(String.Format("Successfully installed package '{0}' to location '{1}'", pkgInfo.Name, installPath));
496-
pkgsSuccessfullyInstalled.Add(pkgInfo.Name);
529+
_cmdletPassedIn.WriteVerbose(String.Format("Successfully installed package '{0}' to location '{1}'", pkg.Name, installPath));
530+
pkgsSuccessfullyInstalled.Add(pkg.Name);
497531
}
498532
catch (Exception e)
499533
{
500534
_cmdletPassedIn.WriteError(
501535
new ErrorRecord(
502536
new PSInvalidOperationException(
503-
message: $"Unable to successfully install package '{pkgInfo.Name}': '{e.Message}'",
537+
message: $"Unable to successfully install package '{pkg.Name}': '{e.Message}'",
504538
innerException: e),
505539
"InstallPackageFailed",
506540
ErrorCategory.InvalidOperation,

0 commit comments

Comments
 (0)