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

First draft of elm encoder backend #9

Merged
merged 5 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions cli/morphir.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ const util = require('util')
const fs = require('fs')
const readdir = util.promisify(fs.readdir)
const readFile = util.promisify(fs.readFile)
const worker = require('./Morphir.Elm.CLI').Elm.Morphir.Elm.CLI.init()
const packageDefWorker = require('./Morphir.Elm.CLI').Elm.Morphir.Elm.CLI.init()
const elmEncoderWorker = require('./Morphir.Elm.EncodersCLI').Elm.Morphir.Elm.EncodersCLI.init()
Comment on lines +7 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Longer term I think we should just have one worker and use it as a facade to consolidate all the functionality but I'm guessing this was meant to be a temporary solution anyway so OK for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good suggestion. I did think of that but like you said, for the purposes of getting something out quicker I fell short of consolidating it.



worker.ports.decodeError.subscribe(res => {
packageDefWorker.ports.decodeError.subscribe(res => {
console.error(res)
})

worker.ports.packageDefinitionFromSourceResult.subscribe(res => {
packageDefWorker.ports.packageDefinitionFromSourceResult.subscribe(res => {
console.log(JSON.stringify(res))
})

elmEncoderWorker.ports.elmEncoderBackend.subscribe(res => {
console.log(res)
})


const packageInfo = {
name: "morphir",
Expand All @@ -23,10 +28,11 @@ const packageInfo = {


const sourceDir = "../src"
const testDir = "../tests/Morphir/Codec/Tests"

readElmSources(sourceDir)
.then((sourceFiles) => {
worker.ports.packageDefinitionFromSource.send([packageInfo, sourceFiles])
packageDefWorker.ports.packageDefinitionFromSource.send([packageInfo, sourceFiles])
sourceFiles.forEach(element => {
console.log(element.path)
});
Expand All @@ -35,6 +41,17 @@ readElmSources(sourceDir)
console.error(err)
})

readElmSources(testDir)
.then((sourceFiles) => {
console.log ("Generating elm encoders for following:")
sourceFiles.forEach(element => {
console.log(element.path)
});
console.log ("")
elmEncoderWorker.ports.elmFrontEnd.send([packageInfo, sourceFiles])
console.log ("")
})



async function readElmSources(dir) {
Expand Down
131 changes: 131 additions & 0 deletions cli/src/Morphir/Elm/EncodersCLI.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
port module Morphir.Elm.EncodersCLI exposing (..)

import Dict as Dict exposing (..)
import Elm.Syntax.Declaration as S exposing (..)
import Elm.Syntax.Exposing exposing (..)
import Elm.Syntax.File exposing (..)
import Elm.Syntax.Module exposing (..)
import Elm.Syntax.Node exposing (..)
import Elm.Syntax.Range exposing (..)
import Elm.Writer exposing (..)
import Json.Decode as Decode exposing (..)
import Morphir.Elm.Backend.Codec.Gen exposing (..)
import Morphir.Elm.Frontend exposing (..)
import Morphir.IR.AccessControlled exposing (..)
import Morphir.IR.Advanced.Module as Advanced exposing (..)
import Morphir.IR.Name exposing (..)
import Set exposing (..)


main =
Platform.worker
{ init = init, update = update, subscriptions = subscriptions }


type Msg
= PackageDefinitionFromSource ( Decode.Value, List SourceFile )


type alias Model =
()


init : () -> ( Model, Cmd Msg )
init _ =
( (), Cmd.none )


update : Msg -> Model -> ( Model, Cmd Msg )
update (PackageDefinitionFromSource ( _, sourceFiles )) model =
( model, genEncodersFile sourceFiles |> elmEncoderBackend )


subscriptions : Model -> Sub Msg
subscriptions _ =
elmFrontEnd PackageDefinitionFromSource


port elmEncoderBackend : String -> Cmd msg


port elmFrontEnd : (( Decode.Value, List SourceFile ) -> msg) -> Sub msg


genEncodersFile : List SourceFile -> String
genEncodersFile sources =
let
file =
{ moduleDefinition =
emptyRangeNode <|
NormalModule
{ moduleName = emptyRangeNode [ "AEncoders" ]
, exposingList = emptyRangeNode (All emptyRange)
}
, imports = []
, declarations =
case encoderDeclarations sources of
Ok maybList ->
case maybList of
Just list ->
list

Nothing ->
[]

Err _ ->
[]
, comments = []
}
in
writeFile file |> write


encoderDeclarations : List SourceFile -> Result Errors (Maybe (List (Node S.Declaration)))
encoderDeclarations sourceFiles =
packageDefinitionFromSource emptyPackageInfo sourceFiles
|> Result.map .modules
|> Result.map (Dict.get [ [ "a" ] ])
|> Result.map (Maybe.map getEncodersFromModuleDef)


getEncodersFromModuleDef : AccessControlled (Advanced.Definition SourceLocation) -> List (Node S.Declaration)
getEncodersFromModuleDef accessCtrlModuleDef =
case accessCtrlModuleDef of
Public { types, values } ->
Dict.toList types
|> List.map
(\typeNameAndDef ->
typeDefToEncoder emptySourceLocation
(Tuple.first typeNameAndDef)
(Tuple.second typeNameAndDef)
)
|> List.map (Node emptyRange)

_ ->
[]


emptySourceLocation : SourceLocation
emptySourceLocation =
{ source =
{ path = ""
, content = ""
}
, range =
{ start =
{ row = 0
, column = 0
}
, end =
{ row = 0
, column = 0
}
}
}


emptyPackageInfo : PackageInfo
emptyPackageInfo =
{ name = []
, exposedModules = Set.fromList [ [ fromString "a" ] ]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Elm bindings for Morphir",
"scripts": {
"test": "elm-test",
"make-cli": "cd cli && elm make src/Morphir/Elm/CLI.elm --output Morphir.Elm.CLI.js --optimize"
"make-cli": "cd cli && elm make src/Morphir/Elm/CLI.elm --output Morphir.Elm.CLI.js --optimize && elm make src/Morphir/Elm/EncodersCLI.elm --output Morphir.Elm.EncodersCLI.js --optimize"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use a better build tool such as Gulp. Created issue for it: #10

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no experience with JS build tools. Would be fun to know what's out there since I have always been intrigued by the choices available in the JS ecosystem.

},
"repository": {
"type": "git",
Expand Down
Loading