From d98fbfe2bf6a22850d817b7dd1520843d5be784a Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Thu, 15 Sep 2022 10:59:33 -0700 Subject: [PATCH] Add starting point for wiring up elm-codegen for Main.elm. --- codegen/Generate.elm | 12 +++- codegen/GenerateMain.elm | 63 +++++++++++++++++++ .../src/generate-template-module-connector.js | 8 ++- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/codegen/Generate.elm b/codegen/Generate.elm index 7cf4996b..9dd65741 100644 --- a/codegen/Generate.elm +++ b/codegen/Generate.elm @@ -18,6 +18,7 @@ import Gen.Path import Gen.Server.Response import Gen.String import Gen.Tuple +import GenerateMain import Pages.Internal.RoutePattern as RoutePattern exposing (RoutePattern) import Pretty import Regex exposing (Regex) @@ -34,8 +35,17 @@ main = Platform.worker { init = \{ 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 = \_ model -> diff --git a/codegen/GenerateMain.elm b/codegen/GenerateMain.elm index e69de29b..dd5dbae6 100644 --- a/codegen/GenerateMain.elm +++ b/codegen/GenerateMain.elm @@ -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" + ] + ) + ] diff --git a/generator/src/generate-template-module-connector.js b/generator/src/generate-template-module-connector.js index 29fae10b..1c0fc81b 100644 --- a/generator/src/generate-template-module-connector.js +++ b/generator/src/generate-template-module-connector.js @@ -38,12 +38,15 @@ async function generateTemplateModuleConnector(basePath, phase) { ], }; } - const routesModule = await runElmCodegenCli( + const elmCodegenFiles = await runElmCodegenCli( sortTemplates(templates), basePath ); + const routesModule = elmCodegenFiles[0].contents; + const newMain = elmCodegenFiles[1].contents; return { + // mainModule: newMain, mainModule: `port module Main exposing (..) import Api @@ -1044,8 +1047,9 @@ async function runElmCodegenCli(templates, basePath) { } }); const filesToGenerate = await promise; + console.dir(filesToGenerate.map((file) => file.path)); - return filesToGenerate[0].contents; + return filesToGenerate; } function emptyRouteParams(name) {