elm-pages-v3-beta/codegen/GenerateMain.elm

538 lines
20 KiB
Elm
Raw Normal View History

module GenerateMain exposing (..)
import Elm exposing (File)
import Elm.Annotation as Type
import Elm.Case
import Elm.CodeGen
import Elm.Declare
2022-09-16 00:10:35 +03:00
import Elm.Extra exposing (expose, fnIgnore, topLevelValue)
import Elm.Op
import Elm.Pretty
2022-09-15 23:44:59 +03:00
import Gen.ApiRoute
import Gen.Basics
2022-09-15 22:38:52 +03:00
import Gen.Bytes
2022-09-16 00:30:55 +03:00
import Gen.Bytes.Decode
import Gen.Bytes.Encode
import Gen.CodeGen.Generate exposing (Error)
2022-09-15 23:44:59 +03:00
import Gen.DataSource
import Gen.Head
import Gen.Html
import Gen.Html.Attributes
2022-09-15 23:44:59 +03:00
import Gen.HtmlPrinter
2022-09-15 22:38:52 +03:00
import Gen.Json.Decode
import Gen.Json.Encode
import Gen.List
2022-09-15 23:18:27 +03:00
import Gen.Maybe
2022-09-15 22:31:05 +03:00
import Gen.Pages.Internal.Platform
2022-09-16 00:53:33 +03:00
import Gen.Pages.Internal.RoutePattern
2022-09-15 22:24:31 +03:00
import Gen.Pages.ProgramConfig
import Gen.Path
2022-09-16 00:14:27 +03:00
import Gen.Platform.Sub
import Gen.Server.Response
import Gen.String
import Gen.Tuple
2022-09-15 21:11:05 +03:00
import Gen.Url
import Pages.Internal.RoutePattern as RoutePattern exposing (RoutePattern)
import Pretty
import Regex exposing (Regex)
2022-09-15 23:21:30 +03:00
type Phase
= Browser
| Cli
otherFile : List RoutePattern.RoutePattern -> String -> File
otherFile routes phaseString =
2022-09-15 22:31:05 +03:00
let
2022-09-15 23:21:30 +03:00
phase : Phase
phase =
case phaseString of
"browser" ->
Browser
_ ->
Cli
2022-09-15 22:31:05 +03:00
config :
{ declaration : Elm.Declaration
, reference : Elm.Expression
, referenceFrom : List String -> Elm.Expression
}
config =
2022-09-15 22:48:39 +03:00
{ init = todo
, update = todo
, subscriptions = todo
2022-09-16 00:32:25 +03:00
, sharedData =
Elm.value { name = "template", importFrom = [ "Shared" ], annotation = Nothing }
|> Elm.get "data"
2022-09-15 22:48:39 +03:00
, data = todo
, action = todo
, onActionData = todo
, view = todo
, handleRoute = todo
2022-09-16 01:26:08 +03:00
, getStaticRoutes =
case phase of
Browser ->
Gen.DataSource.succeed (Elm.list [])
Cli ->
getStaticRoutes.reference
|> Gen.DataSource.map (Gen.List.call_.map (Elm.val "Just"))
2022-09-15 23:18:27 +03:00
, urlToRoute =
Elm.value
{ annotation = Nothing
, name = "urlToRoute"
, importFrom = [ "Route" ]
}
, routeToPath =
Elm.fn ( "route", Nothing )
(\route ->
route
|> Gen.Maybe.map
(\value ->
Elm.apply
(Elm.value
{ annotation = Nothing
, name = "routeToPath"
, importFrom = [ "Route" ]
}
)
[ value ]
)
|> Gen.Maybe.withDefault (Elm.list [])
)
2022-09-15 23:23:03 +03:00
, site =
case phase of
Browser ->
Elm.nothing
Cli ->
Elm.just
(Elm.value
{ name = "config"
, annotation = Nothing
, importFrom = [ "Site" ]
}
)
2022-09-16 00:14:27 +03:00
, toJsPort = Elm.val "toJsPort"
, fromJsPort = applyIdentityTo (Elm.val "fromJsPort")
, gotBatchSub =
case phase of
Browser ->
Gen.Platform.Sub.none
Cli ->
applyIdentityTo (Elm.val "gotBatchSub")
2022-09-15 22:52:06 +03:00
, hotReloadData =
2022-09-16 00:14:27 +03:00
applyIdentityTo (Elm.val "hotReloadData")
2022-09-16 00:53:33 +03:00
, onPageChange = Elm.val "OnPageChange"
2022-09-15 22:48:39 +03:00
, apiRoutes = todo
2022-09-16 00:53:33 +03:00
, pathPatterns = pathPatterns.reference
2022-09-15 22:48:39 +03:00
, basePath = todo
2022-09-16 00:14:27 +03:00
, sendPageData = Elm.val "sendPageData"
2022-09-15 22:48:39 +03:00
, byteEncodePageData = todo
, byteDecodePageData = todo
2022-09-16 00:30:55 +03:00
, encodeResponse = encodeResponse.reference
2022-09-15 22:48:39 +03:00
, encodeAction = todo
2022-09-16 00:30:55 +03:00
, decodeResponse = decodeResponse.reference
2022-09-15 23:44:59 +03:00
, globalHeadTags =
case phase of
Browser ->
Elm.nothing
Cli ->
Elm.just globalHeadTags.reference
2022-09-15 22:48:39 +03:00
, cmdToEffect =
Elm.value
{ annotation = Nothing
, name = "fromCmd"
, importFrom = [ "Effect" ]
2022-09-15 22:31:05 +03:00
}
2022-09-15 22:52:06 +03:00
, perform =
Elm.value
{ annotation = Nothing
, name = "perform"
, importFrom = [ "Effect" ]
}
, errorStatusCode =
Elm.value
{ annotation = Nothing
, name = "statusCode"
, importFrom = [ "ErrorPage" ]
}
, notFoundPage =
Elm.value
{ annotation = Nothing
, name = "notFound"
, importFrom = [ "ErrorPage" ]
}
, internalError =
Elm.value
{ annotation = Nothing
, name = "internalError"
, importFrom = [ "ErrorPage" ]
}
, errorPageToData = Elm.val "DataErrorPage____"
, notFoundRoute = Elm.nothing
2022-09-15 22:48:39 +03:00
}
|> Gen.Pages.ProgramConfig.make_.programConfig
|> Elm.withType
(Gen.Pages.ProgramConfig.annotation_.programConfig
(Type.named [] "Msg")
(Type.named [] "Model")
(Type.maybe (Type.named [ "Route" ] "Route"))
(Type.named [] "PageData")
(Type.named [] "ActionData")
(Type.named [ "Shared" ] "Data")
(Type.namedWith [ "Effect" ] "Effect" [ Type.named [] "Msg" ])
(Type.var "mappedMsg")
(Type.named [ "ErrorPage" ] "ErrorPage")
)
|> topLevelValue "config"
2022-09-15 23:46:54 +03:00
2022-09-16 00:53:33 +03:00
pathPatterns :
{ declaration : Elm.Declaration
, reference : Elm.Expression
, referenceFrom : List String -> Elm.Expression
}
pathPatterns =
topLevelValue "routePatterns3"
(routes
|> List.map routePatternToSyntax
|> Elm.list
)
2022-09-15 23:46:54 +03:00
globalHeadTags :
{ declaration : Elm.Declaration
, reference : Elm.Expression
, referenceFrom : List String -> Elm.Expression
}
globalHeadTags =
topLevelValue "globalHeadTags"
(Elm.Op.cons
(Elm.value
{ importFrom = [ "Site" ]
, annotation = Nothing
, name = "config"
}
|> Elm.get "head"
)
(Elm.apply
(Elm.value
{ importFrom = [ "Api" ]
, annotation = Nothing
, name = "routes"
}
)
[ getStaticRoutes.reference
, Gen.HtmlPrinter.values_.htmlToString
]
|> Gen.List.call_.filterMap Gen.ApiRoute.values_.getGlobalHeadTagsDataSource
)
|> Gen.DataSource.call_.combine
|> Gen.DataSource.call_.map Gen.List.values_.concat
|> Elm.withType
(Gen.DataSource.annotation_.dataSource
(Type.list Gen.Head.annotation_.tag)
)
)
2022-09-16 00:30:55 +03:00
encodeResponse :
{ declaration : Elm.Declaration
, reference : Elm.Expression
, referenceFrom : List String -> Elm.Expression
}
encodeResponse =
topLevelValue "encodeResponse"
(Elm.apply
(Elm.value
{ annotation = Nothing
, name = "w3_encode_ResponseSketch"
, importFrom =
[ "Pages", "Internal", "ResponseSketch" ]
}
)
[ Elm.val "w3_encode_PageData"
, Elm.val "w3_encode_ActionData"
, Elm.value
{ annotation = Nothing
, name = "w3_encode_Data"
, importFrom =
[ "Shared" ]
}
]
|> Elm.withType
(Type.function
[ Type.namedWith [ "Pages", "Internal", "ResponseSketch" ]
"ResponseSketch"
[ Type.named [] "PageData"
, Type.named [] "ActionData"
, Type.named [ "Shared" ] "Data"
]
]
Gen.Bytes.Encode.annotation_.encoder
)
)
decodeResponse :
{ declaration : Elm.Declaration
, reference : Elm.Expression
, referenceFrom : List String -> Elm.Expression
}
decodeResponse =
topLevelValue "decodeResponse"
(Elm.apply
(Elm.value
{ annotation = Nothing
, name = "w3_decode_ResponseSketch"
, importFrom =
[ "Pages", "Internal", "ResponseSketch" ]
}
)
[ Elm.val "w3_decode_PageData"
, Elm.val "w3_decode_ActionData"
, Elm.value
{ annotation = Nothing
, name = "w3_decode_Data"
, importFrom =
[ "Shared" ]
}
]
|> Elm.withType
(Type.namedWith [ "Pages", "Internal", "ResponseSketch" ]
"ResponseSketch"
[ Type.named [] "PageData"
, Type.named [] "ActionData"
, Type.named [ "Shared" ] "Data"
]
|> Gen.Bytes.Decode.annotation_.decoder
)
)
2022-09-15 23:46:54 +03:00
getStaticRoutes :
{ declaration : Elm.Declaration
, reference : Elm.Expression
, referenceFrom : List String -> Elm.Expression
}
getStaticRoutes =
2022-09-15 23:49:43 +03:00
topLevelValue "getStaticRoutes"
(Gen.DataSource.combine
2022-09-16 00:05:21 +03:00
(routes
|> List.map
(\route ->
Elm.value
{ name = "route"
, annotation = Nothing
, importFrom = "Route" :: (route |> RoutePattern.toModuleName)
}
|> Elm.get "staticRoutes"
|> Gen.DataSource.map
(Gen.List.call_.map
(if RoutePattern.hasRouteParams route then
Elm.value
{ annotation = Nothing
, name =
(route |> RoutePattern.toModuleName)
|> String.join "__"
, importFrom = [ "Route" ]
}
else
2022-09-16 00:10:35 +03:00
fnIgnore
(Elm.value
{ annotation = Nothing
, name =
(route |> RoutePattern.toModuleName)
|> String.join "__"
, importFrom = [ "Route" ]
}
)
2022-09-16 00:05:21 +03:00
)
)
)
)
2022-09-15 23:49:43 +03:00
|> Gen.DataSource.call_.map Gen.List.values_.concat
|> Elm.withType
(Gen.DataSource.annotation_.dataSource
(Type.list (Type.named [ "Route" ] "Route"))
)
)
2022-09-15 22:31:05 +03:00
in
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"
]
)
2022-09-15 21:11:05 +03:00
, Elm.customType "Msg"
((routes
|> List.map
(\route ->
Elm.variantWith
("Msg"
++ (RoutePattern.toModuleName route |> String.join "__")
)
[ Type.named
("Route"
:: RoutePattern.toModuleName route
)
"Msg"
]
)
)
++ [ Elm.variantWith "MsgGlobal" [ Type.named [ "Shared" ] "Msg" ]
, Elm.variantWith "OnPageChange"
[ Type.record
[ ( "protocol", Gen.Url.annotation_.protocol )
, ( "host", Type.string )
, ( "port_", Type.maybe Type.int )
, ( "path", pathType )
, ( "query", Type.maybe Type.string )
, ( "fragment", Type.maybe Type.string )
, ( "metadata", Type.maybe (Type.named [ "Route" ] "Route") )
]
]
, Elm.variantWith "MsgErrorPage____" [ Type.named [ "ErrorPage" ] "Msg" ]
]
)
, Elm.customType "PageData"
((routes
|> List.map
(\route ->
Elm.variantWith
("Data"
++ (RoutePattern.toModuleName route |> String.join "__")
)
[ Type.named
("Route"
:: RoutePattern.toModuleName route
)
"Data"
]
)
)
++ [ Elm.variant "Data404NotFoundPage____"
, Elm.variantWith "DataErrorPage____" [ Type.named [ "ErrorPage" ] "ErrorPage" ]
]
)
, Elm.customType "ActionData"
(routes
|> List.map
(\route ->
Elm.variantWith
("ActionData"
++ (RoutePattern.toModuleName route |> String.join "__")
)
[ Type.named
("Route"
:: RoutePattern.toModuleName route
)
"ActionData"
]
)
)
2022-09-15 22:31:05 +03:00
, Gen.Pages.Internal.Platform.application config.reference
|> Elm.declaration "main"
|> expose
, config.declaration
2022-09-15 23:49:43 +03:00
, getStaticRoutes.declaration
2022-09-15 22:38:52 +03:00
, Elm.portOutgoing "sendPageData"
(Type.record
[ ( "oldThing", Gen.Json.Encode.annotation_.value )
, ( "binaryPageData", Gen.Bytes.annotation_.bytes )
]
)
2022-09-15 23:44:59 +03:00
, globalHeadTags.declaration
2022-09-16 00:30:55 +03:00
, encodeResponse.declaration
2022-09-16 00:53:33 +03:00
, pathPatterns.declaration
2022-09-16 00:30:55 +03:00
, decodeResponse.declaration
2022-09-15 22:38:52 +03:00
, Elm.portIncoming "hotReloadData"
[ Gen.Bytes.annotation_.bytes ]
2022-09-16 00:14:27 +03:00
, Elm.portOutgoing "toJsPort"
Gen.Json.Encode.annotation_.value
2022-09-15 22:38:52 +03:00
, Elm.portIncoming "fromJsPort"
[ Gen.Json.Decode.annotation_.value ]
, Elm.portIncoming "gotBatchSub"
[ Gen.Json.Decode.annotation_.value ]
]
2022-09-15 21:11:05 +03:00
2022-09-16 00:14:27 +03:00
applyIdentityTo : Elm.Expression -> Elm.Expression
applyIdentityTo to =
Elm.apply to [ Gen.Basics.values_.identity ]
2022-09-15 22:24:31 +03:00
todo : Elm.Expression
todo =
Elm.apply (Elm.val "Debug.todo") [ Elm.string "" ]
2022-09-15 21:11:05 +03:00
pathType : Type.Annotation
pathType =
Type.named [ "Path" ] "Path"
2022-09-16 00:53:33 +03:00
routePatternToSyntax : RoutePattern -> Elm.Expression
routePatternToSyntax route =
Gen.Pages.Internal.RoutePattern.make_.routePattern
{ segments =
route.segments
|> List.map
(\segment ->
case segment of
RoutePattern.StaticSegment name ->
Gen.Pages.Internal.RoutePattern.make_.staticSegment (Elm.string name)
RoutePattern.DynamicSegment name ->
Gen.Pages.Internal.RoutePattern.make_.dynamicSegment (Elm.string name)
)
|> Elm.list
, ending =
route.ending
|> Maybe.map
(\ending ->
case ending of
RoutePattern.Optional name ->
Gen.Pages.Internal.RoutePattern.make_.optional (Elm.string name)
RoutePattern.RequiredSplat ->
Gen.Pages.Internal.RoutePattern.make_.requiredSplat
RoutePattern.OptionalSplat ->
Gen.Pages.Internal.RoutePattern.make_.optionalSplat
)
|> Elm.maybe
}