modern-unix-win.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
$binPath = Join-Path $PSScriptRoot 'bin'
$supportPath = Join-Path $PSScriptRoot 'support'
$env:Path += ";$binPath"

<#
.SYNOPSIS
List the installed tools.
#>

function Get-ModernUnixTools {
  [OutputType([String])]
  [CmdletBinding()] param([string] $Like = '*')
  (gci $supportPath -Directory).Name -like $Like
}

<#
.SYNOPSIS
Return the content of the man file from the support folder for the given tool, if it exists.
#>

function Get-ModernUnixManual {
  [OutputType([String])]
  [CmdletBinding()]
  param(
    [ArgumentCompleter( { Get-ModernUnixTools })]
    [Parameter(Mandatory)]
    [string] $ToolName)

  $manPath = Join-Path $supportPath $ToolName "$ToolName.1"
  if (Test-Path $manPath) { Get-Content $manPath }
  else { Write-Error "No man file found for '$ToolName'." }
}

<#
.SYNOPSIS
Enable command completion for the given tool, or for all of the installed tools if the argument is omitted.
#>

function Enable-ModernUnixCompletions {
  [OutputType([void])]
  [CmdletBinding()]
  param([ArgumentCompleter( { Get-ModernUnixTools })] [string] $ToolName)

  if (!$ToolName) {
    gci $supportPath -Directory | % { Enable-ModernUnixCompletions $_.Name -ErrorAction:SilentlyContinue }
  }

  else {
    $completionPath = Join-Path $supportPath $ToolName "_$ToolName.ps1"
    if (Test-Path $completionPath) { . $completionPath }
    elseif (!$SuppressError) { Write-Error "No completion file found for '$ToolName'." }
  }
}

<#
.SYNOPSIS
One off initialisation of `cheat`. Sets up a directory structure and downloads community cheatsheets.
#>

function Initialize-Cheat {
  $root = Join-Path $env:APPDATA 'cheat'
  $cheatSheets = Join-Path $root 'cheatsheets'
  $personal, $community = 'personal', 'community' | % { Join-Path $cheatSheets $_ }
  mkdir $personal, $community -Force
  $env:CHEAT_CONFIG_PATH = Join-Path $root 'config.yml'
  [Environment]::SetEnvironmentVariable('CHEAT_CONFIG_PATH', $env:CHEAT_CONFIG_PATH, [EnvironmentVariableTarget]::User)
  if (!(Test-Path $env:CHEAT_CONFIG_PATH)) {
    (
      cheat --init
    ).Replace('cheatsheets/community', $community).Replace('cheatsheets/personal', $personal) > $env:CHEAT_CONFIG_PATH
  }
  if (!(gci $community)) {
    git clone https://github.com/cheat/cheatsheets.git $community
  }
}

<#
.SYNOPSIS
PowerShell `broot` compatibility wrapper.
https://github.com/Canop/broot/issues/159#issue-549501252
#>

function Invoke-Broot {
  $tempFile = New-TemporaryFile
  try {
    $broot = if ($env:BROOT) { $env:BROOT } else { 'broot' }
    &$broot --outcmd $tempFile $args
    if ($LASTEXITCODE -ne 0) {
      Write-Error "$broot exited with code $LASTEXITCODE"
      return
    }
    $command = Get-Content $tempFile
    if ($command) {
      # broot returns extended-length paths but not all PowerShell/Windows
      # versions might handle this so strip the '\\?'
      Invoke-Expression $command.Replace("\\?\", "")
    }
  }
  finally {
    Remove-Item -force $tempFile
  }
}

Set-Alias br Invoke-Broot
Set-Alias mutools Get-ModernUnixTools
Set-Alias muman Get-ModernUnixManual
Set-Alias mucomplete Enable-ModernUnixCompletions

$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
  $env:Path = $env:Path.Replace(";$binPath", "")
}