Downloads:

1,835

Downloads of v 0.0.77:

324

Last Update:

13 Apr 2015

Package Maintainer(s):

Software Author(s):

  • Chris Dostert

Tags:

Powershell Appease TaskRunner CI ContinuousIntegration Build Deployment admin

appease.client.powershell

This is not the latest version of appease.client.powershell available.

0.0.77 | Updated: 13 Apr 2015

Downloads:

1,835

Downloads of v 0.0.77:

324

Maintainer(s):

Software Author(s):

  • Chris Dostert

appease.client.powershell 0.0.77

This is not the latest version of appease.client.powershell available.

All Checks are Unknown

2 Test of Unknown Status


Validation Testing Unknown


Verification Testing Unknown

To install appease.client.powershell, run the following command from the command line or from PowerShell:

>

To upgrade appease.client.powershell, run the following command from the command line or from PowerShell:

>

To uninstall appease.client.powershell, run the following command from the command line or from PowerShell:

>

NOTE: This applies to both open source and commercial editions of Chocolatey.

1. Ensure you are set for organizational deployment

Please see the organizational deployment guide

  • Open Source or Commercial:
    • Proxy Repository - Create a proxy nuget repository on Nexus, Artifactory Pro, or a proxy Chocolatey repository on ProGet. Point your upstream to https://chocolatey.org/api/v2. Packages cache on first access automatically. Make sure your choco clients are using your proxy repository as a source and NOT the default community repository. See source command for more information.
    • You can also just download the package and push it to a repository Download

3. Enter your internal repository url

