refactor: rewrite the portable mode (#851)

This commit is contained in:
leo 2024-12-30 15:10:42 +08:00
parent 093176d10b
commit dc649e6142
No known key found for this signature in database
3 changed files with 9 additions and 14 deletions

View file

@ -64,7 +64,8 @@ This software creates a folder `$"{System.Environment.SpecialFolder.ApplicationD
| macOS | `${HOME}/Library/Application Support/SourceGit` | | macOS | `${HOME}/Library/Application Support/SourceGit` |
> [!TIP] > [!TIP]
> You can open this data storage directory from the main menu. > * You can open this data storage directory from the main menu `Open Data Storage Directory`.
> * You can create a `data` folder next to the `SourceGit` executable to force this app to store data (user settings, downloaded avatars and crash logs) into it (Portable-Mode). Only works on Windows.
For **Windows** users: For **Windows** users:
@ -81,7 +82,6 @@ For **Windows** users:
scoop install sourcegit scoop install sourcegit
``` ```
* Pre-built binaries can be found in [Releases](https://github.com/sourcegit-scm/sourcegit/releases/latest) * Pre-built binaries can be found in [Releases](https://github.com/sourcegit-scm/sourcegit/releases/latest)
* You can run `dotnet publish -c Release -r win-x64 -p:EnablePortable=true -o $YOUR_PUBLISH_DIR .\src\SourceGit.csproj` to build a portable version.
For **macOS** users: For **macOS** users:

View file

@ -1,8 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
#if ENABLE_PORTABLE
using System.Diagnostics; using System.Diagnostics;
#endif
using System.IO; using System.IO;
using Avalonia; using Avalonia;
@ -58,16 +56,17 @@ namespace SourceGit.Native
public static void SetupDataDir() public static void SetupDataDir()
{ {
#if ENABLE_PORTABLE
if (OperatingSystem.IsWindows()) if (OperatingSystem.IsWindows())
{ {
var execFile = Process.GetCurrentProcess().MainModule!.FileName; var execFile = Process.GetCurrentProcess().MainModule!.FileName;
DataDir = Path.Combine(Path.GetDirectoryName(execFile), "data"); var portableDir = Path.Combine(Path.GetDirectoryName(execFile), "data");
if (!Directory.Exists(DataDir)) if (Directory.Exists(portableDir))
Directory.CreateDirectory(DataDir); {
return; DataDir = portableDir;
return;
}
} }
#endif
var osAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); var osAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (string.IsNullOrEmpty(osAppDataDir)) if (string.IsNullOrEmpty(osAppDataDir))
DataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".sourcegit"); DataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".sourcegit");

View file

@ -30,10 +30,6 @@
<DefineConstants>$(DefineConstants);DISABLE_UPDATE_DETECTION</DefineConstants> <DefineConstants>$(DefineConstants);DISABLE_UPDATE_DETECTION</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(EnablePortable)' == 'true'">
<DefineConstants>$(DefineConstants);ENABLE_PORTABLE</DefineConstants>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<AvaloniaResource Include="App.ico" /> <AvaloniaResource Include="App.ico" />
<AvaloniaResource Include="Resources/Fonts/*" /> <AvaloniaResource Include="Resources/Fonts/*" />