Skip to content

Commit

Permalink
Merge pull request #252 from TheJumpCloud/SA-1275_Get-JCSystemInsight…
Browse files Browse the repository at this point in the history
…s_SDK

Sa 1275 get jc system insights sdk
  • Loading branch information
Elliott Panipinto authored Aug 12, 2020
2 parents 53398cb + eb1518a commit 38def5b
Show file tree
Hide file tree
Showing 11 changed files with 618 additions and 609 deletions.
155 changes: 155 additions & 0 deletions PowerShell/Deploy/BuildNuspecFromPsd1.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
. ($PSScriptRoot + '/' + 'Get-Config.ps1')

# Set Variables for New-NuspecFile
$ManifestPath = "$($FilePath_psd1)"
$OutputPath = "$($FolderPath_Module)"
$Psd1 = Import-PowerShellDataFile -Path:($ManifestPath)
$Id = $(Get-Item ($ManifestPath)).BaseName
$Version = $Psd1.ModuleVersion
$Description = $Psd1.Description
$Authors = $Psd1.Author
$Owners = $Psd1.CompanyName
$ReleaseNotes = $Psd1.PrivateData.PSData.ReleaseNotes
$Copyright = $Psd1.Copyright
$Tags = $Psd1.PrivateData.PSData.Tags
$LicenseUrl = $Psd1.PrivateData.PSData.LicenseUri
$ProjectUrl = $Psd1.PrivateData.PSData.ProjectUri
$IconUrl = $Psd1.PrivateData.PSData.IconUri
$Dependencies = $Psd1.RequiredModules

# Addapted from PowerShell Get
# https://github.com/PowerShell/PowerShellGetv2/blob/7de99ee0c38611556e5c583ffaca98bb1922a0d4/src/PowerShellGet/private/functions/New-NuspecFile.ps1
function New-NuspecFile {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[string]$OutputPath,

[Parameter(Mandatory = $true)]
[string]$Id,

[Parameter(Mandatory = $true)]
[string]$Version,

[Parameter(Mandatory = $true)]
[string]$Description,

[Parameter(Mandatory = $true)]
[string[]]$Authors,

[Parameter()]
[string[]]$Owners,

[Parameter()]
[string]$ReleaseNotes,

[Parameter()]
[bool]$RequireLicenseAcceptance,

[Parameter()]
[string]$Copyright,

[Parameter()]
[string[]]$Tags,

[Parameter()]
[string]$LicenseUrl,

[Parameter()]
[string]$ProjectUrl,

[Parameter()]
[string]$IconUrl,

[Parameter()]
[PSObject[]]$Dependencies,

[Parameter()]
[PSObject[]]$Files

)
Set-StrictMode -Off

Write-Verbose "Calling New-NuspecFile"

$nameSpaceUri = "http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"
[xml]$xml = New-Object System.Xml.XmlDocument

$xmlDeclaration = $xml.CreateXmlDeclaration("1.0", "utf-8", $null)
$xml.AppendChild($xmlDeclaration) | Out-Null

#create top-level elements
$packageElement = $xml.CreateElement("package", $nameSpaceUri)
$metaDataElement = $xml.CreateElement("metadata", $nameSpaceUri)

# warn we're over 4000 characters for standard nuget servers
$tagsString = $Tags -Join " "
if ($tagsString.Length -gt 4000) {
Write-Warning -Message "Tag list exceeded 4000 characters and may not be accepted by some Nuget feeds."
}

$metaDataElementsHash = [ordered]@{
id = $Id
version = $Version
description = $Description
authors = $Authors -Join ","
owners = $Owners -Join ","
releaseNotes = $ReleaseNotes
requireLicenseAcceptance = $RequireLicenseAcceptance.ToString().ToLower()
copyright = $Copyright
tags = $tagsString
}

if ($LicenseUrl) { $metaDataElementsHash.Add("licenseUrl", $LicenseUrl) }
if ($ProjectUrl) { $metaDataElementsHash.Add("projectUrl", $ProjectUrl) }
if ($IconUrl) { $metaDataElementsHash.Add("iconUrl", $IconUrl) }

