Skip to content

Commit

Permalink
Split out Scr/Lib to Private/Public
Browse files Browse the repository at this point in the history
  • Loading branch information
kilasuit committed Oct 23, 2016
1 parent 785d22e commit 8ffa05f
Show file tree
Hide file tree
Showing 500 changed files with 34,531 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Lability.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
RootModule = 'Lability.psm1';
ModuleVersion = '0.10.1';
ModuleVersion = '0.10.2';
GUID = '374126b4-f3d4-471d-b25e-767f69ee03d0';
Author = 'Iain Brighton';
CompanyName = 'Virtual Engine';
Expand Down
98 changes: 98 additions & 0 deletions Min.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path

$PrivateFunctions = Get-ChildItem "$here\Private\" -Filter '*.ps1' -Recurse | Where-Object {$_.name -NotMatch "Tests.ps1"}
$PublicFunctions = Get-ChildItem "$here\Public\" -Filter '*.ps1' -Recurse | Where-Object {$_.name -NotMatch "Tests.ps1"}

$PrivateFunctionsTests = Get-ChildItem "$here\Private\" -Filter '*Tests.ps1' -Recurse
$PublicFunctionsTests = Get-ChildItem "$here\Public\" -Filter '*Tests.ps1' -Recurse

$Rules = Get-ScriptAnalyzerRule

$manifest = Get-Item "$Here\Lability.psd1"

$module = $manifest.BaseName

Import-Module "$Here\Lability.psd1"

$ModuleData = Get-Module $Module
$AllFunctions = & $moduleData {Param($modulename) Get-command -CommandType Function -Module $modulename} $module

$PublicFunctionPath = "$here\Public\"
$PrivateFunctionPath = "$here\Private\"

$TestsPath = "$here\Tests\"

$CurrentTests = Get-ChildItem $TestsPath -Recurse -File

if ($PrivateFunctions.count -gt 0) {
foreach($PrivateFunction in $PrivateFunctions)
{

Describe "Testing Private Function - $($PrivateFunction.BaseName) for Standard Processing" {

It "Is valid Powershell (Has no script errors)" {

$contents = Get-Content -Path $PrivateFunction.FullName -ErrorAction Stop
$errors = $null
$null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors)
$errors.Count | Should Be 0
}


}

}
}


if ($PublicFunctions.count -gt 0) {

foreach($PublicFunction in $PublicFunctions)
{

Describe "Testing Public Function - $($PublicFunction.BaseName) for Standard Processing" {

It "Is valid Powershell (Has no script errors)" {

$contents = Get-Content -Path $PublicFunction.FullName -ErrorAction Stop
$errors = $null
$null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors)
$errors.Count | Should Be 0
}

}
<#
$function = $AllFunctions.Where{ $_.Name -eq $PublicFunction.BaseName}
$publicfunctionTests = $Publicfunctionstests.Where{$_.Name -match $PublicFunction.BaseName }
foreach ($publicfunctionTest in $publicfunctionTests) {
. $($PublicFunctionTest.FullName) $function
} #>
}
}



Describe 'ScriptAnalyzer Rule Testing' {

Context 'Public Functions' {

It 'Passes the Script Analyzer ' {
(Invoke-ScriptAnalyzer -Path $PublicFunctionPath -Recurse ).Count | Should Be 0

}
}

Context 'Private Functions' {

It 'Passes the Script Analyzer ' {
(Invoke-ScriptAnalyzer -Path $PrivateFunctionPath ).Count | Should Be 0

}
}
}


Foreach ($currentTest in $CurrentTests) {
. $currentTest.FullName
}
16 changes: 16 additions & 0 deletions PesterTest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path


$invokePesterParams = @{
Path = "$here\Min.Tests.ps1";
OutputFile = "$here\TestResult.xml";
OutputFormat = 'NUnitXml';
Strict = $true;
PassThru = $true;
Verbose = $false;
}

$testResult = Invoke-Pester @invokePesterParams;
if ($testResult.FailedCount -gt 0) {
Write-Error ('Failed "{0}" unit tests.' -f $testResult.FailedCount);
}
60 changes: 60 additions & 0 deletions Private/AddDiskImageHotfix.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
function AddDiskImageHotfix {

<#
.SYMOPSIS
Adds a Windows update/hotfix package to an image.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[System.String] $Id,

## Mounted VHD(X) Operating System disk image
[Parameter(Mandatory)]
[ValidateNotNull()]
[System.Object] $Vhd, # Microsoft.Vhd.PowerShell.VirtualHardDisk

## Disk image partition scheme
[Parameter(Mandatory)]
[ValidateSet('MBR','GPT')]
[System.String] $PartitionStyle,

## Lab DSC configuration data
[Parameter(ValueFromPipelineByPropertyName)]
[System.Collections.Hashtable]
[Microsoft.PowerShell.DesiredStateConfiguration.ArgumentToConfigurationDataTransformationAttribute()]
$ConfigurationData
)
process {

if ($PartitionStyle -eq 'MBR') {

$partitionType = 'IFS';
}
elseif ($PartitionStyle -eq 'GPT') {

$partitionType = 'Basic';
}
$vhdDriveLetter = GetDiskImageDriveLetter -DiskImage $Vhd -PartitionType $partitionType;

$resolveLabMediaParams = @{
Id = $Id;
}
if ($PSBoundParameters.ContainsKey('ConfigurationData')) {
$resolveLabMediaParams['ConfigurationData'] = $ConfigurationData;
}
$media = ResolveLabMedia @resolveLabMediaParams;

foreach ($hotfix in $media.Hotfixes) {

$hotfixFileInfo = InvokeLabMediaHotfixDownload -Id $hotfix.Id -Uri $hotfix.Uri;
$packageName = [System.IO.Path]::GetFileNameWithoutExtension($hotfixFileInfo.FullName);

AddDiskImagePackage -Name $packageName -Path $hotfixFileInfo.FullName -DestinationPath $vhdDriveLetter;
}

} #end process

}

