From d87adfc7f02abebbaee0a6da32ba09769e08d407 Mon Sep 17 00:00:00 2001 From: Holger Boerchers Date: Sat, 26 May 2018 23:18:39 +0200 Subject: [PATCH] Refactored ChangelogHelper --- Creator/Program.cs | 10 +++--- Katteker/ChangelogHelper.cs | 65 +++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/Creator/Program.cs b/Creator/Program.cs index 1596123..ab192ea 100644 --- a/Creator/Program.cs +++ b/Creator/Program.cs @@ -10,7 +10,7 @@ namespace KattekerCreator { public class Program { - private const string _executable = @"C:\Program Files (x86)\NSIS\makensis.exe"; + private const string Executable = @"C:\Program Files (x86)\NSIS\makensis.exe"; private readonly string _baseDirectory = AppDomain.CurrentDomain.BaseDirectory; private ApplicationArguments _appArguments; private AssemblyFileInfo _assemblyFileInfo; @@ -118,13 +118,13 @@ namespace KattekerCreator private static string CompileSetupScript(string templateFile) { - if (!File.Exists(_executable)) throw new FileNotFoundException("NSIS not installed properly."); + if (!File.Exists(Executable)) throw new FileNotFoundException("NSIS not installed properly."); int exitcode; using (var p = new Process()) { p.StartInfo = new ProcessStartInfo { - FileName = _executable, + FileName = Executable, Arguments = $"\"{templateFile}\"", UseShellExecute = false }; @@ -132,10 +132,10 @@ namespace KattekerCreator p.Start(); p.WaitForExit(); exitcode = p.ExitCode; - Log.WriteInfoLine($"{Path.GetFileName(_executable)} has exited with Exitcode: {exitcode} (Took: {sw.ElapsedMilliseconds}ms)"); + Log.WriteInfoLine($"{Path.GetFileName(Executable)} has exited with Exitcode: {exitcode} (Took: {sw.ElapsedMilliseconds}ms)"); } - if (exitcode != 0) throw new Exception($"{Path.GetFileName(_executable)} has exited with Exitcode: {exitcode}"); + if (exitcode != 0) throw new Exception($"{Path.GetFileName(Executable)} has exited with Exitcode: {exitcode}"); return Path.ChangeExtension(templateFile, "exe"); } diff --git a/Katteker/ChangelogHelper.cs b/Katteker/ChangelogHelper.cs index 22c1207..2485a63 100644 --- a/Katteker/ChangelogHelper.cs +++ b/Katteker/ChangelogHelper.cs @@ -48,34 +48,49 @@ namespace Katteker { if (!path.EndsWith("/", StringComparison.Ordinal)) path += "/"; - var webReq = WebRequest.Create(path + filename); - try - { - using (var response = await webReq.GetResponseAsync().ConfigureAwait(false)) - using (var sr = new StreamReader(response.GetResponseStream())) - { - return await sr.ReadToEndAsync().ConfigureAwait(false); - } - } - catch (Exception) - { - try - { - var changelogFilename = Path.GetFileName(filename); - if (changelogFilename == null) return null; - var currentChangelogPath = Path.Combine(Environment.CurrentDirectory, changelogFilename); - if (File.Exists(currentChangelogPath)) - { - return File.ReadAllText(currentChangelogPath); - } + var changelog = await ReadFromRemoteFile(path + filename) ?? ReadFromLocalFile(filename); + return changelog; + } - } - catch (Exception) - { - // ignore; - } + return null; + } + + private static async Task ReadFromRemoteFile(string path) + { + var webReq = WebRequest.Create(path); + try + { + using (var response = await webReq.GetResponseAsync().ConfigureAwait(false)) + using (var sr = new StreamReader(response.GetResponseStream())) + { + return await sr.ReadToEndAsync().ConfigureAwait(false); } } + catch (Exception) + { + // ignore; + } + + return null; + } + + private static string ReadFromLocalFile(string filename) + { + try + { + var changelogFilename = Path.GetFileName(filename); + if (changelogFilename == null) return null; + + var currentChangelogPath = Path.Combine(Environment.CurrentDirectory, changelogFilename); + if (File.Exists(currentChangelogPath)) + { + return File.ReadAllText(currentChangelogPath); + } + } + catch (Exception) + { + // ignore; + } return null; }