Skip to content

Commit d705e05

Browse files
committed
Changing NuGet client resolution and adding to runtime on build
1 parent 9cc6004 commit d705e05

File tree

3 files changed

+36
-46
lines changed

3 files changed

+36
-46
lines changed

WebJobs.Script.proj

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<SiteExtensionVersion>1.0.$(BuildNumber)</SiteExtensionVersion>
1111
<SiteExtensionBasePath>$(PublishPath)\Packages\SiteExtension</SiteExtensionBasePath>
1212
<SiteExtensionPath>$(SiteExtensionBasePath)\$(SiteExtensionVersion)</SiteExtensionPath>
13+
<FunctionsSiteExtensionPath>.\src\WebJobs.Script.WebHost\Publish\SiteExtensions\Functions</FunctionsSiteExtensionPath>
1314
<ExtensionXml>src\SiteExtension\extension.xml</ExtensionXml>
1415
<SetConfiguration Condition=" '$(Configuration)' != '' ">Configuration=$(Configuration)</SetConfiguration>
1516
<SetPlatform Condition=" '$(Platform)' != '' ">Platform=$(Platform)</SetPlatform>
@@ -103,7 +104,11 @@
103104
BuildInParallel="$(BuildInParallel)"/>
104105
</Target>
105106

106-
<Target Name="PackageScriptHost" DependsOnTargets="Build">
107+
<Target Name="DownloadRuntimeNugetClient">
108+
<DownloadNuGet OutputFileName="$(FunctionsSiteExtensionPath)\bin\tools\nuget.exe" MinimumVersion="3.3.0" DownloadUrl="https://azfunc.blob.core.windows.net/public/nuget.exe" />
109+
</Target>
110+
111+
<Target Name="PackageScriptHost" DependsOnTargets="Build;DownloadRuntimeNugetClient">
107112
<PropertyGroup>
108113
<ScriptHostOutput>.\src\WebJobs.Script.Host\bin\$(Configuration)</ScriptHostOutput>
109114
</PropertyGroup>
@@ -122,7 +127,10 @@
122127
/>
123128
</Target>
124129

125-
<Target Name="PackageWebHost" DependsOnTargets="Build">
130+
<Target Name="PackageWebHost" DependsOnTargets="Build;DownloadRuntimeNugetClient">
131+
<ItemGroup>
132+
<SiteExtensionSource Include="$(FunctionsSiteExtensionPath)\**\*.*" />
133+
</ItemGroup>
126134
<MSBuild Projects="src\WebJobs.Script.WebHost\WebJobs.Script.WebHost.csproj"
127135
Properties="DeployOnBuild=true; PublishProfile=FileSystem">
128136
</MSBuild>
@@ -134,10 +142,6 @@
134142
<!--Create site extension package -->
135143
<MakeDir Directories="@(SiteExtensionPath)"/>
136144

137-
<ItemGroup>
138-
<SiteExtensionSource Include=".\src\WebJobs.Script.WebHost\Publish\SiteExtensions\Functions\**\*.*" />
139-
</ItemGroup>
140-
141145
<Copy SourceFiles="@(SiteExtensionSource)" DestinationFiles="@(SiteExtensionSource->'$(SiteExtensionPath)\%(RecursiveDir)%(Filename)%(Extension)')" />
142146
<Copy SourceFiles="$(ExtensionXml)" DestinationFolder="$(SiteExtensionBasePath)" />
143147

@@ -213,12 +217,14 @@
213217
<ParameterGroup>
214218
<OutputFileName ParameterType="System.String" Required="true" />
215219
<MinimumVersion ParameterType="System.String" Required="true" />
220+
<DownloadUrl ParameterType="System.String" Required="false" />
216221
</ParameterGroup>
217222
<Task>
218223
<Using Namespace="System.Diagnostics" />
219224
<Using Namespace="System.Net" />
220225
<Code Type="Fragment" Language="cs">
221226
<![CDATA[
227+
DownloadUrl = DownloadUrl ?? "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe";
222228
Version minimumRequiredVersion;
223229
224230
if (!Version.TryParse(MinimumVersion, out minimumRequiredVersion))
@@ -265,12 +271,12 @@
265271
File.Delete(OutputFileName);
266272
}
267273
}
268-
274+
Directory.CreateDirectory(Path.GetDirectoryName(OutputFileName));
269275
if (!File.Exists(OutputFileName))
270276
{
271-
Log.LogMessage("Downloading latest version of NuGet.exe...");
277+
Log.LogMessage("Downloading NuGet.exe from {0}...", DownloadUrl);
272278
WebClient webClient = new WebClient();
273-
webClient.DownloadFile("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", OutputFileName);
279+
webClient.DownloadFile(DownloadUrl, OutputFileName);
274280
}
275281
276282
return true;

src/WebJobs.Script/Description/DotNet/PackageManager.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Globalization;
99
using System.IO;
1010
using System.Linq;
11+
using System.Reflection;
1112
using System.Security.Cryptography;
1213
using System.Text;
1314
using System.Threading.Tasks;
@@ -135,23 +136,19 @@ internal static string GetCurrentLockFileHash(string functionDirectory)
135136
}
136137
}
137138

