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 support #124

Open
zimbatm opened this Issue May 24, 2014 · 20 comments

Comments

7 participants
@zimbatm
Copy link
Member

zimbatm commented May 24, 2014

Alternatively to Cygwin, it should be possible to support the Microsoft PowerShell. The only unclear requirement that needs to be researched is if PowerShell would support evaluating code before each prompt.

  • Check how eval works and if there is a hook for executing direnv before each prompt
  • Install bash trough http://mingw.org/wiki/msys
  • Add a new shell_powershell.go that supports generating set instructions from the env diff.

@zimbatm zimbatm added this to the Cygwin support milestone May 24, 2014

@zimbatm zimbatm added the Feature label May 24, 2014

@CMCDragonkai

This comment has been minimized.

Copy link
Contributor

CMCDragonkai commented May 25, 2014

To support window's terminals, 3 types of scripts need to be executed:

  1. script.ps1
  2. script.bat
  3. script.cmd

Powershell runs .ps scripts, CMD should be used to run bat or cmd scripts. There are some minor differences between .bat and .cmd: http://stackoverflow.com/questions/148968/windows-batch-files-bat-vs-cmd (cmd is recommended script format now)

In order to set environment variables in CMD you need to use SET:

SET blah=blah
echo %cmd%

In order set environment variables in powershell (it uses a different syntax) see:

When using Cygwin, any environment variables set in Powershell or CMD are transferred to Cygwin child shell or vice versa.

So I can do this in Cygwin:

export EDITOR="nano"
CMD /C "echo %EDITOR%"

Which echos out nano.

@CMCDragonkai

This comment has been minimized.

Copy link
Contributor

CMCDragonkai commented May 25, 2014

Which means you may need shell_powershell.go and shell_cmd.go. Most windows installations have powershell now, but older installations may only have CMD. I think XP and up?

@CMCDragonkai

This comment has been minimized.

Copy link
Contributor

CMCDragonkai commented May 25, 2014

The starting area for PowerShell scripts is here:

$profile

As explained here: http://technet.microsoft.com/en-au/library/ee692764.aspx

However in order to run ps1 scripts, one has to activate their execution policy:

Here's my profile powershell:

Set-Location $env:USERPROFILE

CMD does not automatically look for any kind of profile script to run. People will need to hook in a script manually upon starting CMD: http://blog.cachemiss.com/articles/My%20cmd.exe%20Profile.pod

@zimbatm

This comment has been minimized.

Copy link
Member

zimbatm commented May 26, 2014

Interesting, thanks for all the material. CMD support is probably too legacy and require too much effort/hacks to work properly.

Apart from msys I also found an independent port of bash: http://win-bash.sourceforge.net/ Not sure what it's worth though.

Found this article on how to sign script: http://blogs.technet.com/b/heyscriptingguy/archive/2010/06/17/hey-scripting-guy-how-can-i-sign-windows-powershell-scripts-with-an-enterprise-windows-pki-part-2-of-2.aspx . According to http://technet.microsoft.com/en-us/magazine/2008.04.powershell.aspx?pr=blog I could also get a SSL cert for direnv.net and use it to sign the script.

@mdekstrand

This comment has been minimized.

Copy link

mdekstrand commented Apr 20, 2016

For PowerShell, it looks like worst-case a custom Prompt function could look up the direnv stuff, and then repeat the default prompt behavior: https://technet.microsoft.com/en-us/library/hh847739.aspx

A quick search isn't revealing any other kind of hooking capabilities, but since the prompt is generated by running a function, that's a natural place to do it.

@zimbatm

This comment has been minimized.

Copy link
Member

zimbatm commented Apr 20, 2016

Cool, thanks for looking that up. Are you planning on working on this further ? I would be keen on adding first-class windows support but don't have a development environment.

@mdekstrand

This comment has been minimized.

Copy link

mdekstrand commented Apr 20, 2016

@zimbatm I'm not sure at present. For one thing, I do not know any Go. I have also been considering implementing DirEnv's core functionality directly in PowerShell.

I am very interested in having something like direnv in PowerShell, and am willing to do some work to get it. So far, have mostly just been trying to document what I know or learn while investigating whether it was already supported, to help whoever does eventually implement it.

@zimbatm

This comment has been minimized.