(this should look similar to https://chocolatey.org/api/v2)

4. Choose your deployment method:


choco upgrade appease.client.powershell -y --source="'STEP 3 URL'" [other options]

See options you can pass to upgrade.

See best practices for scripting.

Add this to a PowerShell script or use a Batch script with tools and in places where you are calling directly to Chocolatey. If you are integrating, keep in mind enhanced exit codes.

If you do use a PowerShell script, use the following to ensure bad exit codes are shown as failures:


choco upgrade appease.client.powershell -y --source="'STEP 3 URL'"
$exitCode = $LASTEXITCODE

Write-Verbose "Exit code was $exitCode"
$validExitCodes = @(0, 1605, 1614, 1641, 3010)
if ($validExitCodes -contains $exitCode) {
  Exit 0
}

Exit $exitCode

- name: Ensure appease.client.powershell installed
  win_chocolatey:
    name: appease.client.powershell
    state: present
    version: 0.0.77
    source: STEP 3 URL

See docs at https://docs.ansible.com/ansible/latest/modules/win_chocolatey_module.html.

Coming early 2020! Central Managment Reporting available now! More information...


chocolatey_package 'appease.client.powershell' do
  action    :install
  version  '0.0.77'
  source   'STEP 3 URL'
end

See docs at https://docs.chef.io/resource_chocolatey_package.html.


Chocolatey::Ensure-Package
(
    Name: appease.client.powershell,
    Version: 0.0.77,
    Source: STEP 3 URL
);

Requires Otter Chocolatey Extension. See docs at https://inedo.com/den/otter/chocolatey.


cChocoPackageInstaller appease.client.powershell
{
   Name     = 'appease.client.powershell'
   Ensure   = 'Present'
   Version  = '0.0.77'
   Source   = 'STEP 3 URL'
}

Requires cChoco DSC Resource. See docs at https://github.com/chocolatey/cChoco.


package { 'appease.client.powershell':
  provider => 'chocolatey',
  ensure   => '0.0.77',
  source   => 'STEP 3 URL',
}

Requires Puppet Chocolatey Provider module. See docs at https://forge.puppet.com/puppetlabs/chocolatey.


salt '*' chocolatey.install appease.client.powershell version="0.0.77" source="STEP 3 URL"

See docs at https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.chocolatey.html.

5. If applicable - Chocolatey configuration/installation

See infrastructure management matrix for Chocolatey configuration elements and examples.

This package was approved by moderator ferventcoder on 24 Apr 2015.

Description

The Appease client for Windows PowerShell

Installation notes:
- installs to: C:\ProgramFiles\Appease\PowerShell\Appease.Client
- appends C:\ProgramFiles\Appease\PowerShell to PSModulePath environment variable


tools\Appease.Client\Appease.Client.psd1
 
tools\Appease.Client\Appease.Client.psm1

function Invoke-AppeaseDevOp(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ConfigurationName,

    [string[]]
    [ValidateCount( 1, [Int]::MaxValue)]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $TemplateSource = $DefaultTemplateSources,

    [String]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath='.'

){    

    if($ConfigurationName){
        $Configuration = DevOpStorage\Get-AppeaseConfiguration -Name $ConfigurationName -DevOpName $Name -ProjectRootDirPath $ProjectRootDirPath        
    }
    
    # get devop
    $DevOp = DevOpStorage\Get-AppeaseDevOp -Name $Name -ProjectRootDirPath $ProjectRootDirPath

    foreach($Task in $DevOp.Tasks){
        $TaskName = $Task.Name

        # add variables from configuration
        if($Configuration.Variables){
            $Variables = $Configuration.Variables          
        }
        else{
            $Variables = @{}
        }
        # add automatic variables
        $Variables.'Appease.ProjectRootDirPath' = (Resolve-Path $ProjectRootDirPath)
        $Variables.'Appease.Task.Name' = $TaskName
        # @todo: support composite variables (perform variable substitution on variables)

Write-Debug "Ensuring task template installed"
        TemplateManagement\Install-AppeaseTaskTemplate -Id $Task.TemplateId -Version $Task.TemplateVersion -Source $TemplateSource
        $TaskTemplateInstallDirPath = TemplateManagement\Get-AppeaseTaskTemplateInstallDirPath -Id $Task.TemplateId -Version $Task.TemplateVersion -ProjectRootDirPath $ProjectRootDirPath
        $TaskTemplateMetadata = TemplateManagement\Get-AppeaseTaskTemplateMetadata -TaskTemplateDirPath $TaskTemplateInstallDirPath

        # perform variable substitution on task parameters
        $TaskParameters = $Configuration.TaskParameters.$TaskName
        # @todo: support default parameter values
        foreach($TaskParameter in $TaskParameters.GetEnumerator()){
            foreach($Variable in $Variables.GetEnumerator()){
                $TaskParameter.Value = $TaskParameter.Value -replace "#{$($Variable.Key)}",$Variable.Value
            }
        }

        # perform parameter substitution on task invocation command
        $TaskInvocationCommand = $TaskTemplateMetadata.Invocation.Command
        foreach($TaskParameter in $TaskParameters.GetEnumerator()){
            $TaskParameterValueJson = $TaskParameter.Value | ConvertTo-Json -Depth 12 -Compress
            $TaskInvocationCommand = $TaskInvocationCommand -replace "#{Appease.Task.Parameters.$($TaskParameter.Key)}",$TaskParameterValueJson
        }
        $TaskParametersJson = $TaskParameters | ConvertTo-Json -Depth 12 -Compress
        $TaskInvocationCommand = $TaskInvocationCommand -replace "#{Appease.Task.Parameters}",$TaskParametersJson
        # see: http://docs.aws.amazon.com/cli/latest/userguide/cli-using-param.html#quoting-strings
        $TaskInvocationCommand = $TaskInvocationCommand -replace '"','\"'

        # invoke task template
        $OriginalLocation = Get-Location
        Try
        {
            Set-Location "$TaskTemplateInstallDirPath\bin"
            
            $TaskInvocationHandler = $TaskTemplateMetadata.Invocation.Handler

Write-Debug `
@"
Invoking task '$TaskName':
    & $TaskInvocationHandler $TaskInvocationCommand
"@
        #& 'C:\Users\chris\Downloads\EchoArgs.exe' $TaskInvocationCommand
        & $TaskInvocationHandler $TaskInvocationCommand
        }
        Finally{
            Set-Location $OriginalLocation
        }

    }
}

function Add-AppeaseTask(

    [CmdletBinding(
        DefaultParameterSetName="Add-AppeaseTaskLast")]

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $TemplateId,

    [string]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $TemplateVersion,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Name = $TemplateId,

    [switch]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true,
        ParameterSetName='Add-AppeaseTaskFirst')]
    $First,

    [string]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true,
        ParameterSetName='Add-AppeaseTaskAfter')]
    $After,

    [string]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true,
        ParameterSetName='Add-AppeaseTaskBefore')]
    $Before,

    [switch]
    [Parameter(
        ValueFromPipelineByPropertyName=$true,
        ParameterSetName='Add-AppeaseTaskLast')]
    $Last,

    [switch]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Force,

    [string[]]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $TemplateSource= $DefaultTemplateSources,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){

    <#
        .SYNOPSIS
        Adds a new task to a devop
        
        .PARAMETER TemplateVersion
        Description: 
        The version of the task template (identified by the TemplateId parameter) to use
               
        Default:                 
        the latest available version of the task template (identified by the TemplateId parameter)

        .PARAMETER Name
        Description: 
        The tasks name
               
        Default:                 
        the value of the TemplateId parameter

        .EXAMPLE
        Add-AppeaseTask -DevOpName DeployToAzure -TemplateId DeployNupkgToAzureWebsites -TemplateVersion '0.0.3'-Name LastTask 
        
        Description:

        Adds a task 'LastTask' after all existing tasks in devop 'DeployToAzure'. The task uses version '0.0.3' of
        the task template with id 'DeployNupkgToAzureWebsites'

        .EXAMPLE
        Add-AppeaseTask -DevOpName DeployToAzure -TemplateId DeployNupkgToAzureWebsites -First

        Description:

        Adds a task 'DeployNupkgToAzureWebsites' before all existing tasks in devop 'DeployToAzure'. The task 
        uses the latest version of the task template with id 'DeployNupkgToAzureWebsites'

        .EXAMPLE
        Add-AppeaseTask -DevOpName DeployToAzure -TemplateId DeployNupkgToAzureWebsites -After SecondTask

        Description:

        Adds a task 'DeployNupkgToAzureWebsites' after the existing task 'SecondTask' in devop 'DeployToAzure'.
        The task uses the latest version of the task template with id 'DeployNupkgToAzureWebsites'

        .EXAMPLE
        Add-AppeaseTask -DevOpName DeployToAzure -TemplateId DeployNupkgToAzureWebsites -Name BeforeSecondTask -Before SecondTask

        Description:

        Adds a task 'BeforeSecondTask" before the existing task 'SecondTask' in devop 'DeployToAzure'. The task 
        uses the latest version of the task template with id 'DeployNupkgToAzureWebsites'

    #>

        
        if([string]::IsNullOrWhiteSpace($TemplateVersion)){
            $TemplateVersion = TemplateManagement\Get-AppeaseTaskTemplateLatestVersion -Source $TemplateSource -Id $TemplateId
Write-Debug "using greatest available template version : $TemplateVersion"
        }
                
        if($First.IsPresent){
        
            $TaskIndex = 0
        
        }
        elseif('Add-AppeaseTaskAfter' -eq $PSCmdlet.ParameterSetName){
            
            $Tasks = DevOpStorage\Get-AppeaseDevOp -Name $DevOpName -ProjectRootDirPath $ProjectRootDirPath | Select -ExpandProperty Tasks
            $indexOfAfter = $Tasks.IndexOf(($Tasks|?{$_.Name -eq $After}))
            # ensure Task with key $After exists
            if($indexOfAfter -lt 0){
                throw "Task '$After' could not be found."
            }
            $TaskIndex = $indexOfAfter + 1
        
        }
        elseif('Add-AppeaseTaskBefore' -eq $PSCmdlet.ParameterSetName){        
        
            $Tasks = DevOpStorage\Get-AppeaseDevOp -Name $DevOpName -ProjectRootDirPath $ProjectRootDirPath | Select -ExpandProperty Tasks
            $indexOfBefore = $Tasks.IndexOf(($Tasks|?{$_.Name -eq $Before}))
            # ensure Task with key $Before exists
            if($indexOfBefore -lt 0){
                throw "Task '$Before' could not be found."
            }
            $TaskIndex = $indexOfBefore
        
        }
        else{
        
            $Tasks = DevOpStorage\Get-AppeaseDevOp -Name $DevOpName -ProjectRootDirPath $ProjectRootDirPath | Select -ExpandProperty Tasks
            $TaskIndex = $Tasks.Count
        }

        DevOpStorage\Add-AppeaseTask `
            -DevOpName $DevOpName `
            -Name $Name `
            -TemplateId $TemplateId `
            -TemplateVersion $TemplateVersion `
            -Index $TaskIndex `
            -Force:$Force `
            -ProjectRootDirPath $ProjectRootDirPath
}

function Update-AppeaseTaskTemplate(

    [CmdletBinding(
        DefaultParameterSetName="Update-All")]

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true)]
    $DevOpName,

    [string[]]
    [ValidateCount( 1, [Int]::MaxValue)]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true,
        ParameterSetName="Update-Single")]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true,
        ParameterSetName="Update-Multiple")]
    $Id,

    [string]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true,
        ParameterSetName="Update-Single")]
    $Version,

    [switch]
    [Parameter(
        ParameterSetName="Update-All")]
    $All,

    [string[]]
    [ValidateCount( 1, [Int]::MaxValue)]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Source = $DefaultTemplateSources,

    [String]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath='.'

){

    $DevOp = DevOpStorage\Get-AppeaseDevOp -Name $DevOpName -ProjectRootDirPath $ProjectRootDirPath

    # build up list of template updates
    $templateUpdates = @{}
    If('Update-Multiple' -eq $PSCmdlet.ParameterSetName){

        foreach($templateId in $Id){

            $templateUpdates.Add($templateId,(TemplateManagement\Get-AppeaseTaskTemplateLatestVersion -Source $Source -Id $templateId))

        }
    }
    ElseIf('Update-Single' -eq $PSCmdlet.ParameterSetName){
        
        if($Id.Length -ne 1){
            throw "Updating to an explicit template version is only allowed when updating a single template"
        }

        $templateUpdates.Add($Id,$Version)
    }
    Else{        
        
        foreach($Task in $DevOp.Tasks){

            $templateUpdates.Add($Task.TemplateId,(TemplateManagement\Get-AppeaseTaskTemplateLatestVersion -Source $Source -Id $Task.TemplateId))
        
        }
    }

    foreach($Task in $DevOp.Tasks){

        $updatedTemplateVersion = $templateUpdates.($Task.TemplateId)

        if($null -ne $updatedTemplateVersion){

            TemplateManagement\Uninstall-AppeaseTaskTemplate -Id $Task.TemplateId -Version $Task.TemplateVersion -ProjectRootDirPath $ProjectRootDirPath

Write-Debug `
@"
Updating task template '$($Task.TemplateId)'
from version '$($Task.TemplateVersion)'
to version '$($updatedTemplateVersion)'
for task '$($Task.Name)'
"@
            DevOpStorage\Set-AppeaseTaskTemplateVersion `
                -DevOpName $DevOpName `
                -TaskName $Task.Name `
                -TemplateVersion $updatedTemplateVersion `
                -ProjectRootDirPath $ProjectRootDirPath
        }
    }
}

Export-ModuleMember -Function @(
                                # DevOp API
                                'Invoke-AppeaseDevOp',
                                'New-AppeaseDevOp',
                                'Remove-AppeaseDevOp',
                                'Rename-AppeaseDevOp',
                                'Get-AppeaseDevOp',
                    
                                # Task API
                                'Add-AppeaseTask',
                                'Remove-AppeaseTask',
                                'Rename-AppeaseTask',
                                'Set-AppeaseTaskParameter',

                                # Configuration API
                                'Add-AppeaseConfiguration',
                                'Get-AppeaseConfiguration',
                                'Set-AppeaseConfigurationParentName',
                                'Set-AppeaseConfigurationVariable',
                                'Remove-AppeaseConfigurationVariable',
                                'Rename-AppeaseConfiguration',
                                'Remove-AppeaseConfiguration'

                                # Task Template API
                                'Update-AppeaseTaskTemplate',
                                'New-AppeaseTaskTemplatePackage',
                                'Publish-AppeaseTaskTemplateToNuGetFeed')
tools\Appease.Client\DevOpStorage\DevOpStorage.psm1
function Get-AppeaseDevOpDirPath(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    <#
        .Synopsis
        An Internal utility function that returns the dir path of a devop
    #>

    Write-Output "$ProjectRootDirPath\.Appease\DevOps\$Name"
}

function Get-AppeaseDevOpFilePath(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    <#
        .Synopsis
        An Internal utility function that returns the file path of a devop
    #>

   $DevOpDirPath = Get-AppeaseDevOpDirPath -Name $Name -ProjectRootDirPath $ProjectRootDirPath
   Write-Output "$DevOpDirPath\$Name.json"
}

function Save-AppeaseDevOp(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [PSCustomObject[]]
    [ValidateNotNull()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Task = @(),

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'
){
    <#
        .Synopsis
        An Internal utility function that saves a devop to disk
    #>
    
    $DevOpFilePath = Get-AppeaseDevOpFilePath -Name $Name -ProjectRootDirPath $ProjectRootDirPath

    if(!(Test-Path -Path $DevOpFilePath)){
        New-Item -ItemType File -Path $DevOpFilePath -Force
    }

    $DevOp = @{Tasks=$Task}

    Set-Content $DevOpFilePath -Value (ConvertTo-Json -InputObject $DevOp -Depth 12) -Force
}

function New-AppeaseDevOp(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [switch]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Force,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    <#
        .Synopsis
        creates a new devop
    #>

    $DevOpFilePath = Get-AppeaseDevOpFilePath -Name $Name -ProjectRootDirPath $ProjectRootDirPath

    If(!$Force.IsPresent -and (Test-Path $DevOpFilePath)){
throw `
@"
dev op "$($Name)" already exists
for project "$(Resolve-Path $ProjectRootDirPath)".

dev op names must be unique.
If you want to overwrite the existing dev op use the -Force parameter
"@
    }Else{

        Save-AppeaseDevOp -Name $Name -ProjectRootDirPath $ProjectRootDirPath
    }
}

function Get-AppeaseDevOp(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    <#
        .SYNOPSIS
        an internal utility function that retrieves a DevOp from storage
    #>

    $DevOpFilePath = Get-AppeaseDevOpFilePath -Name $Name -ProjectRootDirPath $ProjectRootDirPath
    
    $DevOp = Get-Content $DevOpFilePath | Out-String | ConvertFrom-Json
    
    # convert tasks from Array to Collection
    $DevOp.Tasks = {$DevOp.Tasks}.Invoke()

    # convert task parameters from PSCustomObject to Hashtable
    # see : http://stackoverflow.com/questions/22002748/hashtables-from-convertfrom-json-have-different-type-from-powershells-built-in-h
    for($i = 0; $i -lt $DevOp.Tasks.Count; $i++)
    {
        if($DevOp.Tasks[$i].Parameters){
            $ParametersHashtable = @{}
            $DevOp.Tasks[$i].Parameters.PSObject.Properties | %{$ParametersHashtable[$_.Name] = $_.Value}
            $DevOp.Tasks[$i].Parameters = $ParametersHashtable
        }
    }

    Write-Output $DevOp

}

function Rename-AppeaseDevOp(

    [string]
    [ValidateNotNullOrEmpty()]
    $OldName,

    [string]
    [ValidateNotNullOrEmpty()]
    $NewName,

    [switch]
    $Force,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    <#
        .SYNOPSIS
        an internal utility function that updates the name of a DevOp in storage
    #>

    $OldDevOpDirPath = Get-AppeaseDevOpDirPath -Name $OldName -ProjectRootDirPath $ProjectRootDirPath
    $NewDevOpDirPath = Get-AppeaseDevOpDirPath -Name $NewName -ProjectRootDirPath $ProjectRootDirPath
        
    mv $OldDevOpDirPath $NewDevOpDirPath -Force:$Force
}

function Remove-AppeaseDevOp(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [switch]
    $Force,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    <#
        .SYNOPSIS
        Removes a DevOp from storage
    #>
    
    $ConfirmationPromptQuery = "Are you sure you want to remove devop '$Name'?"
    $ConfirmationPromptCaption = 'Confirm Task removal'

    if($Force.IsPresent -or $PSCmdlet.ShouldContinue($ConfirmationPromptQuery,$ConfirmationPromptCaption)){
            
        $DevOpFilePath = Get-AppeaseDevOpFilePath -Name $Name -ProjectRootDirPath $ProjectRootDirPath    
        Remove-Item -Path $DevOpFilePath -Force

    }
}

function Get-AppeaseDevOpTasks(
 
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
}

function Add-AppeaseTask(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $TemplateId,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $TemplateVersion,

    [int]
    [ValidateScript({$_ -gt -1})]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Index,

    [switch]
    $Force,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    <#
        .SYNOPSIS
        an internal utility function that adds a task to a DevOp in storage
    #>    
    
    # fetch DevOp
    $DevOp = Get-AppeaseDevOp -Name $DevOpName -ProjectRootDirPath $ProjectRootDirPath

    # if this is first task
    If(!$DevOp.Tasks){
        $DevOp.Tasks = {@()}.Invoke()
    }

    # guard against unintentionally overwriting existing tasks
    if(!$Force.IsPresent -and ($DevOp.Tasks|?{$_.Name -eq $Name})){
throw `
@"
Task '$Name' already exists in devop '$DevOpName'
for project '$(Resolve-Path $ProjectRootDirPath)'.

Task names must be unique.
If you want to overwrite the existing task use the -Force parameter
"@
    }

    # construct task object
    $Task = @{Name=$Name;TemplateId=$TemplateId;TemplateVersion=$TemplateVersion}

    # add task to dev op 
    $DevOp.Tasks.Insert($Index,$Task)

    # save
    Save-AppeaseDevOp -Name $DevOpName -Task $DevOp.Tasks -ProjectRootDirPath $ProjectRootDirPath

}

function Remove-AppeaseTask(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    $ConfirmationPromptQuery = "Are you sure you want to remove the Task with name $Name`?"
    $ConfirmationPromptCaption = 'Confirm Task removal'

    if($Force.IsPresent -or $PSCmdlet.ShouldContinue($ConfirmationPromptQuery,$ConfirmationPromptCaption)){

        # fetch tasks
        $DevOp = Get-AppeaseDevOp -Name $DevOpName -ProjectRootDirPath $ProjectRootDirPath

        # remove task
        $DevOp.Tasks.Remove(($DevOp.Tasks|?{$_.Name -eq $Name}))|Write-Debug

        # save
        Save-AppeaseDevOp -Name $DevOpName -Task $DevOp.Tasks -ProjectRootDirPath $ProjectRootDirPath

    }   

}

function Rename-AppeaseTask(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $OldName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $NewName,

    [switch]
    $Force,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
        
    # fetch devop
    $DevOp.Tasks = Get-AppeaseDevOp -Name $DevOpName -ProjectRootDirPath $ProjectRootDirPath

    # fetch task
    $Task = $DevOp.Tasks|?{$_.Name -eq $OldName}

    # handle task not found
    if(!$Task){
throw `
@"
Task '$TaskName' not found in dev op '$DevOpName'
for project '$(Resolve-Path $ProjectRootDirPath)'.
"@
    }

    # guard against unintentionally overwriting existing task
    if(!$Force.IsPresent -and ($DevOp.Tasks|?{$_.Name -eq $NewName})){
throw `
@"
Task '$NewName' already exists in dev op '$DevOpName'
for project '$(Resolve-Path $ProjectRootDirPath)'.

Task names must be unique.
If you want to overwrite the existing task use the -Force parameter
"@
    }

    # update name
    $Task.Name = $NewName

    # save
    Save-AppeaseDevOp -Name $DevOpName -Task $DevOp.Tasks -ProjectRootDirPath $ProjectRootDirPath

}

function Set-AppeaseTaskParameter(
    
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $ConfigurationName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $TaskName,

    [Hashtable]
    [ValidateNotNullOrEmpty()]
    [ValidateCount(1,[int]::MaxValue)]
    [Parameter(
        Mandatory=$true)]
    $TaskParameter,

    [switch]
    $Force,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){

    # fetch devop
    $DevOp = Get-AppeaseDevOp -Name $DevOpName -ProjectRootDirPath $ProjectRootDirPath
    
    # handle task not found
    if(!($DevOp.Tasks|?{$_.Name -eq $TaskName})){
throw `
@"
Task '$TaskName' not found in devop '$DevOpName'
for project '$(Resolve-Path $ProjectRootDirPath)'.
"@
    }

    # fetch configuration
    $Configuration = Get-AppeaseConfiguration -Name $ConfigurationName -DevOpName $DevOpName -ProjectRootDirPath $ProjectRootDirPath
    
    # handle first task parameter value mapped
    if(!$Configuration.TaskParameters.$TaskName){
        $Configuration.TaskParameters.$TaskName = @{}
    }

    foreach($TaskParameterEntry in $TaskParameter.GetEnumerator()){
        $ParameterName = $TaskParameterEntry.Key
        $ParameterValue = $TaskParameterEntry.Value
        # guard against unintentionally overwriting existing parameter value
        If(!$Force.IsPresent -and ($Configuration.TaskParameters.$TaskName.$ParameterName)){
throw `
@"
A value of '$($Configuration.TaskParameters.$TaskName.$ParameterName)' has already been set for configuration '$ConfigurationName' 
of devop '$DevOpName' task '$TaskName' parameter '$ParameterName'
in project '$(Resolve-Path $ProjectRootDirPath)'.

If you want to overwrite the existing parameter value use the -Force parameter
"@
        }
        Else{    
            $Configuration.TaskParameters.$TaskName.$ParameterName = $ParameterValue
        }
    }
        
    # build up Save-AppeaseConfiguration parameters
    $SaveAppeaseConfigurationParameters = @{
        Name=$ConfigurationName;
        DevOpName=$DevOpName;
        TaskParameter=$Configuration.TaskParameters;
        ProjectRootDirPath=$ProjectRootDirPath
    }
    if($Configuration.ParentName){
        $SaveAppeaseConfigurationParameters.ParentName = $Configuration.ParentName
    }

    # save
    [PSCustomObject]$SaveAppeaseConfigurationParameters | Save-AppeaseConfiguration
}

function Set-AppeaseTaskTemplateVersion(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $TaskName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $TemplateVersion,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
        
    # get tasks from file
    $Tasks = Get-AppeaseDevOp -Name $DevOpName -ProjectRootDirPath $ProjectRootDirPath | Select -ExpandProperty Tasks

    # fetch task
    $Task = $Tasks|?{$_.Name -eq $TaskName}

    # handle task not found
    if(!$Task){
throw `
@"
Task '$TaskName' not found in devop '$DevOpName'
for project '$(Resolve-Path $ProjectRootDirPath)'.
"@
    }

    # update task version
    $Task.TemplateVersion = $TemplateVersion
    
    # save to file
    Save-AppeaseDevOp -Name $DevOpName -Task $Tasks -ProjectRootDirPath $ProjectRootDirPath

}

function Get-AppeaseConfigurationFilePath(
    
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,
    
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    <#
        .Synopsis
        An Internal utility function that returns the file path of a devop configuration
    #>

    Write-Output "$ProjectRootDirPath\.Appease\DevOps\$DevOpName\Configurations\$Name.json"
}

function Save-AppeaseConfiguration(
    
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,
    
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [Hashtable]
    [ValidateNotNull()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Variable = @{},
    
    [Hashtable]
    [ValidateNotNull()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $TaskParameter = @{},

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ParentName,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    <#
        .Synopsis
        An Internal utility function that saves a devop configuration to disk
    #>
    
    $ConfigurationFilePath = Get-AppeaseConfigurationFilePath  -Name $Name -DevOpName $DevOpName -ProjectRootDirPath $ProjectRootDirPath

    if(!(Test-Path -Path $ConfigurationFilePath)){
        New-Item -ItemType File -Path $ConfigurationFilePath -Force
    }

    $Configuration = @{
        Variables = $Variable;
        TaskParameters = $TaskParameter
    }

    if($ParentName){
        $Configuration.ParentName = $ParentName
    }

    Set-Content $ConfigurationFilePath -Value (ConvertTo-Json -InputObject $Configuration -Depth 12) -Force
}

function Add-AppeaseConfiguration(
    
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,
    
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [switch]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Force,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    <#
        .Synopsis
        Creates a new configuration
    #>

    $ConfigurationFilePath = Get-AppeaseConfigurationFilePath -Name $Name -DevOpName $DevOpName -ProjectRootDirPath $ProjectRootDirPath

    If(!$Force.IsPresent -and (Test-Path $ConfigurationFilePath)){
throw `
@"
configuration "$Name" already exists
for project "$(Resolve-Path $ProjectRootDirPath)".

configuration names must be unique.
If you want to overwrite the existing configuration use the -Force parameter
"@
    }Else{

        Save-AppeaseConfiguration -Name $Name -DevOpName $DevOpName -ProjectRootDirPath $ProjectRootDirPath
    }
}

function Get-AppeaseConfiguration(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    <#
        .SYNOPSIS
        Retrieves a configuration from storage
    #>

    $ConfigurationFilePath = Get-AppeaseConfigurationFilePath -Name $Name -DevOpName $DevOpName -ProjectRootDirPath $ProjectRootDirPath
    
    $Configuration = Get-Content $ConfigurationFilePath | Out-String | ConvertFrom-Json

    # Convert the Variables property from a PSCustomObject to a Hashtable
    $VariablesHashtable = @{}
    $Configuration.Variables.PSObject.Properties | %{$VariablesHashtable[$_.Name] = $_.Value}
    $Configuration.Variables = $VariablesHashtable 
    
    # Convert the TaskParameters property from a PSCustomObject of PSCustomObjects to a Hashtable of Hashtables
    $TaskParametersHashtable = @{}
    foreach($TaskParameter in $Configuration.TaskParameters.PSObject.Properties)
    {
        $TaskName = $TaskParameter.Name
        $TaskParametersHashtable.$TaskName = @{}
        $Configuration.TaskParameters.$TaskName.PSObject.Properties | %{$TaskParametersHashtable.$TaskName[$_.Name] = $_.Value}
    }
    $Configuration.TaskParameters = $TaskParametersHashtable
    
    Write-Output $Configuration

}

function Set-AppeaseConfigurationParentName(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [string]    
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $ParentName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    $Configuration = Get-AppeaseConfiguration -Name $Name -DevOpName $DevOpName -ProjectRootDirPath $ProjectRootDirPath

    # build up Save-AppeaseConfiguration parameters
    $SaveAppeaseConfigurationParameters = @{
        Name = $ConfigurationName;
        DevOpName = $DevOpName;
        ParentName = $ParentName;
        ProjectRootDirPath = $ProjectRootDirPath
    }

    if($Configuration.Variables){
        $SaveAppeaseConfigurationParameters.Variable = $Configuration.Variables
    }

    if($Configuration.TaskParameters){
        $SaveAppeaseConfigurationParameters.TaskParameter = $Configuration.TaskParameters
    }

    [PSCustomObject]$SaveAppeaseConfigurationParameters | Save-AppeaseConfiguration
}

function Set-AppeaseConfigurationVariable(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [PSCustomObject]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Value,

    [string]    
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $ConfigurationName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){
    
    $Configuration = Get-AppeaseConfiguration -Name $ConfigurationName -DevOpName $DevOpName -ProjectRootDirPath $ProjectRootDirPath

    $Configuration.Variables.$Name = $Value

    # build up Save-AppeaseConfiguration parameters
    $SaveAppeaseConfigurationParameters = @{
        Name = $ConfigurationName;
        DevOpName = $DevOpName;
        Variable = $Configuration.Variables;
        ProjectRootDirPath = $ProjectRootDirPath
    }

    if($Configuration.TaskParameters){
        $SaveAppeaseConfigurationParameters.TaskParameter = $Configuration.TaskParameters
    }

    if($Configuration.ParentName){
        $SaveAppeaseConfigurationParameters.ParentName = $Configuration.ParentName
    }

    [PSCustomObject]$SaveAppeaseConfigurationParameters | Save-AppeaseConfiguration

}

function Remove-AppeaseConfigurationVariable(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [string]    
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $ConfigurationName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'

){

    Get-AppeaseConfiguration -Name $ConfigurationName -DevOpName $DevOpName -ProjectRootDirPath $ProjectRootDirPath

    $Configuration.Variables.Remove($Name)

    # build up Save-AppeaseConfiguration parameters
    $SaveAppeaseConfigurationParameters = @{
        Name = $ConfigurationName;
        DevOpName = $DevOpName;
        ProjectRootDirPath = $ProjectRootDirPath
    }

    if($Configuration.Variables){
        $SaveAppeaseConfigurationParameters.Variable = $Configuration.Variables
    }

    if($Configuration.TaskParameters){
        $SaveAppeaseConfigurationParameters.TaskParameter = $Configuration.TaskParameters
    }

    if($Configuration.ParentName){
        $SaveAppeaseConfigurationParameters.ParentName = $Configuration.ParentName
    }

    [PSCustomObject]$SaveAppeaseConfigurationParameters | Save-AppeaseConfiguration

}

function Rename-AppeaseConfiguration(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $OldName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $NewName,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.',

    [switch]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Force

){
    <#
        .SYNOPSIS
        Updates the name of a configuration in storage
    #>
        
    $OldConfigurationFilePath = Get-AppeaseConfigurationFilePath -Name $OldName -DevOpName $DevOpName -ProjectRootDirPath $ProjectRootDirPath
    $NewConfigurationFilePath = Get-AppeaseConfigurationFilePath -Name $NewName -DevOpName $DevOpName -ProjectRootDirPath $ProjectRootDirPath

    mv $OldConfigurationFilePath $NewConfigurationFilePath -Force:$Force

}

function Remove-AppeaseConfiguration(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Name,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $DevOpName,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'
){
    <#
        .SYNOPSIS
        Removes a configuration from storage
    #>
    
    $ConfigurationFilePath = Get-AppeaseConfigurationFilePath -Name $Name -DevOpName $DevOpName -ProjectRootDirPath $ProjectRootDirPath
    
    Remove-Item -Path $ConfigurationFilePath -Force
}

Export-ModuleMember -Function @(

                    # DevOp API
                    'New-AppeaseDevOp',
                    'Get-AppeaseDevOp',
                    'Rename-AppeaseDevOp',
                    'Remove-AppeaseDevOp',

                    # Task API
                    'Add-AppeaseTask',
                    'Remove-AppeaseTask',
                    'Rename-AppeaseTask',
                    'Set-AppeaseTaskParameter',
                    'Set-AppeaseTaskTemplateVersion'
                    
                    # Configuration API
                    'Add-AppeaseConfiguration',
                    'Get-AppeaseConfiguration',
                    'Set-AppeaseConfigurationParentName',
                    'Set-AppeaseConfigurationVariable',
                    'Remove-AppeaseConfigurationVariable',
                    'Rename-AppeaseConfiguration',
                    'Remove-AppeaseConfiguration')
tools\Appease.Client\HashtableExtensions\HashtableExtensions.psm1

function Get-UnionOfHashtables(
[Hashtable]
[ValidateNotNull()]
[Parameter(
    ValueFromPipelineByPropertyName=$true)]
$Source1,

[Hashtable]
[ValidateNotNull()]
[Parameter(
    ValueFromPipelineByPropertyName=$true)]
$Source2){
    $destination = $Source1.Clone()
    Write-Debug "After adding `$Source1, destination is $($destination|Out-String)"

    $Source2.GetEnumerator() | ?{!$destination.ContainsKey($_.Key)} |%{$destination[$_.Key] = $_.Value}
    Write-Debug "After adding `$Source2, destination is $($destination|Out-String)"

    Write-Output $destination
}

Export-ModuleMember -Function Get-UnionOfHashtables
tools\Appease.Client\Install.ps1
# installer based on guidelines provided by Microsoft 
# for installing shared/3rd party powershell modules
# (see: https://msdn.microsoft.com/en-us/library/dd878350%28v=vs.85%29.aspx )

$ModuleName = (gi $PSScriptRoot).Name
if($PSVersionTable.PSVersion.Major -lt 3) {
    Write-Warning "$ModuleName requires PowerShell 3.0 or better; you have version $($Host.Version)."
    return
}

# prepare install dir
$RootInstallationDirPath = "$env:ProgramFiles\Appease"
$RootPowerShellModuleInstallationDirPath = "$RootInstallationDirPath\PowerShell"
$ModuleInstallationDirPath = "$RootPowerShellModuleInstallationDirPath\$ModuleName"

# handle upgrade scenario
if(Test-Path $ModuleInstallationDirPath){
    Write-Debug "removing previous $ModuleName installation"
    . "$PSScriptRoot\Uninstall.ps1"
}

if(!(Test-Path $RootPowerShellModuleInstallationDirPath)){
    New-Item $RootPowerShellModuleInstallationDirPath -ItemType Directory -Force | Out-Null
}

Copy-Item -Path $PSScriptRoot -Destination $RootPowerShellModuleInstallationDirPath -Recurse

$PSModulePath = [Environment]::GetEnvironmentVariable('PSModulePath','Machine')

# if $RootPowerShellModuleInstallationDirPath is not already in path then add it.
if(!($PSModulePath.Split(';').Contains($RootPowerShellModuleInstallationDirPath))){
    Write-Debug "adding $RootPowerShellModuleInstallationDirPath to '$env:PSModulePath'"
    
    # trim trailing semicolon if exists
    $PSModulePath = $PSModulePath.TrimEnd(';');

    # append path to Appease installation
    $PSModulePath += ";$RootPowerShellModuleInstallationDirPath"
    
    # save
    [Environment]::SetEnvironmentVariable('PSModulePath',$PSModulePath,'Machine')    
    
    # make effective in current session
    $env:PSModulePath = $env:PSModulePath + ";$RootPowerShellModuleInstallationDirPath"
}
tools\Appease.Client\TemplateManagement\TemplateManagement.psm1
Import-Module "$PSScriptRoot\Versioning"

$DefaultTemplateSources = @('https://www.myget.org/F/appease')
$NuGetCommand = "nuget"
$ChocolateyCommand = "chocolatey"

function Get-AppeaseTaskTemplateLatestVersion(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName = $true)]
    $Id, 

    [string[]]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $Source = $DefaultTemplateSources

){
    
    $versions = @()

    foreach($templateSource in $Source){
        $uri = "$templateSource/api/v2/package-versions/$Id"
Write-Debug "Attempting to fetch template versions:` uri: $uri "
        $versions = $versions + (Invoke-RestMethod -Uri $uri)
Write-Debug "response from $uri was: ` $versions"
    }
    if(!$versions -or ($versions.Count -lt 1)){
throw "no versions of $Id could be located.` searched: $Source"
    }

Write-Output ([Array](Get-SortedSemanticVersions -InputArray $versions -Descending))[0]
}

