public/Read-GzVisualStudioSolution.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
$vsWebGuids = @{
    "ASP.NET 5" =     "{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}";
    "ASP.NET MVC 1" = "{603C0E0B-DB56-11DC-BE95-000D561079B0}";
    "ASP.NET MVC 2"    = "{F85E285D-A4E0-4152-9332-AB1D724D3325}";
    "ASP.NET MVC 3"     = "{E53F8FEA-EAE0-44A6-8774-FFD645390401}";
    "ASP.NET MVC 4" = "{E3E379DF-F4C6-4180-9B81-6769533ABE47}";
    "ASP.NET MVC 5" = "{349C5851-65DF-11DA-9384-00065B846F21}";
    "WebSite" = "{E24C65DC-7377-472B-9ABA-BC803B73C61A}";
}

$vsWpfGuid = "{60dc8134-eba5-43b8-bcc9-bb4bc16c2548}";

$languageGuids = @{
    "C#" = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
    "C++" = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"
    "F#" = "{F2A71F9B-5D33-465A-A702-920D77279786}"
    "J#" = "{E6FDF86B-F3D1-11D4-8576-0002A516ECE8}"
    "VB.NET" = "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}"
}


$fmgVsProjectFiles = @{
    '.csproj' = '.cs'
    '.vcxproj' = '.cpp'
    '.vbproj' = '.vb'
    '.fsproj' = '.fs'
    '.vsjproj' = '.jsl'
    '.wixproj' = '.wxs'
    '.njsproj' = '.js'
    '.ccproj' = '.csdef'
} 

$testRefs = @("Microsoft.VisualStudio.QualityTools.UnitTestFramework", "xunit", "NUnit.Framework", "NUnitLite")
$testPackageRef = @("Microsoft.NET.Test.Sdk") 

$tsImport = "`$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v`$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets"
$vslibSdkValue = "Microsoft.NET.Sdk"
$vsWebSdkValue = "Microsoft.NET.Sdk.Web" 
$ns = @{"msb" = "http://schemas.microsoft.com/developer/msbuild/2003"}
$serviceRef = "System.ServiceProcess"

