Skip to content

Commit e52fb18

Browse files
authored
Merge pull request #29 from mdevlamynck/env-var
Add support for global variable replacement through env vars
2 parents 4bc1f6c + 268f875 commit e52fb18

25 files changed

+379
-174
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.2.0
4+
5+
New features:
6+
* Add support for global variable replacement through env vars
7+
38
## 1.1.4
49

510
Bug fixes:

doc/Usage.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,19 @@ It also supports variants to handle plural rules. The special variable `%count%`
153153

154154
Finally if there is a `keyname` in the translation path, an extra function is created accepting a keyname variable to choose the translation. This is for convenience and should be used with care, if the keyname does not match any translation the function returns an empty string.
155155

156+
## Replace variable during compile time
157+
158+
Variables can be resolved at compile time using the `envVariables` option :
159+
160+
```
161+
envVariables: {
162+
'{site_name}': 'SITE_NAME', // example of variable in routing
163+
'%site_name%': 'SITE_NAME', // example of variable in translation
164+
}
165+
```
166+
167+
Values are read from `./.env.local` or `./env` :
168+
169+
```
170+
SITE_NAME=value
171+
```

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "elm-symfony-bridge",
3-
"version": "1.1.4",
3+
"version": "1.2.0",
44
"author": "Matthias Devlamynck <[email protected]>",
55
"license": "MIT",
66
"homepage": "https://github.com/mdevlamynck/elm-symfony-bridge#readme",
@@ -42,6 +42,6 @@
4242
"elm-hot": "^1.1.4",
4343
"elm-test": "^0.19.1-revision2",
4444
"elm-verify-examples": "^5.0.0",
45-
"node-elm-compiler": "^5.0.4"
45+
"node-elm-compiler": "^5.0.6"
4646
}
4747
}

parcel/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ This plugin follows parcel's zero-config philosophy and will automatically confi
7070
* `watchExtensions`: Which file extensions to watch (defaults to `php`, `yaml`, `yml`, `xml`)
7171
* `watchConfig`: Which files to watch for configuration changes (defaults to `elm.json`, `elm-package.json`, `package.json`, `composer.json`)
7272
* `dev`: Use symfony's env=dev or env=prod (defaults to parcel's own dev value)
73-
* `generatedCodeFolder`: Where to put generated code. Automatically added to your elm's `source-directories` config (defaults to `elm-stuff/generated-code/elm-symfony-bridge`)
74-
* `tmpFolder`: Where to put temporary (defaults to `elm-stuff/generated-code/elm-symfony-bridge`)
73+
* `elmRoot`: Where to put generated code. Automatically added to your elm's `source-directories` config (defaults to `elm-stuff/generated-code/elm-symfony-bridge`)
74+
* `outputFolder`: Where to put temporary (defaults to `elm-stuff/generated-code/elm-symfony-bridge`)
7575
* `elmVersion`: Elm version the generated code should be compatible with (defaults to 0.19 if a `elm.json` file is present, 0.18 if a `elm-package.json` file is present)
7676
* `enableRouting`: Enable generating routes (defaults to true)
7777
* `urlPrefix`: When dev is true, which prefix to use when generating urls (defaults to `/index.php` or `/app_dev.php` depending on which is found)
7878
* `enableTranslations`: Enable generating translations (defaults to true if the willdurand/js-translation-bundle package is installed)
7979
* `lang`: Lang to use when exporting translations (defaults to the default lang configured in symfony)
80+
* `envVariables`: Variables to replace during compile time
8081

8182
If these rules don't work for you, you can override any of these parameters in your `package.json` under the `elm-symfony-bridge` key like so:
8283

