Skip to content

Convert nested modules to buildable modules #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Migrate unit tests to Pester 5
- IPv6 preparation work
- Increased code coverage to >85% fixes [#60](https://github.com/dsccommunity/DhcpServerDsc/issues/60).
- `DhcpServerDsc.Common`
- Convert to buildable module with per file functions.
- `DhcpServerDsc.OptionValueHelper`
- Convert to buildable module with per file functions.

### Added

Expand Down
27 changes: 21 additions & 6 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
CopyPaths:
- en-US
- DSCResources
- Modules
Encoding: UTF8
VersionedOutputDirectory: true
BuiltModuleSubdirectory: builtModule
Expand All @@ -16,11 +15,27 @@ BuiltModuleSubdirectory: builtModule
####################################################

NestedModule:
DscResource.Common:
CopyOnly: true
Path: ./output/RequiredModules/DscResource.Common
AddToManifest: false
Exclude: PSGetModuleInfo.xml
DscResource.Common:
CopyOnly: true
Path: ./output/RequiredModules/DscResource.Common
AddToManifest: false
Exclude: PSGetModuleInfo.xml
DhcpServerDsc.Common:
Prefix: prefix.ps1
VersionedOutputDirectory: false
CopyPaths:
- en-US
Encoding: UTF8
AddToManifest: false
Exclude: PSGetModuleInfo.xml
DhcpServerDsc.OptionValueHelper:
Prefix: prefix.ps1
VersionedOutputDirectory: false
CopyPaths:
- en-US
Encoding: UTF8
AddToManifest: false
Exclude: PSGetModuleInfo.xml

####################################################
# Pipeline Configuration #
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@
Description = 'Common functions used by the DSC resources in DhcpServerDsc.'

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @(
'New-TerminatingError'
'Get-ValidIPAddress'
'Assert-ScopeParameter'
'Write-PropertyMessage'
'Get-ValidTimeSpan'
)
FunctionsToExport = @()

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,83 +1,3 @@
$script:resourceHelperModulePath = Join-Path -Path $PSScriptRoot -ChildPath '../../Modules/DscResource.Common'

Import-Module -Name $script:resourceHelperModulePath

$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'

# Internal function to throw terminating error with specified ErrorCategory, ErrorId and ErrorMessage
function New-TerminatingError
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$ErrorId,

[Parameter(Mandatory = $true)]
[System.String]
$ErrorMessage,

[Parameter(Mandatory = $true)]
[System.Management.Automation.ErrorCategory]
$ErrorCategory
)

$exception = New-Object -TypeName System.InvalidOperationException -ArgumentList $ErrorMessage
$errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $exception, $ErrorId, $ErrorCategory, $null
throw $errorRecord
}

# Internal function to translate a string to valid IPAddress format
function Get-ValidIPAddress
{
[CmdletBinding()]
[OutputType([System.Net.IPAddress])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$IpString,

[Parameter(Mandatory = $true)]
[ValidateSet('IPv4', 'IPv6')]
[System.String]
$AddressFamily,

[Parameter(Mandatory = $true)]
[System.String]
$ParameterName
)

if ($AddressFamily -eq 'IPv4')
{
$ipAddressFamily = 'InterNetwork'
}
else
{
$ipAddressFamily = 'InterNetworkV6'
}

[System.Net.IPAddress] $ipAddress = $null

$result = [System.Net.IPAddress]::TryParse($IpString, [ref] $ipAddress)

if (-not $result)
{
$errorMsg = $($script:localizedData.InvalidIPAddressFormat) -f $ParameterName

New-TerminatingError -ErrorId 'NotValidIPAddress' -ErrorMessage $errorMsg -ErrorCategory InvalidType
}

if ($ipAddress.AddressFamily -ne $ipAddressFamily)
{
$errorMsg = $($script:localizedData.InvalidIPAddressFamily) -f $ipAddress, $AddressFamily

New-TerminatingError -ErrorId 'InvalidIPAddressFamily' -ErrorMessage $errorMsg -ErrorCategory SyntaxError
}

return $ipAddress
}

<#
.SYNOPSIS
Expand Down Expand Up @@ -190,60 +110,3 @@ function Assert-ScopeParameter
}
}
}

# Internal function to write verbose messages for collection of properties
function Write-PropertyMessage
{
param
(
[Parameter(Mandatory = $true)]
[System.Collections.Hashtable]
$Parameters,

[Parameter(Mandatory = $true)]
[System.String[]]
$KeysToSkip,

[Parameter(Mandatory = $true)]
[System.String]
$MessageTemplate
)

foreach ($key in $parameters.keys)
{
if ($keysToSkip -notcontains $key)
{
$msg = $MessageTemplate -f $key, $parameters[$key]
Write-Verbose -Message $msg
}
}
}

