Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell on Windows support #84

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ tmp/
_site/
Gemfile.lock
.goxc.local.json
*.exe
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ This will define the scmpuff shell functions as well as some handy shortcuts.

[fish]: https://fishshell.com/

For Powershell on Windows, add the following to your `$PROFILE` file:

scmpuff init --shell=pwsh | Out-String | Invoke-Expression


## Usage

Expand Down
6 changes: 6 additions & 0 deletions commands/inits/data/aliases.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function gs { scmpuff_status }
function ga { git add }
function gd { git diff }
function gl { git log }
function gco { git checkout }
function grs { git reset }
19 changes: 19 additions & 0 deletions commands/inits/data/git_wrapper.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
$SCMPUFF_GIT_CMD = Get-Command git | Select-Object -ExpandProperty Definition

function git {
switch -regex -casesensitive($args[0]) {
"^(commit|blame|log|rebase|merge)$" {
& scmpuff exec -- $SCMPUFF_GIT_CMD $args
}
"^(checkout|diff|rm|reset)$" {
& scmpuff exec --relative -- $SCMPUFF_GIT_CMD $args
}
"^add$" {
& scmpuff exec -- $SCMPUFF_GIT_CMD $args
scmpuff_status
}
default {
& $SCMPUFF_GIT_CMD $args
}
}
}
42 changes: 42 additions & 0 deletions commands/inits/data/status_shortcuts.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function scmpuff_status {
$scmpuff_env_char = "e"

# Run scmpuff status command and capture the output
$cmd_output = & scmpuff status --filelist @args

# if there was an error, exit prematurely, and pass along the exit code
$es = $LastExitCode
if ($es -ne 0) {
return $es
}

# Fetch list of files (from first line of script output)
$files = ($cmd_output | Select-Object -First 1) -split '\s+'

# Export numbered env variables for each file
scmpuff_clear_vars
$e = 1
foreach ($file in $files) {
Set-Item "env:$($scmpuff_env_char + $e)" $file
$e++
}

# Print status (from line two onward)
$cmd_output | Select-Object -Skip 1
}

# Clear numbered env variables
function scmpuff_clear_vars {
# Define the environment variable character
$scmpuff_env_char = "e"

# Iterate through environment variables and unset those starting with 'e'
for ($i = 1; $i -le 999; $i++) {
$env_var_i = $scmpuff_env_char + $i
if (Get-Item "env:$($env_var_i)" -ErrorAction Ignore) {
Remove-Item "env:$env_var_i"
} else {
break
}
}
}
8 changes: 6 additions & 2 deletions commands/inits/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ There are a number of flags to customize the shell integration.
fmt.Println(fishCollection.Output(wrapGit, includeAliases))
os.Exit(0)

case "pwsh":
fmt.Println(pwshCollection.Output(wrapGit, includeAliases))
os.Exit(0)

default:
fmt.Fprintf(os.Stderr, "Unrecognized shell '%s'\n", shellType)
os.Exit(1)
Expand Down Expand Up @@ -88,7 +92,7 @@ There are a number of flags to customize the shell integration.
InitCmd.Flags().StringVarP(
&shellType,
"shell", "s", "",
"Output shell type: sh | bash | zsh | fish",
"Output shell type: sh | bash | zsh | fish | pwsh",
)
InitCmd.Flag("shell").NoOptDefVal = defaultShellType()

Expand All @@ -101,7 +105,7 @@ func defaultShellType() string {
if shellenv, ok := os.LookupEnv("SHELL"); ok {
base := filepath.Base(shellenv)
switch base {
case "sh", "bash", "zsh", "fish":
case "sh", "bash", "zsh", "fish", "pwsh":
return base
}
}
Expand Down
15 changes: 15 additions & 0 deletions commands/inits/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@ var scriptStatusShortcuts string
//go:embed data/status_shortcuts.fish
var scriptStatusShortcutsFish string

//go:embed data/status_shortcuts.ps1
var scriptStatusShortcutsPwsh string

//go:embed data/aliases.sh
var scriptAliases string

//go:embed data/aliases.ps1
var scriptAliasesPwsh string

//go:embed data/git_wrapper.sh
var scriptGitWrapper string

//go:embed data/git_wrapper.fish
var scriptGitWrapperFish string

//go:embed data/git_wrapper.ps1
var scriptGitWrapperPwsh string

type scriptCollection struct {
statusShortcuts string
gitWrapper string
Expand All @@ -38,6 +47,12 @@ var fishCollection = scriptCollection{
aliases: scriptAliases,
}

var pwshCollection = scriptCollection{
statusShortcuts: scriptStatusShortcutsPwsh,
gitWrapper: scriptGitWrapperPwsh,
aliases: scriptAliasesPwsh,
}

func (sc scriptCollection) Output(wrapGit, aliases bool) string {
var b strings.Builder
b.WriteString(sc.statusShortcuts)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module github.com/mroth/scmpuff

go 1.18

require github.com/spf13/cobra v1.4.0
require github.com/spf13/cobra v1.8.1

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading