@@ -52,78 +52,126 @@ if [[ `uname -s` == "Darwin" ]]; then
5252    TB=' ' 
5353fi 
5454
55- #  Ensure there is a <packageSources>...</packageSources> section.
56- grep -i " <packageSources>" $ConfigFile 
57- if  [ " $? " !=  " 0" ;  then 
58-     echo  " Adding <packageSources>...</packageSources> section." 
59-     ConfigNodeHeader=" <configuration>" 
60-     PackageSourcesTemplate=" ${TB} <packageSources>${NL}${TB} </packageSources>" 
55+ #  Enables an internal package source by name, if found. Returns 0 if found and enabled, 1 if not found.
56+ EnableInternalPackageSource () {
57+     local  PackageSourceName=" $1 " 
58+     
59+     #  Check if disabledPackageSources section exists
60+     grep -i " <disabledPackageSources>" " $ConfigFile " >  /dev/null
61+     if  [ " $? " !=  " 0" ;  then 
62+         return  1  #  No disabled sources section
63+     fi 
64+     
65+     #  Check if this source name is disabled
66+     grep -i " <add key=\" $PackageSourceName \"  value=\" true\" " " $ConfigFile " >  /dev/null
67+     if  [ " $? " ==  " 0" ;  then 
68+         echo  " Enabling internal source '$PackageSourceName '." 
69+         #  Remove the disabled entry
70+         local  OldDisableValue=" <add key=\" $PackageSourceName \"  value=\" true\"  />" 
71+         local  NewDisableValue=" <!-- Reenabled for build : $PackageSourceName  -->" 
72+         sed -i.bak " s|$OldDisableValue |$NewDisableValue |" " $ConfigFile " 
73+         
74+         #  Add the source name to PackageSources for credential handling
75+         PackageSources+=(" $PackageSourceName " 
76+         return  0  #  Found and enabled
77+     fi 
78+     
79+     return  1  #  Not found in disabled sources
80+ }
81+ 
82+ #  Add source entry to PackageSources
83+ AddPackageSource () {
84+     local  SourceName=" $1 " 
85+     local  SourceEndPoint=" $2 " 
86+     
87+     #  Check if source already exists
88+     grep -i " <add key=\" $SourceName \" " " $ConfigFile " >  /dev/null
89+     if  [ " $? " ==  " 0" ;  then 
90+         echo  " Package source $SourceName  already present and enabled." 
91+         PackageSources+=(" $SourceName " 
92+         return 
93+     fi 
94+     
95+     echo  " Adding package source $SourceName " 
96+     PackageSourcesNodeFooter=" </packageSources>" 
97+     PackageSourceTemplate=" ${TB} <add key=\" $SourceName \"  value=\" $SourceEndPoint \"  />" 
98+     
99+     sed -i.bak " s|$PackageSourcesNodeFooter |$PackageSourceTemplate ${NL} $PackageSourcesNodeFooter |" " $ConfigFile " 
100+     PackageSources+=(" $SourceName " 
101+ }
102+ 
103+ #  Adds or enables the package source with the given name
104+ AddOrEnablePackageSource () {
105+     local  SourceName=" $1 " 
106+     local  SourceEndPoint=" $2 " 
107+     
108+     #  Try to enable if disabled, if not found then add new source
109+     EnableInternalPackageSource " $SourceName " 
110+     if  [ " $? " !=  " 0" ;  then 
111+         AddPackageSource " $SourceName " " $SourceEndPoint " 
112+     fi 
113+ }
61114
62-     sed -i.bak " s|$ConfigNodeHeader |$ConfigNodeHeader ${NL} $PackageSourcesTemplate |" $ConfigFile 
63- fi 
115+ #  Enable all darc-int package sources
116+ EnableMaestroInternalPackageSources () {
117+     #  Check if disabledPackageSources section exists
118+     grep -i " <disabledPackageSources>" " $ConfigFile " >  /dev/null
119+     if  [ " $? " !=  " 0" ;  then 
120+         return   #  No disabled sources section
121+     fi 
122+     
123+     #  Find all darc-int disabled sources
124+     local  DisabledDarcIntSources=()
125+     DisabledDarcIntSources+=$( grep -oh ' "darc-int-[^"]*" value="true"' " $ConfigFile " |  tr -d ' "' ) 
126+     
127+     for  DisabledSourceName  in  ${DisabledDarcIntSources[@]}  ;  do 
128+         if  [[ $DisabledSourceName  ==  darc-int*  ]];  then 
129+             EnableInternalPackageSource " $DisabledSourceName " 
130+         fi 
131+     done 
132+ }
64133
65- #  Ensure there is a <packageSourceCredentials >...</packageSourceCredentials > section.  
66- grep -i " <packageSourceCredentials >" $ConfigFile 
134+ #  Ensure there is a <packageSources >...</packageSources > section.
135+ grep -i " <packageSources >" $ConfigFile 
67136if  [ " $? " !=  " 0" ;  then 
68-     echo  " Adding <packageSourceCredentials>...</packageSourceCredentials> section." 
69- 
70-     PackageSourcesNodeFooter=" </packageSources>" 
71-     PackageSourceCredentialsTemplate=" ${TB} <packageSourceCredentials>${NL}${TB} </packageSourceCredentials>" 
72- 
73-     sed -i.bak " s|$PackageSourcesNodeFooter |$PackageSourcesNodeFooter ${NL} $PackageSourceCredentialsTemplate |" $ConfigFile 
137+     Write-PipelineTelemetryError -Category ' Build' " Error: Eng/common/SetupNugetSources.sh returned a non-zero exit code. NuGet config file must contain a packageSources section: $ConfigFile " 
138+     ExitWithExitCode 1
74139fi 
75140
76141PackageSources=()
77142
78- #  Ensure dotnet3.1-internal and dotnet3.1-internal-transport are in the packageSources if the public dotnet3.1 feeds are present
79- grep -i " <add key=\" dotnet3.1\" " $ConfigFile 
80- if  [ " $? " ==  " 0" ;  then 
81-     grep -i " <add key=\" dotnet3.1-internal\" " $ConfigFile 
143+ #  Set feed suffix based on whether credentials are provided
144+ FeedSuffix=" v3/index.json" 
145+ if  [ -n  " $CredToken " ;  then 
146+     FeedSuffix=" v2" 
147+     
148+     #  Ensure there is a <packageSourceCredentials>...</packageSourceCredentials> section.
149+     grep -i " <packageSourceCredentials>" $ConfigFile 
82150    if  [ " $? " !=  " 0" ;  then 
83-         echo  " Adding dotnet3.1-internal to the packageSources." 
84-         PackageSourcesNodeFooter=" </packageSources>" 
85-         PackageSourceTemplate=" ${TB} <add key=\" dotnet3.1-internal\"  value=\" https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal/nuget/v2\"  />" 
151+         echo  " Adding <packageSourceCredentials>...</packageSourceCredentials> section." 
86152
87-         sed -i.bak " s|$PackageSourcesNodeFooter |$PackageSourceTemplate ${NL} $PackageSourcesNodeFooter |" $ConfigFile 
88-     fi 
89-     PackageSources+=(' dotnet3.1-internal' 
90- 
91-     grep -i " <add key=\" dotnet3.1-internal-transport\" >" $ConfigFile 
92-     if  [ " $? " !=  " 0" ;  then 
93-         echo  " Adding dotnet3.1-internal-transport to the packageSources." 
94153        PackageSourcesNodeFooter=" </packageSources>" 
95-         PackageSourceTemplate =" ${TB} <add key= \" dotnet3.1-internal-transport \"  value= \" https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal-transport/nuget/v2 \"  / >" 
154+         PackageSourceCredentialsTemplate =" ${TB} <packageSourceCredentials> ${NL}${TB} </packageSourceCredentials >" 
96155
97-         sed -i.bak " s|$PackageSourcesNodeFooter |$PackageSourceTemplate  ${NL} $PackageSourcesNodeFooter  |" $ConfigFile 
156+         sed -i.bak " s|$PackageSourcesNodeFooter |$PackageSourcesNodeFooter  ${NL} $PackageSourceCredentialsTemplate  |" $ConfigFile 
98157    fi 
99-     PackageSources+=(' dotnet3.1-internal-transport' 
158+ fi 
159+ 
160+ #  Check for disabledPackageSources; we'll enable any darc-int ones we find there
161+ grep -i " <disabledPackageSources>" $ConfigFile  >  /dev/null
162+ if  [ " $? " ==  " 0" ;  then 
163+     echo  " Checking for any darc-int disabled package sources in the disabledPackageSources node" 
164+     EnableMaestroInternalPackageSources
100165fi 
101166
102167DotNetVersions=(' 5' ' 6' ' 7' ' 8' ' 9' ' 10' 
103168
104169for  DotNetVersion  in  ${DotNetVersions[@]}  ;  do 
105170    FeedPrefix=" dotnet${DotNetVersion} " ; 
106-     grep -i " <add key=\" $FeedPrefix \" " $ConfigFile 
171+     grep -i " <add key=\" $FeedPrefix \" " $ConfigFile   >  /dev/null 
107172    if  [ " $? " ==  " 0" ;  then 
108-         grep -i " <add key=\" $FeedPrefix -internal\" " $ConfigFile 
109-         if  [ " $? " !=  " 0" ;  then 
110-             echo  " Adding $FeedPrefix -internal to the packageSources." 
111-             PackageSourcesNodeFooter=" </packageSources>" 
112-             PackageSourceTemplate=" ${TB} <add key=\" $FeedPrefix -internal\"  value=\" https://pkgs.dev.azure.com/dnceng/internal/_packaging/$FeedPrefix -internal/nuget/v2\"  />" 
113- 
114-             sed -i.bak " s|$PackageSourcesNodeFooter |$PackageSourceTemplate ${NL} $PackageSourcesNodeFooter |" $ConfigFile 
115-         fi 
116-         PackageSources+=(" $FeedPrefix -internal" 
117- 
118-         grep -i " <add key=\" $FeedPrefix -internal-transport\" >" $ConfigFile 
119-         if  [ " $? " !=  " 0" ;  then 
120-             echo  " Adding $FeedPrefix -internal-transport to the packageSources." 
121-             PackageSourcesNodeFooter=" </packageSources>" 
122-             PackageSourceTemplate=" ${TB} <add key=\" $FeedPrefix -internal-transport\"  value=\" https://pkgs.dev.azure.com/dnceng/internal/_packaging/$FeedPrefix -internal-transport/nuget/v2\"  />" 
123- 
124-             sed -i.bak " s|$PackageSourcesNodeFooter |$PackageSourceTemplate ${NL} $PackageSourcesNodeFooter |" $ConfigFile 
125-         fi 
126-         PackageSources+=(" $FeedPrefix -internal-transport" 
173+         AddOrEnablePackageSource " $FeedPrefix -internal" " https://pkgs.dev.azure.com/dnceng/internal/_packaging/$FeedPrefix -internal/nuget/$FeedSuffix " 
174+         AddOrEnablePackageSource " $FeedPrefix -internal-transport" " https://pkgs.dev.azure.com/dnceng/internal/_packaging/$FeedPrefix -internal-transport/nuget/$FeedSuffix " 
127175    fi 
128176done 
129177
@@ -139,29 +187,12 @@ if [ "$CredToken" ]; then
139187        #  Check if there is no existing credential for this FeedName
140188        grep -i " <$FeedName >" $ConfigFile  
141189        if  [ " $? " !=  " 0" ;  then 
142-             echo  " Adding credentials  for $FeedName . " 
190+             echo  " 	Inserting credential  for feed:  $FeedName " 
143191
144192            PackageSourceCredentialsNodeFooter=" </packageSourceCredentials>" 
145-             NewCredential=" ${TB}${TB} <$FeedName >${NL} <add key=\" Username\"  value=\" dn-bot\"  />${NL} <add key=\" ClearTextPassword\"  value=\" $CredToken \"  />${NL} </$FeedName >" 
193+             NewCredential=" ${TB}${TB} <$FeedName >${NL}${TB}  <add key=\" Username\"  value=\" dn-bot\"  />${NL}${TB}${TB}  <add key=\" ClearTextPassword\"  value=\" $CredToken \"  />${NL}${TB}${TB } </$FeedName >" 
146194
147195            sed -i.bak " s|$PackageSourceCredentialsNodeFooter |$NewCredential ${NL} $PackageSourceCredentialsNodeFooter |" $ConfigFile 
148196        fi 
149197    done 
150198fi 
151- 
152- #  Re-enable any entries in disabledPackageSources where the feed name contains darc-int
153- grep -i " <disabledPackageSources>" $ConfigFile 
154- if  [ " $? " ==  " 0" ;  then 
155-     DisabledDarcIntSources=()
156-     echo  " Re-enabling any disabled \" darc-int\"  package sources in $ConfigFile " 
157-     DisabledDarcIntSources+=$( grep -oh ' "darc-int-[^"]*" value="true"' $ConfigFile   |  tr -d ' "' ) 
158-     for  DisabledSourceName  in  ${DisabledDarcIntSources[@]}  ;  do 
159-         if  [[ $DisabledSourceName  ==  darc-int*  ]]
160-             then 
161-                 OldDisableValue=" <add key=\" $DisabledSourceName \"  value=\" true\"  />" 
162-                 NewDisableValue=" <!-- Reenabled for build : $DisabledSourceName  -->" 
163-                 sed -i.bak " s|$OldDisableValue |$NewDisableValue |" $ConfigFile 
164-                 echo  " Neutralized disablePackageSources entry for '$DisabledSourceName '" 
165-         fi 
166-     done 
167- fi 
0 commit comments