2022-09-12 21:49:02 +03:00
|
|
|
port module Generate exposing (main)
|
2022-09-12 20:16:00 +03:00
|
|
|
|
|
|
|
{-| -}
|
|
|
|
|
2022-09-12 21:49:02 +03:00
|
|
|
import Elm exposing (File)
|
2022-09-13 00:36:54 +03:00
|
|
|
import Elm.Annotation
|
2022-09-13 01:01:59 +03:00
|
|
|
import Elm.Case
|
2022-09-13 02:50:08 +03:00
|
|
|
import Elm.CodeGen
|
2022-09-13 00:36:54 +03:00
|
|
|
import Elm.Op
|
2022-09-13 02:50:08 +03:00
|
|
|
import Elm.Pretty
|
2022-09-13 00:36:54 +03:00
|
|
|
import Gen.Basics
|
2022-09-12 22:16:18 +03:00
|
|
|
import Gen.CodeGen.Generate exposing (Error)
|
2022-09-13 00:36:54 +03:00
|
|
|
import Gen.List
|
|
|
|
import Gen.Path
|
|
|
|
import Gen.Server.Response
|
|
|
|
import Gen.String
|
2022-09-13 01:01:59 +03:00
|
|
|
import Pages.Internal.RoutePattern as RoutePattern exposing (RoutePattern)
|
2022-09-13 02:50:08 +03:00
|
|
|
import Pretty
|
2022-09-12 20:16:00 +03:00
|
|
|
|
|
|
|
|
2022-09-12 21:49:02 +03:00
|
|
|
type alias Flags =
|
|
|
|
{ templates : List (List String)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
main : Program Flags () ()
|
2022-09-12 20:16:00 +03:00
|
|
|
main =
|
2022-09-12 21:49:02 +03:00
|
|
|
Platform.worker
|
|
|
|
{ init =
|
|
|
|
\{ templates } ->
|
|
|
|
( ()
|
|
|
|
, onSuccessSend [ file templates ]
|
|
|
|
)
|
|
|
|
, update =
|
|
|
|
\_ model ->
|
|
|
|
( model, Cmd.none )
|
|
|
|
, subscriptions = \_ -> Sub.none
|
|
|
|
}
|
2022-09-12 20:16:00 +03:00
|
|
|
|
|
|
|
|
2022-09-12 21:49:02 +03:00
|
|
|
file : List (List String) -> Elm.File
|
|
|
|
file templates =
|
|
|
|
let
|
|
|
|
routes : List RoutePattern.RoutePattern
|
|
|
|
routes =
|
|
|
|
templates
|
|
|
|
|> List.filterMap RoutePattern.fromModuleName
|
|
|
|
in
|
2022-09-13 00:36:54 +03:00
|
|
|
Elm.file
|
|
|
|
[ "Route" ]
|
2022-09-12 20:16:00 +03:00
|
|
|
[ Elm.customType "Route"
|
2022-09-12 22:16:18 +03:00
|
|
|
(routes |> List.map RoutePattern.toVariant)
|
2022-09-12 23:43:02 +03:00
|
|
|
|> expose
|
2022-09-13 01:01:59 +03:00
|
|
|
, Elm.declaration "segmentsToRoute"
|
|
|
|
(Elm.fn
|
|
|
|
( "segments"
|
|
|
|
, Elm.Annotation.list Elm.Annotation.string |> Just
|
|
|
|
)
|
|
|
|
(\segments ->
|
2022-09-13 02:50:08 +03:00
|
|
|
(routes
|
2022-09-13 07:45:34 +03:00
|
|
|
|> List.concatMap RoutePattern.routeToBranch
|
2022-09-13 16:35:42 +03:00
|
|
|
|> List.map (Tuple.mapSecond (\constructRoute -> Elm.CodeGen.apply [ Elm.CodeGen.val "Just", constructRoute ]))
|
2022-09-13 02:50:08 +03:00
|
|
|
|> Elm.CodeGen.caseExpr (Elm.CodeGen.val "segments")
|
|
|
|
)
|
|
|
|
|> Elm.Pretty.prettyExpression
|
|
|
|
|> Pretty.pretty 120
|
|
|
|
|> Elm.val
|
2022-09-13 01:01:59 +03:00
|
|
|
|> Elm.withType
|
|
|
|
(Elm.Annotation.named [] "Route"
|
|
|
|
|> Elm.Annotation.maybe
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> expose
|
|
|
|
, Elm.declaration "urlToRoute"
|
|
|
|
(Elm.fn
|
|
|
|
( "url"
|
|
|
|
, Elm.Annotation.extensible "url" [ ( "path", Elm.Annotation.string ) ]
|
|
|
|
|> Just
|
|
|
|
)
|
|
|
|
(\url ->
|
|
|
|
url
|
|
|
|
|> Elm.get "path"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> expose
|
2022-09-12 23:43:02 +03:00
|
|
|
, Elm.declaration "baseUrl" (Elm.string "/")
|
|
|
|
|> expose
|
2022-09-13 00:36:54 +03:00
|
|
|
, Elm.declaration "baseUrlAsPath"
|
|
|
|
(Gen.List.call_.filter
|
|
|
|
(Elm.fn ( "item", Nothing )
|
|
|
|
(\item ->
|
|
|
|
Gen.Basics.call_.not
|
|
|
|
(Gen.String.call_.isEmpty item)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
(Gen.String.call_.split (Elm.string "/")
|
|
|
|
(Elm.val "baseUrl")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> expose
|
|
|
|
, Elm.declaration "toPath"
|
|
|
|
(Elm.fn ( "route", Elm.Annotation.named [] "Route" |> Just )
|
|
|
|
(\route ->
|
|
|
|
Gen.Path.call_.fromString
|
|
|
|
(Gen.String.call_.join
|
|
|
|
(Elm.string "/")
|
|
|
|
(Elm.Op.append
|
|
|
|
(Elm.val "baseUrlAsPath")
|
|
|
|
(Elm.apply (Elm.val "routeToPath")
|
|
|
|
[ route ]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> expose
|
|
|
|
, Elm.declaration "toString"
|
|
|
|
(Elm.fn ( "route", Elm.Annotation.named [] "Route" |> Just )
|
|
|
|
(\route ->
|
|
|
|
Gen.Path.toAbsolute
|
|
|
|
(Elm.apply (Elm.val "toPath") [ route ])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> expose
|
|
|
|
, Elm.declaration "redirectTo"
|
|
|
|
(Elm.fn ( "route", Elm.Annotation.named [] "Route" |> Just )
|
|
|
|
(\route ->
|
|
|
|
Gen.Server.Response.call_.temporaryRedirect
|
|
|
|
route
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> expose
|
2022-09-12 20:16:00 +03:00
|
|
|
]
|
2022-09-12 21:49:02 +03:00
|
|
|
|
|
|
|
|
2022-09-13 01:01:59 +03:00
|
|
|
routeToBranch : RoutePattern -> Elm.Case.Branch
|
|
|
|
routeToBranch route =
|
|
|
|
Elm.Case.branchList 0
|
|
|
|
(\_ ->
|
|
|
|
Elm.val "Index"
|
|
|
|
|> Elm.just
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-09-12 23:43:02 +03:00
|
|
|
expose : Elm.Declaration -> Elm.Declaration
|
|
|
|
expose declaration =
|
|
|
|
declaration
|
|
|
|
|> Elm.exposeWith
|
|
|
|
{ exposeConstructor = True
|
|
|
|
, group = Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-12 21:49:02 +03:00
|
|
|
port onSuccessSend : List File -> Cmd msg
|
|
|
|
|
|
|
|
|
|
|
|
port onFailureSend : List Error -> Cmd msg
|
|
|
|
|
|
|
|
|
|
|
|
port onInfoSend : String -> Cmd msg
|