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

Get-IniContent doesn't work properly with Set-StrictMode -Version Latest #19

Closed
davidhayesbc opened this issue Aug 14, 2015 · 4 comments

Comments

@davidhayesbc
Copy link
Contributor

I've fixed this in my own code, body needs to be (see below)

[CmdletBinding()]
Param(
    [ValidateNotNullOrEmpty()]
    [ValidateScript({(Test-Path $_)})]
    [Parameter(ValueFromPipeline=$True,Mandatory=$True)]
    [string]$FilePath,
    [char[]]$CommentChar = @(";"),
    [switch]$IgnoreComments
)

Begin
{
    Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"
    $commentRegex = "^([$($CommentChar -join '')].*)$"
}

Process
{
    Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath"
    $commentCount =0
    $ini = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)
    switch -regex -file $FilePath
    {
        "^\[(.+)\]$" # Section
        {
            $section = $matches[1]
            $ini[$section] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)
            $commentCount = 0
            continue
        }
        $commentRegex # Comment 
        {
            if (!$IgnoreComments)
            {
                if (!(test-path "variable:section"))
                {
                    $section = "_"
                    $ini[$section] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)
                }
                $value = $matches[1]
                $commentCount++
                $name = "Comment" + $commentCount
                $ini[$section][$name] = $value
            }
            continue
        }  
        "(.+?)\s*=\s*(.*)" # Key
        {
            if (!(test-path "variable:section"))
            {
                $section = "_" 
                $ini[$section] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)
            }
            $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"}

}

@craibuc
Copy link
Collaborator

craibuc commented Aug 14, 2015

Please fork the project, add your code, ensure that the tests pass, then submit a pull request.

@davidhayesbc
Copy link
Contributor Author

Thanks, I'm new to github so I'm still figuring out the process

@lipkau
Copy link
Owner

lipkau commented Aug 15, 2015

Hi.

First of, the code you edited is Get-IniContent. And not Out-IniFile.

But I like the idea.
I intend to rename the variables according to PoshCode/PowerShellPracticeAndStyle #36.

@davidhayesbc: If you want to contribute, do as @craibuc said. You can find some help here: help.github.com

@davidhayesbc davidhayesbc changed the title Out-IniFile doesn't work properly with Set-StrictMode -Version Latest Get-IniContent doesn't work properly with Set-StrictMode -Version Latest Aug 15, 2015
@davidhayesbc
Copy link
Contributor Author

Whoops, you're right I changed the issue. I was trying to get too many things done on a Friday afternoon

davidhayesbc added a commit to davidhayesbc/PsIni that referenced this issue Aug 15, 2015
Added Set-StrictMode -Version Latest, fixed the check for !$section and
initializing $commentCount
lipkau added a commit that referenced this issue Aug 16, 2015
@lipkau lipkau added resolved and removed on hold labels Aug 16, 2015
@lipkau lipkau self-assigned this Aug 16, 2015
@lipkau lipkau closed this as completed Aug 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants