Skip to content
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

Refactored PSM1 into individual PS1 files #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions Get-IniContent.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"

Describe "Get-IniContent" {

# assert
Context "Alias" {
It "Get-IniContent alias should exist" {
(Get-Alias -Definition Get-IniContent).name | Should Be "get-ini"
}
}

}
113 changes: 113 additions & 0 deletions Get-IniContent.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
Function Get-IniContent {
<#
.Synopsis
Gets the content of an INI file

.Description
Gets the content of an INI file and returns it as a hashtable

.Notes
Author : Oliver Lipkau <oliver@lipkau.net>
Blog : http://oliver.lipkau.net/blog/
Source : https://github.com/lipkau/PsIni
http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91
Version : 1.0.0 - 2010/03/12 - OL - Initial release
1.0.1 - 2014/12/11 - OL - Typo (Thx SLDR)
Typo (Thx Dave Stiff)
1.0.2 - 2015/06/06 - OL - Improvment to switch (Thx Tallandtree)
1.0.3 - 2015/06/18 - OL - Migrate to semantic versioning (GitHub issue#4)
1.0.4 - 2015/06/18 - OL - Remove check for .ini extension (GitHub Issue#6)

#Requires -Version 2.0

.Inputs
System.String

.Outputs
System.Collections.Hashtable

.Parameter FilePath
Specifies the path to the input file.

.Example
$FileContent = Get-IniContent "C:\myinifile.ini"
-----------
Description
Saves the content of the c:\myinifile.ini in a hashtable called $FileContent

.Example
$inifilepath | $FileContent = Get-IniContent
-----------
Description
Gets the content of the ini file passed through the pipe into a hashtable called $FileContent

.Example
C:\PS>$FileContent = Get-IniContent "c:\settings.ini"
C:\PS>$FileContent["Section"]["Key"]
-----------
Description
Returns the key "Key" of the section "Section" from the C:\settings.ini file

.Link
Out-IniFile
#>

[CmdletBinding()]
Param(
[ValidateNotNullOrEmpty()]
[ValidateScript({(Test-Path $_)})]
[Parameter(ValueFromPipeline=$True,Mandatory=$True)]
[string]$FilePath
)

Begin
{Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}

Process
{
Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath"

$ini = @{}
switch -regex -file $FilePath
{
"^\[(.+)\]$" # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
continue
}
"^(;.*)$" # Comment
{
if (!($section))
{
$section = "No-Section"
$ini[$section] = @{}
}
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$ini[$section][$name] = $value
continue
}
"(.+?)\s*=\s*(.*)" # Key
{
if (!($section))
{
$section = "No-Section"
$ini[$section] = @{}
}
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
continue
}
}
Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath"
Return $ini
}

End
{Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}
}

Set-Alias get-ini Get-IniContent
16 changes: 16 additions & 0 deletions Out-IniFile.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"

Describe "Out-IniFile" {

Context "Alias" {

# assert
It "Out-IniFile alias should exist" {
(Get-Alias -Definition Out-IniFile).name | Should Be "set-ini"
}

}

}
150 changes: 150 additions & 0 deletions Out-IniFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
Function Out-IniFile {
<#
.Synopsis
Write hash content to INI file

.Description
Write hash content to INI file

.Notes
Author : Oliver Lipkau <oliver@lipkau.net>
Blog : http://oliver.lipkau.net/blog/
Source : https://github.com/lipkau/PsIni
http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91
Version : 1.0.0 - 2010/03/12 - OL - Initial release
1.0.1 - 2012/04/19 - OL - Bugfix/Added example to help (Thx Ingmar Verheij)
1.0.2 - 2014/12/11 - OL - Improved handling for missing output file (Thx SLDR)
1.0.3 - 2014/01/06 - CB - removed extra \r\n at end of file
1.0.4 - 2015/06/06 - OL - Typo (Thx Dominik)
1.0.5 - 2015/06/18 - OL - Migrate to semantic versioning (GitHub issue#4)
1.0.6 - 2015/06/18 - OL - Remove check for .ini extension (GitHub Issue#6)

#Requires -Version 2.0

.Inputs
System.String
System.Collections.Hashtable

.Outputs
System.IO.FileSystemInfo

.Parameter Append
Adds the output to the end of an existing file, instead of replacing the file contents.

.Parameter InputObject
Specifies the Hashtable to be written to the file. Enter a variable that contains the objects or type a command or expression that gets the objects.

.Parameter FilePath
Specifies the path to the output file.

.Parameter Encoding
Specifies the type of character encoding used in the file. Valid values are "Unicode", "UTF7",
"UTF8", "UTF32", "ASCII", "BigEndianUnicode", "Default", and "OEM". "Unicode" is the default.

"Default" uses the encoding of the system's current ANSI code page.

"OEM" uses the current original equipment manufacturer code page identifier for the operating
system.

.Parameter Force
Allows the cmdlet to overwrite an existing read-only file. Even using the Force parameter, the cmdlet cannot override security restrictions.

.Parameter PassThru
Passes an object representing the location to the pipeline. By default, this cmdlet does not generate any output.

.Example
Out-IniFile $IniVar "C:\myinifile.ini"
-----------
Description
Saves the content of the $IniVar Hashtable to the INI File c:\myinifile.ini

.Example
$IniVar | Out-IniFile "C:\myinifile.ini" -Force
-----------
Description
Saves the content of the $IniVar Hashtable to the INI File c:\myinifile.ini and overwrites the file if it is already present

.Example
$file = Out-IniFile $IniVar "C:\myinifile.ini" -PassThru
-----------
Description
Saves the content of the $IniVar Hashtable to the INI File c:\myinifile.ini and saves the file into $file

.Example
$Category1 = @{“Key1”=”Value1”;”Key2”=”Value2”}
$Category2 = @{“Key1”=”Value1”;”Key2”=”Value2”}
$NewINIContent = @{“Category1”=$Category1;”Category2”=$Category2}
Out-IniFile -InputObject $NewINIContent -FilePath "C:\MyNewFile.ini"
-----------
Description
Creating a custom Hashtable and saving it to C:\MyNewFile.ini
.Link
Get-IniContent
#>

[CmdletBinding()]
Param(
[switch]$Append,

[ValidateSet("Unicode","UTF7","UTF8","UTF32","ASCII","BigEndianUnicode","Default","OEM")]
[Parameter()]
[string]$Encoding = "Unicode",

[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
#[ValidatePattern('^[a-zA-Z0-9öäüÜÖÄ!§$%&\(\)=;_\''+#\-\.,\`²³\{\[\]\}]{1,255}$')]
[string]$FilePath,

[switch]$Force,

[ValidateNotNullOrEmpty()]
[Parameter(ValueFromPipeline=$True,Mandatory=$True)]
[Hashtable]$InputObject,

[switch]$Passthru
)

Begin
{Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}

Process
{
Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing to file: $Filepath"

if ($append) {$outfile = Get-Item $FilePath}
else {$outFile = New-Item -ItemType file -Path $Filepath -Force:$Force}
if (!($outFile)) {Throw "Could not create File"}
foreach ($i in $InputObject.keys)
{
if (!($($InputObject[$i].GetType().Name) -eq "Hashtable"))
{
#No Sections
Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $i"
Add-Content -Path $outFile -Value "$i=$($InputObject[$i])" -Encoding $Encoding
} else {
#Sections
Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing Section: [$i]"
Add-Content -Path $outFile -Value "[$i]" -Encoding $Encoding
Foreach ($j in $($InputObject[$i].keys | Sort-Object))
{
if ($j -match "^Comment[\d]+") {
Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing comment: $j"
Add-Content -Path $outFile -Value "$($InputObject[$i][$j])" -Encoding $Encoding
} else {
Write-Verbose "$($MyInvocation.MyCommand.Name):: Writing key: $j"
Add-Content -Path $outFile -Value "$j=$($InputObject[$i][$j])" -Encoding $Encoding
}
}
# removing extra `r`n
Add-Content -Path $outFile -Value "" -Encoding $Encoding
}
}
Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Writing to file: $FilePath"
if ($PassThru) {Return $outFile}
}

End
{Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}
}

Set-Alias set-ini Out-IniFile
26 changes: 8 additions & 18 deletions PsIni.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path

Import-Module PsIni -Force

Describe "I/O" {
Describe "PsIni" {

# arrange
$ini = Join-Path $TestDrive "\Settings.ini"

# hash to be persisted and validated
$category1=@{"key1"="value1";"key2"="value2"}
$category2=@{"key3"="value3";"key4"="value4"}

$hashIn=@{"category1"=$category1;"category2"=$category2}
# Write-Host "hashIn: $($hashIn.GetType().FullName)"

Context "Writing INI" {

# act
Out-IniFile -inputobject $hashIn -filepath $ini

# assert
It "creates a file" {
# should exist
Test-Path $ini | Should Be $true
}

# assert
It "content matches expected value" {

$content = "[category1]`r`nkey1=value1`r`nkey2=value2`r`n[category2]`r`nkey3=value3`r`nkey4=value4`r`n"
Expand All @@ -35,31 +36,20 @@ Describe "I/O" {

Context "Reading INI" {

# act
Out-IniFile -inputobject $hashIn -filepath $ini
$hashOut = Get-IniContent $ini
# Write-Host "hashOut: $($hashOut.GetType().FullName)"

# assert
It "creates a hashtable from an INI file" {
# ($hashOut.GetType()) | Should Be $hashIn.GetType()
($hashOut.GetType()) | Should Be hashtable
}

# assert
It "content matches original hashtable" {
Compare-Object $hashIn $hashOut
}

}

}

Describe "Aliases" {

It "Get-IniContent alias should exist" {
(Get-Alias -Definition Get-IniContent).name | Should Be "get-ini"
}

It "Out-IniFile alias should exist" {
(Get-Alias -Definition Out-IniFile).name | Should Be "set-ini"
}

}
Binary file modified PsIni.psd1
Binary file not shown.
Loading