Exemple #1
0
 private static async Task <Api.Client.PackageModel> GetPackageModelAsync(string languageName, string packageName)
 {
     using (var httpClient = new HttpClient())
     {
         var client = new Api.Client.PackageClient(httpClient)
         {
             BaseUrl = SoupApiEndpoint,
         };
         return(await client.GetPackageAsync(languageName, packageName));
     }
 }
Exemple #2
0
        /// <summary>
        /// Publish a package
        /// </summary>
        public static async Task PublishPackageAsync(Path workingDirectory)
        {
            Log.Info($"Publish Project: {workingDirectory}");

            var recipePath =
                workingDirectory +
                BuildConstants.RecipeFileName;

            var(isSuccess, recipe) = await RecipeExtensions.TryLoadRecipeFromFileAsync(recipePath);

            if (!isSuccess)
            {
                throw new InvalidOperationException("Could not load the recipe file.");
            }

            var packageStore = LifetimeManager.Get <IFileSystem>().GetUserProfileDirectory() +
                               new Path(".soup/packages/");

            Log.Info("Using Package Store: " + packageStore.ToString());

            // Create the staging directory
            var stagingPath = EnsureStagingDirectoryExists(packageStore);

            try
            {
                var archivePath = stagingPath + new Path(recipe.Name + ".zip");

                // Create the archive of the package
                using (var writeArchiveFile = LifetimeManager.Get <IFileSystem>().OpenWrite(archivePath, true))
                    using (var zipArchive = new ZipArchive(writeArchiveFile.GetOutStream(), ZipArchiveMode.Create, false))
                    {
                        AddPackageFiles(workingDirectory, zipArchive);
                    }

                // Authenticate the user
                Log.Info("Request Authentication Token");
                var accessToken = await AuthenticationManager.EnsureSignInAsync();

                // Publish the archive
                Log.Info("Publish package");
                using (var httpClient = new HttpClient())
                {
                    var packageClient = new Api.Client.PackageClient(httpClient)
                    {
                        BaseUrl     = SoupApiEndpoint,
                        BearerToken = accessToken,
                    };

                    // Check if the package exists
                    bool packageExists = false;
                    try
                    {
                        var package = await packageClient.GetPackageAsync(recipe.Language, recipe.Name);

                        packageExists = true;
                    }
                    catch (Api.Client.ApiException ex)
                    {
                        if (ex.StatusCode == 404)
                        {
                            packageExists = false;
                        }
                        else
                        {
                            throw;
                        }
                    }

                    // Create the package if it does not exist
                    if (!packageExists)
                    {
                        var createPackageModel = new Api.Client.PackageCreateOrUpdateModel()
                        {
                            Description = string.Empty,
                        };
                        await packageClient.CreateOrUpdatePackageAsync(recipe.Language, recipe.Name, createPackageModel);
                    }

                    var packageVersionClient = new Api.Client.PackageVersionClient(httpClient)
                    {
                        BaseUrl     = SoupApiEndpoint,
                        BearerToken = accessToken,
                    };

                    using (var readArchiveFile = LifetimeManager.Get <IFileSystem>().OpenRead(archivePath))
                    {
                        try
                        {
                            await packageVersionClient.PublishPackageVersionAsync(
                                recipe.Language,
                                recipe.Name,
                                recipe.Version.ToString(),
                                new Api.Client.FileParameter(readArchiveFile.GetInStream(), string.Empty, "application/zip"));

                            Log.Info("Package published");
                        }
                        catch (Api.Client.ApiException ex)
                        {
                            if (ex.StatusCode == 409)
                            {
                                Log.Info("Package version already exists");
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                }

                // Cleanup the staging directory
                Log.Info("Cleanup staging directory");
                LifetimeManager.Get <IFileSystem>().DeleteDirectory(stagingPath, true);
            }
            catch (Exception)
            {
                // Cleanup the staging directory and accept that we failed
                Log.Info("Publish Failed: Cleanup staging directory");
                LifetimeManager.Get <IFileSystem>().DeleteDirectory(stagingPath, true);
                throw;
            }
        }