Skip to content

Commit

Permalink
Add boilerplate for elm-codegen.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonkearns committed Sep 12, 2022
1 parent a0fa6c2 commit ba6f090
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ tests/VerifyExamples/
cypress/videos
cypress/screenshots
.idea
generated/

2 changes: 2 additions & 0 deletions codegen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Gen/

24 changes: 24 additions & 0 deletions codegen/Generate.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Generate exposing (main)

{-| -}

import Elm
import Elm.Annotation as Type
import Gen.CodeGen.Generate as Generate
import Gen.Helper


main : Program {} () ()
main =
Generate.run
[ file
]


file : Elm.File
file =
Elm.file [ "Route" ]
[ Elm.customType "Route"
[ Elm.variant "Index"
]
]
13 changes: 13 additions & 0 deletions codegen/elm.codegen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"elm-codegen-version": "0.2.0",
"codegen-helpers": {
"packages": {
"elm/core": "1.0.5",
"elm/html": "1.0.0"
},
"local": [
"codegen/helpers/",
"src/"
]
}
}
34 changes: 34 additions & 0 deletions codegen/elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

{
"type": "application",
"source-directories": [
"."
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/json": "1.1.3",
"mdgriffith/elm-codegen": "2.0.0"
},
"indirect": {
"elm/parser": "1.1.0",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2",
"elm-community/basics-extra": "4.1.0",
"elm-community/list-extra": "8.6.0",
"miniBill/elm-unicode": "1.0.2",
"rtfeldman/elm-hex": "1.0.0",
"stil4m/elm-syntax": "7.2.9",
"stil4m/structured-writer": "1.0.3",
"the-sett/elm-pretty-printer": "3.0.0"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
21 changes: 15 additions & 6 deletions generator/src/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ global.builtAt = new Date();
* @param {string} basePath
*/
async function generate(basePath) {
const cliCode = generateTemplateModuleConnector(basePath, "cli");
const browserCode = generateTemplateModuleConnector(basePath, "browser");
const cliCode = await generateTemplateModuleConnector(basePath, "cli");
const browserCode = await generateTemplateModuleConnector(
basePath,
"browser"
);
ensureDirSync("./elm-stuff");
ensureDirSync("./.elm-pages");
ensureDirSync("./gen");
Expand Down Expand Up @@ -46,10 +49,13 @@ async function generate(basePath) {
),
fs.promises.writeFile(
"./elm-stuff/elm-pages/.elm-pages/Route.elm",
cliCode.routesModule
cliCode.routesModuleNew
),
fs.promises.writeFile("./.elm-pages/Main.elm", browserCode.mainModule),
fs.promises.writeFile("./.elm-pages/Route.elm", browserCode.routesModule),
fs.promises.writeFile(
"./.elm-pages/Route.elm",
browserCode.routesModuleNew
),
writeFetcherModules("./.elm-pages", browserCode.fetcherModules),
writeFetcherModules(
"./elm-stuff/elm-pages/client/.elm-pages",
Expand Down Expand Up @@ -83,7 +89,10 @@ async function newCopyBoth(modulePath) {
}

async function generateClientFolder(basePath) {
const browserCode = generateTemplateModuleConnector(basePath, "browser");
const browserCode = await generateTemplateModuleConnector(
basePath,
"browser"
);
const uiFileContent = elmPagesUiFile();
ensureDirSync("./elm-stuff/elm-pages/client/app");
ensureDirSync("./elm-stuff/elm-pages/client/.elm-pages");
Expand All @@ -102,7 +111,7 @@ async function generateClientFolder(basePath) {
);
await fs.promises.writeFile(
"./elm-stuff/elm-pages/client/.elm-pages/Route.elm",
browserCode.routesModule
browserCode.routesModuleNew
);
await fs.promises.writeFile(
"./elm-stuff/elm-pages/client/.elm-pages/Pages.elm",
Expand Down
42 changes: 41 additions & 1 deletion generator/src/generate-template-module-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const globby = require("globby");
const path = require("path");
const mm = require("micromatch");
const routeHelpers = require("./route-codegen-helpers");
const { runElmCodegenInstall } = require("./elm-codegen");
const { compileCliApp } = require("./compile-elm");

/**
* @param {string} basePath
* @param {'browser' | 'cli'} phase
*/
function generateTemplateModuleConnector(basePath, phase) {
async function generateTemplateModuleConnector(basePath, phase) {
const templates = globby.sync(["app/Route/**/*.elm"], {}).map((file) => {
const captures = mm.capture("app/Route/**/*.elm", file);
if (captures) {
Expand Down Expand Up @@ -36,6 +38,7 @@ function generateTemplateModuleConnector(basePath, phase) {
],
};
}
const routesModuleNew = await runElmCodegenCli(templates);

return {
mainModule: `port module Main exposing (..)
Expand Down Expand Up @@ -997,6 +1000,7 @@ decodeBytes bytesDecoder items =
-- Lamdera.Wire3.bytesDecodeStrict bytesDecoder items
|> Result.fromMaybe "Decoding error"
`,
routesModuleNew,
routesModule: `module Route exposing (baseUrlAsPath, Route(..), link, matchers, routeToPath, toLink, urlToRoute, toPath, redirectTo, toString)
{-|
Expand Down Expand Up @@ -1142,6 +1146,42 @@ redirectTo route =
};
}

async function runElmCodegenCli(templates) {
// await runElmCodegenInstall();
await compileCliApp(
// { debug: true },
{},
`Generate.elm`,
path.join(process.cwd(), "elm-stuff/elm-pages-codegen.js"),
path.join(__dirname, "../../codegen"),

path.join(process.cwd(), "elm-stuff/elm-pages-codegen.js")
);

// TODO use uncached require here to prevent stale code from running

const promise = new Promise((resolve, reject) => {
const elmPagesCodegen = require(path.join(
process.cwd(),
"./elm-stuff/elm-pages-codegen.js"
)).Elm.Generate;

const app = elmPagesCodegen.init({ flags: {} });
if (app.ports.onSuccessSend) {
app.ports.onSuccessSend.subscribe(resolve);
}
if (app.ports.onInfoSend) {
app.ports.onInfoSend.subscribe((info) => console.log(info));
}
if (app.ports.onFailureSend) {
app.ports.onFailureSend.subscribe(reject);
}
});
const filesToGenerate = await promise;

return filesToGenerate[0].contents;
}

function emptyRouteParams(name) {
return routeHelpers.parseRouteParams(name).length === 0;
}
Expand Down
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@types/node": "12.20.12",
"@types/serve-static": "^1.15.0",
"cypress": "^10.6.0",
"elm-codegen": "^0.2.0",
"elm-optimize-level-2": "^0.1.5",
"elm-review": "^2.7.4",
"elm-test": "^0.19.1-revision9",
Expand Down

0 comments on commit ba6f090

Please sign in to comment.