function New-NuGetPackage(
    
    [string]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $NuspecFilePath

){

    $OutputDirectory = (gi $NuspecFilePath).DirectoryName
    
    $NugetParameters = @('pack',$NuspecFilePath,'-OutputDirectory',$OutputDirectory)

Write-Debug `
@"
Invoking nuget:
& $NuGetCommand $($NugetParameters|Out-String)
"@
    & $NuGetCommand $NuGetParameters

    # handle errors
    if ($LastExitCode -ne 0) {
        throw $Error
    }
}

function Publish-NuGetPackage(

    [string]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $NupkgFilePath,

    [string]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $SourcePathOrUrl,

    [string]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $ApiKey
    
){

    if(Test-Path $SourcePathOrUrl -PathType Container){
        $SourcePathOrUrl = Resolve-Path $SourcePathOrUrl
    }

    $NuGetParameters = @('push',(Resolve-Path $NupkgFilePath),'-Source',$SourcePathOrUrl)

    if($ApiKey){
        $NuGetParameters = $NuGetParameters + @('-ApiKey',$ApiKey)
    }

Write-Debug `
@"
Invoking nuget:
$NuGetCommand $($NuGetParameters|Out-String)
"@
    & $NuGetCommand $NuGetParameters
        
    # handle errors
    if ($LastExitCode -ne 0) {
        throw $Error
        
    }
}