function Read-GzVisualStudioSolution() {
<#
.SYNOPSIS
    Reads a Visual Studio solution file and returns meta
    information including the version of Visual Studio
    for the solution file and a hashtable of projects
    with meta info about the projects.
 
.DESCRIPTION
    Reads a Visual Studio solution file and returns meta
    information including the version of Visual Studio
    for the solution file and a hashtable of projects
    with meta info about the projects.
 
    The hashtable key will be the project names. The value
    will have meta info about the project including:
    - Name: name of the project
    - File: the relative path to the project file
    - Id: the id of project e.g. guid
    - IsWebProject: true if the project is a web project
    - WebProjectType: the type of web project
    - IsTestProject: true if the project is a test project
    - IsWindowsService: true if the project is a windows service
    - IsCloudService: true if the project is a cloud service
    - IsWixSharpProject: true if the project is a wix sharp project
    - IsWixProject: true if the project is a wix project.
    - Ext: project extension e.g. csproj
    - LanguageExt: the code file extension e.g. .cs
    - IsSdk: true if the project is a dotnet standard/core project
  
 
.PARAMETER WebProjects
    Optional. The switch instructs the cmdlet to test for
    web projects in the solution.
 
.PARAMETER WindowsServiceProjects
    Optional. The switch instructs the cmdlet to test for
    windows service projects in the solution.
 
.PARAMETER TestProjects
    Optional. The switch instructs the cmdlet to test
    for test projects in the solution.
 
.PARAMETER WixSharpProjects
    Options. The switch instructs the cmdlet to test for
    Wix Sharp projects in the solution.
 
.EXAMPLE
    PS C:\> info = Read-VisualStudioSolution "$Home/Projects/Project.sln"
 
.OUTPUTS
    a custom object with meta information about the projects in the
    solution and the version of Visual Studio that created the sln.
#>

    [CmdletBinding()]
    Param(
        [Parameter(Position = 0)]
        [string] $Path,
        [Switch] $WebProjects,
        [Switch] $WindowServiceProjects,
        [Switch] $TestProjects,
        [Switch] $WixSharpProjects,
        [Switch] $All 
    )
    $projects = @{};
    $sln = Get-Item $Path -Ea SilentlyContinue
    
    if(!$sln) {
        throw [System.IO.FileNotFoundException] $Path 
    }

   
    $parentDir = Split-Path $Path 
    $lines = Get-Content ($sln.FullName)

    $version = $null;

    foreach($line in $lines) {

        if($line.StartsWith("VisualStudioVersion =")) {
            $version = $line.Substring($line.IndexOf("=") + 1).Trim()
        }

        if($line.StartsWith("Project(")) {
             $data = $line.Substring($line.IndexOf("=") + 1);
             $data = $data.Split(",")
             if($data.Length -lt 3) {
                 continue;
             }
          
             $projName = $data[0].Trim().Trim('"')

           

             $projFile = $data[1].Trim().Trim('"')
             $projFile = "$parentDir\$projFile"
             $projId = $data[2].Trim().Trim('"')
             $webProject = $false;
             $webProjectType = $false;
             $testProject = $false;
             $windowsServiceProject = $false;
             $wixSharpProject = $false;
             $ext = [System.IO.Path]::GetExtension($projFile)
             $languageExt = $null
             $isSdk = $false;

             if($fmgVsProjectFiles.ContainsKey($ext)) {
                $languageExt = $fmgVsProjectFiles[$ext]
             }

             if(![IO.Path]::HasExtension($projFile)) {
                 continue;
             }

             try {
                    $xml = [xml](Get-Content $projFile)
                    if($xml.DocumentElement.HasAttribute("Sdk")) {
                        $isSdk = $true;
                    }
                } catch {

                }
               

            $xml = $null
            if($WebProjects.ToBool() -or $All.ToBool()) {
     
                $webProject = $false;
                
                if($null -eq $xml) {
                    $xml = [xml](Get-Content $projFile)
                }

                if($xml.DocumentElement.HasAttribute("Sdk")) {
                    $isSdk = $true;
                    $sdk = $xml.DocumentElement.Attributes["Sdk"];
                    
                    if($sdk -eq $vsWebSdkValue) {
                        $webProject = $true;
                        $webProjectType = "ASP.NET Core"
                        $selection = $xml | Select-Xml "//TargetFramework"
                   
                        if($selection) {
                            $value = $selection.Node.Value;
                            $value = $value.Replace("netcoreapp", "");
                            $webProjectType = "$webProjectType $value" 
                        }
                    }
                } else {
                    $selection = $xml | Select-Xml "//msb:ProjectTypeGuids" -Namespace $ns
                    $guids = @();
                    if($selection) {
                        $guids = $selection.Node.InnerText
                    
                        if($guids -and $guids.Length -gt 0) {
                            $guids = $guids.Split(";");
                        }
                    }

                    foreach($guid in $guids) {
                        
                        foreach($key in $vsWebGuids.Keys) {
                            $value = $vsWebGuids[$key];
                        
                            if($value -eq $guid) {
                                $webProject = $true;
                                $webProjectType = $key;
                                break;
                            }
                        }

                        if($webProject) {
                            break;
                        }
                    }
                }
             }

             if($WixSharpProjects.ToBool()-or $All.ToBool()) {
            
                $wixSharpProject = $false;

                if($null -eq $xml) {
                    $xml = [xml](Get-Content $projFile)
                }
              
                $xml | Select-Xml -XPath "//msb:Reference" -Namespace $ns | ForEach-Object {
                    if($wixSharpProject) {
                        return;
                    } 
                    $value = ""
                    if($_.Node.HasAttribute("Include")) {
                        $value = $_.Node.Attributes["Include"].Value;
                    }
                   
                    if($value -eq "WixSharp.Msi") {
                        $selection = $xml | Select-Xml "//msb:OutputType" -Namespace $ns 
                        
                        if($selection) {
                            $value = $selection.Node.InnerText
                            if($value -eq "Exe" -or $value -eq "WinExe") {
                                 $wixSharpProject = $true;
                            }
                        }
                       
                        return;
                    }
                }
            
             }
     
             if($WindowServiceProjects.ToBool() -or $All.ToBool()) {
                $windowsServiceProject = $false;

                if($null -eq $xml) {
                    $xml = [xml](Get-Content $projFile)
                }
                
                $xml | Select-Xml -XPath "//msb:Reference" -Namespace $ns | ForEach-Object {
                    if($windowsServiceProject) {
                        return;
                    }
                    
                    $value = $null;
                    if($_.Node.HasAttribute("Include")) {
                         $value = $_.Node.Attributes["Include"].Value
                    }
                   
                    
                    
                    if($value -eq "System.ServiceProcess") {
                      
                        $selection = $xml | Select-Xml -XPath "//msb:OutputType" -Namespace $ns 
                        

                        if($selection) {
                            $value = $selection.Node.InnerText
                      
                            if($value -eq "Exe" -or $value -eq "WinExe") {
                               
                                 $windowsServiceProject = $true;
                                 return;
                            }
                        }
                       
                        return;
                    }
                }
             }

             if($TestProjects.ToBool() -or $All.ToBool()) {
                $testProject = $false;

                if($null -eq $selection) {
                    $xml = [xml](Get-Content $projFile)
                }

                if($xml.DocumentElement.HasAttribute("Sdk")) {
                     $isSdk = $true;
                    $xml | Select-Xml "//PackageReference" | ForEach-Object {
                        if($testProject) {
                            return;
                        }
                        $package = $null
                        if($_.Node.HasAttribute("Include")) {
                             $package = $_.Node.Attributes["Include"].Value;
                        }
                       
                        if($package -eq $testPackageRef) {
                            $testProject = $true;
                        }
                    }
                } else {
                    $xml | Select-Xml "//msb:Reference" -Namespace $ns | ForEach-Object {
                        if($testProject) {
                            return;
                        }

                        $value = ""
                        if($_.Node.HasAttribute("Include")) {
                            $value = $_.Node.Attributes["Include"].Value
                        }
                        
                       
                        foreach($testRef in $testRefs) {
                            if($value.StartsWith($testRef)) {
                                $testProject = $true;
                                break;
                            }
                        }
                    }
                }
             }

             if($xml -and $xml.DocumentElement.HasAttribute("Sdk")) {
                $isSdk = $true;
             }
             
         

             $model = New-Object PsObject -Property @{
                 'Name' = $projName
                 'File' = $projFile
                 'Id' = $projId
                 'IsWebProject' = $webProject
                 'WebProjectType' = $webProjectType
                 'IsTestProject' = $testProject
                 'IsWindowsService' = $windowsServiceProject
                 'IsCloudService' = $ext -eq '.ccproj'
                 'IsWixSharpProject' = $wixSharpProject
                 'IsWixProject' = $ext -eq ".wixproj"
                 'Ext' = $ext
                 'LanguageExt' = $languageExt
                 'IsSdk' = $isSdk
             }

           $projectDir = Split-Path $projFile
            
            $packageJson = $null;
            $bowerrc = $null;
            $gulpfile = $null;
            $gruntfile = $null;
            if(Test-Path "$projectDir/package.json") {
                    $packageJson = "$projectDir/package.json"
            }
            if(Test-Path "$projectDir/.bowerrc") {
                $bowerrc = "$projectDir/.bowerrc"
            }
            if(Test-Path "$projectDir/gulpfile.js") {
                $gulpfile = "$projectDir/gulpfile.js"
            }
            if(Test-Path "$projectDir/Gruntfile.js") {
                $gruntfile = "$projectDir/Gruntfile.js"
            }
            
            $model | Add-Member -MemberType NoteProperty -Name "PackageJson" -Value $packageJson
            $model | Add-Member -MemberType NoteProperty -Name "Bower" -Value $bowerrc
            $model | Add-Member -MemberType NoteProperty -Name "Gulp" -Value $gulpfile
            $model | Add-Member -MemberType NoteProperty -Name "Grunt" -Value $gruntfile
      
            $nuspec =  Get-Item "$projectDir/*.nuspec" -ErrorAction SilentlyContinue
            if($nuspec) {
                $nuspec = $nuspec.FullName   
            } else {
                $nuspec = $false
            }
        
            $model | Add-Member -MemberType NoteProperty -Name "NuSpec" -Value $nuspec
           
        

            $projects.Add($projName, $model);
        }
    }

    $meta = [PSCustomObject] (New-Object PSCustomObject -Property @{
        Version = $version
        Projects = $projects
    })

    return $meta;
}