Public/Invoke-Cygwin.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<#
.SYNOPSIS
    Short description
.DESCRIPTION
    Long description
.EXAMPLE
    Example of how to use this cmdlet
.EXAMPLE
    Another example of how to use this cmdlet
 
@("ls -la", "pwd") | Invoke-Cygwin
Invoke-Cygwin @("ls -la", "pwd")
 
Invoke-Cygwin -Script "ls -la", "pwd"
Invoke-Cygwin "ls -la", "pwd"
"ls -la", "pwd" | Invoke-Cygwin
 
Invoke-Cygwin -ScriptBlock { ls -la; pwd }
Invoke-Cygwin { ls -la; pwd }
{ ls -la; pwd } | Invoke-Cygwin
 
 
.INPUTS
    Inputs to this cmdlet (if any)
.OUTPUTS
    Output from this cmdlet (if any)
.NOTES
    General notes
.COMPONENT
    The component this cmdlet belongs to
.ROLE
    The role this cmdlet belongs to
.FUNCTIONALITY
    The functionality that best describes this cmdlet
#>

function Invoke-Cygwin
{
    [CmdletBinding(DefaultParameterSetName = "Lines", PositionalBinding = $true)]
    param (
        # Parameter help description
        [Parameter(ParameterSetName = "Lines", ValueFromPipeline = $true, ValueFromRemainingArguments = $true, Position = 0)]
        [string[]] $Script,

        # Parameter help description
        [Parameter(ParameterSetName = "ScriptBlock", ValueFromPipeline = $true, ValueFromRemainingArguments = $false)]
        [scriptblock] $ScriptBlock,

        [Parameter()]
        [Cygwin.InstallInfo] $CygwinEnvironment
    )

    begin
    {
        $selectedEnvironment = $CygwinEnvironment;
        if ($null -eq $CygwinEnvironment)
        {
            # get latest of already installed
            $selectedEnvironment = Get-Cygwin | Sort-Object Timestamp -Descending | Select-Object -First 1;
            Write-Verbose ('Selecting [{0}] as current environment.' -f $selectedEnvironment.Path);
        }

        $executable = Join-Path -Path $selectedEnvironment.Path -ChildPath 'bin/bash.exe';
        # . "$RootDir\bin\bash.exe" --login -c $Script;

        [array]$arguments = @('-c', "export PATH=/bin:/usr/bin:`$PATH`n");

        [array]$commandsToExecute = @();
    }

    process
    {
        switch ($PSCmdlet.ParameterSetName)
        {
            'Lines'
            {
                $commandsToExecute += $Script;
            }
            'ScriptBlock'
            {
                $commandsToExecute += $ScriptBlock.ToString();
            }
        }
    }

    end
    {
        $arguments[1] += ("{0}" -f ($commandsToExecute -join "`n"));
        . $executable @arguments 2>&1
    }
}
# SIG # Begin signature block
# MIIFtAYJKoZIhvcNAQcCoIIFpTCCBaECAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUEYr+w5yhuAGfXObf5UH0v8zU
# MZ6gggM9MIIDOTCCAiWgAwIBAgIQ0IShyb7pW4dHi1pXwpsXLzAJBgUrDgMCHQUA
# MCwxKjAoBgNVBAMTIVBvd2VyU2hlbGwgTG9jYWwgQ2VydGlmaWNhdGUgUm9vdDAe
# Fw0yMDAzMTUwOTEwNTBaFw0zOTEyMzEyMzU5NTlaMBUxEzARBgNVBAMTCmRvbWlu
# YXRpb24wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDctlfJa2xVJBNF
# fWOhk/h++cnl/tO5May7dmrFqqMjO1/C4zcCBIarkap31lj2Ky+427sOiH5GSRV7
# GvmjIOUYyabzDuo6N/O/Vuy4D3Dosn2JVQdvz6ZXPMfQBbtw2a0iOhsmxj143de1
# LKCLjDfnoyK2lcEnZmCcal0fdAAGFINNMkFed+9rlS2Qm1wqxjTQGVS0czXrVf1u
# uxGVgXFKKcW6v38GDe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