Skip to content

Commit

Permalink
Offer to automate the setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Viir committed Feb 20, 2020
1 parent 96df8df commit e91a2d3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
66 changes: 65 additions & 1 deletion implement/elm-fullstack/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Kalmit.PersistentProcess.WebHost;
using McMaster.Extensions.CommandLineUtils;

namespace elm_fullstack
{
class Program
{
static string AppVersionId => "2020-02-15";
static string AppVersionId => "2020-02-20";

static int Main(string[] args)
{
Expand Down Expand Up @@ -67,6 +69,19 @@ static int Main(string[] args)
});
});

app.Command("install-command", installCmd =>
{
var (commandName, _, registerExecutableDirectoryOnPath) = CheckIfExecutableIsRegisteredOnPath();

installCmd.Description = "Installs the '" + commandName + "' command for the current user account.";
installCmd.ThrowOnUnexpectedArgument = true;

installCmd.OnExecute(() =>
{
registerExecutableDirectoryOnPath();
});
});

app.OnExecute(() =>
{
Console.WriteLine("Please specify a subcommand.");
Expand All @@ -77,5 +92,54 @@ static int Main(string[] args)

return app.Execute(args);
}

static (string commandName, bool executableIsRegisteredOnPath, Action registerExecutableDirectoryOnPath)
CheckIfExecutableIsRegisteredOnPath()
{
var environmentVariableName = "PATH";

var environmentVariableScope = EnvironmentVariableTarget.User;

string getCurrentValueOfEnvironmentVariable() =>
Environment.GetEnvironmentVariable(environmentVariableName, environmentVariableScope);

var (executableFilePath, executableDirectoryPath, executableFileName) = GetCurrentProcessExecutableFilePathAndComponents();

var commandName = Regex.Match(executableFileName, @"(.+?)(?=\.exe$|$)").Groups[1].Value;

var registerExecutableForCurrentUser = new Action(() =>
{
var newValueForPathEnv =
executableDirectoryPath +
System.IO.Path.PathSeparator +
getCurrentValueOfEnvironmentVariable();

Environment.SetEnvironmentVariable(environmentVariableName, newValueForPathEnv, environmentVariableScope);

// https://stackoverflow.com/questions/32650063/get-environment-variable-out-of-new-process-in-c-sharp/32650213#32650213
// https://devblogs.microsoft.com/oldnewthing/?p=91591
// https://docs.microsoft.com/en-us/previous-versions//cc723564(v=technet.10)?redirectedfrom=MSDN#XSLTsection127121120120

Console.WriteLine(
"I added the path '" + executableDirectoryPath + "' to the '" + environmentVariableName +
"' environment variable for the current user account. You will be able to use the '" + commandName + "' command in newer instances of the Command Prompt.");
});

var executableIsRegisteredOnPath =
(getCurrentValueOfEnvironmentVariable() ?? "")
.Split(System.IO.Path.PathSeparator).Contains(executableDirectoryPath);

return (commandName, executableIsRegisteredOnPath, registerExecutableForCurrentUser);
}

static string GetCurrentProcessExecutableFilePath() =>
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

static (string filePath, string directoryPath, string fileName) GetCurrentProcessExecutableFilePathAndComponents()
{
var filePath = GetCurrentProcessExecutableFilePath();

return (filePath, System.IO.Path.GetDirectoryName(filePath), System.IO.Path.GetFileName(filePath));
}
}
}
4 changes: 2 additions & 2 deletions implement/elm-fullstack/elm-fullstack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>elm_fullstack</RootNamespace>
<AssemblyName>elm-fullstack</AssemblyName>
<AssemblyVersion>2020.0213.0.0</AssemblyVersion>
<FileVersion>2020.0213.0.0</FileVersion>
<AssemblyVersion>2020.0220.0.0</AssemblyVersion>
<FileVersion>2020.0220.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit e91a2d3

Please sign in to comment.