11using System ;
22using System . IO ;
33using System . Reflection ;
4+ using System . Runtime . InteropServices ;
45using Newtonsoft . Json ;
56using Shrinker . Parser ;
67// ReSharper disable UnusedMember.Global
@@ -32,8 +33,7 @@ private UserSettings()
3233 companyName = RemoveInvalidChars ( companyName ) ;
3334 productName = RemoveInvalidChars ( productName ) ;
3435
35- var appData = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ;
36- var appDirectory = Path . Combine ( appData , companyName , productName ) ;
36+ var appDirectory = Path . Combine ( GetAppDataPath ( ) , companyName , productName ) ;
3737 Directory . CreateDirectory ( appDirectory ) ;
3838 m_filePath = Path . Combine ( appDirectory , "settings.json" ) ;
3939
@@ -48,6 +48,27 @@ private string RemoveInvalidChars(string s)
4848 return s ;
4949 }
5050
51+ private static string GetAppDataPath ( )
52+ {
53+ var appDataPath = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ;
54+
55+ if ( string . IsNullOrEmpty ( appDataPath ) )
56+ {
57+ var homePath = Environment . GetEnvironmentVariable ( "HOME" ) ;
58+ if ( string . IsNullOrEmpty ( homePath ) )
59+ {
60+ // Fallback to using ~ if HOME environment variable is not set
61+ homePath = "~" ;
62+ }
63+
64+ appDataPath = RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) ? Path . Combine ( homePath , "Library" , "Preferences" ) : homePath ;
65+ }
66+
67+ Directory . CreateDirectory ( appDataPath ) ;
68+
69+ return appDataPath ;
70+ }
71+
5172 public void Dispose ( ) =>
5273 File . WriteAllText ( m_filePath , JsonConvert . SerializeObject ( this , Formatting . Indented ) ) ;
5374}
0 commit comments