function Publish-AppeaseTaskTemplateToNuGetFeed(

    [string]    
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName = $true)]
    $TaskTemplateDirPath,
    
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $DestinationPathOrUrl = $DefaultTemplateSources[0],

    [string]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $ApiKey

){

    <#
        .SYNOPSIS
        Publishes a task template to a .nupkg source
    #>

    $TaskTemplateMetadata = Get-AppeaseTaskTemplateMetadata -TaskTemplateDirPath $TaskTemplateDirPath
    
    # generate nuspec xml
$NuspecXmlString =
@"
<?xml version="1.0"?>
<package>
  <metadata>
    <id>$($TaskTemplateMetadata.Id)</id>
    <version>$($TaskTemplateMetadata.Version)</version>
    <authors>$([string]::Join(',',($TaskTemplateMetadata.Maintainers|%{$_.Name})))</authors>
    $(if($TaskTemplateMetadata.ProjectUrl){"<projectUrl>$($TaskTemplateMetadata.ProjectUrl)</projectUrl>"})
    $(if($TaskTemplateMetadata.IconUrl){"<iconUrl>$($TaskTemplateMetadata.IconUrl)</iconUrl>"})
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$($TaskTemplateMetadata.Description)</description>
    $(if($TaskTemplateMetadata.Tags){"<tags>$([string]::Join(" ",$TaskTemplateMetadata.Tags))</tags>"})
  </metadata>
  <files>
    <file src="**" target=""/>
  </files>
</package>
"@
        $NuspecXml = [xml]($NuspecXmlString)

        Try
        {
            # generate a temp nuspec file
            $NuspecFilePath = Join-Path -Path $TaskTemplateDirPath -ChildPath "$([Guid]::NewGuid()).nuspec"
            New-Item -ItemType File -Path $NuspecFilePath -Force
            $NuspecXml.Save($(Resolve-Path $NuspecFilePath))

            # build a nupkg file
            New-NuGetPackage -NuspecFilePath $NuspecFilePath
            $NupkgFilePath = Join-Path -Path $TaskTemplateDirPath -ChildPath "$($TaskTemplateMetadata.Id).$($TaskTemplateMetadata.Version).nupkg"

            # publish nupkg file
            Publish-NuGetPackage -NupkgFilePath $NuPkgFilePath -SourcePathOrUrl $DestinationPathOrUrl -ApiKey $ApiKey
            
        }
        Finally{
            Remove-Item $NuspecFilePath -Force
            Remove-Item $NupkgFilePath -Force
        }
}

