Added another command line parser. Has a nice parameter verification framework

This commit is contained in:
Holger Boerchers
2018-08-16 21:36:25 +02:00
parent a08d551403
commit bfb6971564
11 changed files with 118 additions and 283 deletions

View File

@@ -5,66 +5,41 @@ using System.IO;
using System.Linq;
using Katteker;
using KattekerCreator.Helper;
using McMaster.Extensions.CommandLineUtils;
namespace KattekerCreator
{
public class Program
// ReSharper disable once ClassNeverInstantiated.Global
public partial class Program
{
//private const string MakeNsis = @"C:\Program Files (x86)\NSIS\makensis.exe";
private const string MakeNsis = @"C:\Program Files (x86)\NSIS\makensis.exe";
private readonly string _baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
private string MakeNsis => Path.Combine(_baseDirectory, "NSIS", "makensis.exe");
private ApplicationArguments _appArguments;
//private string MakeNsis => Path.Combine(_baseDirectory, "NSIS", "makensis.exe");
private AssemblyFileInfo _assemblyFileInfo;
private TemporaryDirectory _tempDir;
private Releases _releases;
private static int Main(string[] args)
private static int Main(string[] args) => CommandLineApplication.Execute<Program>(args);
// ReSharper disable once UnusedMember.Local
private int OnExecute(CommandLineApplication app)
{
var program = new Program();
try
{
return program.Run(args);
}
catch (Exception ex)
{
Log.WriteErrorLine(ex.Message);
if (ex.InnerException != null)
Log.WriteErrorLine(ex.InnerException.Message);
#if DEBUG
throw;
#else
return -1;
#endif
}
}
private int Run(string[] args)
{
//Parse App-Arguments
var parser = new SimpleCommandLineParser<ApplicationArguments>(args);
if (args.Length == 0)
{
parser.ShowHelp();
return 1924;
}
_appArguments = parser.GetObject();
//Create tempdir
using (_tempDir = Utils.CreateTempDirectory())
{
_releases = new Releases(_appArguments.OutputDir);
parser.ShowProgramArguments();
_releases = new Releases(OutputDir);
// TODO parser.ShowProgramArguments();
//Modify AppStub
var appStubFile = ModifyAppStub();
//Acquire infos from Executable.
_assemblyFileInfo = new AssemblyFileInfo(_appArguments, _tempDir.Path);
_assemblyFileInfo = new AssemblyFileInfo(ProgramFile, Version, _tempDir.Path);
var configFile = CreateConfigFile();
//Generate NSIS-Script
var additionalFiles = new[]
{
new PhysicalFile(appStubFile, Path.GetFileName(_appArguments.ProgramFile)),
new PhysicalFile(appStubFile, Path.GetFileName(ProgramFile)),
new PhysicalFile(configFile, Path.Combine($"app-{_assemblyFileInfo.AssemblyVersion}", Path.GetFileName(configFile) ?? string.Empty))
};
var templateFile = GenerateNsisTemplate(additionalFiles);
@@ -83,7 +58,7 @@ namespace KattekerCreator
private void CopyAsSetup(string setupFilePath, ReleaseEntry releaseEntry)
{
if (!_releases.IsLatestEntry(releaseEntry)) return;
var target = Path.Combine(_appArguments.OutputDir, Constants.SetupFile);
var target = Path.Combine(OutputDir, Constants.SetupFile);
File.Delete(target);
File.Copy(setupFilePath, target);
}
@@ -109,18 +84,18 @@ namespace KattekerCreator
var setupFile = Path.GetFileName(setupFilePath);
if (string.IsNullOrEmpty(setupFile)) throw new ArgumentException();
if (!File.Exists(setupFilePath)) throw new FileNotFoundException(setupFile);
if (!Directory.Exists(_appArguments.OutputDir)) Directory.CreateDirectory(_appArguments.OutputDir);
if (!string.IsNullOrEmpty(_appArguments.ChangeLog))
if (!Directory.Exists(OutputDir)) Directory.CreateDirectory(OutputDir);
if (!string.IsNullOrEmpty(ChangeLog))
{
var changeLogFilename = Path.GetFileName(_appArguments.ChangeLog);
var changeLogFilename = Path.GetFileName(ChangeLog);
if (!File.Exists(_appArguments.ChangeLog)) throw new FileNotFoundException(_appArguments.ChangeLog);
File.Copy(_appArguments.ChangeLog, Path.Combine(_appArguments.OutputDir, changeLogFilename), true);
if (!File.Exists(ChangeLog)) throw new FileNotFoundException(ChangeLog);
File.Copy(ChangeLog, Path.Combine(OutputDir, changeLogFilename), true);
}
File.Copy(setupFilePath, Path.Combine(_appArguments.OutputDir, setupFile), true);
File.Copy(setupFilePath, Path.Combine(OutputDir, setupFile), true);
}
private string CompileSetupScript(string templateFile)
private static string CompileSetupScript(string templateFile)
{
if (!File.Exists(MakeNsis)) throw new FileNotFoundException("NSIS not installed properly.");
int exitcode;
@@ -148,9 +123,9 @@ namespace KattekerCreator
var pathToFile = Path.Combine(_tempDir.Path, Constants.KattekerConfig);
var kattekerConfig = new KattekerConfig
{
Publish = _appArguments.PublishDir ?? _appArguments.OutputDir,
Changelog = Path.GetFileName(_appArguments.ChangeLog),
Channel = _appArguments.Channel
Publish = PublishDir ?? OutputDir,
Changelog = Path.GetFileName(ChangeLog),
Channel = Channel
};
kattekerConfig.WriteToFile(pathToFile);
return pathToFile;
@@ -160,7 +135,7 @@ namespace KattekerCreator
{
var baseFile = new FileInfo(Path.Combine(_baseDirectory, Constants.ExecutionStub));
var targetFile = baseFile.CopyTo(Path.Combine(_tempDir.Path, Constants.ExecutionStub));
Utils.CopyResources(_appArguments.ProgramFile, targetFile.FullName);
Utils.CopyResources(ProgramFile, targetFile.FullName);
return targetFile.FullName;
}
@@ -183,11 +158,11 @@ namespace KattekerCreator
UserInterface = Path.Combine(_baseDirectory, "contrib", "LoadingBar_Icon.exe"),
UninstallIcon = Path.Combine(_baseDirectory, "contrib", "uninstall.ico"),
OutFile = Path.GetFileName(Path.ChangeExtension(outFile, "exe")),
ReleaseChannel = _appArguments.Channel
ReleaseChannel = Channel
};
var path = Path.GetDirectoryName(_appArguments.ProgramFile) ?? string.Empty;
var path = Path.GetDirectoryName(ProgramFile) ?? string.Empty;
setupTmpl.Files.AddRange(additionalFiles);
var files = Utils.EnumerateFiles(path, _appArguments.Filter).ToArray();
var files = Utils.EnumerateFiles(path, Filter).ToArray();
setupTmpl.InstallSize = (long) Math.Floor(files.Sum(x => x.Length) / 1024f);
setupTmpl.Files.AddRange(files.Select(x => new PhysicalFile(x.FullName, x.FullName.Replace(path, $"app-{_assemblyFileInfo.AssemblyVersion}"))));
var transformText = setupTmpl.TransformText();