modern-unix-win.psm1

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

$tools = Import-Csv $toolsPath

function Get-ModernUnixTools {
  $tools | select Name, Completion
}

function Get-Manual {
  param([ArgumentCompleter( { Get-ModernUnixTools | select -expand Name })] [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( { Get-ModernUnixTools | ? completion | select -expand Name })] [string] $ToolName)

  if (!$ToolName) {
    $tools | ? completion | % { . (Join-Path $supportPath $_.name $_.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)
  }
}

function Initialize-Cheat {
  $root = Join-Path $env:APPDATA 'cheat'
  $cheatSheets = Join-Path $root 'cheatsheets'
  $personal, $community = (Join-Path $cheatSheets 'personal'), (Join-Path $cheatSheets 'community')
  mkdir $personal, $community -Force
  $env:CHEAT_CONFIG_PATH = Join-Path $root 'config.yml'
  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
  }
}

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