PackageWriters/InstallerPackageWriter.ps1

function Add-InstallerToolsString {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string] $SavePath,
        [Parameter(Mandatory = $true)]
        [string] $packageName,
        [Parameter(Mandatory = $true)]
        [string] $githubRepo,
        [Parameter(Mandatory = $false)]
        [string] $Regex32bit,
        [Parameter(Mandatory = $false)]
        [string] $Regex64bit,
        [Parameter(Mandatory = $false)]
        [string] $installerType,
        [Parameter(Mandatory = $false)]
        [string] $silentArg
    )
    
    begin 
    {
        # set the install string
        $installStr = `
            "Install-ChocolateyPackage -packageName '$packageName' "

        if ($installerType) {
            $installStr += "-FileType $installerType "
            Write-Verbose "Add the installer type"
            Write-Verbose "the current install string is:"
            Write-Verbose "`t $installStr"
        }
        if ($silentArg) {
            $installStr += "-SilentArgs '$silentArg' "
            Write-Verbose "add the scilent args"
            Write-Verbose "the current install string is:"
            Write-Verbose "`t $installStr"
        }
    }
    
    process 
    {
        # get the url
        $url32, $url64 = Get-DownloadUrl -GithubRepo $githubRepo `
                                                -Regex32Bits $Regex32bit `
                                                -Regex64Bits $Regex64bit

        # assembles the install str
        if ($url32) 
        {
            # get the hash for 32 bit assets and add it to the install str
            Write-Host '32 bit assets found' -ForegroundColor Green
            $hash32 = Get-DownloadFileHash -DownloadUrl $url32
            $installStr += "-Url '$url32' -checksum '$hash32' -checksumType 'sha256' "
            Write-Verbose "the 32 bit info is added to the install string"
            Write-Verbose "the current install string is:"
            Write-Verbose "`t $installStr"
        }
        else 
        {
            Write-Warning '32 bit assets found'
            Read-Host 'press enter to continue and press Ctrl-C to stop'
        }
        if ($url64) 
        {
            # get the hash for 64 bit assets
            Write-Host '64 bit assets found' -ForegroundColor Green
            $hash64 = Get-DownloadFileHash -DownloadUrl $url64
            # add the 64 bit infomation to install str
            $installStr += "-Url64 '$url64' -checksum64 '$hash64' -checksumType64 'sha256' "
            Write-Verbose "the 64 bit info is added to the install string"
            Write-Verbose "the current install string is:"
            Write-Verbose "`t $installStr"
        }
        else 
        {
            Write-Warning '64 bit assets not found'
            Read-Host 'press enter to continue and press Ctrl-C to stop'
        }

        # no url found
        if (-Not($url32 -or $url64)) {
            Write-Error -Category ResourceUnavailable -Message 'url of the download item not found in latest release'
        }
    }
    
    end 
    {
        # add the install string to the end of the chocolateyinstall file
        Write-Verbose "writing the install string to $SavePath\chocolateyinstall.ps1"
        Write-Verbose "the install String is: $installStr"
        $installStr | Add-Content "$SavePath\chocolateyinstall.ps1" -Encoding UTF8

    }
}

function New-InstallerVersionPackage  {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [psobject] $profile,
        [Parameter(Mandatory = $true)]
        [string] $GithubRepo,
        [Parameter(Mandatory = $true)]
        [string] $packageName
    )

    begin 
    {
        # load info from remote release
        $newVersion = Get-RemoteVersion -GithubRepo $GithubRepo
        $releaseNote = Get-ReleaseNote -GithubRepo $GithubRepo

        # load info from the local profile
        $Regex32bit = $profile.$packageName.Regex32bit
        $Regex64bit = $profile.$packageName.Regex64bit
        $packagePath = $profile.$packageName.packagePath
        $templatePath = $profile.$packageName.templatePath
        $installerType = $profile.$packageName.installerType
        $silentArg = $profile.$packageName.silentArg
    }
    
    process 
    {
        # create the path
        $newPackagePath = "$packagePath\Versions\$newVersion"
        # use out-null to redirect the output to null. (do not show out put)
        New-Item $newPackagePath -ItemType Directory -Force -Confirm:$false | Out-Null
        Write-Verbose "creating the new package path: $newPackagePath"

        # copy everything in the template to the new packagePath
        Copy-Item -Path "$templatePath\*" -Destination $newPackagePath -Force -Recurse
        Write-Verbose "Copy all the item from template: $templatePath to the new package path: $newPackagePath"
    
    }

    end 
    {
        # create install scripts
        Add-InstallerToolsString -SavePath "$newPackagePath\tools" `
                                    -packageName $packageName -githubRepo $GithubRepo `
                                    -Regex32bit $Regex32bit -Regex64bit $Regex64bit `
                                    -installerType $installerType -silentArg $silentArg
                                    
        Write-NuspecFile -SavePath $newPackagePath -packageName $packageName -version $newVersion -releaseNote $releaseNote -templatePath $templatePath
        New-VersionLog -packagePath $packagePath -VersionNumber $newVersion
    }
    
    
}

function Update-InstallerChocoPackage {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string] $packageName,
        [Parameter(Mandatory = $false)]
        [bool] $Force
    )
    
    Write-Host "updating $packageName" -ForegroundColor Magenta

    # load variable
    $profile = Read-GTCProfile
    $localVersion = $profile.$packageName.version
    $githubRepo = $profile.$packageName.githubRepo
    $remoteVersion = Get-RemoteVersion -GithubRepo $githubRepo

    # execute if not force
    if (-Not $Force) {
        if($remoteVersion -ne $localVersion) {
            Write-Host "Updating to version $remoteVersion" -ForegroundColor Green
            New-InstallerVersionPackage -profile $profile -GithubRepo $githubRepo -packageName $packageName
            $packageUpdated = $true
        }
        else {
            Write-Host 'remote and local version match, exiting...' -ForegroundColor Green
            $packageUpdated = $false
        }
    }
    # force execute
    else {
        Write-Warning 'Force executing'
        Write-Host "Updating to version $remoteVersion" -ForegroundColor Green
        New-InstallerVersionPackage -profile $profile -GithubRepo $githubRepo -packageName $packageName
        $packageUpdated = $true
    }

    # update the profile
    $profile.$packageName.version = $remoteVersion
    Save-GTCProfile -localProfile $profile

    # tell the upstream whether the package is updated
    return $packageUpdated
}