From 722a4c6dfa2da2814e2e4f7eb3ab0edb2e3f7a1e Mon Sep 17 00:00:00 2001 From: Chenfeng Bao Date: Mon, 20 Nov 2023 14:03:00 -0500 Subject: [PATCH] nits and fixes --- .github/pull_request_template.md | 1 + src/D2L.Bmx/BmxPaths.cs | 11 +++++---- src/D2L.Bmx/Program.cs | 41 ++++++++++++++++++-------------- src/D2L.Bmx/UpdateChecker.cs | 5 +--- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index d97a1f29..2194a3ed 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1 +1,2 @@ ### Why + diff --git a/src/D2L.Bmx/BmxPaths.cs b/src/D2L.Bmx/BmxPaths.cs index 12f5045c..ba13e5f8 100644 --- a/src/D2L.Bmx/BmxPaths.cs +++ b/src/D2L.Bmx/BmxPaths.cs @@ -1,11 +1,14 @@ namespace D2L.Bmx; internal static class BmxPaths { - public static readonly string USER_HOME_DIR = Environment.GetFolderPath( Environment.SpecialFolder.UserProfile ); - public static readonly string BMX_DIR = Path.Join( USER_HOME_DIR, ".bmx" ); - public static readonly string CACHE_DIR = Path.Join( BMX_DIR, "\\cache" ); - public static readonly string SESSIONS_FILE_NAME = Path.Join( CACHE_DIR, "sessions" ); + private static readonly string BMX_DIR = Path.Join( + Environment.GetFolderPath( Environment.SpecialFolder.UserProfile ), + ".bmx" ); + + public static readonly string CACHE_DIR = Path.Join( BMX_DIR, "cache" ); public static readonly string CONFIG_FILE_NAME = Path.Join( BMX_DIR, "config" ); + public static readonly string SESSIONS_FILE_NAME = Path.Join( CACHE_DIR, "sessions" ); + public static readonly string SESSIONS_FILE_LEGACY_NAME = Path.Join( BMX_DIR, "sessions" ); public static readonly string UPDATE_CHECK_FILE_NAME = Path.Join( CACHE_DIR, "updateCheck" ); public static readonly string AWS_CREDS_CACHE_FILE_NAME = Path.Join( CACHE_DIR, "awsCreds" ); } diff --git a/src/D2L.Bmx/Program.cs b/src/D2L.Bmx/Program.cs index eb1f18e2..8c791d16 100644 --- a/src/D2L.Bmx/Program.cs +++ b/src/D2L.Bmx/Program.cs @@ -210,25 +210,30 @@ // start bmx return await new CommandLineBuilder( rootCommand ) .UseDefaults() - .AddMiddleware( async ( - context, - next - ) => { - await UpdateChecker.CheckForUpdatesAsync( configProvider.GetConfiguration() ); - //Creating new Cache directory and removing old files. - if( !Directory.Exists( BmxPaths.CACHE_DIR ) ) { - try { - Directory.CreateDirectory( BmxPaths.CACHE_DIR ); - File.Delete( Path.Join( BmxPaths.BMX_DIR, "awsCredsCache" ) ); - File.Delete( Path.Join( BmxPaths.BMX_DIR, "sessions" ) ); - File.Delete( Path.Join( BmxPaths.BMX_DIR, "update_check" ) ); - } catch( Exception ex ) { - Console.Error.WriteLine( "Error cleaning up old files continuing..." ); + .AddMiddleware( + middleware: async ( context, next ) => { + // initialize BMX directories + if( !Directory.Exists( BmxPaths.CACHE_DIR ) ) { + try { + // the cache directory is inside the config directory (~/.bmx), so this ensures both exist + Directory.CreateDirectory( BmxPaths.CACHE_DIR ); + // move the old sessions file (v3.0-) into the new cache directory (v3.1+) + if( File.Exists( BmxPaths.SESSIONS_FILE_LEGACY_NAME ) ) { + File.Move( BmxPaths.SESSIONS_FILE_LEGACY_NAME, BmxPaths.SESSIONS_FILE_NAME ); + } + } catch( Exception ex ) { + throw new BmxException( "Failed to initialize BMX directory (~/.bmx)", ex ); + } } - } - await next( context ); - }, - System.CommandLine.Invocation.MiddlewareOrder.ExceptionHandler + 1 + + await UpdateChecker.CheckForUpdatesAsync( configProvider.GetConfiguration() ); + + await next( context ); + }, + // The default order for new middleware is after the middleware for `--help` & `--version` and can get short-circuited. + // We want our middleware (especially update checks) to almost always run, even on `--help` & `--version`, + // so we specify a custom order just after the exception handler (which is way before `--help` & `--version`). + order: MiddlewareOrder.ExceptionHandler + 1 ) .UseExceptionHandler( ( exception, context ) => { Console.ResetColor(); diff --git a/src/D2L.Bmx/UpdateChecker.cs b/src/D2L.Bmx/UpdateChecker.cs index 39982c5e..54ce9ef6 100644 --- a/src/D2L.Bmx/UpdateChecker.cs +++ b/src/D2L.Bmx/UpdateChecker.cs @@ -4,8 +4,8 @@ using System.Text.Json.Serialization; namespace D2L.Bmx; -internal static class UpdateChecker { +internal static class UpdateChecker { public static async Task CheckForUpdatesAsync( BmxConfig config ) { try { var cachedVersion = GetUpdateCheckCache(); @@ -68,9 +68,6 @@ private static async Task GetLatestReleaseVersionAsync() { } private static void SaveLatestVersion( string version ) { - if( !Directory.Exists( BmxPaths.BMX_DIR ) ) { - Directory.CreateDirectory( BmxPaths.BMX_DIR ); - } if( string.IsNullOrWhiteSpace( version ) ) { return; }