Skip to content

Commit

Permalink
Fix auto-update download
Browse files Browse the repository at this point in the history
  • Loading branch information
ReMinoer committed May 26, 2023
1 parent b34d522 commit d526014
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 35 deletions.
4 changes: 1 addition & 3 deletions Calame.AutoUpdate/GitHubAutoUpdateApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ static public async Task<byte[]> DownloadAsset(GitHubClient client, Release rele
return null;

IApiResponse<object> response = await client.Connection.Get<object>(new Uri(assetUrl), new Dictionary<string, string>(), "application/octet-stream");
byte[] bytes = Encoding.ASCII.GetBytes(response.HttpResponse.Body.ToString());

return bytes;
return response.HttpResponse.Body as byte[];
}
}
}
28 changes: 10 additions & 18 deletions Calame.AutoUpdate/GitHubAutoUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public class GitHubAutoUpdater
{
private const string MessageCaption = "Auto-Update";

static public async Task<string> CheckUpdatesAndAskUserToDownload(IAutoUpdateConfiguration configuration, ILogger logger)
static public async Task<string> CheckUpdatesAndAskUserToDownload(IAutoUpdateConfiguration configuration, ILogger logger, bool silentIfUpToDate = false)
{
GitHubClient gitHubClient = await GetGitHubClientAsync(configuration, logger);
if (gitHubClient is null)
return null;

Release latestRelease = await CheckUpdatesAsync(gitHubClient, configuration, logger);
Release latestRelease = await CheckUpdatesAsync(gitHubClient, configuration, logger, silentIfUpToDate);
if (latestRelease is null)
return null;

Expand Down Expand Up @@ -105,7 +105,7 @@ static private async Task<GitHubClient> GetGitHubClientAsync(IAutoUpdateConfigur
return gitHubClient;
}

static private async Task<Release> CheckUpdatesAsync(GitHubClient gitHubClient, IAutoUpdateConfiguration configuration, ILogger logger)
static private async Task<Release> CheckUpdatesAsync(GitHubClient gitHubClient, IAutoUpdateConfiguration configuration, ILogger logger, bool silentIfUpToDate = false)
{
string repositoryOwner = configuration.RepositoryOwner;
string repositoryName = configuration.RepositoryName;
Expand All @@ -127,14 +127,17 @@ static private async Task<Release> CheckUpdatesAsync(GitHubClient gitHubClient,

if (CalameUtils.IsDevelopmentBuild())
{
message = $"Latest version is: {latestRelease.TagName}.\n\n(This development build will not be updated.)";
message = $"Latest version is: {latestRelease.TagName}\n\n(This development build will not be updated.)";

MessageBox.Show(message, MessageCaption, MessageBoxButton.OK, MessageBoxImage.Information);
return null;
}

if (latestRelease.TagName == CalameUtils.GetVersion())
{
if (silentIfUpToDate)
return null;

message = $"You are using the latest version. ({latestRelease.TagName})";

MessageBox.Show(message, MessageCaption, MessageBoxButton.OK, MessageBoxImage.Information);
Expand All @@ -157,29 +160,18 @@ static private async Task<Release> CheckUpdatesAsync(GitHubClient gitHubClient,

static private async Task<string> DownloadInstallerAsset(GitHubClient gitHubClient, IAutoUpdateConfiguration configuration, Release release)
{
string downloadFolderPath = GetRandomFolderPath(Path.GetTempPath());
string installerAssetName = configuration.InstallerAssetName;
byte[] assetBytes = await GitHubAutoUpdateApi.DownloadAsset(gitHubClient, release, installerAssetName);

string assetFilePath = Path.Combine(downloadFolderPath, installerAssetName);
TempFolder.CreateIfMissing();

string assetFilePath = Path.Combine(TempFolder.Path, installerAssetName);
using (FileStream assetFileStream = File.Create(assetFilePath))
{
await assetFileStream.WriteAsync(assetBytes);
}

return assetFilePath;
}

static private string GetRandomFolderPath(string parentFolderPath)
{
string folderPath;
do
{
folderPath = Path.Combine(parentFolderPath, Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
}
while (Directory.Exists(folderPath));

return folderPath;
}
}
}
7 changes: 1 addition & 6 deletions Calame.Icons/Providers/Base/MahAppsIconProviderModuleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ namespace Calame.Icons.Providers.Base
public abstract class MahAppsIconProviderModuleBase<TKind, TControl> : IIconProviderModule<TKind>
where TControl : PackIconControlBase, new()
{
static MahAppsIconProviderModuleBase()
{
RuntimeIconsFolder.Clean();
}

public bool Handle(IconDescription iconDescription)
{
return iconDescription.Key is TKind;
Expand Down Expand Up @@ -47,7 +42,7 @@ public Bitmap GetBitmap(IconDescription iconDescription, int size)

public Uri GetUri(IconDescription iconDescription, int size)
{
RuntimeIconsFolder.Create();
RuntimeIconsFolder.CreateIfMissing();

string hash = iconDescription.GetHashCode().ToString("X");
string filePath = Path.Combine(RuntimeIconsFolder.Path, Path.ChangeExtension(hash, "png"));
Expand Down
9 changes: 7 additions & 2 deletions Calame.Icons/Providers/Base/RuntimeIconsFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ namespace Calame.Icons.Providers.Base
{
static public class RuntimeIconsFolder
{
static public readonly string Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "CalameMahAppsRuntimeIcons");
static public readonly string Path = System.IO.Path.Combine(TempFolder.Path, "CalameMahAppsRuntimeIcons");

static RuntimeIconsFolder()
{
Clean();
}

static public void Clean()
{
if (Directory.Exists(Path))
Directory.Delete(Path, recursive: true);
}

static public void Create()
static public void CreateIfMissing()
{
if (!Directory.Exists(Path))
Directory.CreateDirectory(Path);
Expand Down
6 changes: 4 additions & 2 deletions Calame/CalameUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ static public class CalameUtils
static public string GetVersion() => GetVersion(GetCurrentExecutablePath());
static public string GetVersion(string executablePath)
{
string executableVersion = FileVersionInfo.GetVersionInfo(executablePath).FileVersion;
return executableVersion != "1.0.0.0" ? executableVersion : null;
string fileVersion = FileVersionInfo.GetVersionInfo(executablePath).FileVersion;
string executableVersion = fileVersion.Substring(0, fileVersion.LastIndexOf('.'));

return executableVersion != "1.0.0" ? executableVersion : null;
}

static public string GetCurrentExecutablePath() => Assembly.GetEntryAssembly().Location;
Expand Down
26 changes: 26 additions & 0 deletions Calame/TempFolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.IO;

namespace Calame
{
static public class TempFolder
{
static public readonly string Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "CalameTemp");

static TempFolder()
{
Clean();
}

static public void Clean()
{
if (Directory.Exists(Path))
Directory.Delete(Path, recursive: true);
}

static public void CreateIfMissing()
{
if (!Directory.Exists(Path))
Directory.CreateDirectory(Path);
}
}
}
6 changes: 3 additions & 3 deletions Modules/Calame.UpdateChecker/Commands/CheckUpdatesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public CommandHandler(IShell shell, ILoggerProvider loggerProvider)
public override Task Run(Command command) => CheckUpdatesAndApply(AutoUpdateConfiguration, _shell, _logger);
}

static public async Task CheckUpdatesAndApply(IAutoUpdateConfiguration autoUpdateConfiguration, IShell shell, ILogger logger)
static public async Task CheckUpdatesAndApply(IAutoUpdateConfiguration autoUpdateConfiguration, IShell shell, ILogger logger, bool silentIfUpToDate = false)
{
string installerFilePath = await GitHubAutoUpdater.CheckUpdatesAndAskUserToDownload(autoUpdateConfiguration, logger);
string installerFilePath = await GitHubAutoUpdater.CheckUpdatesAndAskUserToDownload(autoUpdateConfiguration, logger, silentIfUpToDate);
if (installerFilePath is null)
return;

Process.Start(installerFilePath, Path.GetDirectoryName(CalameUtils.GetCurrentExecutablePath()));
Process.Start("msiexec", $"/i {installerFilePath}");
shell.Close();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/Calame.UpdateChecker/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override async Task PostInitializeAsync()
if (CalameUtils.IsDevelopmentBuild())
return;

await CheckUpdatesCommand.CheckUpdatesAndApply(AutoUpdateConfiguration, _shell, _loggerProvider.CreateLogger(nameof(UpdateChecker)));
await CheckUpdatesCommand.CheckUpdatesAndApply(AutoUpdateConfiguration, _shell, _loggerProvider.CreateLogger(nameof(UpdateChecker)), silentIfUpToDate: true);
}

[Export]
Expand Down

0 comments on commit d526014

Please sign in to comment.