function Get-AppeaseTaskTemplateMetadata(

    [string]    
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName = $true)]
    $TaskTemplateDirPath

){
    <#
        .SYNOPSIS
        Retrieves the metadata for a task template
    #>

    $TaskTemplateMetadataFilePath = "$TaskTemplateDirPath\metadata.json"

    if(!(Test-Path $TaskTemplateMetadataFilePath)){
throw `
@"
task template metadata not found for task template
'$TaskTemplateMetadataFilePath'
"@
    }

    $TaskTemplateMetadata = Get-Content $TaskTemplateMetadataFilePath | Out-String | ConvertFrom-Json
    Write-Output $TaskTemplateMetadata
}

function New-AppeaseTaskTemplatePackage(

    [string]    
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName = $true)]
    $Id,
    
    [string]
    [ValidateScript({
        if($_ | Test-SemanticVersion){
            $true
        }
        else{            
            throw "'$_' is not a valid Semantic Version"
        }
    })]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName = $true)]
    $Version,
    
    [string]
    [ValidateCount(1,[int]::MaxValue)]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName = $true)]
    $SourceDirPath,
    
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName = $true)]
    $Description,
    
    [PSCustomObject[]]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName = $true)]
    $Maintainer,

    [string]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName = $true)]
    $InvocationHandler,

    [string]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName = $true)]
    $InvocationCommand,
    
    [PSCustomObject[]]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $InvocationParameter,

    [PSCustomObject]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $Dependency,
    
    [System.Uri]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $IconUrl,

    [System.Uri]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $ProjectUrl,

    [string[]]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $Tag,

    [switch]
    $Force,
    
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName = $true)]
    $DestinationDirPath = '.'

){
    
    $TaskTemplatePackageDirPath = "$DestinationDirPath\$Id.$Version"
    # handle existing task template package dir
    If(Test-Path $TaskTemplatePackageDirPath){

        If($Force.IsPresent){
            Remove-Item -Path $TaskTemplatePackageDirPath -Recurse -Force
        }
        Else{
        
throw `
@"
Task template already exists at:
'$TaskTemplatePackageDirPath'
To overwrite existing task template include the -Force parameter
"@        
        }

    }

    New-Item -Path $TaskTemplatePackageDirPath -ItemType Directory

    # create metadata file 
    $TaskTemplateMetadataFilePath = "$TaskTemplatePackageDirPath\metadata.json"
    New-Item -ItemType File -Path $TaskTemplateMetadataFilePath -Force
    $TaskTemplateMetadata = @{
        Id=$Id;
        Version=$Version;
        Description=$Description;
        [email protected]{
            Handler=$InvocationHandler;
            Command=$InvocationCommand
        };
    }
    if($InvocationParameter){
        $TaskTemplateMetadata.Invocation.Parameters = $InvocationParameter
    }
    if($Maintainer){
        $TaskTemplateMetadata.Maintainers = $Maintainer
    }
    if($Dependency){
        $TaskTemplateMetadata.Dependencies = $Dependencies
    }
    if($IconUrl){
        $TaskTemplateMetadata.IconUrl = $IconUrl
    }
    if($ProjectUrl){
        $TaskTemplateMetadata.ProjectUrl = $ProjectUrl
    }
    if($Tag){
        $TaskTemplateMetadata.Tags = $Tag
    }
    $TaskTemplateMetadata | ConvertTo-Json -Depth 12 | sc -Path $TaskTemplateMetadataFilePath -Force

    # add binaries
    Copy-Item $SourceDirPath "$TaskTemplatePackageDirPath\bin" -Recurse -Container -Force

}

