private PackageReference ParseParametersDependency(IValue dependency) { if (dependency.IsTable()) { var valueTable = dependency.AsTable(); // Get the require fields if (valueTable.TryGetValue("Reference", out var referenceValue)) { if (referenceValue.IsString()) { return(PackageReference.Parse(referenceValue.AsString())); } else { throw new InvalidOperationException("Recipe dependency Reference must be type String."); } } else { throw new InvalidOperationException("Parameter dependency table missing required Reference value."); } } else { throw new InvalidOperationException("Unknown Parameters dependency type."); } }
public IList <PackageReference> GetNamedDependencies(string name) { if (!HasNamedDependencies(name)) { throw new InvalidOperationException("No named dependencies."); } var values = GetValue(GetDependencies(), name).AsList(); var result = new List <PackageReference>(); foreach (var value in values) { // A dependency can either be a string or a table with reference key if (value.IsString()) { result.Add(PackageReference.Parse(value.AsString())); } else if (value.IsTable()) { var valueTable = value.AsTable(); if (!HasValue(valueTable, Property_Reference)) { throw new InvalidOperationException("Recipe dependency table missing required Reference value."); } var referenceValue = GetValue(valueTable, Property_Reference); if (referenceValue.IsString()) { result.Add(PackageReference.Parse(referenceValue.AsString())); } else { throw new InvalidOperationException("Recipe dependency Reference must be type String."); } } else { throw new InvalidOperationException("Unknown Recipe dependency type."); } } return(result); }
private (PackageReference Reference, bool ExcludeRuntime) ParseRecipeDependency(IValue dependency) { // A dependency can either be a string or a table with reference key if (dependency.IsString()) { return(PackageReference.Parse(dependency.AsString()), false); } else if (dependency.IsTable()) { var valueTable = dependency.AsTable(); // Check for optional fields bool excludeRuntime = false; if (valueTable.TryGetValue("ExcludeRuntime", out var excludeRuntimeValue)) { excludeRuntime = excludeRuntimeValue.AsBoolean(); } // Get the require fields if (valueTable.TryGetValue("Reference", out var referenceValue)) { if (referenceValue.IsString()) { return(PackageReference.Parse(referenceValue.AsString()), excludeRuntime); } else { throw new InvalidOperationException("Recipe dependency Reference must be type String."); } } else { throw new InvalidOperationException("Recipe dependency table missing required Reference value."); } } else { throw new InvalidOperationException("Unknown Recipe dependency type."); } }
/// <summary> /// Restore package lock /// </summary> static async Task RestorePackageLockAsync( Path packagesDirectory, Path stagingDirectory, PackageLock packageLock) { foreach (var languageProjects in packageLock.GetProjects()) { foreach (var project in languageProjects.Value.AsList()) { var projectTable = project.AsTable(); var packageReference = PackageReference.Parse(projectTable["Version"].AsString()); if (!packageReference.IsLocal) { await EnsurePackageDownloadedAsync( languageProjects.Key, packageReference.GetName, packageReference.Version, packagesDirectory, stagingDirectory); } } } }
/// <summary> /// Install a package /// </summary> public static async Task InstallPackageReferenceAsync(Path workingDirectory, string packageReference) { 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 { // Parse the package reference to get the name var targetPackageReference = PackageReference.Parse(packageReference); string packageName = packageReference; if (!targetPackageReference.IsLocal) { packageName = targetPackageReference.GetName; } // Check if the package is already installed var packageNameNormalized = packageName.ToUpperInvariant(); if (recipe.HasRuntimeDependencies) { foreach (var dependency in recipe.RuntimeDependencies) { if (!dependency.IsLocal) { var dependencyNameNormalized = dependency.GetName.ToUpperInvariant(); if (dependencyNameNormalized == packageNameNormalized) { Log.Warning("Package already installed."); return; } } } } // Get the latest version if no version provided if (targetPackageReference.IsLocal) { var packageModel = await GetPackageModelAsync(recipe.Language, packageName); var latestVersion = new SemanticVersion(packageModel.Latest.Major, packageModel.Latest.Minor, packageModel.Latest.Patch); Log.HighPriority("Latest Version: " + latestVersion.ToString()); targetPackageReference = new PackageReference(packageModel.Name, latestVersion); } var closure = new Dictionary <string, IDictionary <string, PackageReference> >(); await CheckRecursiveEnsurePackageDownloadedAsync( recipe.Language, targetPackageReference.GetName, targetPackageReference.Version, packageStore, stagingPath, closure); // Cleanup the working directory Log.Info("Deleting staging directory"); LifetimeManager.Get <IFileSystem>().DeleteDirectory(stagingPath, true); // Register the package in the recipe Log.Info("Adding reference to recipe"); recipe.AddRuntimeDependency(targetPackageReference.ToString()); // Save the state of the recipe await RecipeExtensions.SaveToFileAsync(recipePath, recipe); } catch (Exception) { // Cleanup the staging directory and accept that we failed LifetimeManager.Get <IFileSystem>().DeleteDirectory(stagingPath, true); throw; } }
private PackageReference ParseReferenceString(string referenceString) { var parts = referenceString.Split('"', StringSplitOptions.RemoveEmptyEntries); return(PackageReference.Parse(parts[1], parts[3])); }
/// <inheritdoc /> public bool TryGetScriptText(string text, out string scripttext, string parentRepo = "") { _getScriptTextRecursionDepthCounter++; // TODO: flatten imports if (_getScriptTextRecursionDepthCounter > _getScriptTextRecursionMaxDepth) { scripttext = ""; Log.Fatal( "Maximum recursion depth of {RecursDepth} reached while importing scripts. Maybe the package has circular imports?", _getScriptTextRecursionMaxDepth ); _getScriptTextRecursionDepthCounter--; return(false); } var importrefsregex = new Regex("#load\\s\"uppm-ref:(?<packref>.*)\""); var success = true; var outtext = importrefsregex.Replace(text, match => { var packreftext = match.Groups["packref"].Value.Trim(); Log.Debug("Importing script of {PackRef}", packreftext); var packref = PackageReference.Parse(packreftext); if (!string.IsNullOrWhiteSpace(parentRepo) && !string.IsNullOrWhiteSpace(packref.RepositoryUrl)) { packref.RepositoryUrl = parentRepo; } success = packref.TryGetRepository(out var importPackRepo); if (!success) { Log.Error("Couldn't infer repository for {PackRef} while importing its script.", packreftext); return(""); } Log.Verbose(" at repository {RepoUrl}", importPackRepo.Url); success = success && importPackRepo.TryGetScriptEngine(packref, out var importPackScriptEngine) && importPackScriptEngine is CSharpScriptEngine importCsSe; if (!success) { Log.Error("{PackRef} doesn't appear to have a C# script", packreftext); return(""); } var importScriptText = ""; success = success && importPackRepo.TryGetPackageText(packref, out var importPackText) && TryGetScriptText(importPackText, out importScriptText, parentRepo); if (!success) { Log.Error("Couldn't get the script text of {PackRef}", packreftext); return(""); } try { var filepath = Path.Combine( Uppm.Implementation.TemporaryFolder, "CSharpEngine", packref.ToString().Dehumanize().Kebaberize() + ".csx" ); File.WriteAllText(filepath, importScriptText); Log.Verbose(" saved successfully at {FilePath}", filepath); return($"#load \"{filepath}\""); } catch (Exception e) { success = false; Log.Error(e, "Error during importing script of {PackRef}", packref); return(""); } }); _getScriptTextRecursionDepthCounter--; scripttext = outtext; return(success); }