138-
public static string ResolveNuGetPath(string baseKuduPath = null)
139+
public static string ResolveNuGetPath()
139140
{
140141
// Check if we have the path in the well known environment variable
141142
string path = ScriptSettingsManager.Instance.GetSetting(NugetPathEnvironmentKey);
142143

143-
//// If we don't have the path, try to get a fully qualified path to Kudu's NuGet copy.
144+
//// If we don't have the path, get the runtime's copy of NuGet
144145
if (string.IsNullOrEmpty(path))
145146
{
146-
// Get the latest Kudu extension path
147-
string kuduFolder = baseKuduPath ?? Environment.ExpandEnvironmentVariables("%programfiles(x86)%\\siteextensions\\kudu");
148-
string kuduPath = Directory.Exists(kuduFolder)
149-
? Directory.GetDirectories(kuduFolder).OrderByDescending(d => d).FirstOrDefault()
150-
: null;
147+
string runtimeNugetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\tools", NuGetFileName);
151148

152-
if (!string.IsNullOrEmpty(kuduPath))
149+
if (File.Exists(runtimeNugetPath))
153150
{
154-
path = Path.Combine(kuduPath, "bin\\scripts", NuGetFileName);
151+
path = runtimeNugetPath;
155152
}
156153
}
157154

test/WebJobs.Script.Tests/Description/DotNet/PackageManagerTests.cs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4+
using System;
45
using System.IO;
56
using Microsoft.Azure.WebJobs.Script.Config;
67
using Microsoft.Azure.WebJobs.Script.Description;
8+
using WebJobs.Script.Tests;
79
using Xunit;
810

911
namespace Microsoft.Azure.WebJobs.Script.Tests
@@ -40,7 +42,7 @@ public void RequirePackageRestore_ReturnsExpectedResult(string projectPath, bool
4042
}
4143

4244
[Fact]
43-
public void ResolveNuGetPath_Local_WithNoEnvironmentHint_ReturnsExpectedResult()
45+
public void ResolveNuGetPath_WithNoEnvironmentHint_AndNoLocalFile_ReturnsExpectedResult()
4446
{
4547
using (var variables = new TestScopedSettings(SettingsManager, "AzureWebJobs_NuGetPath", null))
4648
{
@@ -51,45 +53,30 @@ public void ResolveNuGetPath_Local_WithNoEnvironmentHint_ReturnsExpectedResult()
5153
}
5254

5355
[Fact]
54-
public void ResolveNuGetPath_Local_WithEnvironmentHint_ReturnsExpectedResult()
56+
public void ResolveNuGetPath_Local_WithNoEnvironmentHint_ReturnsExpectedResult()
5557
{
56-
string path = @"c:\some\path\to\nuget.exe";
57-
58-
using (var variables = new TestScopedSettings(SettingsManager, "AzureWebJobs_NuGetPath", path))
58+
using (var variables = new TestScopedSettings(SettingsManager, "AzureWebJobs_NuGetPath", null))
59+
using (var nugetDirectory = new TempDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\tools\\")))
5960
{
61+
string nugetPath = Path.Combine(nugetDirectory.Path, "nuget.exe");
62+
File.WriteAllText(nugetPath, string.Empty);
63+
6064
string result = PackageManager.ResolveNuGetPath();
6165

62-
Assert.Equal(path, result);
66+
Assert.Equal(nugetPath, result);
6367
}
6468
}
6569

6670
[Fact]
67-
public void ResolveNuGetPath_WithKuduPath_ReturnsExpectedResult()
71+
public void ResolveNuGetPath_Local_WithEnvironmentHint_ReturnsExpectedResult()
6872
{
69-
string kuduBasePath = Path.Combine(Path.GetTempPath(), @"base\kudu");
73+
string path = @"c:\some\path\to\nuget.exe";
7074

71-
try
75+
using (var variables = new TestScopedSettings(SettingsManager, "AzureWebJobs_NuGetPath", path))
7276
{
73-
string expectedKuduPath = Path.Combine(kuduBasePath, @"kudu2");
74-
75-
if (Directory.Exists(kuduBasePath))
76-
{
77-
Directory.Delete(kuduBasePath, true);
78-
}
79-
80-
Directory.CreateDirectory(expectedKuduPath);
81-
Directory.CreateDirectory(Path.Combine(kuduBasePath, @"kudu1"));
82-
83-
using (var variables = new TestScopedSettings(SettingsManager, "AzureWebJobs_NuGetPath", null))
84-
{
85-
string result = PackageManager.ResolveNuGetPath(kuduBasePath);
77+
string result = PackageManager.ResolveNuGetPath();
8678

87-
Assert.Equal(Path.Combine(expectedKuduPath, "bin\\scripts\\nuget.exe"), result);
88-
}
89-
}
90-
finally
91-
{
92-
Directory.Delete(kuduBasePath, true);
79+
Assert.Equal(path, result);
9380
}
9481
}
9582
}

0 commit comments

Comments
 (0)