Shelling

Every now and then I need to write some powershell, but almost always the time between these events is enough to forget almost everything about it.
This is the reason for this blog, where I try to collect every important things what I use when I’m shelling :)
Probably there are going to be further post containing similar snippets.
The caller script creates a nuspec XML file, modifies it and then creates a nuget package based on it.
There are two basic files what I’m going to use: one containing some functions and filters and another one calling them.
The caller:

# This is the way to get the location of the current file
$currentDir = Get-Location
# Not using Import-Module because it doesn't get actualized when changed
. (Join-Path $currentDir NugetFunctions.ps1)

$csprojFile = Create-NuspecFile
Create-Package $csprojFile

The functions:

# The following is a filter for pipes
filter NodesToRemove
{
    $namesToRemove = 'tags', 'releaseNotes'
    $contentsToRemove = 'DELETE_THIS_LINE'

    if($namesToRemove -contains $_.name -or $_.InnerText.Contains($contentsToRemove))
    {
        return $_
    }
}

# The following function is a pipeable function because of the 'process' keyword
function Remove-ChildNodes($xmlParentNode)
{
    
    begin 
    {
        $parentNodeName = $xmlParentNode.Name
        Write-Host "Children of $parentNodeName are going to be removed" 
    }
    process 
    { 
        $tagName = $_.Name
        Write-Host "The $tagName gets removed"
        $xmlParentNode.RemoveChild($_)
    }
    end { return $xmlParentNode }
}

function Create-NuspecFile()
{
    $currentDir = Get-Location
    . (Join-Path $currentDir ..\..\NuGet.exe) spec -f
    $csprojFile = Get-ChildItem -filter "*.csproj"
    $nuspecFileName = $csprojFile.BaseName + ".nuspec"
    [xml] $nuspecXml = Get-Content $nuspecFileName
    
    # The following statement does the same as the one below
    # $nodesToDelete = $nuspecXml.package.metadata.ChildNodes | ? { $namesToRemove -contains $_.name -or $_.InnerText.Contains($contentsToRemove) }
    $nodesToDelete = $nuspecXml.package.metadata.ChildNodes | NodesToRemove
    
    # The following statement does the same as the one below
    # $nodesToDelete | % { $nuspecXml.package.metadata.RemoveChild($_) > null }
    # The null is needed so that the return value doesn't get extended with the output of this run
    ($nodesToDelete | Remove-ChildNodes $nuspecXml.package.metadata) > null
    
    
    $destinationNuspecFilePath = Join-Path (Join-Path $currentDir "\Nuget\") $nuspecFileName

    $nuspecXml.Save($destinationNuspecFilePath)
    
    return $csprojFile
    
    # This is the way to handle exceptions
    trap
    {         
        Write-Host "There was an error during the creation of the nuspec file!"
        Write-Host $_.errorid
        Write-Host $_.exception.message
        # You can also use 'continue' if you don't want to break the run
        break;
    }
}

# The following is a commented function where the VerbosePreference is set
function Create-Package($csprojFile, [switch]$verbose)
{
    <#
    .SYNOPSIS
    Creates a Nuget package to the csproj file.
    .PARAMETER $csprojFile
    The csproj file.
    #>

    if($verbose.IsPresent)
    {
        $VerbosePreference = "Continue"
    }

    $currentDir = Get-Location
    . (Join-Path $currentDir ..\..\NuGet.exe) pack $csprojFile.Name -BasePath Nuget -ExcludeEmptyDirectories

    trap
    {
        Write-Host "There was an error during the creation of the nuget package!"
        Write-Host $_.ErrorID
        Write-Host $_.Exception.Message
        break;
    }
}

The XML structure looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
    <metadata>
        <id>someid</id>
        <version>1.0.0</version>
        <title>sometitle</title>
        <authors>someauthors</authors>
        <owners>someowners</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>somedescription</description>
        <summary />
        <releaseNotes></releaseNotes>
        <copyright>somecopyright</copyright>
        <tags>some tags</tags>
    </metadata>
</package>

I’m always searching for how to add more help to a function

Get-Help about_comment_based_help
Advertisements

About Tamas Nemeth

Husband and proud father of two daughters in Nürnberg. I'm working as a Senior Software Developer and an enthusiastic Clean-Coder. I spend most of my free time with my family (playing, hiking, etc...). I also play table-tennis and badminton sometimes...
This entry was posted in Technical Interest and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s