function Get-AppeaseTaskTemplateInstallDirPath(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Id,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Version,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath = '.'){

    "$ProjectRootDirPath\.Appease\Templates\$Id.$Version" | Write-Output
    
}

function Install-NuGetPackage(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Id,

    [string]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Version,

    [string[]]
    [ValidateCount( 1, [Int]::MaxValue)]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Source,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $OutputDirPath
){

    $InitialOFS = $OFS
    
    Try{

        $OFS = ';'        
        $NugetParameters = @('install',$Id,'-Source',($Source|Out-String),'-OutputDirectory',$OutputDirPath,'-Version',$Version,'-NonInteractive')

Write-Debug `
@"
Invoking nuget:
& $NuGetCommand $($NugetParameters|Out-String)
"@
        & $NuGetCommand $NugetParameters

        # handle errors
        if ($LastExitCode -ne 0) {
            throw $Error
        }
    
    }
    Finally{
        $OFS = $InitialOFS
    }

}

function Install-ChocolateyPackage(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Id,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Source,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(        
        ValueFromPipelineByPropertyName=$true)]
    $Version,

    [string]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $InstallArguments,

    [switch]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $OverrideArguments,

    [string]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $PackageParameters,

    [switch]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $AllowMultipleVersions,

    [switch]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $IgnoreDependencies

){
    
    $ChocolateyParameters = @('install',$Id,'--confirm')

    if($Source){
        $ChocolateyParameters += @('--source',$Source)
    }

    if($Version){
        $ChocolateyParameters += @('--version',$Version)
    }

    if($InstallArguments){
        $ChocolateyParameters += @('--install-arguments',$InstallArguments)
    }
        
    if($OverrideArguments.IsPresent){
        $ChocolateyParameters += @('--override-arguments')
    }

    if($PackageParameters){
        $ChocolateyParameters += @('--package-parameters',$PackageParameters)
    }

    if($AllowMultipleVersions.IsPresent){
        $ChocolateyParameters += @('--allow-multiple-versions')
    }

    if($IgnoreDependencies.IsPresent){
        $IgnoreDependencies += @('--ignore-dependencies')
    }

Write-Debug `
@"
Invoking chocolatey:
& $ChocolateyCommand $($ChocolateyParameters|Out-String)
"@

    & $ChocolateyCommand $ChocolateyParameters

    # handle errors
    if ($LastExitCode -ne 0) {
        throw $Error
    }
}

function Install-AppeaseTaskTemplate(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Id,

    [string]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Version,

    [string[]]
    [ValidateCount( 1, [Int]::MaxValue)]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $Source,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath='.'){

    <#
        .SYNOPSIS
        Installs a task template to an environment if it's not already installed
    #>

    if([string]::IsNullOrWhiteSpace($Version)){

        $Version = Get-LatestTemplateVersion -Source $Source -Id $Id

Write-Debug "using greatest available template version : $Version"
    
    }

    # install NuGet package containing task template
    Install-NuGetPackage -Id $Id -Version $Version -Source $Source -OutputDirPath "$ProjectRootDirPath\.Appease\Templates"

    $TaskTemplateDirPath = Get-AppeaseTaskTemplateInstallDirPath -Id $Id -Version $Version -ProjectRootDirPath $ProjectRootDirPath
    $AppeaseTaskTemplateMetadata = Get-AppeaseTaskTemplateMetadata -TaskTemplateDirPath $TaskTemplateDirPath

    # install Chocolatey dependencies
    $AppeaseTaskTemplateMetadata.Dependencies.Chocolatey | %{if($_){$_ | Install-ChocolateyPackage}}
}

function Uninstall-AppeaseTaskTemplate(

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Id,

    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    $Version,

    [string]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [Parameter(
        ValueFromPipelineByPropertyName=$true)]
    $ProjectRootDirPath='.'){

    <#
        .SYNOPSIS
        Uninstalls a task template from an environment if it's installed
    #>

    $TaskTemplateInstallationDir = Get-AppeaseTaskTemplateInstallDirPath -Id $Id -Version $Version -ProjectRootDirPath $ProjectRootDirPath


    If(Test-Path $TaskTemplateInstallationDir){
Write-Debug `
@"
Removing template at:
$TaskTemplateInstallationDir
"@
        Remove-Item $TaskTemplateInstallationDir -Recurse -Force
    }
    Else{
Write-Debug `
@"
No template to remove at:
$TaskTemplateInstallationDir
"@
    }

    #TODO: UNINSTALL DEPENDENCIES ?

}

Export-ModuleMember -Variable 'DefaultTemplateSources'
Export-ModuleMember -Function @(
                                'Get-AppeaseTaskTemplateLatestVersion',
                                'Publish-AppeaseTaskTemplateToNuGetFeed',
                                'Get-AppeaseTaskTemplateMetadata',
                                'New-AppeaseTaskTemplatePackage',
                                'Get-AppeaseTaskTemplateInstallDirPath',
                                'Install-AppeaseTaskTemplate',
                                'Uninstall-AppeaseTaskTemplate')
tools\Appease.Client\TemplateManagement\Versioning\Versioning.psm1
$SemanticVersionRegex = "(?<Major>\d+)\.(?<Minor>\d+)\.(?<Patch>\d+)(?:-(?<PreRelease>[0-9A-Za-z-.]*))?(?:\+(?<Build>[0-9A-Za-z-.]*))?"

function Test-SemanticVersion(
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true,
        ValueFromPipeline=$true)]
    $SemanticVersionString){

    $SemanticVersionString -match $SemanticVersionRegex

}

