Skip to content

Commit

Permalink
exception messages and deleting temp dir on cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-fang authored and alex-fang committed Dec 21, 2023
1 parent bf4f31e commit 9476e52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/D2L.Bmx/GithubRelease.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal record GithubRelease {
public List<GithubAsset>? Assets { get; init; }

public Version? GetReleaseVersion() {
string? version = TagName?.TrimStart( 'v' ) ?? null;
string? version = TagName?.TrimStart( 'v' );
if( version is null ) {
return null;
}
Expand Down
30 changes: 15 additions & 15 deletions src/D2L.Bmx/UpdateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@ namespace D2L.Bmx;
internal class UpdateHandler {

public async Task HandleAsync() {
if( !Directory.Exists( BmxPaths.OLD_BMX_VERSIONS_PATH ) ) {
try {
Directory.CreateDirectory( BmxPaths.OLD_BMX_VERSIONS_PATH );
} catch( Exception ex ) {
throw new BmxException( "Failed to initialize temporary BMX file directory (~/.bmx/temp)", ex );
}
}

using var httpClient = new HttpClient();
GithubRelease? releaseData = await GithubRelease.GetLatestReleaseDataAsync();
Version? latestVersion = releaseData?.GetReleaseVersion();
if( latestVersion is null ) {
throw new BmxException( "Failed to grab the latest version" );
throw new BmxException( "Failed to find the latest version of BMX." );
}

var localVersion = Assembly.GetExecutingAssembly().GetName().Version;
if( latestVersion <= localVersion ) {
Console.WriteLine( $"Already own the latest version {latestVersion}" );
Console.WriteLine( $"You already have the latest version {latestVersion}" );
return;
}

string archiveName = GetOSFileName();
string? downloadUrl = releaseData?.Assets?.FirstOrDefault( a => a.Name == archiveName )?.BrowserDownloadUrl;
if( string.IsNullOrWhiteSpace( downloadUrl ) ) {
throw new BmxException( "Failed to get update download url" );
throw new BmxException( "Failed to find the download URL of the latest BMX" );
}

string? currentFilePath = Environment.ProcessPath;
Expand All @@ -38,7 +46,7 @@ public async Task HandleAsync() {
try {
File.Move( currentFilePath, backupPath );
} catch( IOException ex ) {
throw new BmxException( "Could move the old version, try with elevated permissions", ex );
throw new BmxException( "Could not remove the old version. Please try again with elevated permissions.", ex );
} catch {
throw new BmxException( "BMX could not update" );
}
Expand Down Expand Up @@ -87,19 +95,11 @@ private static string GetOSFileName() {
}

public static void Cleanup() {
if( !Directory.Exists( BmxPaths.OLD_BMX_VERSIONS_PATH ) ) {
try {
Directory.CreateDirectory( BmxPaths.OLD_BMX_VERSIONS_PATH );
} catch( Exception ex ) {
throw new BmxException( "Failed to initialize temporary BMX file directory (~/.bmx/temp)", ex );
}
}
string processDirectory = BmxPaths.OLD_BMX_VERSIONS_PATH;
foreach( string file in Directory.GetFiles( processDirectory, "*old.bak" ) ) {
if( Directory.Exists( BmxPaths.OLD_BMX_VERSIONS_PATH ) ) {
try {
File.Delete( file );
Directory.Delete( BmxPaths.OLD_BMX_VERSIONS_PATH,true );
} catch( Exception ) {
Console.Error.WriteLine( $"WARNING: Failed to delete old version {file}" );
Console.Error.WriteLine( "WARNING: Failed to delete old version files" );
}
}
}
Expand Down

0 comments on commit 9476e52

Please sign in to comment.