-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from flyingpie/features/settings
Settings
- Loading branch information
Showing
8 changed files
with
369 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,74 @@ | ||
#tool "nuget:?package=vswhere&version=2.6.7" | ||
#tool "nuget:?package=ILRepack&version=2.0.18" | ||
|
||
var configuration = Argument("configuration", "Release"); | ||
var output = Argument("output", "artifacts"); | ||
var version = Argument("version", "v0.4"); | ||
|
||
var sln = "windows-terminal-quake.sln"; | ||
var bin = "./windows-terminal-quake/bin"; | ||
|
||
Task("Default").Does(() => | ||
{ | ||
NuGetRestore(sln); | ||
Task("Clean") | ||
.Does(() => | ||
{ | ||
CleanDirectory(output); | ||
}); | ||
|
||
Task("Build") | ||
.IsDependentOn("Clean") | ||
.Does(() => | ||
{ | ||
MSBuild(sln, new MSBuildSettings | ||
{ | ||
Configuration = "Release", | ||
Restore = true, | ||
ToolPath = GetFiles(VSWhereLatest() + "/**/MSBuild.exe").FirstOrDefault() | ||
}); | ||
}); | ||
|
||
MSBuild(sln, new MSBuildSettings | ||
Task("Artifact.Regular") | ||
.IsDependentOn("Build") | ||
.Does(() => | ||
{ | ||
Configuration = "Release", | ||
Restore = true, | ||
ToolPath = GetFiles(VSWhereLatest() + "/**/MSBuild.exe").FirstOrDefault() | ||
var art = output + "/Artifact.Regular"; | ||
CopyDirectory(bin, art); | ||
DeleteFiles(art + "/*.config"); | ||
DeleteFiles(art + "/*.pdb"); | ||
}); | ||
}); | ||
|
||
RunTarget("Default"); | ||
Task("Artifact.SingleExe") | ||
.IsDependentOn("Build") | ||
.Does(() => | ||
{ | ||
var deps = GetFiles(bin + "/*.dll"); | ||
var art = output + "/Artifact.SingleExe"; | ||
System.IO.Directory.CreateDirectory(art); | ||
ILRepack( | ||
art + "/windows-terminal-quake.exe", // Output file | ||
bin + "/windows-terminal-quake.exe", // Primary assembly | ||
deps, // Assembly paths | ||
new ILRepackSettings() | ||
); | ||
CopyFile(bin + "/windows-terminal-quake.json", art + "/windows-terminal-quake.json"); | ||
DeleteFile(art + "/windows-terminal-quake.exe.config"); | ||
}); | ||
|
||
Task("Artifact.SingleExe.Zip") | ||
.IsDependentOn("Artifact.SingleExe") | ||
.Does(() => | ||
{ | ||
var art = output + "/Artifact.SingleExe.Zip"; | ||
System.IO.Directory.CreateDirectory(art); | ||
Zip(output + "/Artifact.SingleExe", art + $"/windows-terminal-quake-{version}-{DateTimeOffset.UtcNow:yyyy-MM-dd_HHmm}.zip"); | ||
}); | ||
|
||
Task("Default") | ||
.IsDependentOn("Artifact.Regular") | ||
.IsDependentOn("Artifact.SingleExe") | ||
.IsDependentOn("Artifact.SingleExe.Zip") | ||
.Does(() => {}); | ||
|
||
RunTarget("Default"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Serilog; | ||
using System; | ||
using System.IO; | ||
|
||
namespace WindowsTerminalQuake | ||
{ | ||
public static class Logging | ||
{ | ||
public static void Configure() | ||
{ | ||
var here = Path.GetDirectoryName(new Uri(typeof(Logging).Assembly.Location).AbsolutePath); | ||
|
||
Log.Logger = new LoggerConfiguration() | ||
.MinimumLevel.Is(Serilog.Events.LogEventLevel.Information) | ||
.WriteTo.File( | ||
path: Path.Combine(here, "logs/.txt"), | ||
fileSizeLimitBytes: 10_000_000, | ||
rollingInterval: RollingInterval.Day, | ||
retainedFileCountLimit: 3 | ||
) | ||
.CreateLogger() | ||
; | ||
|
||
Log.Information("Windows Terminal Quake started"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using Newtonsoft.Json; | ||
using Polly; | ||
using Polly.Retry; | ||
using Serilog; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Windows.Forms; | ||
using WindowsTerminalQuake.Native; | ||
using WindowsTerminalQuake.UI; | ||
|
||
namespace WindowsTerminalQuake | ||
{ | ||
public class Settings | ||
{ | ||
public static readonly string[] PathsToSettings = new[] | ||
{ | ||
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "windows-terminal-quake.json"), | ||
Path.Combine(Path.GetDirectoryName(new Uri(typeof(Settings).Assembly.Location).AbsolutePath), "windows-terminal-quake.json"), | ||
}; | ||
|
||
public static SettingsDto Instance { get; private set; } = new SettingsDto() // Defaults | ||
{ | ||
Hotkeys = new List<Hotkey>() | ||
{ | ||
new Hotkey() { Modifiers = KeyModifiers.Control, Key = Keys.Oemtilde }, | ||
new Hotkey() { Modifiers = KeyModifiers.Control, Key = Keys.Q } | ||
}, | ||
Notifications = true, | ||
ToggleDurationMs = 250, | ||
VerticalScreenCoverage = 100 | ||
}; | ||
|
||
private static readonly List<Action<SettingsDto>> _listeners = new List<Action<SettingsDto>>(); | ||
|
||
public static void Get(Action<SettingsDto> action) | ||
{ | ||
_listeners.Add(action); | ||
|
||
action(Instance); | ||
} | ||
|
||
#region Loading & Reloading | ||
|
||
private static readonly RetryPolicy Retry = Policy | ||
.Handle<Exception>() | ||
.WaitAndRetry(new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1) }) | ||
; | ||
|
||
private static readonly List<FileSystemWatcher> _fsWatchers; | ||
|
||
static Settings() | ||
{ | ||
_fsWatchers = PathsToSettings | ||
.Select(path => | ||
{ | ||
Log.Information($"Watching settings file '{path}' for changes"); | ||
var fsWatcher = new FileSystemWatcher(Path.GetDirectoryName(path), Path.GetFileName(path)); | ||
fsWatcher.Changed += (s, a) => | ||
{ | ||
Log.Information($"Settings file '{a.FullPath}' changed"); | ||
Reload(true); | ||
}; | ||
fsWatcher.EnableRaisingEvents = true; | ||
return fsWatcher; | ||
}) | ||
.ToList() | ||
; | ||
|
||
Application.ApplicationExit += (s, a) => _fsWatchers.ForEach(w => w.Dispose()); | ||
|
||
Reload(false); | ||
} | ||
|
||
public static void Reload(bool notify) | ||
{ | ||
Retry.Execute(() => | ||
{ | ||
Log.Information("Reloading settings"); | ||
foreach (var pathToSettings in PathsToSettings) | ||
{ | ||
if (!File.Exists(pathToSettings)) | ||
{ | ||
Log.Warning($"Settings file at '{pathToSettings}' does not exist"); | ||
continue; | ||
} | ||
Log.Information($"Found settings file at '{pathToSettings}'"); | ||
try | ||
{ | ||
Instance = JsonConvert.DeserializeObject<SettingsDto>(File.ReadAllText(pathToSettings)); | ||
Log.Information($"Loaded settings from '{pathToSettings}'"); | ||
if (notify) TrayIcon.Instance.Notify(ToolTipIcon.Info, $"Loaded settings from '{pathToSettings}'"); | ||
break; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Error($"Could not load settings from file '{pathToSettings}': {ex.Message}", ex); | ||
} | ||
} | ||
_listeners.ForEach(l => l(Instance)); | ||
}); | ||
} | ||
|
||
#endregion Loading & Reloading | ||
} | ||
|
||
public class SettingsDto | ||
{ | ||
public List<Hotkey> Hotkeys { get; set; } | ||
|
||
public bool Notifications { get; set; } | ||
|
||
public int VerticalScreenCoverage { get; set; } | ||
|
||
public int ToggleDurationMs { get; set; } | ||
} | ||
|
||
public class Hotkey | ||
{ | ||
public KeyModifiers Modifiers { get; set; } | ||
|
||
public Keys Key { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.