|
1 | 1 | using NuGet.Configuration;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Linq; |
3 <
8000
/td> | 4 |
|
4 | 5 | namespace Dotnet.Script.DependencyModel.ProjectSystem
|
5 | 6 | {
|
6 | 7 | internal static class NuGetUtilities
|
7 | 8 | {
|
8 |
| - struct NuGetConfigSection |
| 9 | + public static void CreateNuGetConfigFromLocation(string pathToEvaluate, string targetDirectory) |
9 | 10 | {
|
10 |
| - public string Name; |
11 |
| - public HashSet<string> KeysForPathValues; |
12 |
| - public bool AreAllValuesPaths; |
| 11 | + var sourceSettings = Settings.LoadDefaultSettings(pathToEvaluate); |
| 12 | + var targetSettings = new Settings(targetDirectory); |
| 13 | + |
| 14 | + CopySection(sourceSettings, targetSettings, "config"); |
| 15 | + CopySection(sourceSettings, targetSettings, "bindingRedirects"); |
| 16 | + CopySection(sourceSettings, targetSettings, "packageRestore"); |
| 17 | + CopySection(sourceSettings, targetSettings, "solution"); |
| 18 | + CopySection(sourceSettings, targetSettings, "packageSources"); |
| 19 | + CopySection(sourceSettings, targetSettings, "packageSourceCredentials"); |
| 20 | + CopySection(sourceSettings, targetSettings, "apikeys"); |
| 21 | + CopySection(sourceSettings, targetSettings, "disabledPackageSources"); |
| 22 | + CopySection(sourceSettings, targetSettings, "activePackageSource"); |
| 23 | + |
| 24 | + targetSettings.SaveToDisk(); |
13 | 25 | }
|
14 | 26 |
|
15 |
| - static readonly NuGetConfigSection[] NuGetSections = |
| 27 | + private static void CopySection(ISettings sourceSettings, ISettings targetSettings, string sectionName) |
16 | 28 | {
|
17 |
| - new NuGetConfigSection { Name = "config", KeysForPathValues = new HashSet<string> { "globalPackagesFolder", "repositoryPath" } }, |
18 |
| - new NuGetConfigSection { Name = "bindingRedirects" }, |
19 |
| - new NuGetConfigSection { Name = "packageRestore" }, |
20 |
| - new NuGetConfigSection { Name = "solution" }, |
21 |
| - new NuGetConfigSection { Name = "packageSources", AreAllValuesPaths = true }, |
22 |
| - new NuGetConfigSection { Name = "packageSourceCredentials" }, |
23 |
| - new NuGetConfigSection { Name = "apikeys" }, |
24 |
| - new NuGetConfigSection { Name = "disabledPackageSources" }, |
25 |
| - new NuGetConfigSection { Name = "activePackageSource" }, |
26 |
| - }; |
| 29 | + var existingAddItems = sourceSettings.GetSection(sectionName)?.Items.Cast<AddItem>(); |
27 | 30 |
|
28 |
| - // Create a NuGet file containing all properties with resolved absolute paths |
29 |
| - public static void CreateNuGetConfigFromLocation(string pathToEvaluate, string targetDirectory) |
30 |
| - { |
31 |
| - var settings = Settings.LoadDefaultSettings(pathToEvaluate); |
32 |
| - var target = new Settings(targetDirectory); |
| 31 | + if (existingAddItems == null) |
| 32 | + { |
| 33 | + return; |
| 34 | + } |
33 | 35 |
|
34 |
| - var valuesToSet = new List<SettingValue>(); |
35 |
| - foreach (var section in NuGetSections) |
| 36 | + foreach (var addItem in existingAddItems) |
36 | 37 | {
|
37 |
| - // Resolve properly path values |
38 |
| - valuesToSet.Clear(); |
39 |
| - if (section.AreAllValuesPaths) |
| 38 | + if (ShouldResolvePath(sectionName, addItem.Key)) |
40 | 39 | {
|
41 |
| - // All values are paths |
42 |
| - var values = settings.GetSettingValues(section.Name, true); |
43 |
| - valuesToSet.AddRange(values); |
| 40 | + targetSettings.AddOrUpdate(sectionName, new AddItem(addItem.Key, addItem.GetValueAsPath())); |
44 | 41 | }
|
45 | 42 | else
|
46 | 43 | {
|
47 |
| - var values = settings.GetSettingValues(section.Name, false); |
48 |
| - if (section.KeysForPathValues != null) |
49 |
| - { |
50 |
| - // Some values are path |
51 |
| - foreach (var value in values) |
52 |
| - { |
53 |
| - if (section.KeysForPathValues.Contains(value.Key)) |
54 |
| - { |
55 |
| - var val = settings.GetValue(section.Name, value.Key, true); |
56 |
| - value.Value = val; |
57 |
| - } |
58 |
| - |
59 |
| - valuesToSet.Add(value); |
60 |
| - } |
61 |
| - } |
62 |
| - else |
63 |
| - // All values are not path |
64 |
| - valuesToSet.AddRange(values); |
| 44 | + targetSettings.AddOrUpdate(sectionName, addItem); |
65 | 45 | }
|
66 |
| - target.SetValues(section.Name, valuesToSet); |
67 | 46 | }
|
68 | 47 | }
|
| 48 | + |
| 49 | + private static bool ShouldResolvePath(string sectionName, string key) |
| 50 | + { |
| 51 | + if (sectionName == "packageSources") |
| 52 | + { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + if (sectionName == "config") |
| 57 | + { |
| 58 | + return key == "globalPackagesFolder" || key == "repositoryPath"; |
| 59 | + } |
| 60 | + |
| 61 | + return false; |
| 62 | + } |
69 | 63 | }
|
70 | 64 | }
|
0 commit comments