Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 4d946b0

Browse files
authored
Cleanup of PSSwagger and helpers (#283)
Cleanup of PSSwagger and helpers
1 parent b6010f1 commit 4d946b0

38 files changed

+600
-323
lines changed

Demos/Demo-AzureRMResourceCommands.ps1

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ $param = @{
2727
}
2828

2929
# AzureRM.Resources
30-
$param['SwaggerSpecUri'] = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-resources/resources/2015-11-01/swagger/resources.json'
30+
$param['SpecificationUri'] = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-resources/resources/2015-11-01/swagger/resources.json'
3131
$param['Name'] = 'Generated.AzureRM.Resources'
3232
New-PSSwaggerModule @param
3333
#endregion Generate AzureRM commands
3434

3535

36-
Import-Module "$PSSwaggerClonePath\PSSwagger\PSSwagger.Common.Helpers\PSSwagger.Common.Helpers.psd1" -verbose -force
37-
Import-Module "$PSSwaggerClonePath\PSSwagger\PSSwagger.Azure.Helpers\PSSwagger.Azure.Helpers.psd1" -verbose -force
36+
Import-Module "$PSSwaggerClonePath\PSSwagger\PSSwaggerUtility\PSSwaggerUtility.psd1" -verbose -force
3837

3938
Import-Module $TargetPath\Generated.AzureRM.Resources -WarningAction SilentlyContinue
4039

Demos/Demo-GeneratedAzureRMCommands.ps1

+5-6
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,29 @@ $param = @{
3939
}
4040

4141
# AzureRM.Resources
42-
$param['SwaggerSpecUri'] = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-resources/resources/2015-11-01/swagger/resources.json'
42+
$param['SpecificationUri'] = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-resources/resources/2015-11-01/swagger/resources.json'
4343
$param['Name'] = 'Generated.AzureRM.Resources'
4444
New-PSSwaggerModule @param
4545

4646
# AzureRM.Storage
47-
$param['SwaggerSpecUri'] = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-storage/2015-06-15/swagger/storage.json'
47+
$param['SpecificationUri'] = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-storage/2015-06-15/swagger/storage.json'
4848
$param['Name'] = 'Generated.AzureRM.Storage'
4949
New-PSSwaggerModule @param
5050

5151
# AzureRM.Network
52-
$param['SwaggerSpecUri'] = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-network/2015-06-15/swagger/network.json'
52+
$param['SpecificationUri'] = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-network/2015-06-15/swagger/network.json'
5353
$param['Name'] = 'Generated.AzureRM.Network'
5454
New-PSSwaggerModule @param
5555

5656
# AzureRM.Compute
57-
$param['SwaggerSpecUri'] = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-compute/2015-06-15/swagger/compute.json'
57+
$param['SpecificationUri'] = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-compute/2015-06-15/swagger/compute.json'
5858
$param['Name'] = 'Generated.AzureRM.Compute'
5959
New-PSSwaggerModule @param
6060

6161
#endregion Generate AzureRM commands
6262

6363
#region initialization
64-
Import-Module "$PSSwaggerClonePath\PSSwagger\PSSwagger.Common.Helpers\PSSwagger.Common.Helpers.psd1" -verbose -force
65-
Import-Module "$PSSwaggerClonePath\PSSwagger\PSSwagger.Azure.Helpers\PSSwagger.Azure.Helpers.psd1" -verbose -force
64+
Import-Module "$PSSwaggerClonePath\PSSwagger\PSSwaggerUtility\PSSwaggerUtility.psd1" -verbose -force
6665
Import-Module $TargetPath\Generated.AzureRM.Resources -WarningAction SilentlyContinue
6766
Import-Module $TargetPath\Generated.AzureRM.Storage -WarningAction SilentlyContinue
6867
Import-Module $TargetPath\Generated.AzureRM.Network -WarningAction SilentlyContinue

Demos/Demo-GeneratedAzureStackRMCommands.ps1

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ Import-Module .\PSSwagger\PSSwagger.psd1 -Force
3333
# AzureStackRM.FabricResourceProvider
3434
$ModuleName = 'Generated.AzureStackRM.FabricResourceProvider'
3535
$param = @{
36-
SwaggerSpecUri = 'C:\code\swaggerrelated\JsonFiles\SwaggerTransformed.json'
36+
SpecificationUri = 'C:\code\swaggerrelated\JsonFiles\SwaggerTransformed.json'
3737
Path = $TargetPath
3838
Name = $ModuleName
3939
UseAzureCsharpGenerator = $false
4040
}
4141
New-PSSwaggerModule @param
4242

43-
Import-Module "$PSSwaggerClonePath\PSSwagger\PSSwagger.Common.Helpers\PSSwagger.Common.Helpers.psd1" -verbose -force
44-
Import-Module "$PSSwaggerClonePath\PSSwagger\PSSwagger.Azure.Helpers\PSSwagger.Azure.Helpers.psd1" -verbose -force
43+
Import-Module "$PSSwaggerClonePath\PSSwagger\PSSwaggerUtility\PSSwaggerUtility.psd1" -verbose -force
4544
Import-Module $TargetPath\$ModuleName -WarningAction SilentlyContinue
4645
Get-Command -Module $ModuleName
4746
Get-Command -Module $ModuleName -Syntax

PSSwagger.LiveTestFramework/PSSwagger.LiveTestFramework.psd1

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
CmdletsToExport = ''
1212
VariablesToExport = ''
1313
AliasesToExport = ''
14-
RequiredModules = @('PSSwagger.Common.Helpers')
14+
RequiredModules = @('PSSwaggerUtility')
1515
NestedModules = @()
1616

1717
FileList = @(

PSSwagger.LiveTestFramework/PSSwagger.LiveTestFramework.psm1

+192-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function Start-PSSwaggerLiveTestServer {
2323
$NoNewWindow
2424
)
2525

26-
$osInfo = PSSwagger.Common.Helpers\Get-OperatingSystemInfo
26+
$osInfo = PSSwaggerUtility\Get-OperatingSystemInfo
2727
if ($osInfo.IsCore) {
2828
$clr = 'coreclr'
2929
} else {
@@ -120,7 +120,7 @@ function Add-PSSwaggerLiveTestLibType {
120120
$SaveAssembly
121121
)
122122

123-
$osInfo = PSSwagger.Common.Helpers\Get-OperatingSystemInfo
123+
$osInfo = PSSwaggerUtility\Get-OperatingSystemInfo
124124
$codeFilePaths = @()
125125
$systemRefs = @()
126126
$packageDependencies = @{}
@@ -206,7 +206,7 @@ function Add-PSSwaggerLiveTestServerType {
206206
$SaveAssembly
207207
)
208208

209-
$osInfo = PSSwagger.Common.Helpers\Get-OperatingSystemInfo
209+
$osInfo = PSSwaggerUtility\Get-OperatingSystemInfo
210210
$codeFilePaths = @()
211211
$systemRefs = @()
212212
$packageDependencies = @{}
@@ -342,7 +342,7 @@ function Add-PSSwaggerLiveTestTypeGeneric {
342342
$GetPSSwaggerAddTypeParametersParams['OutputAssemblyName'] = $OutputFileName
343343
}
344344

345-
$addTypeParamsResult = PSSwagger.Common.Helpers\Get-PSSwaggerAddTypeParameters @GetPSSwaggerAddTypeParametersParams
345+
$addTypeParamsResult = Get-PSSwaggerAddTypeParameters @GetPSSwaggerAddTypeParametersParams
346346

347347
if ($addTypeParamsResult['ResolvedPackageReferences']) {
348348
foreach ($extraRef in $addTypeParamsResult['ResolvedPackageReferences']) {
@@ -378,4 +378,192 @@ function Add-PSSwaggerLiveTestTypeGeneric {
378378
}
379379
}
380380

381+
<#
382+
.DESCRIPTION
383+
Compiles AutoRest generated C# code using the framework of the current PowerShell process.
384+
385+
.PARAMETER Path
386+
All *.Code.ps1 C# files to compile.
387+
388+
.PARAMETER OutputDirectory
389+
Full Path to output directory.
390+
391+
.PARAMETER OutputAssemblyName
392+
Optional assembly file name.
393+
394+
.PARAMETER AllUsers
395+
User has specified to install package dependencies to global location.
396+
397+
.PARAMETER BootstrapConsent
398+
User has consented to bootstrap dependencies.
399+
400+
.PARAMETER TestBuild
401+
Build binaries for testing (disable compiler optimizations, enable full debug information).
402+
403+
.PARAMETER SymbolPath
404+
Path to store PDB file and matching source file.
405+
406+
.PARAMETER PackageDependencies
407+
Map of package dependencies to add as referenced assemblies but don't exist on disk.
408+
409+
.PARAMETER FileReferences
410+
Compilation references that exist on disk.
411+
412+
.PARAMETER PreprocessorDirectives
413+
Preprocessor directives to add to the top of the combined source code file.
414+
#>
415+
function Get-PSSwaggerAddTypeParameters {
416+
[CmdletBinding()]
417+
param(
418+
[Parameter(Mandatory=$false)]
419+
[string[]]
420+
$Path,
421+
422+
[Parameter(Mandatory=$false)]
423+
[AllowEmptyString()]
424+
[string]
425+
$OutputDirectory,
426+
427+
[Parameter(Mandatory=$false)]
428+
[AllowEmptyString()]
429+
[string]
430+
$OutputAssemblyName,
431+
432+
[Parameter(Mandatory=$false)]
433+
[switch]
434+
$AllUsers,
435+
436+
[Parameter(Mandatory=$false)]
437+
[switch]
438+
$BootstrapConsent,
439+
440+
[Parameter(Mandatory=$false)]
441+
[switch]
442+
$TestBuild,
443+
444+
[Parameter(Mandatory=$false)]
445+
[string]
446+
$SymbolPath,
447+
448+
[Parameter(Mandatory=$false)]
449+
[ValidateSet("ConsoleApplication","Library")]
450+
[string]
451+
$OutputType = 'Library',
452+
453+
[Parameter(Mandatory=$false)]
454+
[hashtable]
455+
$PackageDependencies,
456+
457+
[Parameter(Mandatory=$false)]
458+
[string[]]
459+
$FileReferences,
460+
461+
[Parameter(Mandatory=$false)]
462+
[string[]]
463+
$PreprocessorDirectives
464+
)
465+
466+
$resultObj = @{
467+
# The add type parameters to use
468+
Params = $null
469+
# Full path to resolved package reference assemblies
470+
ResolvedPackageReferences = @()
471+
# The expected output assembly full path
472+
OutputAssemblyPath = $null
473+
# The actual source to be emitted
474+
SourceCode = $null
475+
# The file name the returned params expect to exist, if required
476+
SourceFileName = $null
477+
}
478+
479+
# Resolve package dependencies
480+
$extraRefs = @()
481+
if ($PackageDependencies) {
482+
foreach ($entry in ($PackageDependencies.GetEnumerator() | Sort-Object { $_.Value.LoadOrder })) {
483+
$reference = $entry.Value
484+
$resolvedRef = Get-PSSwaggerDependency -PackageName $reference.PackageName `
485+
-RequiredVersion $reference.RequiredVersion `
486+
-References $reference.References `
487+
-Framework $reference.Framework `
488+
-AllUsers:$AllUsers -Install -BootstrapConsent:$BootstrapConsent
489+
$extraRefs += $resolvedRef
490+
$resultObj['ResolvedPackageReferences'] += $resolvedRef
491+
}
492+
}
493+
494+
# Combine the possibly authenticode-signed *.Code.ps1 files into a single file, adding preprocessor directives to the beginning if specified
495+
$srcContent = @()
496+
$srcContent += $Path | ForEach-Object { "// File $_"; Remove-AuthenticodeSignatureBlock -Path $_ }
497+
if ($PreprocessorDirectives) {
498+
foreach ($preprocessorDirective in $PreprocessorDirectives) {
499+
$srcContent = ,$preprocessorDirective + $srcContent
500+
}
501+
}
502+
503+
$oneSrc = $srcContent -join "`n"
504+
$resultObj['SourceCode'] = $oneSrc
505+
if ($SymbolPath) {
506+
if ($OutputAssemblyName) {
507+
$OutputAssemblyBaseName = [System.IO.Path]::GetFileNameWithoutExtension("$OutputAssemblyName")
508+
$resultObj['SourceFileName'] = Join-Path -Path $SymbolPath -ChildPath "Generated.$OutputAssemblyBaseName.cs"
509+
} else {
510+
$resultObj['SourceFileName'] = Join-Path -Path $SymbolPath -ChildPath "Generated.cs"
511+
}
512+
513+
$addTypeParams = @{
514+
Path = $resultObj['SourceFileName']
515+
WarningAction = 'Ignore'
516+
}
517+
} else {
518+
$addTypeParams = @{
519+
TypeDefinition = $oneSrc
520+
Language = "CSharp"
521+
WarningAction = 'Ignore'
522+
}
523+
}
524+
525+
if (-not (Get-OperatingSystemInfo).IsCore) {
526+
$compilerParameters = New-Object -TypeName System.CodeDom.Compiler.CompilerParameters
527+
$compilerParameters.CompilerOptions = '/debug:full'
528+
if ($TestBuild) {
529+
$compilerParameters.IncludeDebugInformation = $true
530+
} else {
531+
$compilerParameters.CompilerOptions += ' /optimize+'
532+
}
533+
534+
if ($OutputType -eq 'ConsoleApplication') {
535+
$compilerParameters.GenerateExecutable = $true
536+
}
537+
538+
$compilerParameters.WarningLevel = 3
539+
foreach ($ref in ($FileReferences + $extraRefs)) {
540+
$null = $compilerParameters.ReferencedAssemblies.Add($ref)
541+
}
542+
$addTypeParams['CompilerParameters'] = $compilerParameters
543+
} else {
544+
$addTypeParams['ReferencedAssemblies'] = ($FileReferences + $extraRefs)
545+
if ($OutputType -eq 'ConsoleApplication') {
546+
$addTypeParams['ReferencedAssemblies'] = $OutputType
547+
}
548+
}
549+
550+
$OutputPdbName = ''
551+
if ($OutputAssemblyName) {
552+
$OutputAssembly = Join-Path -Path $OutputDirectory -ChildPath $OutputAssemblyName
553+
$resultObj['OutputAssemblyPath'] = $OutputAssembly
554+
if ($addTypeParams.ContainsKey('CompilerParameters')) {
555+
$addTypeParams['CompilerParameters'].OutputAssembly = $OutputAssembly
556+
} else {
557+
$addTypeParams['OutputAssembly'] = $OutputAssembly
558+
}
559+
} else {
560+
if ($addTypeParams.ContainsKey('CompilerParameters')) {
561+
$addTypeParams['CompilerParameters'].GenerateInMemory = $true
562+
}
563+
}
564+
565+
$resultObj['Params'] = $addTypeParams
566+
return $resultObj
567+
}
568+
381569
Export-ModuleMember -Function Start-PSSwaggerLiveTestServer,Add-PSSwaggerLiveTestLibType,Add-PSSwaggerLiveTestServerType

PSSwagger.LiveTestFramework/docs/dev.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ While you can directly import PSSwagger.LiveTestFramework.psd1 for local testing
55
## C# vs. Code Files
66
The C# code in this module is represented two ways: in ```.cs``` form and in ```.Code.ps1``` form. When distributing, the code is transformed into ```.Code.ps1``` files and authenticode signed. For test code, the C# code is transformed into ```.cs``` form and includes both a VisualStudio-compatible ```.csproj``` (under the ```vs-csproj``` folder) and a dotnet CLI ```.csproj```. The scripts ```~\src\ConvertTo-CSharpFiles.ps1``` and ```~\src\ConvertFrom-CSharpFiles.ps1``` are provided to convert between ```.cs``` and ```.Code.ps1``` files. The module PSSwagger.LiveTestFramework expects ```.Code.ps1``` files only.
77
# PSSwagger.LiveTestFramework.Tests
8-
Under the ```~\test``` folder, you'll find all the tests for PSSwagger.LiveTestFramework. We use a mixture of C# tests for the C# components, and Pester for the PowerShell components. All the build/run scripts are wrapped by PowerShell commands located in this module. Like the product module (PSSwagger.LiveTestFramework), the test module depends on ```PSSwagger.Common.Helpers```. A helper script is available to load the helper module then the test module: ```~\test\Load-TestModule.ps1```.
8+
Under the ```~\test``` folder, you'll find all the tests for PSSwagger.LiveTestFramework. We use a mixture of C# tests for the C# components, and Pester for the PowerShell components. All the build/run scripts are wrapped by PowerShell commands located in this module. Like the product module (PSSwagger.LiveTestFramework), the test module depends on ```PSSwaggerUtility```. A helper script is available to load the helper module then the test module: ```~\test\Load-TestModule.ps1```.
99
## Dependencies
1010
You can install PSSwagger.LiveTestFramework.Tests dependencies using the command ```Initialize-LTFTestsDependencies```. Current test dependencies:
1111
* dotnet CLI

PSSwagger.LiveTestFramework/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ When you, as the service owner, have multiple language SDKs you need to test, yo
1111
Live service <-> Language-specific SDK <-> Language-specific test server <-> Test code
1212

1313
## Usage
14-
After you've imported ```PSSwagger.Common.Helpers```, you can import ```PSSwagger.LiveTestFramework```. Use this module to compile the library and console server types or start the server directly.
14+
After you've imported ```PSSwaggerUtility```, you can import ```PSSwagger.LiveTestFramework```. Use this module to compile the library and console server types or start the server directly.
1515

1616
### Start-PSSwaggerLiveTestServer
1717
TODO

PSSwagger.LiveTestFramework/test/Load-TestModule.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ if (-not $topLevel) {
1212
}
1313

1414
Write-Verbose -Message "Git repo root: $topLevel"
15-
Import-Module (Join-Path -Path $topLevel -ChildPath PSSwagger | Join-Path -ChildPath PSSwagger.Common.Helpers) -Force
15+
Import-Module (Join-Path -Path $topLevel -ChildPath PSSwagger | Join-Path -ChildPath PSSwaggerUtility) -Force
1616
Import-Module (Join-Path -Path $topLevel -ChildPath PSSwagger.LiveTestFramework | Join-Path -ChildPath test | Join-Path -ChildPath PSSwagger.LiveTestFramework.Tests.psd1) -Force

PSSwagger.LiveTestFramework/test/PSSwagger.LiveTestFramework.Tests.psd1

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
CmdletsToExport = ''
1212
VariablesToExport = ''
1313
AliasesToExport = ''
14-
RequiredModules = @('PSSwagger.Common.Helpers')
14+
RequiredModules = @('PSSwaggerUtility')
1515
NestedModules = @()
1616
DefaultCommandPrefix = 'LTFTests'
1717
FileList = @(

PSSwagger.LiveTestFramework/test/PSSwagger.LiveTestFramework.Tests.psm1

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function Install-Dotnet {
231231
[string]$Version = "2.0.0-preview1-005952"
232232
)
233233

234-
$osInfo = PSSwagger.Common.Helpers\Get-OperatingSystemInfo
234+
$osInfo = PSSwaggerUtility\Get-OperatingSystemInfo
235235
$obtainUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain"
236236

237237
# Install for Linux and OS X
@@ -290,7 +290,7 @@ function Get-DotNetPath
290290
[CmdletBinding()]
291291
param()
292292

293-
$osInfo = PSSwagger.Common.Helpers\Get-OperatingSystemInfo
293+
$osInfo = PSSwaggerUtility\Get-OperatingSystemInfo
294294
if ($osInfo.IsWindows) {
295295
$path = "$env:LocalAppData\Microsoft\dotnet"
296296
} else {

0 commit comments

Comments
 (0)