Deploy.ps1

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$directorySeparator = [System.IO.Path]::DirectorySeparatorChar
$moduleName = $PSScriptRoot.Split($directorySeparator)[-1]

$moduleName = "UblionConnect"

$publicFunctionsPath = $PSScriptRoot + $directorySeparator 
$publicFunctionsPath = ""
$publicFunctionsPath += 'Public' + $directorySeparator + 'ps1'

$publicFunctionFiles = Get-ChildItem -Path $publicFunctionsPath -Recurse | Where-Object {$_.Extension -eq '.ps1'}

$listOfFunctions = @()
$filePaths = @()


$publicFunctionFiles | %{

    $firstFile = $_

    
    # $firstFile = $publicFunctionFiles[0]

    $functionFile = get-content ($firstFile.FullName)

    $filePaths += "."+(($firstFile.FullName -Split $moduleName)[1])

    $functionsInFile =   ($functionFile -match "^function ").Replace("function","").Replace("Function","").Replace("{","").Replace("(","").Replace(")","").Trim()
    $functionsInFile | %{
        $listOfFunctions+= ($_)
    }
}


$functionFile = Get-Content *.psm1

$replaceFunctions = "FunctionsToExport = @('"+($listOfFunctions -join "','")+"')"
$replaceFiles = "FileList = @('"+($filePaths -join "','")+"')"

$file = Get-ChildItem *.psd1
$specFile = Get-Content $file.Name

$specFile = $specFile -replace "FunctionsToExport(.*)",$replaceFunctions

$specFile = $specFile -replace "FileList(.*)",$replaceFiles

#write specFile
Set-Content -Path $file.Name -Value $specFile

$nuspecfile = Get-ChildItem *.nuspec
if (-not $nuspecfile) {
    Write-host -BackgroundColor red NUSPEC file not AVAILABLE
    nuget spec ($file.Name.split('.'))[0]
    throw 'nuspec was not found. please update before rerun'
}


$xml = [xml](Get-Content $nuspecfile)

$xml.SelectNodes("//version") | % { 
    write-host tes is $_."#text" 
    $oldVersion = $_."#text"
    $numbers = $_."#text".Split(".");
    $numbers[2] = [int]$numbers[2] += 1
    $_."#text" = $numbers -join '.'
    }
$xml.Save(((Get-location).Path+ "/" +$nuspecfile.Name))

write-host Replacing psd
$filePSD = Get-ChildItem *.psd1
(get-content $filePSD).replace($oldVersion ,$numbers -join '.') | Out-file -FilePath $filePSD -Force


Remove-Item *.nupkg
nuget pack $nuspecfile 
$filePackage = Get-ChildItem *.nupkg
#nuget push -Source "nuget.org" -ApiKey oy2ipnyjfhwklixog7pqjh3fchttciguxptqspvxwqw5oy ($filePackage.FullName)

[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true};
$publishParam = @{
    Path    = ".\"
    NuGetApiKey = "oy2fhfgvk7hn4grt7z7aniqrnnavopwuzkzehjch7rch6m" 
    Force       = $true
}
Publish-Module @publishParam


#Save-Module -Name Ublion-Topbakkers -Repository LeftPowershell -Path '.'
#Publish-Module -Path 'C:\Users\Sascha Greven\Documents\Tobakkers\Ublion-Topbakkers' -Repository LeftPowershell -NuGetApiKey yfxh774wuu37u4e3v7z7xun3xk24petl6cjcobkv44zp5s5u2pqa