From f8998428da22a6635d703210a6452ea6c0edd23f Mon Sep 17 00:00:00 2001 From: Stephane van Gulick Date: Sun, 5 Mar 2023 23:05:11 +0100 Subject: [PATCH 1/7] added first improvements on includes --- PSHTML/PSHTML.psm1 | 47 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/PSHTML/PSHTML.psm1 b/PSHTML/PSHTML.psm1 index c3c8817..9669253 100644 --- a/PSHTML/PSHTML.psm1 +++ b/PSHTML/PSHTML.psm1 @@ -15,6 +15,11 @@ Enum AssetType { Class ConfigurationDocument { [System.IO.FileInfo]$Path = "$PSScriptRoot/pshtml.configuration.json" + [system.Io.FileInfo]$ModuleFolder = $PSScriptRoot + [System.IO.DirectoryInfo]$IncludesProjectFolderPath + [System.IO.DirectoryInfo]$AssetsProjectFolderPath + [System.IO.DirectoryInfo]$IncludesModuleFolderPath + [System.IO.DirectoryInfo]$AssetsModuleFolderPath [Setting[]]$Settings [Asset[]]$Assets [Include[]]$Includes @@ -38,6 +43,11 @@ Class ConfigurationDocument { $ProjectRootFolder = $ec.SessionState.Path.CurrentLocation.Path $ModuleFolder = $This.Path.Directory + + $pfp = [System.IO.FileInfo]$script:MyInvocation.PSCommandPath + $this.SetIncludesProjectFolderPath($pfp.Directory) + + #Assets $ModuleAssetsFolder = Join-Path $ModuleFolder -ChildPath "Assets" $ProjectAssetsFolder = Join-Path $ProjectRootFolder -ChildPath "Assets" @@ -69,7 +79,7 @@ Class ConfigurationDocument { $this.Includes = [IncludeFactory]::Create($IncludesFolder) $ModuleIncludesFolder = Join-Path $ModuleFolder -ChildPath "Includes" - $ProjectIncludesFolder = Join-Path $ProjectRootFolder -ChildPath "Assets" + $ProjectIncludesFolder = Join-Path $ProjectRootFolder -ChildPath "Includes" $ModuleIncludes = [IncludeFactory]::Create($ModuleIncludesFolder) $ProjectIncludes = [IncludeFactory]::Create($ProjectIncludesFolder) @@ -134,9 +144,6 @@ Class ConfigurationDocument { } - [void]hidden LoadLogSettings(){ - - } [String]GetDefaultLogFilePath(){ return $this.GetSetting("Log").GetLogfilePath() @@ -150,6 +157,21 @@ Class ConfigurationDocument { Return $this.Includes | ? {$_.Name -eq $Name} } + [void]SetIncludesProjectFolderPath([System.IO.DirectoryInfo]$IncludesProjectFolderPath){ + $this.IncludesProjectFolderPath = $IncludesProjectFolderPath + } + + [void]SetIncludesModuleFolderPath([System.IO.DirectoryInfo]$IncludesModuleFolderPath){ + $this.IncludesModuleFolderPath = $IncludesModuleFolderPath + } + + [void]SetAssetsProjectFolderPath([System.IO.DirectoryInfo]$AssetsProjectFolderPath){ + $this.AssetsProjectFolderPath = $AssetsProjectFolderPath + } + + [void]SetIncludesModuleFolderPath([System.IO.DirectoryInfo]$AssetsModuleFolderPath){ + $this.AssetsModuleFolderPath = $AssetsModuleFolderPath + } } Class Setting{ @@ -1635,11 +1657,11 @@ Class polarAreaChart : Chart{ #endregion -Class IncludeFile { +Class Include { } -Class Include : IncludeFile { +Class IncludeFile : Include { [String]$Name [System.IO.DirectoryInfo]$FolderPath [System.IO.FileInfo]$FilePath @@ -1652,6 +1674,15 @@ Class Include : IncludeFile { [String]ToString(){ + #Tostring() shouold not execute code in a tostring methode. shall be replaced by render() + $Rawcontent = [IO.File]::ReadAllText($this.FilePath.FullName) + $Content = [scriptBlock]::Create($Rawcontent).Invoke() + return $content + + } + + [String]Render(){ + $Rawcontent = [IO.File]::ReadAllText($this.FilePath.FullName) $Content = [scriptBlock]::Create($Rawcontent).Invoke() return $content @@ -1662,12 +1693,12 @@ Class Include : IncludeFile { Class IncludeFactory { Static [Include[]] Create([System.IO.DirectoryInfo]$Path){ - If(test-Path $Path){ + If(test-Path $Path.FullName){ $Items = Get-ChildItem $Path.FullName -Filter "*.ps1" $AllIncludes = @() Foreach($Item in $Items){ - $AllIncludes += [Include]::New($Item) + $AllIncludes += [IncludeFile]::New($Item) } From 94af29ffb092c1378a52d65d82a0428d7e4fd84b Mon Sep 17 00:00:00 2001 From: Stephane van Gulick Date: Mon, 6 Mar 2023 22:54:27 +0100 Subject: [PATCH 2/7] working solution --- PSHTML/PSHTML.psm1 | 115 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 106 insertions(+), 9 deletions(-) diff --git a/PSHTML/PSHTML.psm1 b/PSHTML/PSHTML.psm1 index 9669253..aa4d2bb 100644 --- a/PSHTML/PSHTML.psm1 +++ b/PSHTML/PSHTML.psm1 @@ -16,6 +16,7 @@ Class ConfigurationDocument { [System.IO.FileInfo]$Path = "$PSScriptRoot/pshtml.configuration.json" [system.Io.FileInfo]$ModuleFolder = $PSScriptRoot + [System.IO.DirectoryInfo]$ProjectFolderPath [System.IO.DirectoryInfo]$IncludesProjectFolderPath [System.IO.DirectoryInfo]$AssetsProjectFolderPath [System.IO.DirectoryInfo]$IncludesModuleFolderPath @@ -41,19 +42,30 @@ Class ConfigurationDocument { $EC = Get-Variable ExecutionContext -ValueOnly $ProjectRootFolder = $ec.SessionState.Path.CurrentLocation.Path - $ModuleFolder = $This.Path.Directory + $mf = $This.Path.Directory $pfp = [System.IO.FileInfo]$script:MyInvocation.PSCommandPath - $this.SetIncludesProjectFolderPath($pfp.Directory) + $this.ProjectFolderPath = $pfp.Directory + $this.LoadIncludes() + $this.loadAssets() + <# - + [System.IO.DirectoryInfo]$IncludesFP = join-PAth -Path $this.ProjectFolderPath -ChildPath "Includes" + if($IncludesFP.Exists){ + $this.SetIncludesProjectFolderPath($IncludesFP) + } + + [System.IO.DirectoryInfo]$AssetsFP = join-Path -Path $this.ProjectFolderPath -ChildPath "Assets" + if($AssetsFP.Exists){ + $this.SetAssetsProjectFolderPath($AssetsFP) + } #Assets - $ModuleAssetsFolder = Join-Path $ModuleFolder -ChildPath "Assets" + $ModuleAssetsFolder = Join-Path $mf -ChildPath "Assets" $ProjectAssetsFolder = Join-Path $ProjectRootFolder -ChildPath "Assets" $ModuleAssets = [AssetsFactory]::CreateAsset($ModuleAssetsFolder) - $ProjectAssets = [AssetsFactory]::CreateAsset($ProjectAssetsFolder) + $ProjectAssets = [AssetsFactory]::CreateAsset($this.AssetsProjectFolderPath) $this.Assets += $ProjectAssets @@ -78,11 +90,11 @@ Class ConfigurationDocument { $IncludesFolder = Join-Path -Path $ProjectRootFolder -ChildPath "Includes" $this.Includes = [IncludeFactory]::Create($IncludesFolder) - $ModuleIncludesFolder = Join-Path $ModuleFolder -ChildPath "Includes" + $ModuleIncludesFolder = Join-Path $mf -ChildPath "Includes" $ProjectIncludesFolder = Join-Path $ProjectRootFolder -ChildPath "Includes" $ModuleIncludes = [IncludeFactory]::Create($ModuleIncludesFolder) - $ProjectIncludes = [IncludeFactory]::Create($ProjectIncludesFolder) + $ProjectIncludes = [IncludeFactory]::Create($this.IncludesProjectFolderPath) $this.Includes += $ProjectIncludes @@ -101,6 +113,83 @@ Class ConfigurationDocument { $This.Includes += $modinc } } + #> + } + + [void]LoadIncludes(){ + + + $ModuleIncludesFolder = Join-Path $this.Path.Directory -ChildPath "Includes" + $ModuleIncludes = [IncludeFactory]::Create($ModuleIncludesFolder) + + if($ModuleIncludes){ + #At the moment the configuration object is created BEFORE the logobject. + #write-verbose "[ConfigurationOBject][LoadIncludes()] Project includes found at $($this.IncludesProjectFolderPath)" + #$this.Includes = $ModuleIncludes + } + + [System.IO.DirectoryInfo]$IncludesFP = join-PAth -Path $this.ProjectFolderPath -ChildPath "Includes" + + if($IncludesFP.Exists){ + $this.SetIncludesProjectFolderPath($IncludesFP) + $projectIncludes = [IncludeFactory]::Create($this.IncludesProjectFolderPath) + if($projectIncludes){ + #write-verbose "[ConfigurationOBject][LoadIncludes()] Project includes found at $($this.IncludesProjectFolderPath)" + $this.Includes = $projectIncludes + } + } + + + + foreach ($modinc in $ModuleIncludes){ + if($this.Includes.name -contains $modinc.name){ + + $PotentialConflictingInclude = $this.Includes | ? {$_.Name -eq $modinc.Name} + if($PotentialConflictingInclude.Type -eq $modinc.type){ + + #write-verbose "Identical asset found at $($modinc.name). Keeping project asset." + Continue + } + + Continue + }else{ + $This.Includes += $modinc + } + } + + + + } + + [Void]LoadAssets(){ + + $ModuleAssetsFolder = Join-Path $this.Path.Directory -ChildPath "Assets" + + $ProjectAssetsFolder = Join-Path $this.ProjectFolderPath -ChildPath "Assets" + + $ModuleAssets = [AssetsFactory]::CreateAsset($ModuleAssetsFolder) + + if(Test-Path $ProjectAssetsFolder){ + + $this.Assets = [AssetsFactory]::CreateAsset($ProjectAssetsFolder) + } + + foreach ($modass in $ModuleAssets){ + if($this.Assets.name -contains $modass.name){ + + $PotentialConflictingAsset = $this.Assets | ? {$_.Name -eq $modass.Name} + if($PotentialConflictingAsset.Type -eq $modass.type){ + + #write-verbose "Identical asset found at $($modass.name). Keeping project asset." + Continue + }else{ + $This.Assets += $modass + } + }else{ + $This.Assets += $modass + } + } + #> } [void]Load([System.IO.FileInfo]$Path){ @@ -169,9 +258,17 @@ Class ConfigurationDocument { $this.AssetsProjectFolderPath = $AssetsProjectFolderPath } - [void]SetIncludesModuleFolderPath([System.IO.DirectoryInfo]$AssetsModuleFolderPath){ + [void]SetAssetsModuleFolderPath([System.IO.DirectoryInfo]$AssetsModuleFolderPath){ $this.AssetsModuleFolderPath = $AssetsModuleFolderPath } + + [bool]HasAssetsProjectFolder(){ + return $this.AssetsProjectFolderPath.Exists + } + + [bool] HasIncludesProjectFolder(){ + return $this.IncludesProjectFolderPath.Exists + } } Class Setting{ @@ -1666,7 +1763,7 @@ Class IncludeFile : Include { [System.IO.DirectoryInfo]$FolderPath [System.IO.FileInfo]$FilePath - Include([System.IO.FileInfo]$FilePath){ + IncludeFile([System.IO.FileInfo]$FilePath){ $this.FilePath = $FilePath $this.FolderPath = $FilePath.Directory $this.Name = $FilePath.BaseName From b4aa0a4dc0e685bbcae7fd8563ae58ec1de4ab19 Mon Sep 17 00:00:00 2001 From: StephaneVg Date: Tue, 16 Jul 2024 12:37:52 +0200 Subject: [PATCH 3/7] Fixed typos in Test --- Tests/Chart.New-PSHTMLChart.Tests.ps1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/Chart.New-PSHTMLChart.Tests.ps1 b/Tests/Chart.New-PSHTMLChart.Tests.ps1 index 3aa7019..550de99 100644 --- a/Tests/Chart.New-PSHTMLChart.Tests.ps1 +++ b/Tests/Chart.New-PSHTMLChart.Tests.ps1 @@ -174,11 +174,11 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart Add-Type -Assembly System.Drawing $bds = New-PSHTMLChartRadarDataSet -Data $data1 -label "2018" -borderColor (get-pshtmlColor -color blue) -backgroundColor "transparent" -hoverBackgroundColor (get-pshtmlColor -color green) -PointRadius 2 - it '[New-PSHTMLChart][-Type Radar][-DataSet BarDataSet][Label][Title][CanvasId] Should not throw' { + it '[New-PSHTMLChart][-Type Radar][-DataSet ChartRadarDataSet][Label][Title][CanvasId] Should not throw' { {New-PSHTMLChart -Type Radar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID} | should not throw } - it '[New-PSHTMLChart][-Type Radar][-DataSet BarDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { + it '[New-PSHTMLChart][-Type Radar][-DataSet ChartRadarDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { $Is = New-PSHTMLChart -Type radar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID If($PSVersionTable.PsEdition -eq 'Core'){ @@ -217,11 +217,11 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart } $bds = New-PSHTMLChartPolarAreaDataSet -Data $Data -BackgroundColor $BackgroundColor -label $Labels - it '[New-PSHTMLChart][-Type polarArea][-DataSet BarDataSet][Label][Title][CanvasId] Should not throw' { + it '[New-PSHTMLChart][-Type polarArea][-DataSet PolarAreaDataSet][Label][Title][CanvasId] Should not throw' { {New-PSHTMLChart -Type polarArea -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID} | should not throw } - it '[New-PSHTMLChart][-Type polarArea][-DataSet BarDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { + it '[New-PSHTMLChart][-Type polarArea][-DataSet PolarAreaDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { $Is = New-PSHTMLChart -Type polarArea -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID <# @@ -355,11 +355,11 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart $TestData = New-PSHTMLChartDoughnutDataSet -Data $data1 -label "March" -backgroundcolor $colors - it '[New-PSHTMLChart][-Type Doughnut][-DataSet PieDataSet][Label][Title][CanvasId] Should not throw' { + it '[New-PSHTMLChart][-Type Doughnut][-DataSet DoughnutDataSet][Label][Title][CanvasId] Should not throw' { {New-PSHTMLChart -Type Doughnut -DataSet $TestData -Labels $Labels -Title $Title -CanvasID $CanvasID} | should not throw } - it '[New-PSHTMLChart][-Type Doughnut][-DataSet PieDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { + it '[New-PSHTMLChart][-Type Doughnut][-DataSet DoughnutDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { $Is = New-PSHTMLChart -Type Doughnut -DataSet $TestData -Labels $Labels -Title $Title -CanvasID $CanvasID #$Is = $Is.Trim() From 9db507a080889df14913c6d6518d7b2c3215192e Mon Sep 17 00:00:00 2001 From: StephaneVg Date: Tue, 16 Jul 2024 12:39:19 +0200 Subject: [PATCH 4/7] Removed old unecessary comments --- Tests/Chart.New-PSHTMLChart.Tests.ps1 | 107 +------------------------- 1 file changed, 3 insertions(+), 104 deletions(-) diff --git a/Tests/Chart.New-PSHTMLChart.Tests.ps1 b/Tests/Chart.New-PSHTMLChart.Tests.ps1 index 550de99..3263ad3 100644 --- a/Tests/Chart.New-PSHTMLChart.Tests.ps1 +++ b/Tests/Chart.New-PSHTMLChart.Tests.ps1 @@ -207,10 +207,7 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart $Title = "Test Title" $CanvasID = "TestCanvasID" $BackgroundColor = @('red', 'green', 'yellow', 'grey', 'blue') - #$bds = - <# mock -CommandName New-PSHTMLChartBarDataSet -MockWith { - New-MockObject -Type "datasetbar" - } #> + it '[New-PSHTMLChartPolarAreaDataSet][-Data $Data][-BackgroundColor $BackgroundColor][-label $Labels] Should not throw' { { New-PSHTMLChartPolarAreaDataSet -Data $Data -BackgroundColor $BackgroundColor -label $Labels } | should not throw @@ -223,12 +220,7 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart it '[New-PSHTMLChart][-Type polarArea][-DataSet PolarAreaDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { $Is = New-PSHTMLChart -Type polarArea -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID -<# -$Should = @' - -'@ -#> $Should = @' var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"polarArea","data":{"labels":["red","green","yellow","grey","blue"],"datasets":[{"borderWidth":1,"backgroundColor":["red","green","yellow","grey","blue"],"borderColor":[""],"borderSkipped":null,"hoverBackgroundColor":[""],"hoverBorderColor":[""],"hoverBorderWidth":0,"data":[3,5,7,2,9],"label":["red","green","yellow","grey","blue"]}]},"options":{"scales":null,"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); @@ -250,10 +242,7 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart $Data = @(3, 5) $Title = "Test Title" $CanvasID = "TestCanvasID" - #$bds = - <# mock -CommandName New-PSHTMLChartBarDataSet -MockWith { - New-MockObject -Type "datasetbar" - } #> + $TestData = New-PSHTMLChartPieDataSet -Data $Data it '[New-PSHTMLChart][-Type Pie][-DataSet PieDataSet][Label][Title][CanvasId] Should not throw' { @@ -471,94 +460,9 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart it '[New-PSHTMLChart][-Type Line][-DataSet LineDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { $Is = New-PSHTMLChart -Type Line -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID #don't touche this part, as the regex is very 'fragile' -<# -$Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); -var myChart = new Chart(ctx, { - "type": "line", - "data": { - "labels": [ - "january", - "february" - ], - "datasets": [ - { - "borderWidth": 1, - "borderDash": [ - 0 - ], - "borderDashOffSet": 0, - "cubicInterpolationMode": "default", - "fill": false, - "lineTension": 0.5, - "pointBackgroundColor": "rgb(255,255,255)", - "pointBorderColor": "rgb(0,0,0)", - "pointBorderWidth": [ - 1 - ], - "pointRadius": 4, - "pointStyle": "circle", - "showLine": true, - "backgroundColor": null, - "borderColor": null, - "borderCapStyle": null, - "borderJoinStyle": null, - "pointRotation": null, - "pointHitRadius": null, - "PointHoverBackgroundColor": null, - "pointHoverBorderColor": null, - "pointHoverBorderWidth": 0, - "pointHoverRadius": null, - "spanGaps": false, - "data": [ - - ], - "label": null - } - ] - }, - "options": { - "showLines": true, - "spanGaps": false, - "barPercentage": 1, - "categoryPercentage": 1, - "responsive": false, - "barThickness": null, - "maxBarThickness": 0, - "offsetGridLines": true, - "scales": { - "yAxes": [ - { - "ticks": { - "beginAtZero": true - } - } - ], - "xAxes": [ - - ] - }, - "title": { - "display": true, - "text": "Test Title" - }, - "animation": { - "onComplete":null - } - } -} -); -'@ -#> If($PSVersionTable.PsEdition -eq 'Core'){ - <# - - $Should = @' - -'@ - #> $Should = @' var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"line","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"borderDash":[0],"borderDashOffSet":0,"cubicInterpolationMode":"default","fill":false,"lineTension":0.5,"pointBackgroundColor":"rgb(255,255,255)","pointBorderColor":"rgb(0,0,0)","pointBorderWidth":[1],"pointRadius":4.0,"pointStyle":"circle","showLine":true,"backgroundColor":null,"borderColor":null,"borderCapStyle":null,"borderJoinStyle":null,"pointRotation":null,"pointHitRadius":0.0,"PointHoverBackgroundColor":null,"pointHoverBorderColor":null,"pointHoverBorderWidth":0,"pointHoverRadius":0.0,"spanGaps":false,"data":[3,5],"label":null}]},"options":{"showLines":true,"spanGaps":false,"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); @@ -566,12 +470,7 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart }else{ - <# - - $Should = @' - -'@ - #> + $Should = @' var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"line","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"borderDash":[0],"borderDashOffSet":0,"cubicInterpolationMode":"default","fill":false,"lineTension":0.5,"pointBackgroundColor":"rgb(255,255,255)","pointBorderColor":"rgb(0,0,0)","pointBorderWidth":[1],"pointRadius":4,"pointStyle":"circle","showLine":true,"backgroundColor":null,"borderColor":null,"borderCapStyle":null,"borderJoinStyle":null,"pointRotation":null,"pointHitRadius":0,"PointHoverBackgroundColor":null,"pointHoverBorderColor":null,"pointHoverBorderWidth":0,"pointHoverRadius":0,"spanGaps":false,"data":[3,5],"label":null}]},"options":{"showLines":true,"spanGaps":false,"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); From 22d158ae70fa4c3aa0f11dca698b2c9278bc17aa Mon Sep 17 00:00:00 2001 From: StephaneVg Date: Tue, 16 Jul 2024 12:48:29 +0200 Subject: [PATCH 5/7] Removed unecessar comments --- Tests/Chart.New-PSHTMLChart.Tests.ps1 | 240 +------------------------- 1 file changed, 3 insertions(+), 237 deletions(-) diff --git a/Tests/Chart.New-PSHTMLChart.Tests.ps1 b/Tests/Chart.New-PSHTMLChart.Tests.ps1 index 3263ad3..b293d04 100644 --- a/Tests/Chart.New-PSHTMLChart.Tests.ps1 +++ b/Tests/Chart.New-PSHTMLChart.Tests.ps1 @@ -1,7 +1,5 @@ $TestsPath = Split-Path $MyInvocation.MyCommand.Path -#$FunctionsPath = join-Path -Path (get-item $TestsPath).Parent -ChildPath "Functions" - $RootFolder = (get-item $TestsPath).Parent Push-Location -Path $RootFolder.FullName @@ -28,10 +26,7 @@ InModuleScope PSHTML { $Data = @(3, 5) $Title = "Test Title" $CanvasID = "TestCanvasID" - #$bds = - <# mock -CommandName New-PSHTMLChartBarDataSet -MockWith { - New-MockObject -Type "datasetbar" - } #> + $bds = New-PSHTMLChartBarDataSet -Data $Data it '[New-PSHTMLChart][-Type Bar][-DataSet BarDataSet][Label][Title][CanvasId] Should not throw' { @@ -42,72 +37,6 @@ InModuleScope PSHTML { $Is = New-PSHTMLChart -Type bar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID #don't touche this part, as the regex is very 'fragile' - <# - $Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); -var myChart = new Chart(ctx, { - "type": "bar", - "data": { - "labels": [ - "january", - "february" - ], - "datasets": [ - { - "borderWidth": 1, - "xAxisID": null, - "yAxisID": null, - "backgroundColor": null, - "borderColor": null, - "borderSkipped": null, - "hoverBackgroundColor": null, - "hoverBorderColor": null, - "hoverBorderWidth": 0, - "data": [ - - ], - "label": null - } - ] - }, - "options": { - "barPercentage": 1, - "categoryPercentage": 1, - "responsive": false, - "barThickness": null, - "maxBarThickness": 0, - "offsetGridLines": true, - "scales": { - "yAxes": [ - { - "ticks": { - "beginAtZero": true - } - } - ], - "xAxes": [ - - ] - }, - "title": { - "display": true, - "text": "Test Title" - }, - "animation": { - "onComplete":null - } - } -} -); -'@ -#> - -<# - -$Should = @' - -'@ -#> $Should = @' var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"bar","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"xAxisID":null,"yAxisID":null,"backgroundColor":null,"borderColor":"","borderSkipped":null,"hoverBackgroundColor":null,"hoverBorderColor":null,"hoverBorderWidth":0,"data":[3,5],"label":null}]},"options":{"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); @@ -128,10 +57,7 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart $Data = @(3, 5) $Title = "Test Title" $CanvasID = "TestCanvasID" - #$bds = - <# mock -CommandName New-PSHTMLChartBarDataSet -MockWith { - New-MockObject -Type "datasetbar" - } #> + $bds = New-PSHTMLChartBarDataSet -Data $Data it '[New-PSHTMLChart][-Type horizontalBar][-DataSet BarDataSet][Label][Title][CanvasId] Should not throw' { @@ -141,13 +67,6 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart it '[New-PSHTMLChart][-Type horizontalBar][-DataSet BarDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { $Is = New-PSHTMLChart -Type horizontalBar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID -<# - -$Should = @' - -'@ -#> - $Should = @' var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"horizontalBar","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"xAxisID":null,"yAxisID":null,"backgroundColor":null,"borderColor":"","borderSkipped":null,"hoverBackgroundColor":null,"hoverBorderColor":null,"hoverBorderWidth":0,"data":[3,5],"label":null}]},"options":{"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); '@ @@ -225,9 +144,7 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart $Should = @' var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"polarArea","data":{"labels":["red","green","yellow","grey","blue"],"datasets":[{"borderWidth":1,"backgroundColor":["red","green","yellow","grey","blue"],"borderColor":[""],"borderSkipped":null,"hoverBackgroundColor":[""],"hoverBorderColor":[""],"hoverBorderWidth":0,"data":[3,5,7,2,9],"label":["red","green","yellow","grey","blue"]}]},"options":{"scales":null,"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); '@ - - #$Is | should be $Should - + $Is | should be $Should } @@ -251,74 +168,6 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart it '[New-PSHTMLChart][-Type Bar][-DataSet PieDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { $IsTemp = New-PSHTMLChart -Type Pie -DataSet $TestData -Labels $Labels -Title $Title -CanvasID $CanvasID - #$Is = $IsTemp.Trim() - <# - $Should =@' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); -var myChart = new Chart(ctx, { - "type": "pie", - "data": { - "labels": [ - "january", - "february" - ], - "datasets": [ - { - "borderColor": "white", - "borderWidth": 1, - "backgroundColor": null, - "hoverBackgroundColor": [ - null - ], - "HoverBorderColor": null, - "HoverBorderWidth": 0, - "data": [ - 3, - 5 - ], - "label": null - } - ] - }, - "options": { - "barPercentage": 1, - "categoryPercentage": 1, - "responsive": false, - "barThickness": null, - "maxBarThickness": 0, - "offsetGridLines": true, - "scales": { - "yAxes": [ - { - "ticks": { - "beginAtZero": true - } - } - ], - "xAxes": [ - - ] - }, - "title": { - "display": true, - "text": "Test Title" - }, - "animation": { - "onComplete":null - } - } -} -); -'@ - -#> - -<# - -$Should = @' - -'@ -#> $Should = @' var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"pie","data":{"labels":["january","february"],"datasets":[{"borderColor":"white","borderWidth":1,"backgroundColor":null,"hoverBackgroundColor":[null],"HoverBorderColor":null,"HoverBorderWidth":0,"data":[3,5],"label":null}]},"options":{"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); @@ -337,10 +186,6 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart $Labels = @("Closed","Unresolved","Pending","Open") $colors = @("LightGreen","Red","LightBlue","LightYellow") - #$bds = - <# mock -CommandName New-PSHTMLChartBarDataSet -MockWith { - New-MockObject -Type "datasetbar" - } #> $TestData = New-PSHTMLChartDoughnutDataSet -Data $data1 -label "March" -backgroundcolor $colors @@ -351,85 +196,6 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart it '[New-PSHTMLChart][-Type Doughnut][-DataSet DoughnutDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { $Is = New-PSHTMLChart -Type Doughnut -DataSet $TestData -Labels $Labels -Title $Title -CanvasID $CanvasID - #$Is = $Is.Trim() - <# -$Should =@' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); -var myChart = new Chart(ctx, { - "type": "doughnut", - "data": { - "labels": [ - "Closed", - "Unresolved", - "Pending", - "Open" - ], - "datasets": [ - { - "borderColor": "white", - "borderWidth": 1, - "backgroundColor": [ - "LightGreen", - "Red", - "LightBlue", - "LightYellow" - ], - "hoverBackgroundColor": [ - "LightGreen", - "Red", - "LightBlue", - "LightYellow" - ], - "HoverBorderColor": null, - "HoverBorderWidth": 0, - "data": [ - 34, - 7, - 11, - 19 - ], - "label": "March" - } - ] - }, - "options": { - "barPercentage": 1, - "categoryPercentage": 1, - "responsive": false, - "barThickness": null, - "maxBarThickness": 0, - "offsetGridLines": true, - "scales": { - "yAxes": [ - { - "ticks": { - "beginAtZero": true - } - } - ], - "xAxes": [ - - ] - }, - "title": { - "display": true, - "text": "Test Title" - }, - "animation": { - "onComplete":null - } - } -} -); -'@ -#> - -<# - -$Should = @' - -'@ -#> $Should = @' var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"doughnut","data":{"labels":["Closed","Unresolved","Pending","Open"],"datasets":[{"borderColor":"white","borderWidth":1,"backgroundColor":["LightGreen","Red","LightBlue","LightYellow"],"hoverBackgroundColor":["LightGreen","Red","LightBlue","LightYellow"],"HoverBorderColor":null,"HoverBorderWidth":0,"data":[34,7,11,19],"label":["March"]}]},"options":{"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); From c0196bf15bda3f8406178c23a9c4caa1d8099d74 Mon Sep 17 00:00:00 2001 From: StephaneVg Date: Tue, 12 Nov 2024 21:25:06 +0100 Subject: [PATCH 6/7] Fixed Includes Type And added support for -AsContent for Assets --- PSHTML/Examples/Example19/AssetContent.ps1 | 29 +++ PSHTML/PSHTML.psd1 | 4 +- PSHTML/PSHTML.psm1 | 246 ++++++++++--------- Tests/Pshtml.Utilities.Assets.Unit.Tests.Ps1 | 28 ++- Tests/Write-PSHTMLInclude.Tests.ps1 | 92 ++++--- Tests/pshtml.components.includes.Tests.Ps1 | 71 +++--- 6 files changed, 285 insertions(+), 185 deletions(-) create mode 100644 PSHTML/Examples/Example19/AssetContent.ps1 diff --git a/PSHTML/Examples/Example19/AssetContent.ps1 b/PSHTML/Examples/Example19/AssetContent.ps1 new file mode 100644 index 0000000..240875d --- /dev/null +++ b/PSHTML/Examples/Example19/AssetContent.ps1 @@ -0,0 +1,29 @@ +# This example highlight how to write assets directly as content. +# This is useful when you want to write the asset content directly in the HTML document so that they can be shared with all the dynamic content. +import-module pshtml -Force + +$html = html { + head { + title "Asset Content" + #Use -AsContent to write the asset content directly in the HTML document + Write-PSHTMLAsset -AsContent + } + body { + h1 "Asset Content" + p "This is the asset content" + $PieCanvasID = "piecanvas" + canvas -Height 400px -Width 400px -Id $PieCanvasID { + + } + Script -content { + + $Data1 = @(34,7,11,19) + $Labels = @("Closed","Unresolved","Pending","Open") + $colors = @("Lightgreen","red","Blue","Yellow") + $dsp1 = New-PSHTMLChartPieDataSet -Data $data1 -label "March" -BackgroundColor $colors + New-PSHTMLChart -type pie -DataSet $dsp1 -title "Pie Chart v2" -Labels $Labels -CanvasID $PieCanvasID + } + } +} + +Out-PSHTMLDocument -HTMLDocument $html -OutPath "C:\Users\Stephane\Code\PSHTML\PSHTML\Examples\Example19\AssetContent.html" -Show \ No newline at end of file diff --git a/PSHTML/PSHTML.psd1 b/PSHTML/PSHTML.psd1 index cbc1b91..8ddb3a9 100644 --- a/PSHTML/PSHTML.psd1 +++ b/PSHTML/PSHTML.psd1 @@ -12,7 +12,7 @@ RootModule = 'PSHTML.psm1' # Version number of this module. -ModuleVersion = '0.8.2' +ModuleVersion = '0.9.0' # Supported PSEditions # CompatiblePSEditions = @() @@ -128,7 +128,7 @@ PrivateData = @{ ReleaseNotes = 'https://github.com/Stephanevg/PSHTML/blob/master/Change_Log.md' # Prerelease string of this module - # Prerelease = '' + Prerelease = 'alpha' # Flag to indicate whether the module requires explicit user acceptance for install/update/save # RequireLicenseAcceptance = $false diff --git a/PSHTML/PSHTML.psm1 b/PSHTML/PSHTML.psm1 index aa4d2bb..f4af718 100644 --- a/PSHTML/PSHTML.psm1 +++ b/PSHTML/PSHTML.psm1 @@ -12,6 +12,11 @@ Enum AssetType { cdn } +Enum LocationType { + Module + Project +} + Class ConfigurationDocument { [System.IO.FileInfo]$Path = "$PSScriptRoot/pshtml.configuration.json" @@ -37,117 +42,67 @@ Class ConfigurationDocument { #Methods [void]Load(){ + #Reminder: Logger CANNOT be used during the load process.(This includes the LoadIncludes() and LoadAssets() methods) #Read data from json $this.Settings = [SettingFactory]::Parse($This.Path) + <# $EC = Get-Variable ExecutionContext -ValueOnly $ProjectRootFolder = $ec.SessionState.Path.CurrentLocation.Path - $mf = $This.Path.Directory + #> + $ScriptCaller = $Script:MyInvocation.PSCommandPath - $pfp = [System.IO.FileInfo]$script:MyInvocation.PSCommandPath - $this.ProjectFolderPath = $pfp.Directory - $this.LoadIncludes() - $this.loadAssets() - <# - - [System.IO.DirectoryInfo]$IncludesFP = join-PAth -Path $this.ProjectFolderPath -ChildPath "Includes" - if($IncludesFP.Exists){ - $this.SetIncludesProjectFolderPath($IncludesFP) - } - - [System.IO.DirectoryInfo]$AssetsFP = join-Path -Path $this.ProjectFolderPath -ChildPath "Assets" - if($AssetsFP.Exists){ - $this.SetAssetsProjectFolderPath($AssetsFP) + if($null -ne $ScriptCaller){ + $ProjectRootFolder = Split-Path -Parent -Path $ScriptCaller + $this.ProjectFolderPath = $ProjectRootFolder + #Write-Verbose "[ConfiguratinDocument][Load()] Project folder found at $($this.ProjectFolderPath.FullName)" } - #Assets - $ModuleAssetsFolder = Join-Path $mf -ChildPath "Assets" - $ProjectAssetsFolder = Join-Path $ProjectRootFolder -ChildPath "Assets" - - $ModuleAssets = [AssetsFactory]::CreateAsset($ModuleAssetsFolder) - $ProjectAssets = [AssetsFactory]::CreateAsset($this.AssetsProjectFolderPath) - $this.Assets += $ProjectAssets - - foreach ($modass in $ModuleAssets){ - if($this.Assets.name -contains $modass.name){ - - $PotentialConflictingAsset = $this.Assets | ? {$_.Name -eq $modass.Name} - if($PotentialConflictingAsset.Type -eq $modass.type){ + $this.LoadIncludes() + $this.loadAssets() + } - #write-verbose "Identical asset found at $($modass.name). Keeping project asset." - Continue - }else{ - $This.Assets += $modass - } - }else{ - $This.Assets += $modass - } + [void]LoadIncludes(){ + #Reminder: Logger CANNOT be used during the load process.(This includes LoadAssets() and Load() methods) + $ModuleIncludes = $null + [System.IO.DirectoryInfo]$IncludeModuleFolder = Join-Path $this.Path.Directory -ChildPath "Includes" + if($IncludeModuleFolder.Exists){ + $This.IncludesModuleFolderPath = $IncludeModuleFolder + $ModuleIncludes = [IncludeFactory]::Create($this.IncludesModuleFolderPath) + + foreach($ModInc in $ModuleIncludes){ + $ModInc.LocationType = [LocationType]::Module + #write-verbose "[ConfigurationOBject][LoadIncludes()] -> Module includes $($ModInc.Name)" } + } - #Includes - #$IncludesFolder = Join-Path -Path $ExecutionContext.SessionState.Path.CurrentLocation.Path -ChildPath "Includes" #Join-Path $this.Path.Directory -ChildPath 'Includes' - $IncludesFolder = Join-Path -Path $ProjectRootFolder -ChildPath "Includes" - $this.Includes = [IncludeFactory]::Create($IncludesFolder) - - $ModuleIncludesFolder = Join-Path $mf -ChildPath "Includes" - $ProjectIncludesFolder = Join-Path $ProjectRootFolder -ChildPath "Includes" - - $ModuleIncludes = [IncludeFactory]::Create($ModuleIncludesFolder) - $ProjectIncludes = [IncludeFactory]::Create($this.IncludesProjectFolderPath) - - $this.Includes += $ProjectIncludes - foreach ($modinc in $ModuleIncludes){ - if($this.Includes.name -contains $modinc.name){ - - $PotentialConflictingInclude = $this.Includes | ? {$_.Name -eq $modinc.Name} - if($PotentialConflictingInclude.Type -eq $modinc.type){ + if($this.ProjectFolderPath.Exists){ - #write-verbose "Identical asset found at $($modinc.name). Keeping project asset." - Continue + [System.IO.DirectoryInfo]$IncludesFP = Join-Path -Path $this.ProjectFolderPath -ChildPath "Includes" + + if($IncludesFP.Exists){ + $this.SetIncludesProjectFolderPath($IncludesFP) + #write-verbose "[ConfigurationObject][LoadIncludes()] Project includes folder found at $($IncludesFP.FullName)" + $projectIncludes = [IncludeFactory]::Create($IncludesFP) + if($projectIncludes){ + foreach($ProjInc in $projectIncludes){ + $ProjInc.LocationType = [LocationType]::Project + #write-verbose "[ConfigurationOBject][LoadIncludes()] -> Project includes $($ProjInc.Name)" } - - Continue - }else{ - $This.Includes += $modinc + $this.Includes += $projectIncludes } } - #> - } - - [void]LoadIncludes(){ - - - $ModuleIncludesFolder = Join-Path $this.Path.Directory -ChildPath "Includes" - $ModuleIncludes = [IncludeFactory]::Create($ModuleIncludesFolder) - - if($ModuleIncludes){ - #At the moment the configuration object is created BEFORE the logobject. - #write-verbose "[ConfigurationOBject][LoadIncludes()] Project includes found at $($this.IncludesProjectFolderPath)" - #$this.Includes = $ModuleIncludes - } - - [System.IO.DirectoryInfo]$IncludesFP = join-PAth -Path $this.ProjectFolderPath -ChildPath "Includes" - - if($IncludesFP.Exists){ - $this.SetIncludesProjectFolderPath($IncludesFP) - $projectIncludes = [IncludeFactory]::Create($this.IncludesProjectFolderPath) - if($projectIncludes){ - #write-verbose "[ConfigurationOBject][LoadIncludes()] Project includes found at $($this.IncludesProjectFolderPath)" - $this.Includes = $projectIncludes - } } - - foreach ($modinc in $ModuleIncludes){ if($this.Includes.name -contains $modinc.name){ $PotentialConflictingInclude = $this.Includes | ? {$_.Name -eq $modinc.Name} - if($PotentialConflictingInclude.Type -eq $modinc.type){ + if($PotentialConflictingInclude){ - #write-verbose "Identical asset found at $($modinc.name). Keeping project asset." + #write-verbose "Identical asset found $($modinc.name) at $($modinc.FolderPath.FullName). Keeping project asset." Continue } @@ -156,24 +111,39 @@ Class ConfigurationDocument { $This.Includes += $modinc } } - - + } [Void]LoadAssets(){ + #Reminder: Logger CANNOT be used during the load process.(This includes LoadIncludes() and Load() methods) - $ModuleAssetsFolder = Join-Path $this.Path.Directory -ChildPath "Assets" - - $ProjectAssetsFolder = Join-Path $this.ProjectFolderPath -ChildPath "Assets" + if($this.ProjectFolderPath){ + $ProjectAssetsFolder = Join-Path $this.ProjectFolderPath -ChildPath "Assets" + $this.Assets = [AssetsFactory]::CreateAsset($ProjectAssetsFolder) + + foreach($ProjAss in $this.Assets){ + $ProjAss.LocationType = [LocationType]::Project + #write-verbose "[ConfigurationObject][LoadAssets()] -> Project asset $($ProjAss.Name)" + } + } + + + $ModuleAssetsFolder = Join-Path $this.Path.Directory -ChildPath "Assets" + + $ModuleAssets = [AssetsFactory]::CreateAsset($ModuleAssetsFolder) - if(Test-Path $ProjectAssetsFolder){ + if(-not $ModuleAssets){ + return + } - $this.Assets = [AssetsFactory]::CreateAsset($ProjectAssetsFolder) + foreach($ModAss in $ModuleAssets){ + $ModAss.LocationType = [LocationType]::Module + #write-verbose "[ConfigurationObject][LoadAssets()] -> Module asset $($ModAss.Name)" } - + foreach ($modass in $ModuleAssets){ if($this.Assets.name -contains $modass.name){ @@ -193,6 +163,7 @@ Class ConfigurationDocument { } [void]Load([System.IO.FileInfo]$Path){ + #Reminder: Logger CANNOT be used during the load process.(This includes LoadAssets() LoadIncludes() and Load() methods) $this.Path = $Path $this.Load() } @@ -547,25 +518,31 @@ Class AssetsFactory{ } hidden Static [Asset[]] CreateAssets([System.IO.DirectoryInfo]$AssetsFolderPath) { + <# + Assets MUST be located in a folder named 'Assets/NAME_OF_ASSET' + Example: C:\WoopiDoopy\WebSiteReportTool\Assets\ContosoCommon + Contains a file named Woop.js which contains all the "woop's company" specific javascript code. + + PSHTML Will create the following asset: + + Name : ContosoCommon + FolderPath : C:\WoopiDoopy\WebSiteReportTool\Assets\ContosoCommon + FilePath : C:\WoopiDoopy\WebSiteReportTool\Assets\ContosoCommon\Woop.js + RelativePath : Assets/ContosoCommon/Woop.js + Type : Script + LocationType : Project + #> $Directories = Get-ChildItem $AssetsFolderPath -Directory $AllItems = @() - Foreach($Directory in $Directories){ - $Items = $Directory | Get-ChildItem -File | ? {$_.Extension -eq ".js" -or $_.Extension -eq ".css" -or $_.Extension -eq ".cdn"} #If performance becomes important. Change this to -Filter + Foreach ($Directory in $Directories) { + $Items = $Directory.FullName | Get-ChildItem -Recurse -File | ? { $_.Extension -eq ".js" -or $_.Extension -eq ".css" -or $_.Extension -eq ".cdn" } #If performance becomes important. Change this to -Filter Foreach($Item in $Items){ if(!($Item)){ Continue } - <# - try{ - $Type = [AssetsFactory]::GetAssetType($Item) - }Catch{ - - continue - } - #> $AllItems += [AssetsFactory]::CreateAsset($Item) } @@ -634,6 +611,7 @@ Class Asset{ [System.IO.FileInfo]$FilePath [String]$RelativePath [AssetType]$Type + [LocationType]$LocationType Asset(){} @@ -688,6 +666,16 @@ Class ScriptAsset : Asset { $S = "<{0} src='{1}'>" -f "Script",$this.GetFullFilePath() Return $S } + + [String] ToStringAsContent(){ + $TagName = "Script" + $FileContents = Get-Content -Path $this.GetFullFilePath() -Raw -Encoding utf8 + $StringBuilder = [System.Text.StringBuilder]::new() + $StringBuilder.AppendLine("<$TagName>") + $StringBuilder.AppendLine($FileContents) + $StringBuilder.Append("") + Return $StringBuilder + } } Class StyleAsset : Asset { @@ -704,6 +692,16 @@ Class StyleAsset : Asset { $S = "<{0} rel='{1}' type={2} href='{3}' >" -f "Link","stylesheet","text/css",$this.GetFullFilePath() Return $S } + + [String] ToStringAsContent() { + $TagName = "Style" + $FileContents = Get-Content -Path $this.GetFullFilePath() -Raw -Encoding utf8 + $StringBuilder = [System.Text.StringBuilder]::new() + $StringBuilder.AppendLine("<$TagName>") + $StringBuilder.AppendLine($FileContents) + $StringBuilder.Append("") + Return $StringBuilder + } } Class CDNAsset : Asset { @@ -758,6 +756,10 @@ Class CDNAsset : Asset { $S = "<{0} {1}='{2}' {3} {4}>" -f $t,$p,$this.raw.source,$full_CrossOrigin,$full_Integrity Return $S } + + [String] ToStringAsContent(){ + return $this.ToString() + } } function New-Logfile { @@ -1762,11 +1764,24 @@ Class IncludeFile : Include { [String]$Name [System.IO.DirectoryInfo]$FolderPath [System.IO.FileInfo]$FilePath + [LocationType]$LocationType + + IncludeFile(){} IncludeFile([System.IO.FileInfo]$FilePath){ $this.FilePath = $FilePath $this.FolderPath = $FilePath.Directory $this.Name = $FilePath.BaseName + #If the parent folder of the includes folder is the PSHTML repository, then the include file is a of locationType 'Module' + if($this.FolderPath.Parent.Name -eq 'PSHTML'){ + $this.LocationType = [LocationType]::Module + }else{ + $this.LocationType = [LocationType]::Project + } + } + + [void] SetLocationType([LocationType]$LocationType){ + $this.LocationType = $LocationType } [String]ToString(){ @@ -9580,6 +9595,10 @@ function Write-PSHTMLAsset { The CDN file type must have a specifiy structure, which can be obtained by using the cmdlet New-CDNAssetFile + .PARAMETER AsContent + Use this switch to generate the content of the asset, instead of the link to the asset. + Use this switch to allow dynamic content to be directly integrated in your HTML pages (Allows to have no internet access / send per email.) + .EXAMPLE Write-PSHTMLAsset @@ -9610,7 +9629,8 @@ function Write-PSHTMLAsset { #> [CmdletBinding()] param ( - [ValidateSet("Script","Style","CDN")]$Type + [ValidateSet("Script","Style","CDN")]$Type, + [Switch]$AsContent ) @@ -9662,7 +9682,11 @@ function Write-PSHTMLAsset { } Foreach($A in $Asset){ - $A.ToString() + if($AsContent){ + $A.ToStringAsContent() + }else{ + $A.ToString() + } } } @@ -10003,16 +10027,14 @@ function Write-PSHTMLSymbol { } #Post Content -$ScriptPath = Split-Path -Path $MyInvocation.MyCommand.Path -$ScriptPath = Split-Path -Path $PSScriptRoot New-Alias -Name Include -Value 'Write-PSHTMLInclude' -Description "Include parts of PSHTML documents using include files" -Force function Get-ScriptDirectory { Split-Path -Parent $PSCommandPath } $ScriptPath = Get-ScriptDirectory -$CF = Join-Path -Path $ScriptPath -ChildPath "pshtml.configuration.json" -#Write-host "loading config file: $($CF)" -ForegroundColor Blue +$ConfigFile = Join-Path -Path $ScriptPath -ChildPath "pshtml.configuration.json" + #Setting module variables - $Script:PSHTML_CONFIGURATION = Get-ConfigurationDocument -Path $CF -Force + $Script:PSHTML_CONFIGURATION = Get-ConfigurationDocument -Path $ConfigFile -Force $Script:Logfile = $Script:PSHTML_CONFIGURATION.GetDefaultLogFilePath() $Script:Logger = [Logger]::New($Script:LogFile) diff --git a/Tests/Pshtml.Utilities.Assets.Unit.Tests.Ps1 b/Tests/Pshtml.Utilities.Assets.Unit.Tests.Ps1 index 077fa83..600c5e5 100644 --- a/Tests/Pshtml.Utilities.Assets.Unit.Tests.Ps1 +++ b/Tests/Pshtml.Utilities.Assets.Unit.Tests.Ps1 @@ -48,25 +48,25 @@ InModuleScope PSHTML { It '[Styles] Bootstrap'{ $var | ? {$_.Type -eq 'Style' -and $_.Name -eq 'Bootstrap'} | Should not BeNullOrEmpty - ($var | ? {$_.Type -eq 'Style' -and $_.Name -eq 'Bootstrap'} | Measure).Count | Should be 1 + ($var | ? {$_.Type -eq 'Style' -and $_.Name -eq 'Bootstrap'} | Measure-Object).Count | Should be 1 Test-Path ($var | ? {$_.Type -eq 'Style' -and $_.Name -eq 'Bootstrap'}).GetFullfilePath() | Should be $true } It '[Javascript] Bootstrap'{ $var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Bootstrap'} | Should not BeNullOrEmpty - ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Bootstrap'} | Measure).Count | Should be 1 + ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Bootstrap'} | Measure-Object).Count | Should be 1 Test-Path ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Bootstrap'}).GetFullfilePath() | Should be $true } It '[Javascript] Jquery'{ $var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Jquery'} | Should not BeNullOrEmpty - ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Jquery'} | Measure).Count | Should be 1 + ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Jquery'} | Measure-Object).Count | Should be 1 Test-Path ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Jquery'}).GetFullfilePath() | Should be $true } It '[Javascript] ChartJs'{ $var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'ChartJS'} | Should not BeNullOrEmpty - ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'ChartJS'} | Measure).Count | Should be 1 + ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'ChartJS'} | Measure-Object).Count | Should be 1 Test-Path ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'ChartJS'}).GetFullfilePath() | Should be $true } @@ -182,7 +182,7 @@ InModuleScope PSHTML { #If no type is specified, and that the Asset has one or several script / style tags. All of them should be printed out. $HtmlScripts = Write-PSHTMLAsset -Name BootStrap - ($HtmlScripts | Measure).Count | Should be 2 # Bootstrap has one .js file and .css file. + ($HtmlScripts | Measure-Object).Count | Should be 2 # Bootstrap has one .js file and .css file. $HasScript = $false $HasStyle = $False Foreach($sci in $HtmlScripts){ @@ -202,6 +202,24 @@ InModuleScope PSHTML { } + It '[-Name BootStrap -Type Script -AsContent] Should write asset as content in HTML document in Script tags ' { + + $Assets = Write-PSHTMLAsset -Name BootStrap -Type Style -AsContent + Foreach ($Asset in $Assets) { + $Regex = "^