Powershell GUI fronted (WPF) to run categorized console scripts

āŒˆāŒ‹ āŽ‡ branch:  ClickyColoury


Artifact [17a1f7c10f]

Artifact 17a1f7c10fb97c6b1a37448cf50ef43a25391a49:

  • File tools/plugins/plugin_manager.ps1 — part of check-in [1470549b26] at 2018-05-18 17:54:36 on branch trunk — Add online repo shortcut, disable config option (not going to use plugin scan or add direct download). (user: mario size: 5218)

# api: multitool
# version: 0.3
# title: Plugin Manager
# description: Install/delete user plugins in %APPDATA%
# type: inline
# depends: menu
# category: config
# hidden: 1
# key: plugins?
# config:
#    {"disabled- name: plugin_online_repo, type: str, value: "http://fossil.include-once.org/repo.json/clickycoloury/contrib/*.ps1", description: online repository of additional plugins }
# img: tools
#
#
# Scan contrib/*.ps1 and allow copying files to %APPDATA%/multitool
# Not overly pretty. Just lists some file details with [Install] or [Delete] button.
#


Param(
    $repodir = ".\contrib\",
    $userdir = "$env:APPDATA\multitool\"
)

# scan plugins
Write-Host "ā Scanning available plugins.."
$p_filter = { $_.title -and $_.api -match "ps|powershell|clicky|multitool" -and $_.id -ne "config" }
$p_users = dir $userdir\*.ps1 | % { Extract-PluginMeta $_ } | ? $p_filter
$p_avail = dir $repodir\*.ps1 | % { Extract-PluginMeta $_ } | ? $p_filter | ? { @($p_users) -notcontains @($_) }



#-- Decorative entry for plugin
function Out-PluginDesc($e) {
    $e.Values = $e.Values | % { [Security.SecurityElement]::Escape($_) }
    $xaml = @"
<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Padding="5,2" TextWrapping="Wrap">
<TextBlock.ToolTip>
 <ToolTip Background="#eeeeeeee" Width="400"><TextBlock Width="380" TextWrapping="Wrap">
   <Run Background="Black" Foreground="White">$($e.doc)</Run>
   <LineBreak/>
   <Italic>api</Italic>: <Run Foreground="Red">$($e.api)</Run> <LineBreak/>
   <Italic>state</Italic>: <Bold Foreground="Orange">$($e.state)</Bold> <LineBreak/>
   <Italic>version</Italic>: <Bold Foreground="#ff997711">$($e.version)</Bold> <LineBreak/>
   <Italic>category</Italic>: <Run Foreground="DarkBlue">$($e.category)</Run> <LineBreak/>
   <Italic>icon</Italic>: <Run>$($e.icon)</Run> <LineBreak/>
   <Italic>vars</Italic>: <Run Foreground="Green">$($e.vars | % { $_.name })</Run>
 </TextBlock></ToolTip>
</TextBlock.ToolTip>
 <Bold FontSize="14">$($e.title)</Bold>
 - <Bold Background="#ffffddaa">$($e.version)</Bold>
 - <Italic Foreground="#ff777777">$($e.type)</Italic>
 - <Bold Foreground="#ff222277">$($e.category)</Bold>
 <LineBreak/>
 <Run Foreground="#ff111155" Background="#fff0f3ff">$($e.description)</Run>
</TextBlock>
"@
#    Write-Host $xaml
    [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader ([xml]$xaml)))
}

#-- calbacks
function Install-Plugin($fn) {
    copy $fn $userdir
    $this.Content = "āœ”"
    #$this.visibility = "Hidden"
}
function Uninstall-Plugin($fn) {
    rm $fn
    $this.Content = 'āœ˜';
}


#-- Plugin window
function Run-PluginManager {
    Param(
       $p_avail, $p_users
    )
    $is_inst = $p_users | % { $_.id }
    

    #-- create action widgets
    $installable = @($p_avail | ? {$_} | % {
        $label = "Install"; if ($is_inst -contains $_.id) { $label = "Reinstall" }
        W Separator
        W StackPanel @{Width=500; Background="White"; Orientation="Horizontal"; Add=@(
            (W Button @{Content="$label"; Width=65; Height=35; Add_Click = [scriptblock]::create("Install-Plugin '{0}'" -f $_.fn)}),
            (Out-PluginDesc $_)
        )}
        W Separator
    })
    $removable = @($p_users |  ? {$_} | % {
        W Separator
        W StackPanel @{Width=500; Background="White"; Orientation="Horizontal"; Add=@(
            (W Button @{Content="Delete"; Width=65; Height=35; Add_Click = [scriptblock]::create("Uninstall-Plugin '{0}'" -f $_.fn)}),
            (Out-PluginDesc $_)
        )}
        W Separator
    })

    #-- window
    $w = (W (New-Object System.Windows.Window) @{
      Title="Plugin installation/removal";
      Width=520; Height=450; Top=350; Left=600; TopMost=$true;
      Background="#eef7f7ff";
      Content=(
        W ScrollViewer @{
           Padding="10,5"; HorizontalAlignment="Left";
          Content=(
            W StackPanel @{
              Add=@(
                 @(W Label @{Content="āœŽ Tooltips contain additional script/plugin details.`r`nā˜› Close window when done."}) +
                 @(W Label @{Content="Install new plugins/tools"; FontSize=20}) +
                 $installable +
                 @(W Label @{Content="Remove from %APPDATA% dir"; FontSize=20}) +
                 $removable
                 @(W Label @{Content="Manually manage"; FontSize=20}) +
                 @(W Button @{Content="open %APPDATA%\multitool config dir"; Add_Click={II $userdir}; Width=250; HorizontalAlignment="Left"; Margin=5}),
                 @(W Button @{Content="browse contrib/ plugin dir"; Add_Click={II $repodir}; Width=250; HorizontalAlignment="Left"; Margin=5}),
                 @(W Button @{Content="show ClickyColoury online repo"; Add_Click={Start "http://fossil.include-once.org/clickycoloury/wiki/contrib"}; Width=250; HorizontalAlignment="Left"; Margin=5})
              )
            }
          )
        }
      )
    })#=window
    [void]$w.ShowDialog()
}

Write-Host "ā Show plugin manager.."
Run-PluginManager $p_avail $p_users

Write-Host "ā˜› Restart for changes to take effect."