Skip to content

Commit aaba084

Browse files
github-actions[bot]Jo Shields
andauthored
Use partial classes instead, mirroring iOS approach (#58361)
This should give the same output as legacy Xamarin. If it doesn't, I messed up Co-authored-by: Jo Shields <[email protected]>
1 parent 22b53e8 commit aaba084

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,7 +2098,7 @@
20982098
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.UnixOrBrowser.cs" />
20992099
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.OSVersion.OSX.cs" Condition="'$(IsOSXLike)' == 'true' AND '$(TargetsMacCatalyst)' != 'true'" />
21002100
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.OSVersion.MacCatalyst.cs" Condition="'$(TargetsMacCatalyst)' == 'true'" />
2101-
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.GetFolderPathCore.Unix.cs" Condition="'$(IsiOSLike)' != 'true'" />
2101+
<Compile Include="$(MSBuildThisFileDirectory)System\Environment.GetFolderPathCore.Unix.cs" Condition="'$(IsiOSLike)' != 'true' and '$(TargetsAndroid)' != 'true'" />
21022102
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CalendarData.Unix.cs" />
21032103
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureData.Unix.cs" />
21042104
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureInfo.Unix.cs" />
@@ -2327,4 +2327,4 @@
23272327
<Compile Include="$(MSBuildThisFileDirectory)System\IUnaryNegationOperators.cs" />
23282328
<Compile Include="$(MSBuildThisFileDirectory)System\IUnaryPlusOperators.cs" />
23292329
</ItemGroup>
2330-
</Project>
2330+
</Project>

src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@
272272
<Link>Common\Interop\Unix\System.Native\Interop.GetEnviron.cs</Link>
273273
</Compile>
274274
</ItemGroup>
275+
<ItemGroup Condition="'$(TargetsAndroid)' == 'true'">
276+
<Compile Include="$(BclSourcesRoot)\System\Environment.Android.cs" />
277+
</ItemGroup>
275278
<ItemGroup Condition="'$(TargetsMacCatalyst)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'">
276279
<Compile Include="$(BclSourcesRoot)\System\Environment.iOS.cs" />
277280
<Compile Include="$(CommonPath)Interop\OSX\System.Native\Interop.SearchPath.cs">
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.IO;
5+
using System.Threading;
6+
using System.Collections.Generic;
7+
using System.Runtime.InteropServices;
8+
using System.Diagnostics;
9+
10+
namespace System
11+
{
12+
public static partial class Environment
13+
{
14+
private static Dictionary<SpecialFolder, string>? s_specialFolders;
15+
16+
private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption option)
17+
{
18+
if (s_specialFolders == null)
19+
{
20+
Interlocked.CompareExchange(ref s_specialFolders, new Dictionary<SpecialFolder, string>(), null);
21+
}
22+
23+
string? path;
24+
lock (s_specialFolders)
25+
{
26+
if (!s_specialFolders.TryGetValue(folder, out path))
27+
{
28+
path = GetSpecialFolder(folder) ?? string.Empty;
29+
s_specialFolders[folder] = path;
30+
}
31+
}
32+
return path;
33+
}
34+
35+
private static string? GetSpecialFolder(SpecialFolder folder)
36+
{
37+
string? home = null;
38+
try
39+
{
40+
home = PersistedFiles.GetHomeDirectory();
41+
}
42+
catch (Exception exc)
43+
{
44+
Debug.Fail($"Unable to get home directory: {exc}");
45+
}
46+
47+
// Fall back to '/' when we can't determine the home directory.
48+
// This location isn't writable by non-root users which provides some safeguard
49+
// that the application doesn't write data which is meant to be private.
50+
if (string.IsNullOrEmpty(home))
51+
{
52+
home = "/";
53+
}
54+
55+
switch (folder)
56+
{
57+
case SpecialFolder.Personal:
58+
case SpecialFolder.LocalApplicationData:
59+
return home;
60+
61+
case SpecialFolder.ApplicationData:
62+
return Path.Combine(home, ".config");
63+
64+
case SpecialFolder.Desktop:
65+
case SpecialFolder.DesktopDirectory:
66+
return Path.Combine(home, "Desktop");
67+
68+
case SpecialFolder.MyMusic:
69+
return Path.Combine(home, "Music");
70+
71+
case SpecialFolder.MyPictures:
72+
return Path.Combine(home, "Pictures");
73+
74+
case SpecialFolder.Templates:
75+
return Path.Combine(home, "Templates");
76+
77+
case SpecialFolder.MyVideos:
78+
return Path.Combine(home, "Videos");
79+
80+
case SpecialFolder.CommonTemplates:
81+
return "/usr/share/templates";
82+
83+
case SpecialFolder.Fonts:
84+
return Path.Combine(home, ".fonts");
85+
86+
case SpecialFolder.UserProfile:
87+
return GetEnvironmentVariable("HOME");
88+
89+
case SpecialFolder.CommonApplicationData:
90+
return "/usr/share";
91+
92+
default:
93+
return string.Empty;
94+
}
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)