src/helpers.ps1

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
# *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~* #
# ~~ Collection of helper functions used with the tftools module ~~ #
# *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~* #
function Write-tftoolsLogo {
    Write-Host " __ ______ __ " -ForegroundColor Magenta
    Write-Host " / /_/ __/ /_____ ____ / /____" -ForegroundColor Magenta
    Write-Host " / __/ /_/ __/ __ \/ __ \/ / ___/" -ForegroundColor DarkMagenta
    Write-Host "/ /_/ __/ /_/ /_/ / /_/ / (__ ) " -ForegroundColor DarkMagenta
    Write-Host "\__/_/ \__/\____/\____/_/____/ " -ForegroundColor DarkMagenta
    Write-Host " v0.3.7 " -ForegroundColor DarkGray
}
# This one is pretty cool, and we could probably have used a different
# module for working with Zip files but this is a cool scripting exercise
function Export-ZipFile {
    [CmdletBinding()]
    param (
        [Parameter(Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]
        $ZipFile,
        [Parameter(Position = 1)]
        [ValidateNotNullOrEmpty()]
        [string]
        $OutputFolder
    )
    Add-Type -AssemblyName System.IO.Compression.FileSystem
    [System.IO.Compression.ZipFile]::ExtractToDirectory($ZipFile, $OutputFolder)
}
function Set-PlatformVariables {
    if ($PSVersionTable.Platform -eq "Win32NT") {
        # Windows, PowerShell 6+
        Add-tfProfile
        $global:tfPath = $Env:USERPROFILE + "\.tftools"
        $global:machineOS = "windows_amd64"
        $global:execDir = "$Env:LOCALAPPDATA\Microsoft\WindowsApps"
    }
    elseif ($PSVersionTable.OS -like "Linux*") {
        Add-tfProfile -unix
        $global:tfPath = $HOME + "/.tftools"
        $global:machineOS = "linux_amd64"
        if ($Env:PATH -notlike "*$tfPath*") {
            $Env:PATH += $tfPath
            '{0} += "{1}"' -f '$Env:PATH', ":$tfPath" | Add-Content -Path $PROFILE
        }
    }
    elseif ($PSVersionTable.OS -like "Darwin*") {
        Add-tfProfile -unix
        $global:tfPath = $HOME + "/.tftools"
        $global:machineOS = "darwin_amd64"
        if ($Env:PATH -notlike "*$tfPath*") {
            $Env:PATH += ":$tfPath"
            '{0} += "{1}"' -f '$Env:PATH', ":$tfPath" | Add-Content -Path $PROFILE
        }
    }
    elseif (!$PSVersionTable.Platform) {
        # Windows, Windows PowerShell
        Add-tfProfile
        $global:tfPath = $Env:USERPROFILE + "\.tftools" 
        $global:machineOS = "windows_amd64"
        $global:execDir = "$Env:LOCALAPPDATA\Microsoft\WindowsApps"
    }
}
function Add-tfProfile {
    param (
        [switch]
        $unix
    )
    switch ($unix) {
        $true {
            New-Item $HOME/.config -Type Directory -ErrorAction SilentlyContinue
            New-Item $HOME/.config/powershell -Type Directory -ErrorAction SilentlyContinue
            New-Item $PROFILE -ErrorAction SilentlyContinue
        }
        default {
            New-Item $PROFILE -ErrorAction SilentlyContinue
        }
    }
}