Auto.ps1

#Set-StrictMode -Version Latest
#####################################################
# Auto
#####################################################
<#PSScriptInfo
 
.VERSION 0.15
 
.GUID 602bc07e-a621-4738-8c27-0edf4a4cea8e
 
.AUTHOR David Walker, Sitecore Dave, Radical Dave
 
.COMPANYNAME David Walker, Sitecore Dave, Radical Dave
 
.COPYRIGHT David Walker, Sitecore Dave, Radical Dave
 
.TAGS sitecore powershell local install iis solr
 
.LICENSEURI https://github.com/SitecoreDave/SharedSitecore.SitecoreLocal/blob/main/LICENSE
 
.PROJECTURI https://github.com/SitecoreDave/SharedSitecore.SitecoreLocal
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
.SYNOPSIS
Auto All The Things!
 
.DESCRIPTION
PowerShell script that helps you Automate All The Things!
 
.EXAMPLE
PS> Auto 'name'
 
PS> Auto az armtemplate.json
 
.EXAMPLE
PS> Auto 'name' 'template'
 
.EXAMPLE
PS> Auto 'name' 'template' 'd:\repos'
 
.Link
https://github.com/Radical-Dave/Auto
 
.OUTPUTS
    System.String
#>

[CmdletBinding(SupportsShouldProcess=$true)]
Param(
    [Parameter(Mandatory = $false, Position=0)]
    [string] $action = "help",
    [Parameter(Mandatory = $false, Position=1)]
    [string] $data = "",
    [Parameter(Mandatory = $false, Position=2)]
    [string] $path = "",
    [switch] $Force = $false
)
begin {
    $PSScriptName = ($MyInvocation.MyCommand.Name.Replace(".ps1",""))
    $PSCallingScript = if ($MyInvocation.PSCommandPath) { $MyInvocation.PSCommandPath | Split-Path -Parent } else { $null }
    Write-Verbose "#####################################################"
    Write-Host "# $($PSScriptName):$action $data $path called by:$PSCallingScript" -ForegroundColor White
    
    $StopWatch = New-Object -TypeName System.Diagnostics.Stopwatch
    $StopWatch.Start()

    if (!$path -and $action -ne 'az') {
        if (Test-Path "$PSScriptRoot\$PSScriptName.json") {
            $path = "$PSScriptRoot\$PSScriptName.json"
        } else {
            $profileParent =Split-Path $profile -Parent
            if (Test-Path "$profileParent\$($PSScriptName).json") {
                $path = "$profileParent\$($PSScriptName).json"
            }
        }
    }
    
    Write-Verbose "path:$path"
    #if (!(Test-Path $path)) {
    # throw "ERROR invalid path:$($path)"
    #}

    if ($path) {
        try {
            #$tasks = Get-Content .\auto.json | Out-String | Invoke-Expression
            $config = (Get-Content $path -Raw) | ConvertFrom-Json
            #Write-Host "config:$($config)"
        } catch {
            throw $_
        }
        Write-Verbose "logs:$($config.logs)"
        $tasksNode = $config.psobject.properties["tasks"].value
        #Write-Host "tasksNode:$($tasksNode)"
        $tasks = $tasksNode.PSObject.Properties
        #Write-Host "tasks:$($tasks)"
    }
}
process {
    if ($action -eq 'add' -and $data.IndexOf('=') -gt -1) {
        Write-Host "add task:$data"
        $ds = $data.split('=')
        #if ($config.tasks["$data"]) {
            #if (!Force) {
            # throw "ERROR Task:$action already exists. Use -Force to overwrite."
            #} else {
            # $config.tasks | Add-Member -MemberType NoteProperty -Name "$($ds[0])" -Value "$($ds[1])" -PassThru -Force
            #}
        #} else {
            $config.tasks | Add-Member -MemberType NoteProperty -Name "$($ds[0])" -Value "$($ds[1])" -PassThru -Force
        #}
        $config | ConvertTo-Json | Out-File $path
    } elseif ($action -eq 'del' -or $action -eq 'delete' -and $data) {
        if (!$config -or !$config.tasks) {
            throw 'ERROR no config or config.tasks?'
        }
        Write-Host "delete task:$data"
        $config.tasks.PSObject.Properties.Remove("$data")
        $config | ConvertTo-Json | Out-File $path
    } else {
        $task = @()
        if ($tasks -and $action -ne 'help') {
            $taskProperty = $tasks[$action]
            if ($taskProperty) {
                $task = $taskProperty.value
            }
        }
        if ($task) {
            Write-Verbose "task:$task"
            if ($task -like '*$(data)*') {
                Write-Verbose "data:$data"
                $task = $task.replace('$(data)', "$($data)")
                #$task = $task -replace '$(data)', "$($data)"
            }
            Write-Verbose "task:$task"
            $cmd = $task
            Write-Verbose "cmd:$cmd"
            if ($cmd -like '~\*') {
                $cmd = (Join-Path (Split-Path $profile -Parent) ($task.Remove(0,2)))
            } elseif ($cmd -like '.\*') {
                $cmd = (Join-Path $PSScriptRoot ($task.Remove(0,2)))
            } elseif ($cmd.Substring(0,2) -eq '*\') {
                $name = $cmd.Remove(0,2)
                Write-Host "name:$name"
                $checkPath = $data
                if (!$checkPath) {
                    #$cmd = (Join-Path $PSScriptRoot ($task.Remove(0,2)))
                    $checkPath = "$(Split-Path $path -Parent)\data\tests\nested"
                }                
                
                Write-Host "checkPath:$checkPath"
                $cmd = Get-ChildItem "$checkPath\$name*" -Recurse | Select-Object FullName
                if (!$cmd) {
                    throw "ERROR could not find item $name in $checkPath -Recurse"
                }
            }
            Write-Host "Invoke:$cmd" -ForegroundColor White
            if (!$data) {
                Invoke-Expression -Command $cmd -OutVariable $results
            } else {
                Invoke-Expression -Command "$cmd $data" -OutVariable $results
            }
            Write-Host "RESULTS:$results"
        } else {
            if ($action -ne 'help' -and $action -ne 'az') {
                Write-Host "Task not found in $PSScriptName.json: $action, to add use: -addTask 'AutoScript'" -ForegroundColor White
            }
            if ($action -eq 'az') {
                Write-Host "RUN:$path"

                #todo: finish working with Nick to use set-env (otherwise use set-envs)
                @((Split-Path $profile -Parent),$PSScriptRoot,("$currLocation" -ne "$PSScriptRoot" ? $currLocation : ''),$data