Functions/Open-Project.ps1

# ===========================================================================
# Open-Project.ps1 --------------------------------------------------------
# ===========================================================================

# validation --------------------------------------------------------------
# ---------------------------------------------------------------------------
Class ValidateProjectAlias: System.Management.Automation.IValidateSetValuesGenerator {
    [String[]] GetValidValues() {
        return [String[]] ((Get-ProjectList -Unformatted | Select-Object -ExpandProperty "Alias") + "")
    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Get-PropertyLocation {

    <#
    .DESCRIPTION
        Returns a list with the location of speciefied project property.
     
    .PARAMETER Name
 
    .PARAMETER Property
 
    .OUTPUTS
        ArrayList. List with location of specified property.
    #>


    [CmdletBinding(PositionalBinding=$True)]
    
    [OutputType([System.Collections.ArrayList])]

    Param (
        [ValidateSet([ValidateProjectAlias])]
        [Parameter(Position=1, Mandatory=$True, HelpMessage="Name or alias of an project.")]
        [System.String] $Name,

        [Parameter(Position=2, HelpMessage="Property of project entry, which shall be returned.")]
        [System.String] $Property
    )

    Process{ 

        [System.Array] $selection = Select-Project -Name $Name -Property $Property

        $path_list = [System.Collections.ArrayList]::New()
        if ($selection){
            $selection | ForEach-Object {
                if ($_) {
                    [Void] $path_list.Add($_)
                } 
                else { 
                    Write-FormattedError -Message "Property $($_) of project '$($Name)' is not valid." -Module $SciProfile.Name 
                    return
                }
            }

        }

        return $path_list
    }
}


# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Get-ProjectLocation {

    <#
    .DESCRIPTION
        Returns a list with the location of specified project property.
     
    .PARAMETER Name
 
    .OUTPUTS
        ArrayList. List with location of specified property.
    #>


    [CmdletBinding(PositionalBinding=$True)]
    
    [OutputType([System.Collections.ArrayList])]

    Param (
        [ValidateSet([ValidateProjectAlias])]
        [Parameter(Position=1, Mandatory=$True, HelpMessage="Name or alias of an project.")]
        [System.String] $Name
    )

    Process{ 

        return Get-PropertyLocation -Name $Name -Property "Folder"

    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Get-WebLocation {

    <#
    .DESCRIPTION
        Returns a list with the location of specified project property.
     
    .PARAMETER Name
 
    .OUTPUTS
        ArrayList. List with location of specified property.
    #>
    

    [CmdletBinding(PositionalBinding=$True)]
    
    [OutputType([PSCustomObject])]

    Param (

        [ValidateSet([ValidateProjectAlias])]
        [Parameter(Position=1, HelpMessage="Name or alias of an project.")]
        [System.String] $Name
    )

    Process {
    
        return Get-PropertyLocation -Name $Name -Property "Url"

    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Get-ProjectChildItem {
    
    [CmdletBinding(PositionalBinding=$True)]
    
    [OutputType([PSCustomObject])]

    Param (
        [ValidateSet([ValidateProjectAlias])]
        [Parameter(Position=1, HelpMessage="Name or alias of an project.")]
        [System.String] $Name
    )

    Process{ 
       
        if (-not $Name){
            return Get-ProjectList
        }

        $path_list = Get-ProjectLocation -Name $Name
        foreach ($path in $path_list){
            Get-ChildItem -Path $path
        }
    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Set-ProjectLocation {
    
    [CmdletBinding(PositionalBinding=$True)]
    
    [OutputType([PSCustomObject])]

    Param (
        [ValidateSet([ValidateProjectAlias])]
        [Parameter(Position=1, HelpMessage="Name or alias of an project.")]
        [System.String] $Name
    )

    Process{ 
       
        if (-not $Name){
            return Get-ProjectList
        }

        $path_list = Get-ProjectLocation -Name $Name
        foreach ($path in $path_list){
            Start-Process -FilePath pwsh -WorkingDirectory $path
        }
    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Open-ProjectWorkspace {
    
    [CmdletBinding(PositionalBinding=$True)]
    
    [OutputType([PSCustomObject])]

    Param (
        [ValidateSet([ValidateProjectAlias])]
        [Parameter(Position=1, HelpMessage="Name or alias of an project.")]
        [System.String] $Name
    )

    Process{ 
       
        if (-not $Name){
            return Get-ProjectList
        }

        $path_list = Get-ProjectLocation -Name $Name
        if ($path_list) {
            if (Get-Command "code.cmd" -ErrorAction SilentlyContinue) {
                code --new-window --disable-gpu $($path_list -join " ")
            }
            else { 
                Write-FormattedError -Message "Visual Studio Code is not in system's path or not installed." -Module $SciProfile.Name
            }
        }   
    }
}

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Open-ProjectFileExplorer {
    
    [CmdletBinding(PositionalBinding=$True)]
    
    [OutputType([PSCustomObject])]

    Param (
        [ValidateSet([ValidateProjectAlias])]
        [Parameter(Position=1, HelpMessage="Name or alias of an project.")]
        [System.String] $Name
    )

    Process{ 
       
        if (-not $Name){
            return Get-ProjectList
        }

        $path_list = Get-ProjectLocation -Name $Name
        foreach ($path in $path_list){
            Start-Process -FilePath "explorer.exe" -ArgumentList $path
        }
    }
}
# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Open-ProjectBrowser {
    
    [CmdletBinding(PositionalBinding=$True)]
    
    [OutputType([PSCustomObject])]

    Param (
        [ValidateSet([ValidateProjectAlias])]
        [Parameter(Position=1, HelpMessage="Name or alias of an project.")]
        [System.String] $Name
    )

    Process {
       
        if (-not $Name){
            return Get-ProjectList
        }

        $web_list = Get-WebLocation -Name $Name
        foreach ($url in $web_list){
            Start-Process -FilePath $url
        }
    }
}