foreach ($key in $metaDataElementsHash.Keys) {
$element = $xml.CreateElement($key, $nameSpaceUri)
$elementInnerText = $metaDataElementsHash.item($key)
$element.InnerText = $elementInnerText

$metaDataElement.AppendChild($element) | Out-Null
}


if ($Dependencies) {
$dependenciesElement = $xml.CreateElement("dependencies", $nameSpaceUri)

foreach ($dependency in $Dependencies) {
$element = $xml.CreateElement("dependency", $nameSpaceUri)
# $element.
$element.SetAttribute("id", $dependency)
if ($dependency.version) { $element.SetAttribute("version", $dependency.version) }

$dependenciesElement.AppendChild($element) | Out-Null
}
$metaDataElement.AppendChild($dependenciesElement) | Out-Null
}

if ($Files) {
$filesElement = $xml.CreateElement("files", $nameSpaceUri)

foreach ($file in $Files) {
$element = $xml.CreateElement("file", $nameSpaceUri)
$element.SetAttribute("src", $file.src)
if ($file.target) { $element.SetAttribute("target", $file.target) }
if ($file.exclude) { $element.SetAttribute("exclude", $file.exclude) }

$filesElement.AppendChild($element) | Out-Null
}
}

$packageElement.AppendChild($metaDataElement) | Out-Null
if ($filesElement) { $packageElement.AppendChild($filesElement) | Out-Null }

$xml.AppendChild($packageElement) | Out-Null

$nuspecFullName = Join-Path -Path $OutputPath -ChildPath "$Id.nuspec"
$xml.save($nuspecFullName)

Write-Output $nuspecFullName
}


