-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Extract platform-specifics from ENV to Crystal::System::Env and implement for win32 #6333
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
Merged
RX14
merged 11 commits into
crystal-lang:master
from
straight-shoota:jm/feature/windows-env
Jul 24, 2018
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
05e9b91
Extract Crystal::System::Env interface
straight-shoota be3115e
Implement Crystal::System::Env for win32
straight-shoota 9bf5cb8
Fix use correct bytesize of UTF-16 strings
straight-shoota d2311e1
fixup! Implement Crystal::System::Env for win32
straight-shoota 75715f7
fixup! Extract Crystal::System::Env interface
straight-shoota 7adb2cc
fixup! Implement Crystal::System::Env for win32
straight-shoota af0e517
fixup! Fix use correct bytesize of UTF-16 strings
straight-shoota 7f6d3c1
fixup! Implement Crystal::System::Env for win32
straight-shoota ca1967b
fixup! fixup! Implement Crystal::System::Env for win32
straight-shoota 0b33619
Uncomment specs depending on ENV in file_specs
straight-shoota ce98927
fixup! Uncomment specs depending on ENV in file_specs
straight-shoota File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| module Crystal::System::Env | ||
| # Sets an environment variable or unsets it if *value* is `nil`. | ||
| # def self.set(key : String, value : String?) : Nil | ||
|
|
||
| # Gets an environment variable. | ||
| # def self.get(key : String) : String? | ||
|
|
||
| # Returns `true` if environment variable is set. | ||
| # def self.has_key?(key : String) : Bool | ||
|
|
||
| # Iterates all environment variables. | ||
| # def self.each(&block : String, String ->) | ||
| end | ||
|
|
||
| {% if flag?(:win32) %} | ||
| require "./win32/env" | ||
| {% else %} | ||
| require "./unix/env" | ||
| {% end %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| require "c/stdlib" | ||
|
|
||
| module Crystal::System::Env | ||
| # Sets an environment variable. | ||
| def self.set(key : String, value : String) : Nil | ||
| key.check_no_null_byte("key") | ||
| value.check_no_null_byte("value") | ||
|
|
||
| if LibC.setenv(key, value, 1) != 0 | ||
| raise Errno.new("setenv") | ||
| end | ||
| end | ||
|
|
||
| # Unsets an environment variable. | ||
| def self.set(key : String, value : Nil) : Nil | ||
| key.check_no_null_byte("key") | ||
|
|
||
| if LibC.unsetenv(key) != 0 | ||
| raise Errno.new("unsetenv") | ||
| end | ||
| end | ||
|
|
||
| # Gets an environment variable. | ||
| def self.get(key : String) : String? | ||
| key.check_no_null_byte("key") | ||
|
|
||
| if value = LibC.getenv(key) | ||
| String.new(value) | ||
| end | ||
| end | ||
|
|
||
| # Returns `true` if environment variable is set. | ||
| def self.has_key?(key : String) : Bool | ||
| key.check_no_null_byte("key") | ||
|
|
||
| !!LibC.getenv(key) | ||
| end | ||
|
|
||
| # Iterates all environment variables. | ||
| def self.each(&block : String, String ->) | ||
| environ_ptr = LibC.environ | ||
| while environ_ptr | ||
| environ_value = environ_ptr.value | ||
| if environ_value | ||
| key_value = String.new(environ_value).split('=', 2) | ||
| key = key_value[0] | ||
| value = key_value[1]? || "" | ||
| yield key, value | ||
| environ_ptr += 1 | ||
| else | ||
| break | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| require "crystal/system/windows" | ||
| require "c/winbase" | ||
|
|
||
| module Crystal::System::Env | ||
| # Sets an environment variable or unsets it if *value* is `nil`. | ||
| def self.set(key : String, value : String) : Nil | ||
| key.check_no_null_byte("key") | ||
| value.check_no_null_byte("value") | ||
|
|
||
| if LibC.SetEnvironmentVariableW(key.to_utf16, value.to_utf16) == 0 | ||
| raise WinError.new("SetEnvironmentVariableW") | ||
| end | ||
| end | ||
|
|
||
| # Unsets an environment variable. | ||
| def self.set(key : String, value : Nil) : Nil | ||
| key.check_no_null_byte("key") | ||
|
|
||
| if LibC.SetEnvironmentVariableW(key.to_utf16, nil) == 0 | ||
| raise WinError.new("SetEnvironmentVariableW") | ||
| end | ||
| end | ||
|
|
||
| # Gets an environment variable. | ||
| def self.get(key : String) : String? | ||
| key.check_no_null_byte("key") | ||
|
|
||
| System.retry_wstr_buffer do |buffer, small_buf| | ||
| # `GetEnvironmentVariableW` doesn't set last error on success but we need | ||
| # a success message in order to identify if length == 0 means not found or | ||
| # the value is an empty string. | ||
| LibC.SetLastError(WinError::ERROR_SUCCESS) | ||
| length = LibC.GetEnvironmentVariableW(key.to_utf16, buffer, buffer.size) | ||
|
|
||
| if 0 < length < buffer.size | ||
| return String.from_utf16(buffer[0, length]) | ||
| elsif small_buf && length > 0 | ||
| next length | ||
| else | ||
| case last_error = LibC.GetLastError | ||
| when WinError::ERROR_SUCCESS | ||
| return "" | ||
| when WinError::ERROR_ENVVAR_NOT_FOUND | ||
| return | ||
| else | ||
| raise WinError.new("GetEnvironmentVariableW", last_error) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Returns `true` if environment variable is set. | ||
| def self.has_key?(key : String) : Bool | ||
| key.check_no_null_byte("key") | ||
|
|
||
| buffer = uninitialized UInt16[1] | ||
| LibC.GetEnvironmentVariableW(key.to_utf16, buffer, buffer.size) != 0 | ||
| end | ||
|
|
||
| # Iterates all environment variables. | ||
| def self.each(&block : String, String ->) | ||
| orig_pointer = pointer = LibC.GetEnvironmentStringsW | ||
| raise WinError.new("GetEnvironmentStringsW") if pointer.null? | ||
|
|
||
| begin | ||
| while !pointer.value.zero? | ||
| string, pointer = String.from_utf16(pointer) | ||
| key_value = string.split('=', 2) | ||
| key = key_value[0] | ||
| value = key_value[1]? || "" | ||
| yield key, value | ||
| end | ||
| ensure | ||
| LibC.FreeEnvironmentStringsW(orig_pointer) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please check the diff for #5623 and do this TODO too please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm not sure what you mean... reinstating
%r{\A//}?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, and remove the TODO comment