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

Fixed so PATH modification doesnt error on windows #27

Merged
merged 3 commits into from
Jun 10, 2016

Conversation

gordcorp
Copy link
Contributor

@gordcorp gordcorp commented May 19, 2016

Fix for #25. The tests wouldnt pass on windows, but fixes the error and everything seems to work. Hopefully it works on *nix...

@jnunemaker
Copy link
Collaborator

jnunemaker commented May 19, 2016

It seems like you are trying to change ENV['PATH'] and then set it back to the original, is that correct? If so, you need to use begin/ensure/end or ENV['PATH'] is going to be left dirty. Also, setting ENV['PATH'] like this changes it for the current process, but I don't think it will for the system call. The point of doing PATH in the system call is so that it changes the ghostscript path that imagemagick uses when the imagemagick command is run.

@gordcorp
Copy link
Contributor Author

Yep that's what I'm trying to do. The previous method gave an error on windows. Could also change from backticks to system but I thought that would be riskier and wanted something simple, particularly given the tests dont work for me.
The backtick system call is in a subshell so will inherit the environment from ruby. I've confirmed it does for me on windows when I added a echo %PATH%
I've added in the begin/ensure/end.

@jnunemaker
Copy link
Collaborator

The backtick system call is in a subshell so will inherit the environment from ruby. I've confirmed it does for me on windows when I added a echo %PATH%

Interesting. I guess if this works then it should be fine. I wish we had a good test case for this actually working the whole way through. We'd probably have to vendor ghostscript and imagemagick and somehow do something that ensured we knew we were using the vendored ghostscript version. Sounds like a pain. :)

@jonmagic any thoughts?

@jonmagic
Copy link
Owner

@jonmagic any thoughts?

I'd be keen on trying out http://ruby-doc.org/core-2.3.1/Kernel.html#method-i-system first to see if we can modify the ENV for that single command without having to change PATH back in ensure. I used backticks for this back when that was the only way I knew how to do system calls 😁

@BJgordon would you be up for testing out:

command_env = {}
if @ghostscript_path && @ghostscript_path != DefaultGhostScriptPath
  command_env["PATH"] = "#{File.dirname(@ghostscript_path)}#{File::PATH_SEPARATOR}#{ENV['PATH']}"
end

system(command_env, command.join(" "))

I'm curious if Windows will respect it if we run it that way.

@gordcorp
Copy link
Contributor Author

@jonmagic Yep that code's fine for me on windows.
And ahh I just realised I dont think the original code would have worked as intended: command.unshift("PATH=#{File.dirname(@ghostscript_path)} sets PATH as a shell variable, not in the environment, so even on *nix imagemagick wouldnt see the new PATH. If I'm right, no one has missed this functionality, and would be easiest just to delete the line?

@jnunemaker
Copy link
Collaborator

If I'm right, no one has missed this functionality, and would be easiest just to delete the line?

I don't know if you are right, but it definitely makes a difference on SpeakerDeck. We use it currently to alter the version of ghostscript used by imagemagick and have confirmed that it works as we intended.

@jonmagic
Copy link
Owner

Ok, I think Open3.capture2e is what we want to use here. I think we want to replace these five lines with the following:

command_env = {}
if @ghostscript_path && @ghostscript_path != DefaultGhostScriptPath
  command_env["PATH"] = "#{File.dirname(@ghostscript_path)}#{File::PATH_SEPARATOR}#{ENV['PATH']}"
end

result, status = Open3.capture2e(command_env, command.join(" "))

status.success? || raise(UnprocessablePage, result)

We'll also want to add require "open3" to the top of this file. If we can test this out on Windows and on Speaker Deck to make sure it works for both cases that would be rad.

@gordcorp
Copy link
Contributor Author

Thanks, it works for me on Windows.

@jonmagic
Copy link
Owner

jonmagic commented Jun 1, 2016

@BJgordon if you are up for updating this PR with the suggested changes I can test it with Speaker Deck and then merge if it all works as expected.

@gordcorp
Copy link
Contributor Author

gordcorp commented Jun 2, 2016

@jonmagic sure thing will do

@gordcorp
Copy link
Contributor Author

gordcorp commented Jun 3, 2016

Done.

@jonmagic
Copy link
Owner

Tested. Merging and I will cut a new version of Grim as soon as I get a couple of other improvements in.

@jonmagic jonmagic merged commit 6b8a8e1 into jonmagic:master Jun 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants