diff --git a/CHANGES.md b/CHANGES.md index e386502..dba39cc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,11 @@ ## 7.0.0b14 (unreleased) +- Add PowerShell support to the `completion` command: auto-detect PowerShell on Windows, + add correct profile path, create directory if missing, and fix success message to use + `. $PROFILE` instead of `source`. + [Anu13lol] + - Save `~/.plonecli/config.toml` with a real TOML writer, so quotes, backslashes and newlines in any value round-trip instead of producing a file plonecli can no longer read. An unreadable config now fails with a message naming the path. @@ -512,3 +517,4 @@ New features: - initital version with list templates support and bobtemplates.plone integration [MrTango, tmassman, Gomez] + diff --git a/plonecli/cli.py b/plonecli/cli.py index 3ef604f..bf7d6a2 100644 --- a/plonecli/cli.py +++ b/plonecli/cli.py @@ -741,7 +741,7 @@ def skill(context, action, scope, copy_only, force): @click.argument( "shell", required=False, - type=click.Choice(["bash", "zsh", "fish"]), + type=click.Choice(["bash", "zsh", "fish", "powershell"]), ) @click.option( "--install", is_flag=True, help="Install completion into your shell config" @@ -755,14 +755,18 @@ def completion(shell, install): import os if shell is None: - login_shell = os.path.basename(os.environ.get("SHELL", "")) - if login_shell in ("bash", "zsh", "fish"): - shell = login_shell + + if os.environ.get("PSModulePath"): + shell = "powershell" else: - raise click.UsageError( - f"Could not detect shell (SHELL={os.environ.get('SHELL', '')!r}).\n" - "Please specify one: plonecli completion bash|zsh|fish" - ) + login_shell = os.path.basename(os.environ.get("SHELL", "")) + if login_shell in ("bash", "zsh", "fish"): + shell = login_shell + else: + raise click.UsageError( + f"Could not detect shell (SHELL={os.environ.get('SHELL', '')!r}).\n" + "Please specify one: plonecli completion bash|zsh|fish" + ) env_var = "_PLONECLI_COMPLETE" source_cmd = f"{env_var}={shell}_source plonecli" @@ -785,6 +789,7 @@ def completion(shell, install): "bash": os.path.expanduser("~/.bashrc"), "zsh": os.path.expanduser("~/.zshrc"), "fish": os.path.expanduser("~/.config/fish/completions/plonecli.fish"), + "powershell": str(Path.home() / "Documents" / "PowerShell" / "Microsoft.PowerShell_profile.ps1"), } rc_file = rc_files[shell] @@ -792,6 +797,9 @@ def completion(shell, install): # Fish uses a completions directory with the script itself os.makedirs(os.path.dirname(rc_file), exist_ok=True) eval_line = f"env {source_cmd} | source" + elif shell == "powershell": + os.makedirs(os.path.dirname(rc_file), exist_ok=True) + eval_line = f'$env:_PLONECLI_COMPLETE = "powershell_source"\nplonecli | Out-String | Invoke-Expression' else: eval_line = f'eval "$({source_cmd})"' @@ -807,8 +815,10 @@ def completion(shell, install): f.write(f"\n# plonecli shell completion\n{eval_line}\n") echo(f"Shell completion installed in {rc_file}", fg="green") - echo(f"Restart your shell or run: source {rc_file}", fg="green") - + if shell == "powershell": + echo("Restart your shell or run: . $PROFILE", fg="green") + else: + echo(f"Restart your shell or run: source {rc_file}", fg="green") if __name__ == "__main__": cli()