Skip to content

Commit

Permalink
Merge pull request #1 from Akaizoku/Expansion
Browse files Browse the repository at this point in the history
Expansion
  • Loading branch information
Akaizoku authored Sep 12, 2019
2 parents 6ef9968 + 7807237 commit 9cb0180
Show file tree
Hide file tree
Showing 54 changed files with 2,815 additions and 208 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
tests/
41 changes: 41 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,47 @@ All notable changes to the [PSTK](https://github.com/Akaizoku/PSTK) project will
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.2.0](https://github.com/Akaizoku/PSTK/releases/tag/1.1.0) - 2019-09-12

### Added
The following functions have been added:
- Confirm-Prompt
- Expand-CompressedFile
- Find-Key
- Get-CallerPreference
- Get-EnvironmentVariable
- Get-HTTPStatus
- Get-KeyValue
- Get-Path
- Import-CSVProperties
- Import-Properties
- Out-Hashtable
- Remove-EnvironmentVariable
- Remove-Object
- Resolve-Array
- Resolve-Boolean
- Resolve-Tags
- Resolve-URI
- Select-XMLNode
- Set-EnvironmentVariable
- Set-RelativePath
- Test-EnvironmentVariable
- Test-Object
- Test-Service
- Update-File
- Write-ErrorLog

### Changed
The following functions have been updated:
- Compare-Hashtable
- Compare-Properties
- Complete-RelativePath
- Convert-FileEncoding
- Get-Object
- Test-Object
- Test-SQLConnection
- Write-Log

## [1.1.0](https://github.com/Akaizoku/PSTK/releases/tag/1.1.0) - 2018-10-15

### Added
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Florian Carrier
Copyright (c) 2019 Florian Carrier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
36 changes: 33 additions & 3 deletions PSTK.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# Script module or binary module file associated with this manifest.
RootModule = 'PSTK.psm1'

# Version number of this module.
ModuleVersion = '1.0.0'
# Version number of this module.s
ModuleVersion = '1.2.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand All @@ -27,7 +27,7 @@ Author = 'Florian Carrier'
# CompanyName = 'Florian Carrier'

# Copyright statement for this module
Copyright = '(c) 2018 Florian Carrier. All rights reserved.'
Copyright = '(c) 2019 Florian Carrier. All rights reserved.'

# Description of the functionality provided by this module
Description = 'Collection of useful functions and procedures for PowerShell scripting'
Expand Down Expand Up @@ -73,19 +73,44 @@ FunctionsToExport = @(
"Compare-Hashtable",
"Compare-Properties",
"Complete-RelativePath",
"Confirm-Prompt",
"Convert-FileEncoding",
"ConvertTo-NaturalSort",
"ConvertTo-PDF",
"Copy-OrderedHashtable",
"Expand-CompressedFile",
"Find-Key",
"Format-String",
"Get-CallerPreference",
"Get-EnvironmentVariable",
"Get-HTTPStatus",
"Get-KeyValue",
"Get-Object",
"Get-Path",
"Get-Properties",
"Import-CSVProperties",
"Import-Properties",
"New-DynamicParameter",
"Out-Hashtable",
"Remove-EnvironmentVariable",
"Remove-Object",
"Rename-NumberedFile",
"Resolve-Array",
"Resolve-Boolean",
"Resolve-Tags",
"Resolve-URI",
"Select-XMLNode",
"Set-EnvironmentVariable",
"Set-RelativePath",
"Set-Tags",
"Start-Script",
"Stop-Script",
"Test-EnvironmentVariable",
"Test-Object",
"Test-Service",
"Test-SQLConnection",
"Update-File",
"Write-ErrorLog",
"Write-Log"
)

Expand Down Expand Up @@ -126,6 +151,11 @@ PrivateData = @{

# ReleaseNotes of this module
ReleaseNotes = @'
## 1.2.0
Added support for global preferences
Expanded existing functions
Added new features
## 1.1.0
Updated folder structure
Added about_help
Expand Down
2 changes: 1 addition & 1 deletion PSTK.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Creation date: 23/08/2018
Last modified: 15/10/2018
Repository: https://github.com/Akaizoku/PSTK
Depndencies: Test-SQLConnection requires the SQLServer module
Dependencies: Test-SQLConnection requires the SQLServer module
.LINK
https://github.com/Akaizoku/PSTK
Expand Down
136 changes: 69 additions & 67 deletions Private/Read-Properties.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ function Read-Properties {
.DESCRIPTION
Parse properties file to generate configuration variables
.PARAMETER File
[String] The File parameter should be the name of the property file.
.PARAMETER Directory
[String] The Directory parameter should be the path to the directory containing the
property file.
.PARAMETER Path
The patch parameter corresponds to the path to the property file to read.
.PARAMETER Section
[Switch] The Section parameter indicates if properties should be grouped depending on
Expand All @@ -25,7 +21,7 @@ function Read-Properties {
ordered hash table containing the content of the property file.
.EXAMPLE
Read-Properties -File "default.ini" -Directory ".\conf" -Section
Read-Properties -Path ".\conf\default.ini" -Section
In this example, Read-Properties will parse the default.ini file contained
in the .\conf directory and generate an ordered hashtable containing the
Expand All @@ -36,19 +32,11 @@ function Read-Properties {
[Parameter (
Position = 1,
Mandatory = $true,
HelpMessage = "Property file name"
)]
[ValidateNotNullOrEmpty ()]
[String]
$File,
[Parameter (
Position = 2,
Mandatory = $true,
HelpMessage = "Path to the directory containing the property file"
HelpMessage = "Path to the property file"
)]
[ValidateNotNullOrEmpty ()]
[String]
$Directory,
$Path,
[Parameter (
Position = 3,
Mandatory = $false,
Expand All @@ -57,64 +45,78 @@ function Read-Properties {
[Switch]
$Section
)
# Properties variables
$PropertyFile = Join-Path -Path $Directory -ChildPath $File
$Properties = New-Object -TypeName System.Collections.Specialized.OrderedDictionary
$Sections = New-Object -TypeName System.Collections.Specialized.OrderedDictionary
$Header = $null
# Check that the file exists
if (Test-Path -Path $PropertyFile) {
$FileContent = Get-Content -Path $PropertyFile
$LineNumber = 0
# Read the property file line by line
foreach ($Content in $FileContent) {
$LineNumber += 1
# If properties have to be grouped by section
if ($Section) {
# If end of file and section is open
if ($LineNumber -eq $FileContent.Count -And $Header) {
if ($Content[0] -ne "#" -And $Content[0] -ne ";" -And $Content -ne "") {
$Property = Read-Property -Content $Content
Begin {
# Get global preference variables
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
# Instantiate variables
$Properties = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary"
$Sections = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary"
$Header = $null
$errors = 0
}
Process {
# Check that the file exists
if (Test-Path -Path $Path) {
$ListOfProperties = Get-Content -Path $Path
$LineNumber = 0
# Read the property file line by line
foreach ($Property in $ListOfProperties) {
$LineNumber += 1
# If properties have to be grouped by section
if ($Section) {
# If end of file and section is open
if ($LineNumber -eq $ListOfProperties.Count -And $Header) {
if ($Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property -ne "") {
$Property = Read-Property -Property $Property
if ($Property.Count -gt 0) {
$Sections.Add($Property.Key, $Property.Value)
} else {
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
}
}
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
$Properties.Add($Header, $Clone)
} elseif ($Property[0] -eq "[") {
# If previous section exists add it to the property list
if ($Header) {
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
$Properties.Add($Header, $Clone)
}
# Create new property group
$Header = $Property.Substring(1, $Property.Length - 2)
$Sections.Clear()
} elseif ($Header -And $Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property -ne "") {
$Property = Read-Property -Property $Property
if ($Property.Count -gt 0) {
$Sections.Add($Property.Key, $Property.Value)
} else {
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $PropertyFile"
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
}
}
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
$Properties.Add($Header, $Clone)
} elseif ($Content[0] -eq "[") {
# If previous section exists add it to the property list
if ($Header) {
$Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep
$Properties.Add($Header, $Clone)
}
# Create new property group
$Header = $Content.Substring(1, $Content.Length - 2)
$Sections.Clear()
} elseif ($Header -And $Content[0] -ne "#" -And $Content[0] -ne ";" -And $Content -ne "") {
$Property = Read-Property -Content $Content
if ($Property.Count -gt 0) {
$Sections.Add($Property.Key, $Property.Value)
} else {
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $PropertyFile"
}
}
} else {
# Ignore comments, sections, and blank lines
if ($Content[0] -ne "#" -And $Content[0] -ne ";" -And $Content[0] -ne "[" -And $Content -ne "") {
$Property = Read-Property -Content $Content
if ($Property.Count -gt 0) {
$Properties.Add($Property.Key, $Property.Value)
} else {
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $PropertyFile"
} else {
# Ignore comments, sections, and blank lines
if ($Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property[0] -ne "[" -And $Property -ne "") {
$Property = Read-Property -Property $Property
if ($Property.Count -gt 0) {
try {
$Properties.Add($Property.Key, $Property.Value)
} catch {
Write-Log -Type "WARN" -Object "Two distinct definitions of the property $($Property.Key) have been found in the configuration file"
$Errors += 1
}
} else {
Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path"
}
}
}
}
} else {
# Alert that configuration file does not exist at specified location
Write-Log -Type "ERROR" -Message "Path not found $Path" -ErrorCode 1
}
if ($Errors -gt 0) {
Write-Log -Type "ERROR" -Object "Unable to proceed. Resolve the issues in $Path" -ErrorCode 1
}
} else {
# Alert that configuration file does not exists at specified location
Write-Log -Type "ERROR" -Message "The $File file cannot be found under $(Resolve-Path $Directory)"
return $Properties
}
return $Properties
}
53 changes: 35 additions & 18 deletions Private/Read-Property.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
function Read-Property {
<#
.SYNOPSIS
Parse property content
Parse property
.DESCRIPTION
Parse property content
Parse property content to output key-value pair
.PARAMETER Content
[String] The Content parameter should be the content of the property.
.PARAMETER Property
The property parameter corresponds to the property to read.
.INPUTS
None.
Expand All @@ -20,30 +20,47 @@ function Read-Property {
ordered hashtable containing the name and value of a given property.
.EXAMPLE
Read-Property -Content "Key = Value"
Read-Property -Property "Key = Value"
In this example, Read-Property will parse the content and assign the value
"Value" to the property "Key".
.NOTES
File name: Read-Property.ps1
Author: Florian Carrier
Creation date: 15/10/2018
Last modified: 17/06/2019
#>
[CmdletBinding ()]
Param (
[Parameter (
Position = 1,
Mandatory = $true,
HelpMessage = "Property content"
Position = 1,
Mandatory = $true,
ValueFromPipeline = $true,
HelpMessage = "Property content"
)]
[ValidateNotNullOrEmpty ()]
[String]
$Content
$Property
)
$Property = New-Object -TypeName System.Collections.Specialized.OrderedDictionary
$Index = $Content.IndexOf("=")
if ($Index -gt 0) {
$Offset = 1
$Key = $Content.Substring(0, $Index)
$Value = $Content.Substring($Index + $Offset, $Content.Length - $Index - $Offset)
$Property.Add("Key" , $Key.Trim())
$Property.Add("Value" , $Value.Trim())
Begin {
# Get global preference variables
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
# Instantiate variables
$KeyValuePair = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary"
$Index = $Property.IndexOf("=")
}
Process {
# Check that format is valid
if ($Index -gt 0) {
$Offset = 1
$Key = $Property.Substring(0, $Index)
$Value = $Property.Substring($Index + $Offset, $Property.Length - $Index - $Offset)
# Generate key-value pair
$KeyValuePair.Add("Key" , $Key.Trim())
$KeyValuePair.Add("Value" , $Value.Trim())
}
# Output key-value pair
return $KeyValuePair
}
return $Property
}
Loading

0 comments on commit 9cb0180

Please sign in to comment.