"Missing '=' operator after key in hash literal" Error on installing package

May 29, 2011 at 5:00 PM

guys,

I am trying out a package which only includes 1 .psm1 file and init.ps1.

here is how my psm1 file looks like;

$PROJECT = Get-Project

function Add-Reference {
    param(
        [Parameter(position=0, mandatory=$true)]
        [string]$referenceName
    )
    
    $ref = $PROJECT.Object.References | Where-Object { $_.Name -eq $referenceName } 
    if($ref -eq $null) {$PROJECT.Object.References.Add($referenceName)} else {'WARNING : Reference ' + $referenceName + ' already exists in your project...'}
}

function Remove-Reference {
    param(
        [Parameter(position=0, mandatory=$true)]
        [string]$referenceName
    )
    
    $PROJECT.Object.References | Where-Object { $_.Name -eq $referenceName } | ForEach-Object { $_.Remove() }
}

Register-TabExpansion 'Remove-Reference' @{
    $refs = New-Object System.Collections.ArrayList 
    $PROJECT.Object.References | ForEach-Object { $refs.Add($_.Name) } 
    'referenceName' = { $refs }
}

the blew one is init.ps1;

param($installPath, $toolsPath, $package, $project)

Import-Module (Join-Path $toolsPath AddReference.psm1)

but when I try to install this package through package manager console, I am getting an error liek beleow;

Missing '=' operator after key in hash literal.
At C:\apps\ClassLibrary1\packages\AddReference.1.1.192\tools\AddReference.psm1:24 char:33
+     $PROJECT.Object.References | <<<<  ForEach-Object { $refs.Add($_.Name) } 
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEqualsInHashLiteral
 
Import-Module : The specified module 'c:\apps\ClassLibrary1\packages\AddReference.1.1.192\tools\AddReference.psm1
' was not loaded because no valid module file was found in any module directory.
At C:\apps\ClassLibrary1\packages\AddReference.1.1.192\tools\init.ps1:3 char:14
+ Import-Module <<<<  (Join-Path $toolsPath AddReference.psm1)
    + CategoryInfo          : ResourceUnavailable: (c:\apps\ClassLi...dReference.psm1:String) [Import-Module], F 
   ileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

I couldn't figure out what the problem is here. 

May 29, 2011 at 5:15 PM

ok, now I converted it like below now it works but I am not sure I am doing the right thing here? Also I am wondering if I did the right thing by opening this question here. It looks  like PowerShell-related topic more.

$PROJECT = Get-Project
$REFS = New-Object System.Collections.ArrayList 
$PROJECT.Object.References | ForEach-Object { $REFS.Add($_.Name) } 

function Add-Reference {
    param(
        [Parameter(position=0, mandatory=$true)]
        [string]$referenceName
    )
    
    $ref = $PROJECT.Object.References | Where-Object { $_.Name -eq $referenceName } 
    if($ref -eq $null) {$PROJECT.Object.References.Add($referenceName)} else {'WARNING : Reference ' + $referenceName + ' already exists in your project...'}
    
    $REFS.Clear()
    $PROJECT.Object.References | ForEach-Object { $REFS.Add($_.Name) } 
}

function Remove-Reference {
    param(
        [Parameter(position=0, mandatory=$true)]
        [string]$referenceName
    )
    
    $PROJECT.Object.References | Where-Object { $_.Name -eq $referenceName } | ForEach-Object { $_.Remove() }
    
    $REFS.Clear()
    $PROJECT.Object.References | ForEach-Object { $REFS.Add($_.Name) } 
}

Register-TabExpansion 'Remove-Reference' @{
    
    'referenceName' = { $REFS }
}

May 29, 2011 at 5:21 PM

Make sure you have the module in the specified directory in your package. The error looks like it says it can't find the file. You can see what the package looks like with NuGet Package Explorer.

Have you had an opportunity to read through the docs that talk about creating packages yet?

____
Rob
"Be passionate in all you do"

http://devlicio.us/blogs/rob_reynolds
http://ferventcoder.com
http://twitter.com/ferventcoder

May 29, 2011 at 5:29 PM
Edited May 29, 2011 at 5:29 PM

I don't think it is related to my folder structure, It seems working but there was a problem with Register-TabExpansion thing.  I solved it with the above code but not sure I did the right thing there.

here is the package if you would like to look at : http://nuget.org/List/Packages/AddReference

Developer
May 30, 2011 at 7:11 AM

Register-TabExpansion is fine, the problem was with the code you wrote. You're declaring a hash table from parameter name to lambda so you just wrote the code in the wrong places.

There's a bunch of issues with your script e.g. doesn't support pipelining etc. I'll reply with a bunch of improvements you can make.

Developer
May 30, 2011 at 7:36 AM

 

function AddReference($project) {
    $ref = $project.Object.References | Where-Object { $_.Name -eq $referenceName }
    if(!$ref) {
        if($project.Type -eq 'Web Site') {
            $project.Object.References.AddFromGAC($referenceName) | Out-Null
        }
        elseif($project.Type -eq 'F#') {
            $project.Object.References.Add("*$referenceName") | Out-Null
        }
        else {
            $project.Object.References.Add($referenceName) | Out-Null
        }
        "Successfully added reference to '$($project.Name)'"
    }
    else {
        Write-Warning "Reference $referenceName already exists in your project..."
    }
}

function RemoveReference($project, $referenceName) {
    $project.Object.References | Where-Object { $_.Name -eq $referenceName } | ForEach-Object { $_.Remove() }
}

function ResolveProjects($projectName) {
    if($projectName) {
        Get-Project $projectName
    }
    else {
        Get-Project
    }
}

function Add-Reference {
    param(
        [parameter(position = 0, mandatory=$true)]
        [string]$ReferenceName,
        [parameter(position = 1, ValueFromPipelineByPropertyName = $true)]
        [string[]]$ProjectName
    )
    Process {
        (ResolveProjects $ProjectName) | %{ AddReference $_ $ReferenceName }
    }
}

function Remove-Reference {
    param(
        [Parameter(position=0, mandatory=$true)]
        [string]$ReferenceName,
        [parameter(position = 1, ValueFromPipelineByPropertyName = $true)]
        [string[]]$ProjectName
    )
    Process {
        (ResolveProjects $ProjectName) | %{ RemoveReference $_ $ReferenceName }
    }
}

Register-TabExpansion 'Add-Reference' @{
    'ProjectName' = { Get-Project -All | Select -ExpandProperty Name }
}

Register-TabExpansion 'Remove-Reference' @{
    'ReferenceName' = {param($context)
        if($context.ProjectName) {
            $project = Get-Project $context.ProjectName
        }
        
        if(!$project) {
            $project = Get-Project
        }
        
        $project.Object.References | Sort Name | %{ $_.Name } | Select -Unique
    }
    'ProjectName' = { Get-Project -All | Select -ExpandProperty Name }
}

Export-ModuleMember Add-Reference, Remove-Reference
May 30, 2011 at 9:24 PM

@dfowler

woww, thanks for that. I am new to PowerShell so this was one of my try-outs.

Do you mind if I pack your code up and nuget push it to Nuget Live Feed as new version of AddReference package?

Developer
May 30, 2011 at 10:33 PM

Do it!