Add starting point for wiring up elm-codegen for Main.elm.

This commit is contained in:
Dillon Kearns 2022-09-15 10:59:33 -07:00
parent 3bb7c37e4a
commit d98fbfe2bf
3 changed files with 80 additions and 3 deletions

View File

@ -18,6 +18,7 @@ import Gen.Path
import Gen.Server.Response import Gen.Server.Response
import Gen.String import Gen.String
import Gen.Tuple import Gen.Tuple
import GenerateMain
import Pages.Internal.RoutePattern as RoutePattern exposing (RoutePattern) import Pages.Internal.RoutePattern as RoutePattern exposing (RoutePattern)
import Pretty import Pretty
import Regex exposing (Regex) import Regex exposing (Regex)
@ -34,8 +35,17 @@ main =
Platform.worker Platform.worker
{ init = { init =
\{ templates, basePath } -> \{ templates, basePath } ->
let
routes : List RoutePattern.RoutePattern
routes =
templates
|> List.filterMap RoutePattern.fromModuleName
in
( () ( ()
, onSuccessSend [ file templates basePath ] , onSuccessSend
[ file templates basePath
, GenerateMain.otherFile routes
]
) )
, update = , update =
\_ model -> \_ model ->

View File

@ -0,0 +1,63 @@
module GenerateMain exposing (..)
import Elm exposing (File)
import Elm.Annotation as Type
import Elm.Case
import Elm.CodeGen
import Elm.Declare
import Elm.Op
import Elm.Pretty
import Gen.Basics
import Gen.CodeGen.Generate exposing (Error)
import Gen.Html
import Gen.Html.Attributes
import Gen.List
import Gen.Path
import Gen.Server.Response
import Gen.String
import Gen.Tuple
import Pages.Internal.RoutePattern as RoutePattern exposing (RoutePattern)
import Pretty
import Regex exposing (Regex)
otherFile : List RoutePattern.RoutePattern -> File
otherFile routes =
Elm.file [ "Main" ]
[ Elm.alias "Model"
(Type.record
[ ( "global", Type.named [ "Shared" ] "Model" )
, ( "page", Type.named [] "PageModel" )
, ( "current"
, Type.maybe
(Type.record
[ ( "path", Type.named [ "Path" ] "Path" )
, ( "query", Type.named [ "Path" ] "Path" |> Type.maybe )
, ( "fragment", Type.string |> Type.maybe )
]
)
)
]
)
, Elm.customType "PageModel"
((routes
|> List.map
(\route ->
Elm.variantWith
("Model"
++ (RoutePattern.toModuleName route |> String.join "__")
)
[ Type.named
("Route"
:: RoutePattern.toModuleName route
)
"Model"
]
)
)
++ [ Elm.variantWith "ModelErrorPage____"
[ Type.named [ "ErrorPage" ] "Model" ]
, Elm.variant "NotFound"
]
)
]

View File

@ -38,12 +38,15 @@ async function generateTemplateModuleConnector(basePath, phase) {
], ],
}; };
} }
const routesModule = await runElmCodegenCli( const elmCodegenFiles = await runElmCodegenCli(
sortTemplates(templates), sortTemplates(templates),
basePath basePath
); );
const routesModule = elmCodegenFiles[0].contents;
const newMain = elmCodegenFiles[1].contents;
return { return {
// mainModule: newMain,
mainModule: `port module Main exposing (..) mainModule: `port module Main exposing (..)
import Api import Api
@ -1044,8 +1047,9 @@ async function runElmCodegenCli(templates, basePath) {
} }
}); });
const filesToGenerate = await promise; const filesToGenerate = await promise;
console.dir(filesToGenerate.map((file) => file.path));
return filesToGenerate[0].contents; return filesToGenerate;
} }
function emptyRouteParams(name) { function emptyRouteParams(name) {