Skip to content

Commit

Permalink
Remove DependencyModel's dependency on PlatformAbstractions
Browse files Browse the repository at this point in the history
Working towards #5213
  • Loading branch information
eerhardt committed Feb 21, 2019
1 parent 799be51 commit dbcee46
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;

namespace Microsoft.Extensions.DependencyModel
{
internal static class ApplicationEnvironment
{
public static string ApplicationBasePath { get; } = GetApplicationBasePath();

private static string GetApplicationBasePath()
{
var basePath =
#if NET451
(string)AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") ??
AppDomain.CurrentDomain.BaseDirectory;
#else
AppContext.BaseDirectory;
#endif
return Path.GetFullPath(basePath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Runtime.InteropServices;

namespace Microsoft.Extensions.DependencyModel
{
Expand All @@ -13,5 +14,10 @@ public string GetEnvironmentVariable(string name)
{
return Environment.GetEnvironmentVariable(name);
}

public bool IsWindows()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ namespace Microsoft.Extensions.DependencyModel
internal interface IEnvironment
{
string GetEnvironmentVariable(string name);
bool IsWindows();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Microsoft.DotNet.PlatformAbstractions\HashCodeCombiner.cs" />
</ItemGroup>

<Choose>
<!--
Since we added a target for netstandard2.0 so users aren't forced to download the 1.x dependencies,
Expand Down Expand Up @@ -39,6 +43,7 @@
<Otherwise>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Remove="ArrayBufferWriter.cs" />
Expand All @@ -50,16 +55,11 @@
</Otherwise>
</Choose>

<ItemGroup>
<ProjectReference Include="..\Microsoft.DotNet.PlatformAbstractions\Microsoft.DotNet.PlatformAbstractions.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' Or '$(TargetFramework)' == 'netstandard1.6' ">
<PackageReference Include="System.AppContext" Version="4.1.0" />
<PackageReference Include="System.Diagnostics.Debug" Version="4.0.11" />
<PackageReference Include="System.Dynamic.Runtime" Version="4.0.11" />
<PackageReference Include="System.Linq" Version="4.1.0" />
<PackageReference Include="System.Diagnostics.Debug" Version="4.0.11" />
<PackageReference Include="System.Dynamic.Runtime" Version="4.0.11" />
<PackageReference Include="System.IO.FileSystem" Version="4.0.1" />
<PackageReference Include="System.Linq" Version="4.1.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.DotNet.PlatformAbstractions;

#if !NETSTANDARD1_3

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.DotNet.PlatformAbstractions;
using System.Runtime.InteropServices;

namespace Microsoft.Extensions.DependencyModel.Resolution
{
Expand All @@ -27,14 +27,12 @@ public static string Resolve()

private static string GetDefaultDotNetReferenceAssembliesPath(IFileSystem fileSystem)
{
var os = RuntimeEnvironment.OperatingSystemPlatform;

if (os == Platform.Windows)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return null;
}

if (os == Platform.Darwin &&
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) &&
fileSystem.Directory.Exists("/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/xbuild-frameworks"))
{
return "/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/xbuild-frameworks";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.DotNet.PlatformAbstractions;

namespace Microsoft.Extensions.DependencyModel.Resolution
{
Expand Down Expand Up @@ -35,10 +34,7 @@ internal PackageCompilationAssemblyResolver(IFileSystem fileSystem, string[] nug
_nugetPackageDirectories = nugetPackageDirectories;
}

private static string[] GetDefaultProbeDirectories(IEnvironment environment) =>
GetDefaultProbeDirectories(RuntimeEnvironment.OperatingSystemPlatform, environment);

internal static string[] GetDefaultProbeDirectories(Platform osPlatform, IEnvironment environment)
internal static string[] GetDefaultProbeDirectories(IEnvironment environment)
{
#if !NETSTANDARD1_3
#if NETSTANDARD1_6
Expand All @@ -47,38 +43,37 @@ internal static string[] GetDefaultProbeDirectories(Platform osPlatform, IEnviro
var probeDirectories = AppDomain.CurrentDomain.GetData("PROBING_DIRECTORIES");
#endif

var listOfDirectories = probeDirectories as string;
var listOfDirectories = probeDirectories as string;

if (!string.IsNullOrEmpty(listOfDirectories))
{
return listOfDirectories.Split(new char [] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries );
}
if (!string.IsNullOrEmpty(listOfDirectories))
{
return listOfDirectories.Split(new char[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries);
}
#endif

var packageDirectory = environment.GetEnvironmentVariable("NUGET_PACKAGES");
var packageDirectory = environment.GetEnvironmentVariable("NUGET_PACKAGES");

if (!string.IsNullOrEmpty(packageDirectory))
{
return new string[] { packageDirectory };
}

string basePath;
if (osPlatform == Platform.Windows)
{
basePath = environment.GetEnvironmentVariable("USERPROFILE");
}
else
{
basePath = environment.GetEnvironmentVariable("HOME");
}
if (!string.IsNullOrEmpty(packageDirectory))
{
return new string[] { packageDirectory };
}

if (string.IsNullOrEmpty(basePath))
{
return new string[] { string.Empty };
}
string basePath;
if (environment.IsWindows())
{
basePath = environment.GetEnvironmentVariable("USERPROFILE");
}
else
{
basePath = environment.GetEnvironmentVariable("HOME");
}

return new string[] { Path.Combine(basePath, ".nuget", "packages") };
if (string.IsNullOrEmpty(basePath))
{
return new string[] { string.Empty };
}

return new string[] { Path.Combine(basePath, ".nuget", "packages") };
}

public bool TryResolveAssemblyPaths(CompilationLibrary library, List<string> assemblies)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.DotNet.PlatformAbstractions;

namespace Microsoft.Extensions.DependencyModel.Resolution
{
Expand All @@ -26,8 +25,8 @@ public ReferenceAssemblyPathResolver(string defaultReferenceAssembliesPath, stri

internal ReferenceAssemblyPathResolver(IFileSystem fileSystem, IEnvironment environment)
: this(fileSystem,
GetDefaultReferenceAssembliesPath(fileSystem, RuntimeEnvironment.OperatingSystemPlatform, environment),
GetFallbackSearchPaths(fileSystem, RuntimeEnvironment.OperatingSystemPlatform, environment))
GetDefaultReferenceAssembliesPath(fileSystem, environment),
GetFallbackSearchPaths(fileSystem, environment))
{
}

Expand Down Expand Up @@ -84,9 +83,9 @@ private bool TryResolveReferenceAssembly(string path, out string fullPath)
return false;
}

internal static string[] GetFallbackSearchPaths(IFileSystem fileSystem, Platform platform, IEnvironment environment)
internal static string[] GetFallbackSearchPaths(IFileSystem fileSystem, IEnvironment environment)
{
if (platform != Platform.Windows)
if (!environment.IsWindows())
{
return new string[0];
}
Expand All @@ -100,7 +99,7 @@ internal static string[] GetFallbackSearchPaths(IFileSystem fileSystem, Platform
return new[] { net20Dir };
}

internal static string GetDefaultReferenceAssembliesPath(IFileSystem fileSystem, Platform platform, IEnvironment environment)
internal static string GetDefaultReferenceAssembliesPath(IFileSystem fileSystem, IEnvironment environment)
{
// Allow setting the reference assemblies path via an environment variable
var referenceAssembliesPath = DotNetReferenceAssembliesPathResolver.Resolve(environment, fileSystem);
Expand All @@ -109,7 +108,7 @@ internal static string GetDefaultReferenceAssembliesPath(IFileSystem fileSystem,
return referenceAssembliesPath;
}

if (platform != Platform.Windows)
if (!environment.IsWindows())
{
// There is no reference assemblies path outside of windows
// The environment variable can be used to specify one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests
public class EnvironmentMockBuilder
{
private Dictionary<string, string> _variables = new Dictionary<string, string>();
private bool _isWindows;

internal static IEnvironment Empty { get; } = Create().Build();

Expand All @@ -23,18 +24,26 @@ public EnvironmentMockBuilder AddVariable(string name, string value)
return this;
}

public EnvironmentMockBuilder SetIsWindows(bool value)
{
_isWindows = value;
return this;
}

internal IEnvironment Build()
{
return new EnvironmentMock(_variables);
return new EnvironmentMock(_variables, _isWindows);
}

private class EnvironmentMock : IEnvironment
{
private Dictionary<string, string> _variables;
private bool _isWindows;

public EnvironmentMock(Dictionary<string, string> variables)
public EnvironmentMock(Dictionary<string, string> variables, bool isWindows)
{
_variables = variables;
_isWindows = isWindows;
}

public string GetEnvironmentVariable(string name)
Expand All @@ -43,6 +52,11 @@ public string GetEnvironmentVariable(string name)
_variables.TryGetValue(name, out value);
return value;
}

public bool IsWindows()
{
return _isWindows;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.DependencyModel.Resolution;
using Xunit;
using F = Microsoft.Extensions.DependencyModel.Tests.TestLibraryFactory;
Expand All @@ -24,7 +21,7 @@ public void ShouldUseEnvironmentVariableToGetDefaultLocation()
.AddVariable("NUGET_PACKAGES", PackagesPath)
.Build();

var result = PackageCompilationAssemblyResolver.GetDefaultProbeDirectories(Platform.Unknown, environment);
var result = PackageCompilationAssemblyResolver.GetDefaultProbeDirectories(environment);
// The host for .NET Core 2.0 always sets the PROBING_DIRECTORIES property on the AppContext. Because of that,
// no additional package directories should be returned from this, even if they are set as environment variables.
result.Should().NotContain(PackagesPath);
Expand All @@ -35,10 +32,11 @@ public void ShouldUseEnvironmentVariableToGetDefaultLocation()
public void ShouldUseNugetUnderUserProfileOnWindows()
{
var environment = EnvironmentMockBuilder.Create()
.SetIsWindows(true)
.AddVariable("USERPROFILE", "User Profile")
.Build();

var result = PackageCompilationAssemblyResolver.GetDefaultProbeDirectories(Platform.Windows, environment);
var result = PackageCompilationAssemblyResolver.GetDefaultProbeDirectories(environment);
// The host for .NET Core 2.0 always sets the PROBING_DIRECTORIES property on the AppContext. Because of that,
// no additional package directories should be returned from this, even if they are set as environment variables.
result.Should().NotContain(Path.Combine("User Profile", ".nuget", "packages"));
Expand All @@ -48,10 +46,11 @@ public void ShouldUseNugetUnderUserProfileOnWindows()
public void ShouldUseNugetUnderHomeOnNonWindows()
{
var environment = EnvironmentMockBuilder.Create()
.SetIsWindows(false)
.AddVariable("HOME", "User Home")
.Build();

var result = PackageCompilationAssemblyResolver.GetDefaultProbeDirectories(Platform.Linux, environment);
var result = PackageCompilationAssemblyResolver.GetDefaultProbeDirectories(environment);
// The host for .NET Core 2.0 always sets the PROBING_DIRECTORIES property on the AppContext. Because of that,
// no additional package directories should be returned from this, even if they are set as environment variables.
result.Should().NotContain(Path.Combine("User Home", ".nuget", "packages"));
Expand Down
Loading

0 comments on commit dbcee46

Please sign in to comment.