Skip to content

Commit

Permalink
0.3.8
Browse files Browse the repository at this point in the history
- Fixed Docker automated builds
  - Issue: /docker/README.md still not being used in Docker Hub
- Fixed .csproj detection bug
  • Loading branch information
phil-harmoniq authored Sep 7, 2017
2 parents f0db9d2 + 21c1ecc commit a7d23f2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 81 deletions.
127 changes: 62 additions & 65 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@
using System.Xml;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Shell.NET;
using ConsoleColors;

class Program
{
static Assembly tool = Assembly.GetExecutingAssembly();
static string toolName = tool.GetName().Name;
static string toolVersion = FileVersionInfo.GetVersionInfo(tool.Location).ProductVersion;
static string home = Environment.GetEnvironmentVariable("HOME");
static string configDir = $"{home}/.netpkg-tool";
static int width = 64;
static Bash bash = new Bash();
static string csproj;
static string projectDir;
static string destination;
static string ToolName = tool.GetName().Name;
static string ToolVersion = FileVersionInfo.GetVersionInfo(tool.Location).ProductVersion;
static string Home = Environment.GetEnvironmentVariable("HOME");
static string configDir = $"{Home}/.netpkg-tool";
static int Width = 64;
static Shell.NET.Bash Bash = new Shell.NET.Bash();
static string Csproj;
static string ProjectDir;
static string Destination;
static string DllName;
static string AppName;
static string dotNetVersion;
static string DotNetVersion;
static string Here = AppDomain.CurrentDomain.BaseDirectory;
static string[] Args;

Expand All @@ -31,35 +29,34 @@ class Program
static bool CustomAppName = false;
static bool SelfContainedDeployment = false;
static bool KeepTempFiles = false;


static void Main(string[] args)
{
SayHello();
ParseArgs(args);
CheckPaths(args);
FindCsproj(args[0]);
SayTask(projectDir, $"{destination}/{AppName}");
SayTask(ProjectDir, $"{Destination}/{AppName}");
if (!SkipRestore) RestoreProject();
ComileProject();
TransferFiles();
RunAppImageTool();
if (!KeepTempFiles) DeleteTempFiles();
SayFinished($"New AppImage created at {destination}/{AppName}");
SayFinished($"New AppImage created at {Destination}/{AppName}");
SayBye();
}

static void CheckPaths(string[] args)
{
if (args.Length < 2 || !Directory.Exists(args[0]) && !Directory.Exists(args[1]))
ExitWithError("You must specify a valid .NET project AND destination folder.\n", 1);
ExitWithError("You must specify a valid .NET project AND Destination folder.", 6);
if (Directory.Exists(args[0]) && !Directory.Exists(args[1]))
ExitWithError($"{args[1]} is not a valid folder\n", 2);
ExitWithError($"{args[1]} is not a valid folder", 7);
if (!Directory.Exists(args[0]) && Directory.Exists(args[1]))
ExitWithError($"{args[0]} is not a valid folder\n", 3);
ExitWithError($"{args[0]} is not a valid folder", 8);

projectDir = GetRelativePath(args[0]);
destination = GetRelativePath(args[1]);
ProjectDir = GetRelativePath(args[0]);
Destination = GetRelativePath(args[1]);
}

static void ParseArgs(string[] args)
Expand Down Expand Up @@ -104,18 +101,18 @@ static void ParseArgs(string[] args)

static void FindCsproj(string project)
{
var location = bash.Command($"find {project} -maxdepth 1 -name '*.csproj'", redirect: true).Lines;
var location = Bash.Command($"find {project} -maxdepth 1 -name '*.csproj'", redirect: true).Lines;

if (location.Length < 1)
ExitWithError($"No .csproj found in {GetRelativePath(project)}\n", 10);
if (location == null || location[0] == "")
ExitWithError($"No .csproj found in {GetRelativePath(project)}", 10);
if (location.Length > 1)
ExitWithError($"More than one .csproj found in {GetRelativePath(project)}\n", 11);
ExitWithError($"More than one .csproj found in {GetRelativePath(project)}", 11);

var folderSplit = location[0].Split('/');
csproj = folderSplit[folderSplit.Length - 1];
projectDir = GetRelativePath(project);
dotNetVersion = GetCoreVersion();
var nameSplit = csproj.Split('.');
Csproj = folderSplit[folderSplit.Length - 1];
ProjectDir = GetRelativePath(project);
DotNetVersion = GetCoreVersion();
var nameSplit = Csproj.Split('.');
DllName = string.Join('.', nameSplit.Take(nameSplit.Length - 1));

if (!CustomAppName)
Expand All @@ -131,7 +128,7 @@ static void FindCsproj(string project)

static string GetCoreVersion()
{
var path = GetAbsolutePath($"{projectDir}/{csproj}");
var path = GetAbsolutePath($"{ProjectDir}/{Csproj}");
var node = "/Project/PropertyGroup/TargetFramework";
var xml = new XmlDocument();
xml.LoadXml(File.ReadAllText(path));
Expand All @@ -140,7 +137,7 @@ static string GetCoreVersion()

static bool SingleRuntimeIdentifier()
{
var path = GetAbsolutePath($"{projectDir}/{csproj}");
var path = GetAbsolutePath($"{ProjectDir}/{Csproj}");
var node = "/Project/PropertyGroup/RuntimeIdentifier";
var xml = new XmlDocument();
xml.LoadXml(File.ReadAllText(path));
Expand All @@ -152,12 +149,12 @@ static void RestoreProject()
if (Verbose)
{
Console.WriteLine("Restoring .NET Core project dependencies...");
bash.Command($"cd {projectDir} && dotnet restore", redirect: false);
Bash.Command($"cd {ProjectDir} && dotnet restore", redirect: false);
}
else
{
Console.Write("Restoring .NET Core project dependencies...");
bash.Command($"cd {projectDir} && dotnet restore", redirect: true);
Bash.Command($"cd {ProjectDir} && dotnet restore", redirect: true);
}

CheckCommandOutput(errorCode: 20);
Expand All @@ -168,19 +165,19 @@ static void ComileProject()
string cmd;

if (SelfContainedDeployment)
cmd = $"cd {projectDir} && dotnet publish -c Release -r linux-x64 --no-restore";
cmd = $"cd {ProjectDir} && dotnet publish -c Release -r linux-x64 --no-restore";
else
cmd = $"cd {projectDir} && dotnet publish -c Release --no-restore";
cmd = $"cd {ProjectDir} && dotnet publish -c Release --no-restore";

if (Verbose)
{
Console.WriteLine("Compiling .NET Core project...");
bash.Command(cmd, redirect: false);
Bash.Command(cmd, redirect: false);
}
else
{
Console.Write("Compiling .NET Core project...");
bash.Command(cmd, redirect: true);
Bash.Command(cmd, redirect: true);
}

CheckCommandOutput(errorCode: 21);
Expand All @@ -192,29 +189,29 @@ static void TransferFiles()
string cmd;

if (SelfContainedDeployment)
cmd = $"{path} {projectDir} {DllName} {AppName} {dotNetVersion} {toolVersion} true";
cmd = $"{path} {ProjectDir} {DllName} {AppName} {DotNetVersion} {ToolVersion} true";
else
cmd = $"{path} {projectDir} {DllName} {AppName} {dotNetVersion} {toolVersion}";
cmd = $"{path} {ProjectDir} {DllName} {AppName} {DotNetVersion} {ToolVersion}";

Console.Write($"Creating app directory at /tmp/{AppName}.temp...");
bash.Command(cmd, redirect: true);
Bash.Command(cmd, redirect: true);
CheckCommandOutput(errorCode: 22);
}

static void RunAppImageTool()
{
var appimgtool = $"{Here}/appimagetool/AppRun";
var cmd = $"{appimgtool} -n /tmp/{AppName}.temp {destination}/{AppName}";
var cmd = $"{appimgtool} -n /tmp/{AppName}.temp {Destination}/{AppName}";

if (Verbose)
{
Console.WriteLine($"Compressing app directory into an AppImage...");
bash.Command(cmd, redirect: false);
Bash.Command(cmd, redirect: false);
}
else
{
Console.Write($"Compressing app directory into an AppImage...");
bash.Command(cmd, redirect: true);
Bash.Command(cmd, redirect: true);
}

CheckCommandOutput(errorCode: 23);
Expand All @@ -223,14 +220,14 @@ static void RunAppImageTool()
static void DeleteTempFiles()
{
Console.Write("Deleting temporary files...");
bash.Rm($"/tmp/{DllName}.temp", "-rf");
Bash.Rm($"/tmp/{DllName}.temp", "-rf");
CheckCommandOutput(24);
}

static void SayHello()
{
var title = $" {toolName} v{toolVersion} ";
var newWidth = width - title.Length;
var title = $" {ToolName} v{ToolVersion} ";
var newWidth = Width - title.Length;
var leftBar = new String('-', newWidth / 2);
string rightBar;

Expand All @@ -251,15 +248,15 @@ static void HelpMenu()
+ $"[{Frmt.Underline}Destination{Reset.Code}] "
+ $"[{Frmt.Underline}Flags{Reset.Code}]\n\n"
+ $" {Frmt.Bold}{Clr.Cyan}Flags:{Reset.Code}\n"
+ $" --verbose or -v: Verbose output\n"
+ $" --compile or -c: Skip restoring dependencies\n"
+ $" --name or -n: Set ouput file to a custom name\n"
+ $" --scd or -s: Self-Contained Deployment (SCD)\n"
+ @" --verbose or -v: Verbose output\n"
+ @" --compile or -c: Skip restoring dependencies\n"
+ @" --name or -n: Set ouput file to a custom name\n"
+ @" --scd or -s: Self-Contained Deployment (SCD)\n"
+ @" --keep or -k: Keep /tmp/{AppName}.temp directory\n"
+ $" --help or -h: Help menu (this page)\n\n"
+ $" More information & source code available on github:\n"
+ $" https://github.com/phil-harmoniq/netpkg-tool\n"
+ $" Copyright (c) 2017 - MIT License\n"
+ @" --help or -h: Help menu (this page)\n\n"
+ @" More information & source code available on github:\n"
+ @" https://github.com/phil-harmoniq/netpkg-tool\n"
+ @" Copyright (c) 2017 - MIT License\n"
);
SayBye();
Environment.Exit(0);
Expand All @@ -268,7 +265,7 @@ static void HelpMenu()
static void ClearLogs()
{
Console.Write($"Clear log at {GetRelativePath(configDir)}/error.log");
bash.Rm($"{configDir}/error.log", "-f");
Bash.Rm($"{configDir}/error.log", "-f");
CheckCommandOutput(errorCode: 5);
SayBye();
Environment.Exit(0);
Expand Down Expand Up @@ -299,39 +296,39 @@ static void WriteToErrorLog(string message, int code)
var now = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
var dir = Directory.GetCurrentDirectory();

tw.WriteLine($"{new string('-', width)}");
tw.WriteLine($"{new string('-', Width)}");
tw.WriteLine($"{GetRelativePath(dir)}$ netpkg-tool {string.Join(' ', Args)}");
tw.WriteLine($"Errored with code {code} - ({now}):\n");
tw.WriteLine(message.TrimEnd('\n'));
tw.WriteLine($"{new string('-', width)}");
tw.WriteLine($"{new string('-', Width)}");
}
}

/// <param name="errorCode">Desired error code if the command didn't run properly</param>
static void CheckCommandOutput(int errorCode = 1)
{
if (bash.ExitCode != 0)
if (Bash.ExitCode != 0)
{
SayFail();
if (string.IsNullOrEmpty(bash.ErrorMsg))
ExitWithError(bash.Output, errorCode);
if (string.IsNullOrEmpty(Bash.ErrorMsg))
ExitWithError(Bash.Output, errorCode);
else
ExitWithError(bash.ErrorMsg, errorCode);
ExitWithError(Bash.ErrorMsg, errorCode);
}
SayPass();
}

static string GetRelativePath(string path) =>
bash.Command($"cd {path} && dirs -0", redirect: true).Lines[0];
Bash.Command($"cd {path} && dirs -0", redirect: true).Output;

static string GetAbsolutePath(string path) =>
bash.Command($"readlink -f {path}", redirect: true).Lines[0];
Bash.Command($"readlink -f {path}", redirect: true).Output;

static void SayBye() =>
Console.WriteLine(new String('-', width) + "\n");
Console.WriteLine(new String('-', Width) + "\n");

static void SayTask(string project, string destination) =>
Printer.WriteLine($"{Clr.Cyan}{project} => {destination}{Clr.Default}");
static void SayTask(string project, string Destination) =>
Printer.WriteLine($"{Clr.Cyan}{project} => {Destination}{Clr.Default}");

static void SayFinished(string message) =>
Printer.WriteLine($"{Clr.Green}{message}{Clr.Default}");
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
Pre-packaged versions of *netpkg-tool* are available from the [releases tab](https://github.com/phil-harmoniq/netpkg-tool/releases):

```bash
# Github releases are tagged with their version (ex: 0.3.7)
wget https://github.com/phil-harmoniq/netpkg-tool/releases/download/0.3.7/netpkg-tool
# Github releases are tagged with their version (ex: 0.3.8)
wget https://github.com/phil-harmoniq/netpkg-tool/releases/download/0.3.8/netpkg-tool
chmod a+x ./netpkg-tool

# Place netpkg-tool somewhere on your $PATH (Optional)
Expand Down
19 changes: 6 additions & 13 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ netpkg-tool Docker Image

This Docker image contains the [*netpkg-tool*](https://github.com/phil-harmoniq/netpkg-tool) binary and is based on the [microsoft/dotnet](https://hub.docker.com/r/microsoft/dotnet/) 2.0-sdk image. Using this Docker image allows you to use *netpkg-tool* without installing the .NET Core SDK.

Pull
-----

Docker images are automatically built using the latest source files. Use the `latest` tag for the most recent master build or the `unstable` tag for the most recent development build:
Supported tags and respective `Dockerfile` links
------------------------------------------------

```bash
# Latest master image
docker pull philharmoniq/netpkg-tool

# Latest development image
docker pull philhamroniq/netpkg-tool:unstable
```
- [`latest` (docker/Dockerfile)](https://github.com/phil-harmoniq/netpkg-tool/blob/master/docker/Dockerfile)
- [`develop` (docker/Dockerfile)](https://github.com/phil-harmoniq/netpkg-tool/blob/develop/docker/Dockerfile)

Usage
-----
Expand All @@ -24,15 +17,15 @@ Usage
```bash
# Assuming the working directory contains your .NET Core project.
# Note: ${PWD}/out doesn't have to exist - Docker creates the directory if necessary
docker run --rm -v ${PWD}:/root/src -v ${PWD}/out:/root/out local/netpkg-tool
docker run --rm -v ${PWD}:/root/src -v ${PWD}/out:/root/out philhamroniq/netpkg-tool
```

You can also supply parameters like `-n MyApp` or `--scd` to the container. **Full example**:

```bash
git clone https://github.com/phil-harmoniq/Hello
cd Hello
docker run --rm -v ${PWD}:/root/src -v ${PWD}/out:/root/out local/netpkg-tool -n MyApp
docker run --rm -v ${PWD}:/root/src -v ${PWD}/out:/root/out philhamroniq/netpkg-tool -n MyApp
./out/MyApp
```

Expand Down
2 changes: 1 addition & 1 deletion netpkg-tool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Authors>Phil Hawkins</Authors>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<VersionPrefix>0.3.7</VersionPrefix>
<VersionPrefix>0.3.8</VersionPrefix>
<VersionSuffix>alpha</VersionSuffix>
<Description>Bundle .NET Core applications into convenient Linux binaries using appimagetool</Description>
<Copyright>MIT</Copyright>
Expand Down

0 comments on commit a7d23f2

Please sign in to comment.