Add cool command-line-parser, switch to netcoreapp

This commit is contained in:
CreepyCrafter24 2020-03-08 12:51:37 +01:00
parent 0aef0daf07
commit 34a7b806e9
2 changed files with 68 additions and 97 deletions

View File

@ -3,70 +3,75 @@ using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.CommandLine;
using System.CommandLine.Invocation;
namespace UpTool_build_tool
{
internal static class Program
{
public static void Main(string[] args)
public static int Main(string[] args)
{
Console.WriteLine("-------------------------------");
Console.WriteLine("| UpTool2 package build tools |");
Console.WriteLine("-------------------------------");
Console.WriteLine();
Console.WriteLine($"Using version {Assembly.GetExecutingAssembly().GetName().Version}");
Console.WriteLine();
if (args == null || args.Length == 0)
args = new[] { "help" };
args[0] = args[0].TrimStart('-', '/');
switch (args[0])
RootCommand rootCommand = new RootCommand();
Command build = new Command("build", "Builds a generic package with or without shortcuts from a directory");
build.AddOption(new Option<string>("--binDir", "Directory to package"));
build.AddOption(new Option<string>("--mainBin", "The applications main binary"));
build.AddOption(new Option<string>("--packageFile", "Directory to package"));
build.AddOption(new Option<string>("--tempPath", Path.GetTempPath, "Directory to package"));
build.AddOption(new Option<bool>("--noShortcuts", "When this is enabled the scripts will not generate a start-menu item"));
build.AddOption(new Option<bool>("--noLogo", "Disables the logo"));
build.Handler = CommandHandler.Create<string, string, string, string, bool, bool>(Build);
rootCommand.AddCommand(build);
return rootCommand.InvokeAsync(args).Result;
}
private static void Build(string binDir, string mainBin, string packageFile, string tempPath, bool noLogo, bool noShortcuts)
{
if (!noLogo)
{
case "build":
Console.WriteLine("Parsing arguments...");
string targetDir = args[1];
string targetFileName = args[2];
string packageFile = args.Length > 3 ? args[3] : Path.Combine(targetDir, "package.zip");
string tempPath = Path.Combine(args.Length > 4 ? args[4] : Path.GetTempPath(), "UpTool2Pkg");
Console.WriteLine("Removing previous files...");
if (File.Exists(packageFile))
File.Delete(packageFile);
if (Directory.Exists(tempPath))
Directory.Delete(tempPath, true);
Directory.CreateDirectory(tempPath);
Console.WriteLine("Copying binary dir...");
ZipFile.CreateFromDirectory(targetDir, Path.Combine(tempPath, "dataDir.zip"));
Directory.CreateDirectory(Path.Combine(tempPath, "Data"));
ZipFile.ExtractToDirectory(Path.Combine(tempPath, "dataDir.zip"), Path.Combine(tempPath, "Data"));
File.Delete(Path.Combine(tempPath, "dataDir.zip"));
Console.WriteLine("Cleaning up .xml and .pdb files...");
Directory.GetFiles(Path.Combine(tempPath, "Data"))
.Where(s => new[] { ".xml", ".pdb" }.Contains(Path.GetExtension(s)))
.ToList().ForEach(File.Delete);
Console.WriteLine("Creating batch scripts...");
string programName = Path.GetFileNameWithoutExtension(targetFileName);
File.WriteAllText(Path.Combine(tempPath, "Install.bat"),
$"@echo off\r\necho INSTALL\r\npowershell \"$s=(New-Object -COM WScript.Shell).CreateShortcut('%appdata%\\Microsoft\\Windows\\Start Menu\\Programs\\{programName}.lnk');$s.TargetPath='%cd%\\{programName}.exe';$s.Save()\"\r\ntimeout /t 1");
File.WriteAllText(Path.Combine(tempPath, "Remove.bat"),
$"@echo off\r\necho REMOVE\r\ndel \"%appdata%\\Microsoft\\Windows\\Start Menu\\Programs\\{programName}.lnk\"\r\ntaskkill /f /im \"{programName}.exe\"\r\ntimeout /t 1");
Console.WriteLine("Packaging...");
ZipFile.CreateFromDirectory(tempPath, packageFile);
Console.WriteLine("Cleaning up temp path...");
Directory.Delete(tempPath, true);
break;
default:
Console.WriteLine("Usage:");
Console.WriteLine(" pkgtool.exe <command> [arguments...]");
Console.WriteLine();
Console.WriteLine("Commands:");
Console.WriteLine("- help");
Console.WriteLine(" Prints this message");
Console.WriteLine("- build");
Console.WriteLine(" Builds a generic package with shortcuts from a directory");
Console.WriteLine(" Arguments:");
Console.WriteLine(" pkgtool.exe build <binary dir> <main binary> [package file] [temp path]");
Console.WriteLine();
break;
Console.WriteLine("-------------------------------");
Console.WriteLine("| UpTool2 package build tools |");
Console.WriteLine("-------------------------------");
Console.WriteLine();
Console.WriteLine($"Using version {Assembly.GetExecutingAssembly().GetName().Version}");
Console.WriteLine();
}
Console.WriteLine("Parsing arguments...");
packageFile ??= Path.Combine(binDir, "package.zip");
tempPath = Path.Combine(tempPath, "UpTool2Pkg");
Console.WriteLine("Removing previous files...");
if (File.Exists(packageFile))
File.Delete(packageFile);
if (Directory.Exists(tempPath))
Directory.Delete(tempPath, true);
Directory.CreateDirectory(tempPath);
Console.WriteLine("Copying binary dir...");
ZipFile.CreateFromDirectory(binDir, Path.Combine(tempPath, "dataDir.zip"));
Directory.CreateDirectory(Path.Combine(tempPath, "Data"));
ZipFile.ExtractToDirectory(Path.Combine(tempPath, "dataDir.zip"), Path.Combine(tempPath, "Data"));
File.Delete(Path.Combine(tempPath, "dataDir.zip"));
Console.WriteLine("Cleaning up .xml and .pdb files...");
Directory.GetFiles(Path.Combine(tempPath, "Data"))
.Where(s => new[] { ".xml", ".pdb" }.Contains(Path.GetExtension(s)))
.ToList().ForEach(File.Delete);
Console.WriteLine("Creating batch scripts...");
string programName = Path.GetFileNameWithoutExtension(mainBin);
if (noShortcuts)
{
File.WriteAllText(Path.Combine(tempPath, "Install.bat"), "@echo off\r\necho INSTALL\r\ntimeout /t 1");
File.WriteAllText(Path.Combine(tempPath, "Remove.bat"), "@echo off\r\necho REMOVE\r\ntimeout /t 1");
}
else
{
File.WriteAllText(Path.Combine(tempPath, "Install.bat"),
$"@echo off\r\necho INSTALL\r\npowershell \"$s=(New-Object -COM WScript.Shell).CreateShortcut('%appdata%\\Microsoft\\Windows\\Start Menu\\Programs\\{programName}.lnk');$s.TargetPath='%cd%\\{programName}.exe';$s.Save()\"\r\ntimeout /t 1");
File.WriteAllText(Path.Combine(tempPath, "Remove.bat"),
$"@echo off\r\necho REMOVE\r\ndel \"%appdata%\\Microsoft\\Windows\\Start Menu\\Programs\\{programName}.lnk\"\r\ntaskkill /f /im \"{programName}.exe\"\r\ntimeout /t 1");
}
Console.WriteLine("Packaging...");
ZipFile.CreateFromDirectory(tempPath, packageFile);
Console.WriteLine("Cleaning up temp path...");
Directory.Delete(tempPath, true);
}
}
}

View File

@ -1,52 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AAB8D6BA-3A43-4DC4-95EE-6757482B77FD}</ProjectGuid>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>UpTool_build_tool</RootNamespace>
<AssemblyName>pkgtool</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>false</Deterministic>
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>"$(TargetPath)" build "$(TargetDir)\" "$(TargetPath)"</PostBuildEvent>
<PostBuildEvent>"$(TargetPath)" build --noLogo --noShortcuts --binDir "$(TargetDir)\""</PostBuildEvent>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20104.2" />
</ItemGroup>
</Project>