/// <summary> /// Initializes source repositories for PowerShell cmdlets, based on config, source string, and/or host active source property value. /// </summary> /// <param name="source">The source string specified by -Source switch.</param> protected void UpdateActiveSourceRepository(string source) { var packageSources = ConsoleHost.LoadPackageSources(); // If source string is not specified, get the current active package source from the host source = ConsoleHost.GetActivePackageSource(source); if (!string.IsNullOrEmpty(source)) { // Look through all available sources (including those disabled) by matching source name and url var matchingSource = packageSources ?.Where(p => StringComparer.OrdinalIgnoreCase.Equals(p.Name, source) || StringComparer.OrdinalIgnoreCase.Equals(p.Source, source)) .FirstOrDefault(); if (matchingSource != null) { activeSourceRepository = ConsoleHost.CreateRepository(matchingSource); } else { // source should be the format of url here; otherwise it cannot resolve from name anyways. activeSourceRepository = CreateRepositoryFromSource(source); } } EnabledSourceRepositories = ConsoleHost .GetRepositories() .Where(r => r.PackageSource.IsEnabled) .ToList(); }
/// <summary> /// Create a package repository from the source by trying to resolve relative paths. /// </summary> protected SourceRepository CreateRepositoryFromSource(string source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } var packageSource = new NuGet.Configuration.PackageSource(source); var repository = ConsoleHost.CreateRepository(packageSource); var resource = repository.GetResource <PackageSearchResource> (); // resource can be null here for relative path package source. if (resource == null) { Uri uri; // if it's not an absolute path, treat it as relative path if (Uri.TryCreate(source, UriKind.Relative, out uri)) { throw new NotImplementedException(); //string outputPath; //bool? exists; //string errorMessage; //// translate relative path to absolute path //if (TryTranslatePSPath (source, out outputPath, out exists, out errorMessage) && exists == true) { // source = outputPath; // packageSource = new Configuration.PackageSource (outputPath); //} } } var sourceRepo = ConsoleHost.CreateRepository(packageSource); // Right now if packageSource is invalid, CreateRepository will not throw. Instead, resource returned is null. var newResource = repository.GetResource <PackageSearchResource> (); if (newResource == null) { // Try to create Uri again to throw UriFormat exception for invalid source input. new Uri(source); } return(sourceRepo); }