Skip to content

Commit 29408a5

Browse files
authored
feat(beam): support Environment.CurrentDirectory and GetEnvironmentVariable (#4801)
1 parent eb31073 commit 29408a5

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/Fable.Transforms/Beam/Replacements.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5775,6 +5775,20 @@ let tryCall
57755775
| "System.Environment" ->
57765776
match info.CompiledName with
57775777
| "get_NewLine" -> makeStrConst "\n" |> Some
5778+
| "GetEnvironmentVariable" ->
5779+
Helper.LibCall(
5780+
com,
5781+
"fable_environment",
5782+
"get_environment_variable",
5783+
t,
5784+
args,
5785+
info.SignatureArgTypes,
5786+
?loc = r
5787+
)
5788+
|> Some
5789+
| "get_CurrentDirectory" ->
5790+
Helper.LibCall(com, "fable_environment", "get_current_directory", t, [], ?loc = r)
5791+
|> Some
57785792
| _ -> None
57795793
| Types.datetime -> dates com ctx r t info thisArg args
57805794
| "System.Uri" -> uris com ctx r t info thisArg args
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-module(fable_environment).
2+
-export([
3+
get_environment_variable/1,
4+
get_current_directory/0
5+
]).
6+
7+
-spec get_environment_variable(binary()) -> binary() | undefined.
8+
-spec get_current_directory() -> binary().
9+
10+
get_environment_variable(Name) ->
11+
case os:getenv(unicode:characters_to_list(Name)) of
12+
false -> undefined;
13+
Value -> unicode:characters_to_binary(Value)
14+
end.
15+
16+
get_current_directory() ->
17+
{ok, Dir} = file:get_cwd(),
18+
unicode:characters_to_binary(Dir).

tests/Beam/EnvironmentTests.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Fable.Tests.Environment
2+
3+
open System
4+
open Util.Testing
5+
6+
[<Fact>]
7+
let ``test Environment.GetEnvironmentVariable returns null for a missing variable`` () =
8+
Environment.GetEnvironmentVariable("__FABLE_NON_EXISTENT_VAR__")
9+
|> equal null
10+
11+
[<Fact>]
12+
let ``test Environment.CurrentDirectory works`` () =
13+
Environment.CurrentDirectory.Length > 0
14+
|> equal true

tests/Beam/Fable.Tests.Beam.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<Compile Include="QueueTests.fs" />
7474
<Compile Include="StackTests.fs" />
7575
<Compile Include="EncodingTests.fs" />
76+
<Compile Include="EnvironmentTests.fs" />
7677
<Compile Include="ApplicativeTests.fs" />
7778
<Compile Include="MiscTests.fs" />
7879
<Compile Include="TimeSpanTests.fs" />

0 commit comments

Comments
 (0)