Skip to content

Commit

Permalink
CLI: Detect if portable
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Jul 9, 2024
1 parent bf1821c commit c786f5f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 38 deletions.
24 changes: 13 additions & 11 deletions Guard.CLI/Guard.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
<Nullable>enable</Nullable>
<SupportedOSPlatformVersion>10.0.18362.0</SupportedOSPlatformVersion>
<ApplicationIcon>totp.ico</ApplicationIcon>
<Title>2FAGuard CLI</Title>
<Version>1.3.1</Version>
<Authors>Timo Kössler and Open Source Contributors</Authors>
<Copyright>$(Authors)</Copyright>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/timokoessler/2FAGuard</RepositoryUrl>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(Version)</AssemblyVersion>
<SelfContained>true</SelfContained>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<PackageProjectUrl>https://2faguard.app</PackageProjectUrl>
<StartupObject>Guard.CLI.Program</StartupObject>
<AssemblyName>2fa</AssemblyName>
<Authors>Timo Kössler and Open Source Contributors</Authors>
<Copyright>$(Authors)</Copyright>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/timokoessler/2FAGuard</RepositoryUrl>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(Version)</AssemblyVersion>
<SelfContained>true</SelfContained>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<PackageProjectUrl>https://2faguard.app</PackageProjectUrl>
<StartupObject>Guard.CLI.Program</StartupObject>
<AssemblyName>2fa</AssemblyName>
<DebugType>embedded</DebugType>
<DefineConstants Condition=" '$(IsPortable)' == 'true' ">$(DefineConstants);PORTABLE</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 7 additions & 4 deletions Guard.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Globalization;
using Guard.CLI.Commands;
using Guard.Core;
using Guard.Core.Storage;
using Spectre.Console.Cli;

namespace Guard.CLI
Expand All @@ -10,11 +9,15 @@ public class Program
{
public static int Main(string[] args)
{
// Todo: Detect InstallationType and isPortable and set it here
InstallationType installationType = InstallationType.CLASSIC_PORTABLE;
InstallationType installationType = InstallationType.CLASSIC_INSTALLER;
#if PORTABLE
installationType = InstallationType.CLASSIC_PORTABLE;
#endif

// Todo support UWP

InstallationContext.Init(
installationType,
false,
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version
?? new Version(0, 0)
);
Expand Down
27 changes: 12 additions & 15 deletions Guard.Core/InstallationContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

namespace Guard.Core
namespace Guard.Core
{
public enum InstallationType
{
Expand All @@ -10,37 +9,36 @@ public enum InstallationType

public static class InstallationContext
{

private static string? appDataFolderPath;
private static InstallationType? installationType;
private static bool isPortable;
private static Version? version;

public static void Init(InstallationType installationType, bool isPortable, Version version)
public static void Init(InstallationType installationType, Version version)
{
InstallationContext.installationType = installationType;
InstallationContext.isPortable = isPortable;
InstallationContext.version = version;

if(isPortable)
if (IsPortable())
{
appDataFolderPath = Path.Combine(
AppContext.BaseDirectory
?? throw new Exception("Could not get process directory"),
"2FAGuard-Data"
);
} else if(installationType == InstallationType.MICROSOFT_STORE)
);
}
else if (installationType == InstallationType.MICROSOFT_STORE)
{
appDataFolderPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"2FAGuardStoreApp"
);
} else
}
else
{
appDataFolderPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"2FAGuard"
);
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"2FAGuard"
);
}
}

Expand Down Expand Up @@ -77,8 +75,7 @@ public static InstallationType GetInstallationType()

public static bool IsPortable()
{
return isPortable;
return installationType == InstallationType.CLASSIC_PORTABLE;
}

}
}
6 changes: 1 addition & 5 deletions Guard.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ protected override async void OnStartup(StartupEventArgs e)
string mutexName = "2FAGuard";

var installationInfo = InstallationInfo.GetInstallationContext();
InstallationContext.Init(
installationInfo.installationType,
installationInfo.isPortable,
installationInfo.version
);
InstallationContext.Init(installationInfo.installationType, installationInfo.version);

if (installationInfo.installationType == InstallationType.CLASSIC_PORTABLE)
{
Expand Down
5 changes: 2 additions & 3 deletions Guard.WPF/Core/Installation/InstallationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Guard.WPF.Core.Installation
{

public class InstallationInfo
{
private static bool IsPortable()
Expand Down Expand Up @@ -39,9 +38,9 @@ private static Version GetVersion()
?? new Version(0, 0);
}

public static (InstallationType installationType, bool isPortable, Version version) GetInstallationContext()
public static (InstallationType installationType, Version version) GetInstallationContext()
{
return (GetInstallationType(), IsPortable(), GetVersion());
return (GetInstallationType(), GetVersion());
}
}
}

0 comments on commit c786f5f

Please sign in to comment.