Skip to content

Commit

Permalink
Merge pull request #36 from martial-god/Dev
Browse files Browse the repository at this point in the history
Removal of the -u --upgrade command line option. Need more testing.
  • Loading branch information
feahnthor authored Nov 3, 2023
2 parents 3572428 + 9307bbe commit 997a5e5
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 97 deletions.
3 changes: 0 additions & 3 deletions Benny-Scraper/CommandlineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,5 @@ public class CommandLineOptions

[Option('S', "search", Required = false, Hidden = true, HelpText = "Search for novel by Title, can seach by partial name [STRING].")]
public string SearchKeyword { get; set; }

[Option('u', "upgrade", Required = false, HelpText = "Upgrade the application to the latest version.")]
public bool Upgrade { get; set; }
}
}
93 changes: 0 additions & 93 deletions Benny-Scraper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ internal class Program
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
private static IContainer Container { get; set; }
private const string AreYouSure = "Are you sure you want to {0}? (y/n)";
private const int MaxTitleWidth = 50;
private const string ProgramVersion = "v1.1.1";
private const string ReleaseUrl = "https://api.github.com/repos/martial-god/Benny-Scraper/releases/latest";
private const string MainProject = "Benny-Scraper.csproj";

public static IConfiguration Configuration { get; set; }

Expand Down Expand Up @@ -185,10 +181,6 @@ private static async Task HandleOptionsAsync(CommandLineOptions options)
if (confirmation.ToLowerInvariant() == "y")
await ClearDatabaseAsync();
}
else if (options.Upgrade)
{
await CheckAndUpgrade();
}
else if (options.DeleteNovelById != Guid.Empty)
{
await DeleteNovelByIdAsync(options.DeleteNovelById);
Expand Down Expand Up @@ -667,91 +659,6 @@ public static async Task DisplayNovelInformationAsync(Guid novelId)
Console.WriteLine("-------------------");
}

private static async Task CheckAndUpgrade()
{
using HttpClient client = new HttpClient();
string url = ReleaseUrl;
client.DefaultRequestHeaders.Add("User-Agent", "Benny-Scraper");

try
{
var response = await client.GetStringAsync(url);
var jsonResponse = Newtonsoft.Json.Linq.JObject.Parse(response);

string latestVersion = jsonResponse["tag_name"].ToString();
string currentVersion = ProgramVersion;
Console.WriteLine($"Current version: {currentVersion}");

if (string.Compare(latestVersion, currentVersion, StringComparison.InvariantCultureIgnoreCase) > 0)
{
Console.WriteLine($"New version {latestVersion} available. Upgrading...");

string downloadUrl = jsonResponse["assets"][0]["browser_download_url"].ToString();
string installDir = AppDomain.CurrentDomain.BaseDirectory;

if (await DownloadAndUpgradeApp(downloadUrl, installDir))
Console.WriteLine($"Upgraded to version {latestVersion}");
else
Console.WriteLine("Upgrade failed.");
}
else
Console.WriteLine("You are on the latest version.");
}
catch (Exception ex)
{
Console.WriteLine($"Error checking for updates: {ex.Message}");
}
}

private static async Task<bool> DownloadAndUpgradeApp(string downloadUrl, string installDirectory)
{
string tempFile = Path.Combine(Path.GetTempPath(), "appUpdate.zip");

using (var client = new HttpClient())
{
var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseContentRead);

using (var fs = new FileStream(tempFile, FileMode.Create))
{
await response.Content.CopyToAsync(fs);
}
}

try
{
string tempDirectory = Path.Combine(Path.GetTempPath(), "appUpdate");
if (Directory.Exists(tempDirectory))
Directory.Delete(tempDirectory, true);
ZipFile.ExtractToDirectory(tempFile, tempDirectory);

// Stop services or processes if necessary

foreach (var file in Directory.GetFiles(tempDirectory))
{
string destFile = Path.Combine(installDirectory, Path.GetFileName(file));
if (File.Exists(destFile))
File.Delete(destFile);
File.Move(file, destFile);
}

foreach (var dir in Directory.GetDirectories(tempDirectory))
{
string destDir = Path.Combine(installDirectory, new DirectoryInfo(dir).Name);
Directory.Move(dir, destDir);
}

File.Delete(tempFile);
Directory.Delete(tempDirectory, true);

return true;
}
catch (Exception ex)
{
Console.WriteLine($"Error during upgrade: {ex.Message}");
return false;
}
}

private static async Task UpdateNovelFileType(Guid id)
{
try
Expand Down
1 change: 0 additions & 1 deletion Benny-Scraper/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
},
"Benny-Scraper": {
"commandName": "Project",
"commandLineArgs": "-u",
"nativeDebugging": true
}
}
Expand Down

0 comments on commit 997a5e5

Please sign in to comment.