@@ -27,14 +27,30 @@ public class InstallWorkloadFromArtifacts : Task
2727 [ Required , NotNull ]
2828 public string ? LocalNuGetsPath { get ; set ; }
2929
30+ [ Required , NotNull ]
31+ public string ? TemplateNuGetConfigPath { get ; set ; }
32+
3033 [ Required , NotNull ]
3134 public string ? SdkDir { get ; set ; }
3235
3336 public bool OnlyUpdateManifests { get ; set ; }
3437
35- public ITaskItem [ ] ExtraNuGetSources { get ; set ; } = Array . Empty < ITaskItem > ( ) ;
38+ private const string s_nugetInsertionTag = "<!-- TEST_RESTORE_SOURCES_INSERTION_LINE -->" ;
3639
3740 public override bool Execute ( )
41+ {
42+ try
43+ {
44+ return ExecuteInternal ( ) ;
45+ }
46+ catch ( LogAsErrorException laee )
47+ {
48+ Log . LogError ( laee . Message ) ;
49+ return false ;
50+ }
51+ }
52+
53+ private bool ExecuteInternal ( )
3854 {
3955 if ( ! HasMetadata ( WorkloadId , nameof ( WorkloadId ) , "Version" ) ||
4056 ! HasMetadata ( WorkloadId , nameof ( WorkloadId ) , "ManifestName" ) )
@@ -48,6 +64,12 @@ public override bool Execute()
4864 return false ;
4965 }
5066
67+ if ( ! File . Exists ( TemplateNuGetConfigPath ) )
68+ {
69+ Log . LogError ( $ "Cannot find TemplateNuGetConfigPath={ TemplateNuGetConfigPath } ") ;
70+ return false ;
71+ }
72+
5173 Log . LogMessage ( MessageImportance . High , $ "{ Environment . NewLine } ** Installing workload manifest { WorkloadId . ItemSpec } **{ Environment . NewLine } ") ;
5274
5375 string nugetConfigContents = GetNuGetConfig ( ) ;
@@ -86,25 +108,11 @@ public override bool Execute()
86108
87109 private string GetNuGetConfig ( )
88110 {
89- StringBuilder nugetConfigBuilder = new ( ) ;
90- nugetConfigBuilder . AppendLine ( $ "<configuration>{ Environment . NewLine } <packageSources>") ;
91-
92- nugetConfigBuilder . AppendLine ( $@ "<add key=""nuget-local"" value=""{ LocalNuGetsPath } "" />") ;
93- foreach ( ITaskItem source in ExtraNuGetSources )
94- {
95- string key = source . ItemSpec ;
96- string value = source . GetMetadata ( "Value" ) ;
97- if ( string . IsNullOrEmpty ( value ) )
98- {
99- Log . LogWarning ( $ "ExtraNuGetSource { key } is missing Value metadata") ;
100- continue ;
101- }
102-
103- nugetConfigBuilder . AppendLine ( $@ "<add key=""{ key } "" value=""{ value } "" />") ;
104- }
111+ string contents = File . ReadAllText ( TemplateNuGetConfigPath ) ;
112+ if ( contents . IndexOf ( s_nugetInsertionTag ) < 0 )
113+ throw new LogAsErrorException ( $ "Could not find { s_nugetInsertionTag } in { TemplateNuGetConfigPath } ") ;
105114
106- nugetConfigBuilder . AppendLine ( $ "</packageSources>{ Environment . NewLine } </configuration>") ;
107- return nugetConfigBuilder . ToString ( ) ;
115+ return contents . Replace ( s_nugetInsertionTag , $@ "<add key=""nuget-local"" value=""{ LocalNuGetsPath } "" />") ;
108116 }
109117
110118 private bool InstallWorkloadManifest ( string name , string version , string nugetConfigContents , bool stopOnMissing )
0 commit comments