# Internal function to translate a string to valid TimeSpan format
function Get-ValidTimeSpan
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$TsString,

[Parameter(Mandatory = $true)]
[System.String]
$ParameterName
)

[System.TimeSpan] $timeSpan = New-TimeSpan

$result = [System.TimeSpan]::TryParse($TsString, [ref] $timeSpan)

if (-not $result)
{
$errorMsg = $($script:localizedData.InvalidTimeSpanFormat) -f $ParameterName

New-TerminatingError -ErrorId 'NotValidTimeSpan' -ErrorMessage $errorMsg -ErrorCategory InvalidType
}

return $timeSpan
}
50 changes: 50 additions & 0 deletions source/Modules/DhcpServerDsc.Common/Public/Get-ValidIPAddress.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Internal function to translate a string to valid IPAddress format
function Get-ValidIPAddress
{
[CmdletBinding()]
[OutputType([System.Net.IPAddress])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$IpString,

[Parameter(Mandatory = $true)]
[ValidateSet('IPv4', 'IPv6')]
[System.String]
$AddressFamily,

[Parameter(Mandatory = $true)]
[System.String]
$ParameterName
)

if ($AddressFamily -eq 'IPv4')
{
$ipAddressFamily = 'InterNetwork'
}
else
{
$ipAddressFamily = 'InterNetworkV6'
}

[System.Net.IPAddress] $ipAddress = $null

$result = [System.Net.IPAddress]::TryParse($IpString, [ref] $ipAddress)

if (-not $result)
{
$errorMsg = $($script:localizedData.InvalidIPAddressFormat) -f $ParameterName

New-TerminatingError -ErrorId 'NotValidIPAddress' -ErrorMessage $errorMsg -ErrorCategory InvalidType
}

if ($ipAddress.AddressFamily -ne $ipAddressFamily)
{
$errorMsg = $($script:localizedData.InvalidIPAddressFamily) -f $ipAddress, $AddressFamily

New-TerminatingError -ErrorId 'InvalidIPAddressFamily' -ErrorMessage $errorMsg -ErrorCategory SyntaxError
}

return $ipAddress
}
28 changes: 28 additions & 0 deletions source/Modules/DhcpServerDsc.Common/Public/Get-ValidTimeSpan.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Internal function to translate a string to valid TimeSpan format
function Get-ValidTimeSpan
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$TsString,

[Parameter(Mandatory = $true)]
[System.String]
$ParameterName
)

[System.TimeSpan] $timeSpan = New-TimeSpan

$result = [System.TimeSpan]::TryParse($TsString, [ref] $timeSpan)

if (-not $result)
{
$errorMsg = $($script:localizedData.InvalidTimeSpanFormat) -f $ParameterName

New-TerminatingError -ErrorId 'NotValidTimeSpan' -ErrorMessage $errorMsg -ErrorCategory InvalidType
}

return $timeSpan
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Internal function to throw terminating error with specified ErrorCategory, ErrorId and ErrorMessage
function New-TerminatingError
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$ErrorId,

[Parameter(Mandatory = $true)]
[System.String]
$ErrorMessage,

[Parameter(Mandatory = $true)]
[System.Management.Automation.ErrorCategory]
$ErrorCategory
)

$exception = New-Object -TypeName System.InvalidOperationException -ArgumentList $ErrorMessage
$errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $exception, $ErrorId, $ErrorCategory, $null
throw $errorRecord
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Internal function to write verbose messages for collection of properties
function Write-PropertyMessage
{
param
(
[Parameter(Mandatory = $true)]
[System.Collections.Hashtable]
$Parameters,

[Parameter(Mandatory = $true)]
[System.String[]]
$KeysToSkip,

[Parameter(Mandatory = $true)]
[System.String]
$MessageTemplate
)

foreach ($key in $parameters.keys)
{
if ($keysToSkip -notcontains $key)
{
$msg = $MessageTemplate -f $key, $parameters[$key]
Write-Verbose -Message $msg
}
}
}
5 changes: 5 additions & 0 deletions source/Modules/DhcpServerDsc.Common/prefix.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$script:resourceHelperModulePath = Join-Path -Path $PSScriptRoot -ChildPath '../../Modules/DscResource.Common'

Import-Module -Name $script:resourceHelperModulePath

$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
Description = 'Functions used by the DSC resources in DhcpServerDsc.'

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @(
'Get-TargetResourceHelper'
'Test-TargetResourceHelper'
'Set-TargetResourceHelper'
)
FunctionsToExport = @()

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
Expand Down
Loading