modern-unix-win.psm1

$toolsPath = Join-Path $PSScriptRoot 'tools.csv'
$binPath = Join-Path $PSScriptRoot 'bin'
$supportPath = Join-Path $PSScriptRoot 'support'
$env:Path += ";$binPath"

$Script:tools = Import-Csv $toolsPath

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

function Get-Manual {
  param ([string] $toolName)

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

function Enable-Completions {
  param([ArgumentCompleter( { $tools | ? completion | select -expand Name })] [string] $ToolName)

  if (!$ToolName) {
    $tools.completion | ? completion | % { . (Join-Path $supportPath $_.name $tool.completion) }
  }

  else {

    $tool = $tools | ? Name -eq $toolName | Select -First 1
    if (!$tool) {
      return "$toolName not found."
    }
    if (!$tool.completion) {
      return "$toolName has no available completions."
    }

    . (Join-Path $supportPath $toolName $tool.completion)
  }
}