54 changes: 54 additions & 0 deletions Private/AddDiskImagePackage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function AddDiskImagePackage {

<#
.SYNOPSIS
Adds a Windows package (.cab) to an image. This is implmented primarily to support injection of
packages into Nano server images.
.NOTES
The real difference between a hotfix and package is that a package can either be specified in the
master VHD(X) image creation OR be injected into VHD(X) differencing disk.
#>
[CmdletBinding()]
param (
## Package name (used for logging)
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.String] $Name,

## File path to the package (.cab) file
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.String] $Path,

## Destination operating system path (mounted VHD), i.e. G:\
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.String] $DestinationPath
)
begin {

## We just want the drive letter
if ($DestinationPath.Length -gt 1) {

$DestinationPath = $DestinationPath.Substring(0,1);
}

}
process {

$logPath = '{0}:\Windows\Logs\{1}' -f $DestinationPath, $labDefaults.ModuleName;
[ref] $null = NewDirectory -Path $logPath -Verbose:$false;

WriteVerbose ($localized.AddingImagePackage -f $Name, $DestinationPath);
$addWindowsPackageParams = @{
PackagePath = $Path;
Path = '{0}:\' -f $DestinationPath;
LogPath = '{0}\{1}.log' -f $logPath, $Name;
LogLevel = 'Errors';
}
[ref] $null = Microsoft.Dism.Powershell\Add-WindowsPackage @addWindowsPackageParams -Verbose:$false;

} #end process

}

46 changes: 46 additions & 0 deletions Private/AddWindowsOptionalFeature.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function AddWindowsOptionalFeature {

<#
.SYMOPSIS
Enables Windows optional features to an image.
#>
[CmdletBinding()]
param (
## Source package file path
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[System.String] $ImagePath,

## Mounted VHD(X) Operating System disk drive
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.String] $DestinationPath,

## Windows packages to add to the image after expansion
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNull()]
[System.String[]] $WindowsOptionalFeature,

## DISM log path
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String] $LogPath = $DestinationPath
)
process {

WriteVerbose ($localized.AddingWindowsFeature -f ($WindowsOptionalFeature -join ','), $DestinationPath);
$enableWindowsOptionalFeatureParams = @{
Source = $ImagePath;
Path = $DestinationPath;
LogPath = $LogPath;
FeatureName = $WindowsOptionalFeature;
LimitAccess = $true;
All = $true;
Verbose = $false;
}
$dismOutput = Microsoft.Dism.Powershell\Enable-WindowsOptionalFeature @enableWindowsOptionalFeatureParams;

} #end process

}

67 changes: 67 additions & 0 deletions Private/AddWindowsPackage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
function AddWindowsPackage {

<#
.SYNOPSIS
Adds a Windows package to an image.
#>
[CmdletBinding()]
param (
## Windows packages (.cab) files to add to the image after expansion
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNull()]
[System.String[]] $Package,

## Path to the .cab files
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.String] $PackagePath,

## Mounted VHD(X) Operating System disk drive
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.String] $DestinationPath,

## Package localization directory/extension (primarily used for Nano Server)
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.String] $PackageLocale = 'en-US',

## DISM log path
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String] $LogPath = $DestinationPath
)
process {

foreach ($packageName in $Package) {

WriteVerbose ($localized.AddingWindowsPackage -f $packagename, $DestinationPath);
$packageFilename = '{0}.cab' -f $packageName;
$packageFilePath = Join-Path -Path $PackagePath -ChildPath $packageFilename;
AddDiskImagePackage -Name $packageName -Path $packageFilePath -DestinationPath $DestinationPath;

## Check for language-specific package (Change from Server 2016 TP releases and Server 2016 Nano RTM)
if ($PSBoundParameters.ContainsKey('PackageLocale')) {

$localizedPackageName = '{0}_{1}' -f $packageName, $packageLocale;
$localizedPackageFilename = '{0}.cab' -f $localizedPackageName;
$localizedPackageDirectoryPath = Join-Path -Path $PackagePath -ChildPath $PackageLocale;
$localizedPackagePath = Join-Path -Path $localizedPackageDirectoryPath -ChildPath $localizedPackageFilename;
if (Test-Path -Path $localizedPackagePath -PathType Leaf) {

WriteVerbose ($localized.AddingLocalizedWindowsPackage -f $localizedPackageName, $DestinationPath);
$addDiskImagePackageParams = @{
Name = $localizedPackageName;
Path = $localizedPackagePath;
DestinationPath = $DestinationPath;
}
AddDiskImagePackage @addDiskImagePackageParams;
}
}

} #end foreach package

} #end process

}

22 changes: 22 additions & 0 deletions Private/CloseGitHubZipArchive.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function CloseGitHubZipArchive {

<#
.SYNOPSIS
Tidies up and closes Zip Archive and file handles
#>
[CmdletBinding()]
param ()
process {

Write-Verbose ($localized.ClosingZipArchive -f $Path);
if ($null -ne $zipArchive) {
$zipArchive.Dispose();
}
if ($null -ne $fileStream) {
$fileStream.Close();
}

} # end process

}

Loading

0 comments on commit 8ffa05f

Please sign in to comment.