TSDProvider.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
$Providername = "TSD"
$TSDPath     = "$env:LOCALAPPDATA\OneGet\TSD"
$CSVFilename  = "$($TSDPath)\OneGetData.csv"

function Get-AuthHeader {
    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/repos/borisyankov/DefinitelyTyped/contents"
        
        write-debug "In $($ProviderName)- Resolve-PackageSources TSD: {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"

    #write-debug "In $($ProviderName) - Find-Package for user {0}" $Name

    if($request.Credential) { $Header = (Get-AuthHeader $request.Credential) }

    ForEach($targetItem in (Invoke-RestMethod "https://api.github.com/repos/borisyankov/DefinitelyTyped/contents" -Header $Header)) {        
           if($request.IsCancelled){break}

        if($targetItem.type -eq 'dir' -and ($targetItem.Name -match $names)) {
            write-debug "In $($ProviderName) - Find-Package item {0} {1}" $targetItem.Name $targetItem.url
            $SWID = @{
                version              = "1.0"
                versionScheme        = "semver"
                fastPackageReference = $targetItem.url
                name                 = $targetItem.name
                source               = "TypeScript"
                summary              = "TypeScript Definition for $($targetItem.name)"
                searchKey            = $targetItem.name
            }           

            $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
    write-debug "Options - {0}" $request.Options.Destination
    write-debug "Creds - {0}" $request.Credential
    
    $OutPath = Resolve-Path $request.Options.Destination
    md -ErrorAction Ignore $TSDPath | out-null
    
    if($request.Credential) { $Header = (Get-AuthHeader $request.Credential) }
    
    foreach($TSD in ( (Invoke-RestMethod $rawUrl -Header $Header) | Where {$_.name -match '\.d\.'})) {    
    
        $details = Invoke-RestMethod $TSD.url -Header $Header
        
        Write-Debug "{0}" $details.name
        Write-Debug "{0}" $details.download_url
        
        $outfile = Join-Path $OutPath $details.name
        
        Invoke-RestMethod $details.download_url -outfile $outfile -Header $Header
        
        write-verbose "Package intstall location {0}" $outfile
    }
}

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
        }
    }
}

function Get-DynamicOptions { 
    param(
        [Microsoft.PackageManagement.MetaProvider.PowerShell.OptionCategory] $category
    )
    
        write-debug "In TSDProvider - Get-DynamicOption for category $category"

    switch( $category ) {
        Package {
            # options when the user is trying to specify a package
            #write-Output (New-DynamicOption $category "SS" SecureString $false )
        }

        Source {
            #options when the user is trying to specify a source
        }
        
        Install {
            #options for installation/uninstallation
            #write-Output (New-DynamicOption $category "Destination" Path $true)
            New-DynamicOption $category "Destination" Path $true
        }
    }
}