New-NuspecFile -OutputPath $OutputPath -Id $Id -Version $Version -Description $Description -Authors $Authors -Owners $Owners -ReleaseNotes $ReleaseNotes -Copyright $Copyright -Tags $Tags -LicenseUrl $LicenseUrl -ProjectUrl $ProjectUrl -IconUrl $IconUrl -Dependencies $Dependencies
123 changes: 63 additions & 60 deletions PowerShell/Deploy/SdkSync/jcapiToSupportSync.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,70 +35,73 @@ If (-not [System.String]::IsNullOrEmpty($Modules))
$FunctionDestination = $Function.Destination
$OutputPath = "$JumpCloudModulePath/$FunctionDestination"
$Command = Get-Command -Name:($FunctionName)
$CommandName = $Command.Name
$NewCommandName = $CommandName.Replace($SdkPrefix, $JumpCloudModulePrefix)
Write-Host ("[STATUS] Building: $NewCommandName") -BackgroundColor:('Black') -ForegroundColor:('Magenta')
# Get content from sdk function
$CommandFilePath = $Command.ScriptBlock.File
$CommandFilePathContent = Get-Content -Path:($CommandFilePath) -Raw
$FunctionContent = If ($CommandFilePath -like '*ProxyCmdletDefinitions.ps1')
foreach ($individualCommand in $Command)
{
<# When the autorest generated module has been installed and imported from the PSGallery all the
cmdlets will exist in a single ProxyCmdletDefinitions.ps1 file. We need to parse
out the specific function in order to gather the parts we need to copy over. #>
$CommandFilePathContent.Replace($MSCopyrightHeader, $Divider).Split($Divider).Where( { $_ -like ('*' + "function $CommandName {" + '*') })
}
Else
{
<# When the autorest generated module has been imported from a local psd1 module the function will
remain in their individual files. #>
$CommandFilePathContent
}
$PSScriptInfo = ($FunctionContent | Select-String -Pattern:('(?s)(<#)(.*?)(#>)')).Matches.Value
$Params = $FunctionContent | Select-String -Pattern:('(?s)( \[Parameter)(.*?)(\})') -AllMatches
$ParameterContent = ($Params.Matches.Value | Where-Object { $_ -notlike '*DontShow*' -and $_ -notlike '*Limit*' -and $_ -notlike '*Skip*' })
$OutputType = ($FunctionContent | Select-String -Pattern:('(\[OutputType)(.*?)(\]\r)')).Matches.Value
$CmdletBinding = ($FunctionContent | Select-String -Pattern:('(\[CmdletBinding)(.*?)(\]\r)')).Matches.Value
If (-not [System.String]::IsNullOrEmpty($PSScriptInfo))
{
$PSScriptInfo = $PSScriptInfo.Replace($SdkPrefix, $JumpCloudModulePrefix)
$PSScriptInfo = $PSScriptInfo.Replace("$NewCommandName.md", "$FunctionName.md")
}
# Build CmdletBinding
If (-not [System.String]::IsNullOrEmpty($OutputType)) { $CmdletBinding = "$($OutputType)`n$($IndentChar)$($CmdletBinding)" }
# Build $BeginContent, $ProcessContent, and $EndContent
$BeginContent = @()
$ProcessContent = @()
$EndContent = @()
# Build "Begin" block
$BeginContent += "$($IndentChar)$($IndentChar)Connect-JCOnline -force | Out-Null"
$BeginContent += "$($IndentChar)$($IndentChar)`$Results = @()"
# Build "Process" block
$ProcessContent += "$($IndentChar)$($IndentChar)`$Results = $($ModuleName)\$($CommandName) @PSBoundParameters"
# Build "End" block
$EndContent += "$($IndentChar)$($IndentChar)Return `$Results"
If (-not [System.String]::IsNullOrEmpty($BeginContent) -and -not [System.String]::IsNullOrEmpty($ProcessContent) -and -not [System.String]::IsNullOrEmpty($EndContent))
{
# Build "Function"
$NewScript = $FunctionTemplate -f $PSScriptInfo, $NewCommandName, $CmdletBinding, ($ParameterContent -join ",`n`n"), ($BeginContent -join "`n"), ($ProcessContent -join "`n"), ($EndContent -join "`n")
# Fix line endings
$NewScript = $NewScript.Replace("`r`n", "`n").Trim()
# Export the function
$OutputFilePath = "$OutputPath/$NewCommandName.ps1"
New-FolderRecursive -Path:($OutputFilePath) -Force
$NewScript | Out-File -FilePath:($OutputFilePath) -Force
# Validate script syntax
$ScriptAnalyzerResult = Invoke-ScriptAnalyzer -Path:($OutputFilePath) -Recurse -ExcludeRule PSShouldProcess, PSAvoidTrailingWhitespace, PSAvoidUsingWMICmdlet, PSAvoidUsingPlainTextForPassword, PSAvoidUsingUsernameAndPasswordParams, PSAvoidUsingInvokeExpression, PSUseDeclaredVarsMoreThanAssignments, PSUseSingularNouns, PSAvoidGlobalVars, PSUseShouldProcessForStateChangingFunctions, PSAvoidUsingWriteHost, PSAvoidUsingPositionalParameters
If ($ScriptAnalyzerResult)
$CommandName = $individualCommand.Name
$NewCommandName = $CommandName.Replace($SdkPrefix, $JumpCloudModulePrefix)
Write-Host ("[STATUS] Building: $NewCommandName") -BackgroundColor:('Black') -ForegroundColor:('Magenta')
# Get content from sdk function
$CommandFilePath = $individualCommand.ScriptBlock.File
$CommandFilePathContent = Get-Content -Path:($CommandFilePath) -Raw
$FunctionContent = If ($CommandFilePath -like '*ProxyCmdletDefinitions.ps1')
{
<# When the autorest generated module has been installed and imported from the PSGallery all the
cmdlets will exist in a single ProxyCmdletDefinitions.ps1 file. We need to parse
out the specific function in order to gather the parts we need to copy over. #>
$CommandFilePathContent.Replace($MSCopyrightHeader, $Divider).Split($Divider).Where( { $_ -like ('*' + "function $CommandName {" + '*') })
}
Else
{
<# When the autorest generated module has been imported from a local psd1 module the function will
remain in their individual files. #>
$CommandFilePathContent
}
$PSScriptInfo = ($FunctionContent | Select-String -Pattern:('(?s)(<#)(.*?)(#>)')).Matches.Value
$Params = $FunctionContent | Select-String -Pattern:('(?s)( \[Parameter)(.*?)(\})') -AllMatches
$ParameterContent = ($Params.Matches.Value | Where-Object { $_ -notlike '*DontShow*' -and $_ -notlike '*Limit*' -and $_ -notlike '*Skip*' })
$OutputType = ($FunctionContent | Select-String -Pattern:('(\[OutputType)(.*?)(\]\r)')).Matches.Value
$CmdletBinding = ($FunctionContent | Select-String -Pattern:('(\[CmdletBinding)(.*?)(\]\r)')).Matches.Value
If (-not [System.String]::IsNullOrEmpty($PSScriptInfo))
{
$PSScriptInfo = $PSScriptInfo.Replace($SdkPrefix, $JumpCloudModulePrefix)
$PSScriptInfo = $PSScriptInfo.Replace("$NewCommandName.md", "$FunctionName.md")
}
# Build CmdletBinding
If (-not [System.String]::IsNullOrEmpty($OutputType)) { $CmdletBinding = "$($OutputType)`n$($IndentChar)$($CmdletBinding)" }
# Build $BeginContent, $ProcessContent, and $EndContent
$BeginContent = @()
$ProcessContent = @()
$EndContent = @()
# Build "Begin" block
$BeginContent += "$($IndentChar)$($IndentChar)Connect-JCOnline -force | Out-Null"
$BeginContent += "$($IndentChar)$($IndentChar)`$Results = @()"
# Build "Process" block
$ProcessContent += "$($IndentChar)$($IndentChar)`$Results = $($ModuleName)\$($CommandName) @PSBoundParameters"
# Build "End" block
$EndContent += "$($IndentChar)$($IndentChar)Return `$Results"
If (-not [System.String]::IsNullOrEmpty($BeginContent) -and -not [System.String]::IsNullOrEmpty($ProcessContent) -and -not [System.String]::IsNullOrEmpty($EndContent))
{
$ScriptAnalyzerResults += $ScriptAnalyzerResult
# Build "Function"
$NewScript = $FunctionTemplate -f $PSScriptInfo, $NewCommandName, $CmdletBinding, ($ParameterContent -join ",`n`n"), ($BeginContent -join "`n"), ($ProcessContent -join "`n"), ($EndContent -join "`n")
# Fix line endings
$NewScript = $NewScript.Replace("`r`n", "`n").Trim()
# Export the function
$OutputFilePath = "$OutputPath/$NewCommandName.ps1"
New-FolderRecursive -Path:($OutputFilePath) -Force
$NewScript | Out-File -FilePath:($OutputFilePath) -Force
# Validate script syntax
$ScriptAnalyzerResult = Invoke-ScriptAnalyzer -Path:($OutputFilePath) -Recurse -ExcludeRule PSShouldProcess, PSAvoidTrailingWhitespace, PSAvoidUsingWMICmdlet, PSAvoidUsingPlainTextForPassword, PSAvoidUsingUsernameAndPasswordParams, PSAvoidUsingInvokeExpression, PSUseDeclaredVarsMoreThanAssignments, PSUseSingularNouns, PSAvoidGlobalVars, PSUseShouldProcessForStateChangingFunctions, PSAvoidUsingWriteHost, PSAvoidUsingPositionalParameters
If ($ScriptAnalyzerResult)
{
$ScriptAnalyzerResults += $ScriptAnalyzerResult
}
}
# Copy tests?
# Copy-Item -Path:($AutoRest_Tests) -Destination:($JCModule_Tests) -Force
# Update .Psd1 file
$Psd1.FunctionsToExport += $NewCommandName
Update-ModuleManifest -Path:($FilePath_psd1) -FunctionsToExport:($Psd1.FunctionsToExport)
}
# Copy tests?
# Copy-Item -Path:($AutoRest_Tests) -Destination:($JCModule_Tests) -Force
# Update .Psd1 file
$Psd1.FunctionsToExport += $NewCommandName
Update-ModuleManifest -Path:($FilePath_psd1) -FunctionsToExport:($Psd1.FunctionsToExport)
}
}
}
Expand Down
Loading

0 comments on commit 38def5b

Please sign in to comment.