Cmdlets/Show-Metis.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
165
166
167
168

Function Show-Metis {
    
    
    $projects = Get-MetisAllProjects
    Write-Host "`r`nProjects Found ($(($projects | Measure).Count)):"
    $projects | Foreach {
        Write-Host "$($_.Name)`r`n $($_.Path)`r`n"
    }
    
    
    $helpText = Get-Content ([System.IO.Path]::Combine($PSScriptRoot, "..\Files\Help.txt")) -Raw
    Write-Host ""
    Write-Host ""
    Write-Host $helpText
}

Export-ModuleMember -Function Show-Metis


################################################
Function Get-MetisAllProjects {
    
    $projects = $env:MetisTestProjectPath -split ";"| Where { -not ([String]::IsNullOrEmpty($_))} | Foreach {
        $name = [System.IO.Path]::GetFileNameWithoutExtension($_)
        $project = New-Object PSObject
        $project | add-member Noteproperty Name $name
        $project | add-member Noteproperty Path $_
        return $project
    } 

    return $projects
    
}

Export-ModuleMember -Function Get-MetisAllProjects

################################################
Function Get-MetisProject {
    Param(
        [String] $ProjectName = $null
    )
    if(-not $ProjectName){
        Write-Error "No ProjectName was provided"
        return $null
    }
    $project = Get-MetisAllProjects | Where {$_.Name -eq $ProjectName}
    if(-not $project)
    {
        Write-Error "$($projectName) was not found..."
    }
    return $project
}

Export-ModuleMember -Function Get-MetisProject

################################################
Function Add-MetisProject {
    Param(
        [String] $projectPath
    )
    
    if(-not ([System.IO.Path]::IsPathRooted($projectPath))){
        $currentLocation = Get-Location
        $projectPath = [System.IO.Path]::Combine($currentLocation, $projectPath)
    }

    if(-not ([System.IO.File]::Exists($projectPath))){
        throw "Project file '$($projectPath)' does not exists"
    }

    if(([System.IO.Path]::GetExtension($projectPath)) -ne ".csproj"){
        throw "Project file '$($projectPath)' was expected to have extension 'csproj' but didnt"
    }

    $oldMetisProject = $env:MetisTestProjectPath
    if(-not $oldMetisProject){
        $oldMetisProject = ""
    }
    $projects = $oldMetisProject -split ";"
    $projects+=$projectPath
    
    $newMetisProject = $projects -join ";"
    
    [Environment]::SetEnvironmentVariable("MetisTestProjectPath", $newMetisProject, "User")
    [Environment]::SetEnvironmentVariable("MetisTestProjectPath", $newMetisProject, "Process")

}

Export-ModuleMember -Function Add-MetisProject

################################################
Function Show-MetisLocations {
    
    Write-Host "PSScriptRoot: $PSPSScriptRoot"
    Write-Host "PSCommandPath: $PSCommandPath"
    Write-Host "Current-Location: $(Get-Location)"

}
Export-ModuleMember -Function Show-MetisLocations



################################################
Function Sync-TestsFromUserStory{
    Param(
        [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
        [string]$ProjectName,
        [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
        [string]$UserStoryID
    )

    $project = Get-MetisProject $ProjectName
    if($project)
    {
        $projectFile      = $project.Path
        $mainScript       = [System.IO.Path]::Combine($PSScriptRoot, "..\..\..\..\..\tools\Metis.Tools\TestCase-Sync-VSTS.ps1")
        $releaseDefScript = [System.IO.Path]::Combine($PSScriptRoot, "..\..\..\..\..\tools\Metis.Tools\Release-Definitions\VSTS-Tests-Sync.releasedef");

        if(-not [System.IO.File]::Exists($mainScript)){
            throw "Main Script '$($mainScript)' does not exists"
        }
        if(-not [System.IO.File]::Exists($releaseDefScript)){
            throw "Release Def '$($releaseDefScript)' does not exists"
        }

        &$mainScript -SettingsFilePath $releaseDefScript -UserStory $UserStoryID -ProjectFile $projectFile    
    }
}

Export-ModuleMember -Function Sync-TestsFromUserStory

################################################
Function Get-MetisPackagePath{
    
    $metisExtensionPath = [System.IO.Path]::Combine($PSScriptRoot, "..\..\..\..\..\")
    return $metisExtensionPath | Resolve-Path

}

Export-ModuleMember -Function Get-MetisPackagePath

################################################
Function Get-MetisPackageRelativePath{
    Param(
        [String] $RelativePath,
        [switch] $ExpectFileToExists = $true
    )
    $file = [System.IO.Path]::Combine((Get-MetisPackagePath), $RelativePath)
    if($ExpectFileToExists){
        if(-not (Test-Path $file)){
            throw "File '$($RelativePath)' was not found and was expected"
        }
    }
    return $file | Resolve-Path

}

Export-ModuleMember -Function Get-MetisPackageRelativePath

################################################
Function Install-MetisVSExtension{
    
    $extensionPath = Get-MetisPackageRelativePath "tools\MCVSEXtension\MC.VS.Extension.vsix" -ExpectFileToExists
    start $extensionPath
}

Export-ModuleMember -Function Install-MetisVSExtension