function ConvertTo-SemanticVersionObject(
    [string]
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Mandatory=$true)]
    $SemanticVersionString){

    <#
        .SYNOPSIS
        creates an object representing a v1.0 & v2.0 semantic version (see: http://semver.org/)
    #>

    $SemanticVersionString -match $SemanticVersionRegex |Out-Null
    $Matches.Remove(0)
    $Matches.Major = [int]$Matches.Major
    $Matches.Minor = [int]$Matches.Minor
    $Matches.Patch = [int]$Matches.Patch

    If($Matches.PreRelease){
        $preReleaseIdentifiers = $Matches.PreRelease.Split('.')|%{if($_ -as [long]){[long]$_}else{[string]$_}}
        $Matches.PreRelease = @{Identifiers=[object[]]$preReleaseIdentifiers}
    }

    $Matches.Clone() | Write-Output
}

function Compare-SemanticVersions(
    
    <#
        .SYNOPSIS
        compares v1.0 & v2.0 semantic versions (see: http://semver.org/)
    #>

    [string]
    $XSemVerString,
    
    [string]
    $YSemVerString){

    $XSemVer = ConvertTo-SemanticVersionObject -SemanticVersionString $XSemVerString
    $YSemVer = ConvertTo-SemanticVersionObject -SemanticVersionString $YSemVerString

    If($XSemVer.Major -ne $YSemVer.Major){
        return $XSemVer.Major - $YSemVer.Major
    }
    ElseIf($XSemVer.Minor -ne $YSemVer.Minor){
        return $XSemVer.Minor - $YSemVer.Minor
    }
    ElseIf($XSemVer.Patch -ne $YSemVer.Patch){
        return $XSemVer.Patch - $YSemVer.Patch
    }

    # per spec: "When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version"
    If(!$XSemVer.PreRelease -and $YSemVer.PreRelease){
        return 1
    }
    ElseIf(!$XSemVer.PreRelease -and !$YSemVer.PreRelease){
        return 0
    }
    ElseIf($XSemVer.PreRelease -and !$YSemVer.PreRelease){
        return -1
    }

    For($i = 0;$i -lt [Math]::Min($XSemVer.PreRelease.Identifiers.Count,$YSemVer.PreRelease.Identifiers.Count);$i++){
        $XIdentifier = $XSemVer.PreRelease.Identifiers[$i]
        $YIdentifier = $YSemVer.PreRelease.Identifiers[$i]
        
        #if x and y numeric
        If(($XIdentifier -is [long]) -and ($YIdentifier -is [long])){
            
            #per spec: "identifiers consisting of only digits are compared numerically"
            $xIdentifierMinusYIdentifier = $XIdentifier - $YIdentifier
            If($xIdentifierMinusYIdentifier -ne 0){
                return $xIdentifierMinusYIdentifier
            }
        }
        #if x or[exclusive] y is numeric
        ElseIf(($XIdentifier -is [long]) -xor ($YIdentifier -is [long])){
        
            #per spec: "Numeric identifiers always have lower precedence than non-numeric identifiers"
            If($XIdentifier -isnot [long]){
                return 1
            }
            Else{
                return -1
            }
        }
        #if x and y both textual
        Else{

            #per spec: "identifiers with letters or hyphens are compared lexically in ASCII sort order"
            If($XIdentifier -gt $YIdentifier){
                return 1
            }
            ElseIf($XIdentifier -lt $YIdentifier){
                return -1
            }
        }
    }

    #per spec: "A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal"
    return $XSemVer.PreRelease.Identifiers.Count - $YSemVer.PreRelease.Identifiers.Count
}

function Get-SortedSemanticVersions(
[string[]]
$InputArray,
[switch]
$Descending){

    <#
        .SYNOPSIS
        sorts v1.0 & v2.0 semantic versions (see: http://semver.org/)
    #>
 
    $counter = 0 
    $compareResultFactor = 1
    if($Descending.IsPresent){
    $compareResultFactor = -1
    }
 
    # $unsorted is the first index of the unsorted region 
    for ($unsorted = 1; $unsorted -lt $InputArray.Count; $unsorted++) 
    { 
        # Next item in the unsorted region 
        $nextItem = $InputArray[$unsorted] 
     
        # Index of insertion in the sorted region 
        $location = $unsorted 
     
        while (($location -gt 0) -and ` 
            (($compareResultFactor *(Compare-SemanticVersions -X $InputArray[$location - 1] -Y $nextItem)) -gt 0)) 
        { 
            $counter++ 
            # Shift to the right 
            $InputArray[$location] = $InputArray[$location - 1] 
            $location-- 
        } 
     
        # Insert $nextItem into the sorted region 
        $InputArray[$location] = $nextItem
    } 

    Write-Output $InputArray
}

Export-ModuleMember -Function @(
                                'Get-SortedSemanticVersions',
                                'Test-SemanticVersion')
tools\Appease.Client\Uninstall.ps1
$ModuleName = (gi $PSScriptRoot).Name
$RootInstallationDirPath = "C:\Program Files\Appease"
$RootPowerShellModuleInstallationDirPath = "$RootInstallationDirPath\PowerShell"
$ModuleInstallationDirPath = "$RootPowerShellModuleInstallationDirPath\$ModuleName"

# make idempotent
if(Test-Path $ModuleInstallationDirPath){
    Remove-Item -Path $ModuleInstallationDirPath -Recurse -Force
}

# make idempotent
if($RootInstallationDirPath -and !(gci $RootPowerShellModuleInstallationDirPath)){
    
    # remove $ModuleInstallationDirPath
    Remove-Item $RootPowerShellModuleInstallationDirPath -Force

    # remove $PSModulePath modification
    $PSModulePath = [Environment]::GetEnvironmentVariable('PSModulePath','Machine')

    $NewPSModulePathParts = @();
    $IsPSModulePathModified = $false
    foreach($part in $PSModulePath.Split(';')){
        if($part -eq $RootPowerShellModuleInstallationDirPath){
            $IsPSModulePathModified = $true
        }
        else{
            $NewPSModulePathParts += $part;        
        }
    }

    $PSModulePath = $NewPSModulePathParts -join ';'

    if($IsPSModulePathModified){
        Write-Debug "updating '$env:PSModulePath' to $PSModulePath"

        # save
        [Environment]::SetEnvironmentVariable('PSModulePath',$PSModulePath,'Machine')
    }
}

if(!(gci $RootInstallationDirPath)){
    
    Remove-Item $RootInstallationDirPath -Force
        
}
tools\chocolateyInstall.ps1
try {    
    
    . "$PSScriptRoot\Appease.Client\Install.ps1"

} catch {

    Write-ChocolateyFailure 'appease.client.powershell' $_.Exception.Message

    throw 
}
tools\chocolateyUninstall.ps1
try {

    . "$PSScriptRoot\Appease.Client\Uninstall.ps1"

} catch {

    Write-ChocolateyFailure 'appease.client.powershell' $_.Exception.Message

    throw 
}

Log in or click on link to see number of positives.

In cases where actual malware is found, the packages are subject to removal. Software sometimes has false positives. Moderators do not necessarily validate the safety of the underlying software, only that a package retrieves software from the official distribution point and/or validate embedded software against official distribution point (where distribution rights allow redistribution).

Chocolatey Pro provides runtime protection from possible malware.

Version Downloads Last Updated Status
appease.client.powershell 0.0.87 317 Thursday, April 2, 2015 Approved
appease.client.powershell 0.0.77 324 Monday, April 13, 2015 Approved
appease.client.powershell 0.0.74 269 Monday, March 30, 2015 Approved

Discussion for the appease.client.powershell Package

Ground Rules:

  • This discussion is only about appease.client.powershell and the appease.client.powershell package. If you have feedback for Chocolatey, please contact the Google Group.
  • This discussion will carry over multiple versions. If you have a comment about a particular version, please note that in your comments.
  • The maintainers of this Chocolatey Package will be notified about new comments that are posted to this Disqus thread, however, it is NOT a guarantee that you will get a response. If you do not hear back from the maintainers after posting a message below, please follow up by using the link on the left side of this page or follow this link to contact maintainers. If you still hear nothing back, please follow the package triage process.
  • Tell us what you love about the package or appease.client.powershell, or tell us what needs improvement.
  • Share your experiences with the package, or extra configuration or gotchas that you've found.
  • If you use a url, the comment will be flagged for moderation until you've been whitelisted. Disqus moderated comments are approved on a weekly schedule if not sooner. It could take between 1-5 days for your comment to show up.
comments powered by Disqus