Copy link
Member

zimbatm commented Apr 20, 2016

Alright that's cool too. The core logic is inside of cmd_export.go which is executed before every shell prompt. Basically it looks for the file and stores a diff inside of an environment variable. If you have a facility to keep a thread running inside of PowerShell you might not have to spawn a new process on each prompt and do that. Also bash is a requirement for direnv which might not fit very well with the windows environment.

@mdekstrand

This comment has been minimized.

Copy link

mdekstrand commented Apr 21, 2016

If Bash is an internal requirement, it seems to me that it'd likely be better for direnv to focus on supporting Unix-like environments (Cygwin, MSYS, etc.) and for a separate project to enable similar functionality in native PowerShell.

@zimbatm

This comment has been minimized.

Copy link
Member

zimbatm commented Apr 21, 2016

That's what's used to parse and execute the .envrc files. I wanted to keep the bash syntax and not re-invent a language. I bet it's possible to ship bash as a standalong executable alongside direnv in windows but yeah, something else might be preferable for PowerShell.

@staxmanade

This comment has been minimized.

Copy link

staxmanade commented May 25, 2016

Hey All,

I'd be interested in this on windows as well... Not sure if it helps, but wrote a custom cd command in PowerShell which give it some fun features. Maybe this could be used as an example on how to hook into the directory changing...

Code here: https://github.com/staxmanade/DevMachineSetup/blob/master/GlobalScripts/Change-Directory.ps1
Some blog posts describing the features here, here, and here

Also, not sure if I have time to work on it as I spend more time in Mac than windows - but if I jump on windows and run into some time I'll see what can be done.

@mcandre

This comment has been minimized.

Copy link

mcandre commented Jun 11, 2016

+1

@petemounce

This comment has been minimized.

Copy link

petemounce commented May 27, 2018

If this is still up for grabs, and no-one else is actively working on it... I know both powershell and enough golang to be dangerous, and would be prepared to test my own work on Windows 10 and Windows Server 2016. Would PRs on that basis be welcome? No promises on timeframe.

I'm not sure how to set things up to prevent them from rotting with regressions on further changes, however - I see travis CI is set up to produce releases, but I didn't see any configuration in it to run tests, and I noticed both various _test.go and what looks like an integration tests directory. Is there a contributing guide that someone could point me at?

@zimbatm

This comment has been minimized.

Copy link
Member

zimbatm commented May 27, 2018

Sounds good!

Is the Linux version of PowerShell close enough to be a realistic target for testing?

@petemounce

This comment has been minimized.

Copy link

petemounce commented May 28, 2018

Not a clue, never used it. Will have to see :)

@petemounce

This comment has been minimized.

Copy link

petemounce commented May 28, 2018

Would a dependency on Windows Subsystem for Linux be acceptable, to provide bash? I would think to look into that, first, as the easiest way to satisfy the dependency, if it also works of course.

@zimbatm

This comment has been minimized.

Copy link
Member

zimbatm commented May 28, 2018

yes if PowerShell for Linux can be installed under WSL then it's fine for development, most likely it will also be installable in Travis

@mcandre

This comment has been minimized.

Copy link

mcandre commented May 30, 2018

Would a dependency on Windows Subsystem for Linux be acceptable

For now, sure! Note that some users configure Git Bash, Cygwin, MSYS, MinGW, and other environments, so testing against bash in these environments would also be helpful.

@klauern

This comment has been minimized.

Copy link

klauern commented Nov 25, 2018

If this is still up for grabs, and no-one else is actively working on it... I know both powershell and enough golang to be dangerous, and would be prepared to test my own work on Windows 10 and Windows Server 2016. Would PRs on that basis be welcome? No promises on timeframe.

Any progress made on this front? I know that with PowerShell Core, this might be an easier to approach problem on CI/CD environments, instead of having to spin up a separate Win10/Server 2016 instance.

@zimbatm

This comment has been minimized.

Copy link
Member

zimbatm commented Nov 25, 2018

I wrote a quick version here: https://gist.github.com/ee97a194d77f162550b0347890eff120

It didn't require to extend direnv itself as PowerShell can read JSON. As you can see I decided to hook into the Global:prompt() since direnv is working better when evaluated before the prompt is displayed (instead of overriding cd).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment