Skip to content

Commit

Permalink
add cross-platform process name detection
Browse files Browse the repository at this point in the history
on POSIX platforms, link names were not getting detected
the ProcessName property compatibly reveals names, though it suppresses
Windows .exe file extension
readme usages & demos are updated to likewise suppress these file extensions
  • Loading branch information
lmmarsano committed Apr 17, 2019
1 parent 2dcf232 commit 143de2e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions Demo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Set-Alias {
& $Target set $Alias $Command @Arguments
if ($?) {
try {
[void](New-Item -Verbose:$VerbosePreference -ErrorAction $ErrorActionPreference -Path $LinkPath -ItemType HardLink -Value $Target)
[void](New-Item -Verbose:$VerbosePreference -ErrorAction $ErrorActionPreference -Path "$LinkPath.exe" -ItemType HardLink -Value $Target)
Write-Verbose -Message ('{0} created' -f $LinkPath)
} catch {
Write-Verbose -Message ('{0} not created' -f $LinkPath)
Expand Down Expand Up @@ -60,7 +60,7 @@ function Remove-Alias {
& $Target unset $Alias
if ($?) {
try {
[void](Remove-Item -Verbose:$VerbosePreference -ErrorAction $ErrorActionPreference -Path $LinkPath)
[void](Remove-Item -Verbose:$VerbosePreference -ErrorAction $ErrorActionPreference -Path "$LinkPath.exe")
Write-Verbose -Message ('{0} removed' -f $LinkPath)
} catch {
Write-Verbose -Message ('{0} not removed' -f $LinkPath)
Expand All @@ -72,7 +72,7 @@ function Remove-Alias {
Remove-Alias @Arguments
'@ | Set-Content -Path alias-unset.ps1
$Alias = Resolve-Path -Path Alias.exe
& $Alias set alias-set.exe powershell.exe -- -NoProfile -NoLogo -File (Resolve-Path -Path alias-set.ps1)
& $Alias set alias-set powershell -- -NoProfile -NoLogo -File (Resolve-Path -Path alias-set.ps1)
$AliasSet = New-Item -ItemType HardLink -Path alias-set.exe -Value Alias.exe
& $AliasSet alias-unset.exe powershell.exe -- -NoProfile -NoLogo -File (Resolve-Path -Path alias-unset.ps1)
& $AliasSet mklink.exe cmd /c mklink
& $AliasSet alias-unset powershell -- -NoProfile -NoLogo -File (Resolve-Path -Path alias-unset.ps1)
& $AliasSet mklink cmd /c mklink
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Remove-Item -Path alias.zip
In a directory containing the `alias` executable
```PowerShell
New-Item -Type HardLink -Name test.exe -Value alias.exe
alias set test.exe cmd /c echo
alias set test cmd /c echo
.\test hello world
# hello world
```
Expand All @@ -46,7 +46,7 @@ alias set test.exe cmd /c echo
By convention, this guide assumes the alias application has the unassigned name `alias`, though the user can rename the application to any unassigned name.

## Run External Command
In the same directory as the the alias executable, link or copy the executable to `name`, configure `name`,
In the same directory as the the alias executable, link or copy the executable to *name* (*name*`.exe` on Windows), configure *name*,
> `alias set` name command initial-argument*
and run
Expand Down Expand Up @@ -129,7 +129,7 @@ code source\Alias
An executable is built under `source\Alias\bin\Debug\netcoreapp3.0`.
```PowerShell
Set-Location -Path source\Alias\bin\Debug\netcoreapp3.0
.\Alias.exe
.\Alias
```

# Demo
Expand All @@ -138,9 +138,9 @@ Try it out by running the demo script
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
.\Demo.ps1
.\Alias list
# alias-set.exe
# alias-unset.exe
# mklink.exe
# alias-set
# alias-unset
# mklink
```
and running the generated executables.
- `mklink.exe` calls the link creation command from the `cmd.exe` environment
Expand Down
6 changes: 3 additions & 3 deletions source/Alias/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public Environment(SCG.IEnumerable<string> arguments) {
} catch (S.Exception error) {
throw TerminalFileException.CurrentDirectoryUnavailable(ConfigurationFilePath, error);
}
using (var currentProcess = SD.Process.GetCurrentProcess())
using (var mainModule = currentProcess.MainModule) {
using (var currentProcess = SD.Process.GetCurrentProcess()) {
ApplicationName = currentProcess.ProcessName;
using var mainModule = currentProcess.MainModule;
// No exceptions should get thrown on a process necessarily running to execute this code.
ApplicationFile = GetFileInfo(mainModule.FileName);
}
ApplicationName = ApplicationFile.Name;
ApplicationDirectory = ApplicationFile.DirectoryName;
ConfigurationFilePath = SIO.Path.Combine(ApplicationDirectory, _configurationFileName);
ConfigurationFile = GetFileInfo(ConfigurationFilePath);
Expand Down
2 changes: 1 addition & 1 deletion source/Alias/IEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface IEnvironment {
/// <summary>Path to the application’s parent directory.</summary>
string ApplicationDirectory { get; }
/**
* <summary>The application’s file name.</summary>
* <summary>The application’s file name without .exe extension (if any).</summary>
*/
string ApplicationName { get; }
/// <summary>Application file information.</summary>
Expand Down

0 comments on commit 143de2e

Please sign in to comment.