GistProvider.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
$Providername = "Gist"
$GistPath     = "$env:LOCALAPPDATA\OneGet\Gist"
$CSVFilename  = "$($GistPath)\OneGetData.csv"

function Get-GistAuthHeader {
    param(
        [pscredential]$Credential
    )    

    $authInfo = "{0}:{1}" -f $Credential.UserName, $Credential.GetNetworkCredential().Password
    $authInfo = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($authInfo))

    @{
        "Authorization" = "Basic " + $authInfo
        "Content-Type" = "application/json"
    }
}

function Initialize-Provider     { write-debug "In $($Providername) - Initialize-Provider" }
function Get-PackageProviderName { return $Providername }

function Resolve-PackageSource { 

    write-debug "In $($ProviderName)- Resolve-PackageSources"    
    
    $IsTrusted    = $false
    $IsRegistered = $false
    $IsValidated  = $true
    
    foreach($Name in @($request.PackageSources)) {
        $Location = "https://api.github.com/users/$($Name)/gists"
        
        write-debug "In $($ProviderName)- Resolve-PackageSources gist: {0}" $Location

        New-PackageSource $Name $Location $IsTrusted $IsRegistered $IsValidated
    }        
}

function Find-Package { 
    param(
        [string[]] $names,
        [string] $requiredVersion,
        [string] $minimumVersion,
        [string] $maximumVersion
    )

    write-debug "In $($ProviderName)- Find-Package"
    
    ForEach($Name in @($request.PackageSources)) {
        
        write-debug "In $($ProviderName)- Find-Package for user {0}" $Name
        
        if($request.Credential) { $Header = (Get-GistAuthHeader $request.Credential) }
        
        #write-debug "In $($ProviderName)- Find-Package {0}" $(help New-SoftwareIdentity | out-string)
        ForEach($gist in (Invoke-RestMethod "https://api.github.com/users/$($Name)/gists" -Header $Header)) {
            
            if($request.IsCancelled){break}
            
            $FileName = ($gist.files| Get-Member -MemberType NoteProperty).Name
            
            write-debug "In $($ProviderName)- Find-Package found file {0}" $FileName
            $rawUrl = ($gist.files).($FileName).raw_url
            
            if($rawUrl -And ($FileName -match $names)) {
                $SWID = @{
                    version              = "1.0"
                    versionScheme        = "semver"
                    fastPackageReference = $rawUrl
                    name                 = $FileName
                    source               = "Gist/$($Name)"
                    summary              = ($gist.description).tostring()
                    searchKey            = $FileName.split('.')[0]
                }           
                
                $SWID.fastPackageReference = $SWID | ConvertTo-JSON -Compress
                New-SoftwareIdentity @SWID
            }
        }
    }
}

function Install-Package { 
    param(
        [string] $fastPackageReference
    )       
        $rawUrl = ($fastPackageReference|ConvertFrom-Json).fastpackagereference
    
    write-debug "In $($ProviderName) - Install-Package - {0}" $rawUrl
    
    if(!(Test-Path $GistPath)) { md $GistPath | Out-Null }    
    
    $psFileName = Split-Path -Leaf $rawUrl
    $targetOut = "$($GistPath)\$($psFileName)"

    write-verbose "Package intstall location {0}" $targetOut
    Invoke-RestMethod -Uri $rawUrl | 
        Set-Content -Encoding Ascii $targetOut
    
    ## Update the catalog of gists installed
    ($fastPackageReference | ConvertFrom-Json) |
         Export-Csv -Path $CSVFilename -Append -NoTypeInformation -Encoding ASCII -Force
}

function ConvertTo-HashTable {
    param(
        [Parameter(ValueFromPipeline)]
        $Data
    )

    process {
        if(!$Fields) {            
            $Fields=($Data|Get-Member -MemberType NoteProperty ).Name
        }
        
        $h=[Ordered]@{}
        foreach ($Field in $Fields)
        {
            $h.$Field = $Data.$Field                        
        }
        $h
    }
}

function Get-InstalledPackage {
    param()

    if(Test-Path $CSVFilename) {
        $installedPackages = Import-Csv $CSVFilename
        
        write-debug "In $($ProviderName) - Get-InstalledPackage {0}" @($installedPackages).Count   
        
        foreach ($item in ($installedPackages | ConvertTo-HashTable))
        {    
            New-SoftwareIdentity @item
        }
    }
}