private async Task <string> GetTagName(PackageConfiguration configuration) { string tag = GetCommandInputValue(tagNameInput); if (string.IsNullOrEmpty(tag)) { string newTag = DateTime.Now.ToString("yyyyMMdd"); string[] existingTags = await onlineStoreService.GetAllPackageTagsAsync(configuration.Name); string lastTag = existingTags.FirstOrDefault(); if (string.IsNullOrEmpty(lastTag)) { tag = newTag; } if (lastTag == newTag) { newTag = lastTag + ".1"; } else if (lastTag.StartsWith(newTag)) { string[] split = newTag.Split('.'); if (int.TryParse(split.Last(), out int counter)) { newTag = newTag + "." + (++counter); } else { throw new ArgumentException($"Can not create auto tag. Last tag is {lastTag}"); } } else if (!existingTags.Contains(newTag)) { tag = newTag; } else { throw new ArgumentException("Can not auto tag"); } } return(tag); }
protected async override Task RunCommandAsync() { string packageNameAndTag = GetCommandInputValue(packageNameInput); string packageName = packageNameAndTag.Split('@').FirstOrDefault(); string packageTagName = packageNameAndTag.Contains("@") ? packageNameAndTag.Split('@').LastOrDefault() : string.Empty; string[] packageTagHistory = null; bool createConfig = false; if (configService.TryGetConfig(out Config.PackageConfiguration config)) { if (packageName != config.Name) { throw new InvalidOperationException($"Folder binded to another package {config.Name}"); } string currentPackageName = $"{config.Name}@{config.Tag}"; packageTagHistory = await onlineStoreService.GetPackageTagsAsync(packageNameAndTag, currentPackageName); } else { createConfig = true; packageTagHistory = await onlineStoreService.GetAllPackageTagsAsync(packageName); if (!string.IsNullOrEmpty(packageTagName)) { packageTagHistory = packageTagHistory.SkipWhile(t => t != packageTagName).ToArray(); } else { uiService.AddMessage($"Pulling {packageName}@{packageTagHistory.FirstOrDefault()}"); } } foreach (string packageTag in packageTagHistory.Reverse()) { PackageInfo packageInfo = await onlineStoreService.GetPackageVersionAsync($"{packageName}@{packageTag}"); FolderVersionEntry folderVersion = JsonConvert.DeserializeObject <FolderVersionEntry>(packageInfo.VersionInfo); if (!localStoreService.PackageExist(packageInfo)) { HttpOperationWithProgress downloadOperation = onlineStoreService.DownloadPackage(packageName, packageTag); downloadOperation.OnProgress += (processedCount, totalCount) => { uiService.DisplayProgress((float)processedCount * 100 / totalCount); }; HttpResponseMessage response = await downloadOperation.GetOperationResultAsync(); localStoreService.SavePackage(packageInfo, await response.Content.ReadAsByteArrayAsync()); } localStoreService.RestorePackage(packageName, packageTag); if (createConfig) { configService.CreateConfig(packageNameAndTag, packageInfo.Hash); createConfig = false; } else { configService.SetTag(packageTag); } } }