-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Prepare for 1.0.0 release #1
Comments
My current version for the next release of Atom Beautify: https://github.com/Glavin001/atom-beautify/blob/240368fb09efaf67b31f10f0cf7f3fdba46fa65f/src/beautifiers/beautifier.coffee#L80-L131 ###
Get Shell Environment variables
Special thank you to @ioquatix
See https://github.com/ioquatix/script-runner/blob/v1.5.0/lib/script-runner.coffee#L45-L63
###
_envCache: null
_envCacheDate: null
_envCacheExpiry: 10000 # 10 seconds
getShellEnvironment: ->
return new @Promise((resolve, reject) =>
# Check Cache
if @_envCache? and @_envCacheDate?
# Check if Cache is old
if (new Date() - @_envCacheDate) < @_envCacheExpiry
# Still fresh
return resolve(@_envCache)
# Check if Windows
isWin = /^win/.test(process.platform)
if isWin
# Windows
# Use default
resolve(process.env)
else
# Mac & Linux
# I tried using ChildProcess.execFile but there is no way to set detached and
# this causes the child shell to lock up.
# This command runs an interactive login shell and
# executes the export command to get a list of environment variables.
# We then use these to run the script:
child = spawn process.env.SHELL, ['-ilc', 'env'],
# This is essential for interactive shells, otherwise it never finishes:
detached: true,
# We don't care about stdin, stderr can go out the usual way:
stdio: ['ignore', 'pipe', process.stderr]
# We buffer stdout:
buffer = ''
child.stdout.on 'data', (data) -> buffer += data
# When the process finishes, extract the environment variables and pass them to the callback:
child.on 'close', (code, signal) =>
if code isnt 0
return reject(new Error("Could not get Shell Environment. Exit code: "+code+", Signal: "+signal))
environment = {}
for definition in buffer.split('\n')
[key, value] = definition.split('=', 2)
environment[key] = value if key != ''
# Cache Environment
@_envCache = environment
@_envCacheDate = new Date()
resolve(environment)
) It's Promise-style using Bluebird. We can convert it over to callback-style for It supports Window (falls back to |
Regarding cachingI don't believe caching belongs in the core API of this library. It might be the case that we can provide an easy caching wrapper though. I do understand the value of caching the results. The main reason against caching is that some tools change the login environment, e.g. I don't really know what to do in this area, but perhaps there is an easy module for adding caching support or perhaps we should roll our own? Or perhaps we leave it up to application code to make their own caching policy - it isn't that hard? Regarding windows supportPlease feel free to add windows support to the existing code. Regarding promisesIf this makes sense let's make a PR after all the other issues are addressed. The main thing which counts for it IMHO is consistency and ease of use. The thing which counts against it is integration with other systems which might not be able to use that dependency or might want to keep dependencies to a minimum. |
So, on the following points 1/ Naming - changed name to |
Totally agree.
At least we detect Windows now and don't run the detecting environment code, that only works on Linux, on Windows
We could run this in Travis CI. Let's inquire about their Multiple-OS feature: http://docs.travis-ci.com/user/multi-os/
We can probably stick with callback style and the user can make it promise style if they like by wrapping it with promises.
I like having caching and while it is kind of out of scope for this, I think it adds value (since this does take time to process) and would be nice to be reusable, since I may not be the only user who wants it to cache. This way developers, like ourselves, would not be required to implement their own caching layer. |
@huba do you want to have a go at integrating this into script-runner? |
@ioquatix working on it. |
So I'm still getting these https://gist.github.com/huba/6849a7d00475974bfe1e but this time I'm getting useful key-value pairs too. |
I guess the logic for extracting values needs to be improved. One option is to use the "unique identifier" idea I had. Another is to improve the split('=') logic which is probably a bit foobar. Can you give me the entire output of |
Do you think there is any way to do something like |
Yes, this was one of my other ideas:
The output of env goes into fd 4 only. So, you can just read from that. Whether or not that works in node.js/child_process is another thing entirely. |
If you want to have a go at implementing that in Node.js I think that would be a good solution. The only problem I can see is that not all shells will have the same output redirection, i.e. |
Yea there are over 9000 shells (exaggeration) to consider. It seems to work just as |
Yeah, that's why making a generic script-env.py might make sense... It should be possible to guarantee that fd 4 is writable from python and then dump I think in any case, |
So, the reason why this happens when you run it in atom.. using script-runner.. is because script-runner has environment variables being defined incorrectly, and the shell process is picking them up. |
Okay, so I'm going to close this issue now that we are officially using this in script runner. But I'm going to open separate issues for any outstanding things. |
This code is a quick hack/extract of the existing code from script-runner. and was motivated by Glavin001/atom-beautify#164 (comment)
I would suggest the only things that need to really be addressed:
platform.env
on windows?exports
function called? Perhaps an options hash?/cc @Glavin001 @huba
The text was updated successfully, but these errors were encountered: