public string GetBestMatch(IEnumerable <string> packageVersions) { var versions = packageVersions.Select(UniversalPackageVersion.Parse).OrderByDescending(v => v).ToList(); if (string.Equals(this.Value, "latest", StringComparison.OrdinalIgnoreCase)) { return(versions.FirstOrDefault()?.ToString()); } else if (this.Major != null && this.Minor != null) { return(versions.FirstOrDefault(v => v.Major == this.Major && v.Minor == this.Minor)?.ToString()); } else if (this.Major != null && this.Minor == null) { return(versions.FirstOrDefault(v => v.Major == this.Major)?.ToString()); } else { var semver = UniversalPackageVersion.Parse(this.Value); return(versions.FirstOrDefault(v => v == semver)?.ToString()); } }
public override async Task ExecuteAsync(IOperationExecutionContext context) { if (string.IsNullOrWhiteSpace(this.PackageFile)) { if (string.IsNullOrWhiteSpace(this.FeedUrl)) { this.LogError("FeedUrl is required if PackageFile is not specified."); return; } if (string.IsNullOrWhiteSpace(this.PackageName)) { this.LogError("Name is required if PackageFile is not specified."); return; } var endpoint = new UniversalFeedEndpoint(new Uri(this.FeedUrl), this.UserName, this.Password); this.LogInformation($"Getting package information for {this.PackageName} from {endpoint}..."); var client = new UniversalFeedClient(endpoint); var versions = await client.ListPackageVersionsAsync(UniversalPackageId.Parse(this.PackageName)); this.LogDebug($"Server return info for {versions.Count} packages."); RemoteUniversalPackageVersion package; if (!string.IsNullOrWhiteSpace(this.PackageVersion)) { this.LogDebug($"Checking for {this.PackageVersion} in result set..."); var parsedVersion = UniversalPackageVersion.Parse(this.PackageVersion); package = versions.FirstOrDefault(p => p.Version == parsedVersion); if (package != null) { this.LogInformation($"Package {this.PackageName} {this.PackageVersion} found."); } else { this.LogInformation($"Package {this.PackageName} {this.PackageVersion} not found."); } } else { if (versions.Count > 0) { this.LogDebug($"Determining latest version of {this.PackageName}..."); package = versions.Aggregate((p1, p2) => p1.Version >= p2.Version ? p1 : p2); this.LogInformation($"Latest version of {this.PackageName} is {package.Version}."); } else { this.LogInformation($"Package {this.PackageName} not found."); package = null; } } if (package != null) { this.Exists = true; this.Metadata = this.Convert(package.AllProperties).AsDictionary(); } else { this.Exists = false; } } else { if (!string.IsNullOrWhiteSpace(this.FeedUrl)) { this.LogWarning("FeedUrl is ignored when PackageFile is specified."); } if (!string.IsNullOrWhiteSpace(this.PackageName)) { this.LogError("Name is ignored when PackageFile is specified."); } if (!string.IsNullOrWhiteSpace(this.PackageVersion)) { this.LogError("Version is ignored when PackageFile is specified."); } var fileOps = await context.Agent.GetServiceAsync <IFileOperationsExecuter>(); var fullPath = context.ResolvePath(this.PackageFile); this.LogInformation($"Reading {fullPath}..."); if (await fileOps.FileExistsAsync(fullPath)) { this.LogDebug("Package file exists; reading metadata..."); UniversalPackageMetadata metadata; try { using (var stream = await fileOps.OpenFileAsync(fullPath, FileMode.Open, FileAccess.Read)) using (var package = new UniversalPackage(stream)) { metadata = package.GetFullMetadata(); } } catch (Exception ex) { this.LogError("Error reading package: " + ex); return; } this.Exists = true; this.Metadata = this.Convert(metadata).AsDictionary(); } else { this.LogInformation("Package file not found."); this.Exists = false; } } }
protected override async Task <object> RemoteExecuteAsync(IRemoteOperationExecutionContext context) { var fullPath = context.ResolvePath(this.FileName); this.LogInformation($"Changing \"{fullPath}\" package version to {AH.CoalesceString(this.NewVersion, "remove pre-release label")}..."); var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("n")); try { DirectoryEx.Create(tempPath); UniversalPackageMetadata currentMetadata; using (var upack = new UniversalPackage(fullPath)) { currentMetadata = upack.GetFullMetadata(); await upack.ExtractAllItemsAsync(tempPath, context.CancellationToken); FileEx.Delete(PathEx.Combine(tempPath, "upack.json")); } var newMetadata = currentMetadata.Clone(); if (string.IsNullOrEmpty(this.NewVersion)) { newMetadata.Version = new UniversalPackageVersion(currentMetadata.Version.Major, currentMetadata.Version.Minor, currentMetadata.Version.Patch); } else { newMetadata.Version = UniversalPackageVersion.Parse(this.NewVersion); } if (currentMetadata.Version == newMetadata.Version) { this.LogWarning($"Current package version {currentMetadata.Version} and the new version {newMetadata.Version} are the same; nothing to do."); return(null); } this.LogInformation("New version: " + newMetadata.Version); this.LogDebug("Adding repacking entry..."); newMetadata.RepackageHistory.Add( new RepackageHistoryEntry { Id = new UniversalPackageId(currentMetadata.Group, currentMetadata.Name) + ":" + currentMetadata.Version, Date = DateTimeOffset.Now, Using = SDK.ProductName + "/" + SDK.ProductVersion, Reason = this.Reason } ); using (var builder = new UniversalPackageBuilder(fullPath, newMetadata)) { await builder.AddRawContentsAsync(tempPath, string.Empty, true, c => true, context.CancellationToken); } this.LogInformation("Package version changed."); return(null); } finally { try { this.LogDebug($"Deleting temporary files from {tempPath}..."); DirectoryEx.Clear(tempPath); DirectoryEx.Delete(tempPath); } catch (Exception ex) { this.LogWarning("Unable to delete temporary files: " + ex.Message); } } }