简体   繁体   中英

Programmatically Add a packageSource to packageSources in Nuget.Config

I am trying to use the library NuGet.Configuration from NuGet to programmatically add a packageSource to my nuget.config. (I can't use nuget.exe in AWS CodeBuild, at least without a ton of heavy lifting).

I can't make heads or tails of the NuGet.Configuration classes and how to operate them.

Essentially what I want to do is:

  1. Load a NuGet.Config file
  2. Add a package source
  3. Add a packageSourceCredentials
  4. Save it back to disk.

I can't find any examples out there. Advise?

No time to "dress this up" but essentially it is this:

                var setting = Settings.LoadSpecificSettings(Directory.GetCurrentDirectory(), "NuGet.Config");

            // get sources 
            var packageSourceProvider = new PackageSourceProvider(setting);

            var packageSource = new PackageSource(repo.RepositoryEndpoint, "my-repo")
            {
                Credentials = new PackageSourceCredential("my-repo", "aws", authorizationTokenAsync.AuthorizationToken, true, string.Empty)
            };

            packageSourceProvider.AddPackageSource(packageSource);

            setting.SaveToDisk();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM