Skip to content

Commit 49ce074

Browse files
committed
fix: application data dir may not have been created before crash (#161)
1 parent f1f54bf commit 49ce074

File tree

4 files changed

+8
-12
lines changed

4 files changed

+8
-12
lines changed

src/App.axaml.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public static void Main(string[] args)
6262
builder.Append(ex.StackTrace);
6363

6464
var time = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
65-
var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
66-
"SourceGit",
67-
$"crash_{time}.log");
65+
var file = Path.Combine(Native.OS.DataDir, $"crash_{time}.log");
6866
File.WriteAllText(file, builder.ToString());
6967
}
7068
}

src/Models/AvatarManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static string SelectedServer
2525

2626
static AvatarManager()
2727
{
28-
_storePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SourceGit", "avatars");
28+
_storePath = Path.Combine(Native.OS.DataDir, "avatars");
2929
if (!Directory.Exists(_storePath))
3030
Directory.CreateDirectory(_storePath);
3131

src/Native/OS.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34

45
using Avalonia;
56

@@ -20,6 +21,7 @@ public interface IBackend
2021
void OpenWithDefaultEditor(string file);
2122
}
2223

24+
public static readonly string DataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SourceGit");
2325
public static string GitExecutable { get; set; } = string.Empty;
2426
public static List<Models.ExternalTool> ExternalTools { get; set; } = new List<Models.ExternalTool>();
2527

@@ -72,6 +74,9 @@ public static bool SetShell(Models.Shell shell)
7274

7375
public static void SetupApp(AppBuilder builder)
7476
{
77+
if (!Directory.Exists(DataDir))
78+
Directory.CreateDirectory(DataDir);
79+
7580
_backend.SetupApp(builder);
7681
}
7782

src/ViewModels/Preference.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,6 @@ public static Repository AddRepository(string rootDir, string gitDir)
459459

460460
public static void Save()
461461
{
462-
var dir = Path.GetDirectoryName(_savePath);
463-
if (!Directory.Exists(dir))
464-
Directory.CreateDirectory(dir);
465-
466462
var data = JsonSerializer.Serialize(_instance, JsonCodeGen.Default.Preference);
467463
File.WriteAllText(_savePath, data);
468464
}
@@ -515,10 +511,7 @@ private static bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList<Reposi
515511
}
516512

517513
private static Preference _instance = null;
518-
private static readonly string _savePath = Path.Combine(
519-
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
520-
"SourceGit",
521-
"preference.json");
514+
private static readonly string _savePath = Path.Combine(Native.OS.DataDir, "preference.json");
522515

523516
private string _locale = "en_US";
524517
private string _theme = "Default";

0 commit comments

Comments
 (0)