parcel/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ function run(bundler) {
1212
options: {
1313
watch: bundler.options.watch,
1414
dev: !bundler.options.production,
15-
generatedCodeFolder: 'elm-stuff/generated-code/elm-symfony-bridge',
16-
tmpFolder: 'elm-stuff/generated-code/elm-symfony-bridge',
15+
elmRoot: './elm-stuff/generated-code/elm-symfony-bridge',
16+
outputFolder: './elm-stuff/generated-code/elm-symfony-bridge',
1717
elmVersion: '0.19',
1818
enableRouting: true,
1919
urlPrefix: '/index.php',
@@ -43,6 +43,8 @@ function run(bundler) {
4343
function loadConfig(global) {
4444
global.options = utils.overrideDefaultsIfProvided(config.guessImplicit(global), global.options);
4545
global.options = utils.overrideDefaultsIfProvided(config.readExplicit(), global.options);
46+
47+
config.loadEnvVariables(global);
4648
}
4749

4850
function generate(global) {
@@ -53,9 +55,9 @@ function generate(global) {
5355

5456
function prepareFolderForGeneratedCode(global) {
5557
const file = global.options.elmVersion === '0.19' ? 'elm.json' : 'elm-package.json';
56-
fs.editJsonFile(file, (elmConfig) => {
57-
return utils.mapKey(elmConfig, 'source-directories', (sources) => {
58-
return utils.arrayPushIfNotPresent(sources, global.options.generatedCodeFolder);
58+
fs.editJsonFile(file, elmConfig => {
59+
return utils.mapKey(elmConfig, 'source-directories', sources => {
60+
return utils.arrayPushIfNotPresent(sources, global.options.elmRoot);
5961
});
6062
});
6163
}

parcel/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "parcel-plugin-elm-symfony-bridge",
3-
"version": "1.1.4",
3+
"version": "1.2.0",
44
"author": "Matthias Devlamynck <[email protected]>",
55
"license": "MIT",
66
"homepage": "https://github.com/mdevlamynck/elm-symfony-bridge/tree/master/parcel#readme",
@@ -33,6 +33,7 @@
3333
},
3434
"dependencies": {
3535
"chokidar": "^3.4.0",
36+
"dotenv": "^16.0.0",
3637
"glob": "^7.1.6"
3738
},
3839
"devDependencies": {

parcel/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,11 @@ dotenv-expand@^5.1.0:
21482148
resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-5.1.0.tgz#3fbaf020bfd794884072ea26b1e9791d45a629f0"
21492149
integrity sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==
21502150

2151+
dotenv@^16.0.0:
2152+
version "16.0.0"
2153+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.0.tgz#c619001253be89ebb638d027b609c75c26e47411"
2154+
integrity sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q==
2155+
21512156
dotenv@^5.0.0:
21522157
version "5.0.1"
21532158
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-5.0.1.tgz#a5317459bd3d79ab88cff6e44057a6a3fbb1fcef"

src/Main.elm

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ port module Main exposing (main, Msg(..), update, decodeJsValue)
66
77
-}
88

9-
import Dict
9+
import Dict exposing (Dict)
10+
import Dict.Extra as Dict
1011
import Elm exposing (Version(..))
1112
import Json.Decode as Decode exposing (Decoder)
13+
import Json.Decode.Pipeline as Decode
1214
import Json.Encode as Encode exposing (Value)
1315
import Platform exposing (Program, worker)
1416
import Platform.Cmd exposing (Cmd)
@@ -50,6 +52,7 @@ type Msg
5052
= NoOp
5153
| TranspileRouting Routing.Command
5254
| TranspileTranslation Translation.Command
55+
| CommandError String
5356

5457

5558
{-| Run received commands.
@@ -75,54 +78,75 @@ update message =
7578
|> formatResult translation.name
7679
|> encodeTranslationResult
7780

81+
CommandError error ->
82+
Encode.object
83+
[ ( "succeeded", Encode.bool False )
84+
, ( "error", Encode.string error )
85+
]
86+
7887

7988
{-| Decode json commands.
8089
-}
8190
decodeJsValue : Value -> Msg
8291
decodeJsValue =
83-
Decode.decodeValue ((Decode.dict << Decode.dict) Decode.string)
84-
>> Result.toMaybe
85-
>> Maybe.andThen (Dict.toList >> List.head)
86-
>> Maybe.andThen
87-
(\( command, commandArgs ) ->
88-
let
89-
urlPrefix =
90-
Dict.get "urlPrefix" commandArgs
91-
92-
fileName =
93-
Dict.get "name" commandArgs
94-
95-
content =
96-
Dict.get "content" commandArgs
97-
98-
version =
99-
Dict.get "version" commandArgs
100-
|> Maybe.andThen
101-
(\versionString ->
102-
case versionString of
103-
"0.18" ->
104-
Just Elm_0_18
105-
106-
"0.19" ->
107-
Just Elm_0_19
108-
109-
_ ->
110-
Nothing
111-
)
112-
in
113-
case command of
114-
"routing" ->
115-
Maybe.map3 Routing.Command urlPrefix content version
116-
|> Maybe.map TranspileRouting
117-
118-
"translation" ->
119-
Maybe.map3 Translation.Command fileName content version
120-
|> Maybe.map TranspileTranslation
92+
Decode.decodeValue commandDecoder
93+
>> Result.mapError (CommandError << Decode.errorToString)
94+
>> Result.merge
95+
96+
97+
commandDecoder : Decoder Msg
98+
commandDecoder =
99+
Decode.oneOf
100+
[ routingDecoder
101+
, translationDecoder
102+
]
103+
104+
105+
routingDecoder : Decoder Msg
106+
routingDecoder =
107+
Decode.succeed TranspileRouting
108+
|> Decode.required "routing"
109+
(Decode.succeed Routing.Command
110+
|> Decode.required "urlPrefix" Decode.string
111+
|> Decode.required "content" Decode.string
112+
|> Decode.required "version" versionDecoder
113+
|> Decode.required "envVariables" envVariableDecoder
114+
)
115+
116+
117+
translationDecoder : Decoder Msg
118+
translationDecoder =
119+
Decode.succeed TranspileTranslation
120+
|> Decode.required "translation"
121+
(Decode.succeed Translation.Command
122+
|> Decode.required "name" Decode.string
123+
|> Decode.required "content" Decode.string
124+
|> Decode.required "version" versionDecoder
125+
|> Decode.required "envVariables" envVariableDecoder
126+
)
127+
128+
129+
versionDecoder : Decoder Version
130+
versionDecoder =
131+
Decode.string
132+
|> Decode.andThen
133+
(\versionString ->
134+
case versionString of
135+
"0.18" ->
136+
Decode.succeed Elm_0_18
137+
138+
"0.19" ->
139+
Decode.succeed Elm_0_19
121140

122141
_ ->
123-
Nothing
142+
Decode.fail <| "Unsupported version: " ++ versionString
124143
)
125-
>> Maybe.withDefault NoOp
144+
145+
146+
envVariableDecoder : Decoder (Dict String String)
147+
envVariableDecoder =
148+
Decode.dict (Decode.maybe Decode.string)
149+
|> Decode.map (Dict.filterMap (\k maybeV -> maybeV))
126150

127151

128152
{-| Encode transpiled routing results.

src/Routing/Transpiler.elm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type alias Command =
2222
{ urlPrefix : String
2323
, content : String
2424
, version : Version
25+
, envVariables : Dict String String
2526
}
2627

2728

@@ -31,6 +32,7 @@ transpileToElm : Command -> Result String String
3132
transpileToElm command =
3233
command.content
3334
|> readJsonContent
35+
|> Result.map (replaceEnvVariables command.envVariables)
3436
|> Result.andThen parseRouting
3537
|> Result.map (convertToElm command.version command.urlPrefix)
3638

@@ -67,6 +69,15 @@ decodeRouting =
6769
)
6870

6971

72+
replaceEnvVariables : Dict String String -> Dict String JsonRouting -> Dict String JsonRouting
73+
replaceEnvVariables envVariables =
74+
let
75+
replace routing =
76+
Dict.foldl String.replace routing envVariables
77+
in
78+
Dict.map (\k r -> { r | path = replace r.path })
79+
80+
7081
{-| Turns the raw extracted data into our internal representation.
7182
-}
7283
parseRouting : Dict String JsonRouting -> Result String (Dict String Routing)

src/Translation/Transpiler.elm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type alias Command =
2424
{ name : String
2525
, content : String
2626
, version : Version
27+
, envVariables : Dict String String
2728
}
2829

2930

@@ -41,6 +42,7 @@ transpileToElm : Command -> Result String File
4142
transpileToElm command =
4243
command.content
4344
|> readJsonContent
45+
|> Result.map (replaceEnvVariables command.envVariables)
4446
|> Result.andThen parseTranslationDomain
4547
|> Result.map (convertToElm command.version)
4648

@@ -108,6 +110,15 @@ readJsonContent =
108110
)
109111

110112

113+
replaceEnvVariables : Dict String String -> JsonTranslationDomain -> JsonTranslationDomain
114+
replaceEnvVariables envVariables json =
115+
let
116+
replace translation =
117+
Dict.foldl String.replace translation envVariables
118+
in
119+
{ json | translations = Dict.map (\k v -> replace v) json.translations }
120+
121+
111122
{-| Parses the translations into use usable type.
112123
-}
113124
parseTranslationDomain : JsonTranslationDomain -> Result String TranslationDomain

0 commit comments

Comments
 (0)