Skip to content

Commit

Permalink
feat: 生成 UI 初始化代码和示例 main() 函数代码
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Apr 13, 2024
1 parent d032b51 commit 7ee7065
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 18 deletions.
18 changes: 11 additions & 7 deletions lib/compiler/ts-loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs";
import path from "path";
import ts from "typescript";
import { getResourceLoaderName } from "./utils.js";

/** @type {Loader} */
export default async function TsLoader(content) {
Expand Down Expand Up @@ -84,6 +85,7 @@ export default async function TsLoader(content) {
componentFunc.displayName ||
componentFunc.name ||
path.parse(loader.resourcePath).name;
const resourceLoaderName = getResourceLoaderName(name, componentName);

if (!fs.existsSync(sourceFilePath)) {
loader.emitFile(
Expand All @@ -94,20 +96,22 @@ export default async function TsLoader(content) {
if (!fs.existsSync(headerFilePath)) {
loader.emitFile(
headerFilePath,
`#include <ui.h>\n\n${result.declarationCode}`
`#include <ui.h>\n\n${result.declarationCode}${
resourceLoaderName ? `\nvoid ${resourceLoaderName}(void);\n` : ""
}`
);
}
if (!loader.data.components) {
loader.data.components = {};
}
loader.data.components[
path.relative(loader.rootContext, loader.resourcePath)
] = [
{
name: componentName,
headerFilePath: path.relative(loader.rootContext, headerFilePath),
},
];
] = {
resourceLoaderName,
headerFilePath: path.relative(loader.rootContext, headerFilePath),
assets,
components: [componentName],
};
return {
name: "lcui-app",
children: [
Expand Down
18 changes: 7 additions & 11 deletions lib/compiler/ui-loader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import path from "path";

function toIdent(str) {
return str.replace(/[^a-zA-Z0-9]/g, "_");
}
import { getResourceLoaderName, toIdent } from "./utils.js";

function toSnakeCase(str) {
return str.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
Expand Down Expand Up @@ -70,7 +67,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
"typedef struct {",
...refs.map((ref) => `${indentStr}ui_widget_t *${ref};`),
`} ${identPrefix}_refs_t;`,
"",
""
);
}
if (currentSchema.typesCode) {
Expand Down Expand Up @@ -132,12 +129,11 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
if (resourceLines.length < 1) {
return [];
}
let identPrefix = toIdent(fileName);
if (Object.keys(schemas).length == 1 && currentSchema.name) {
identPrefix = toIdent(currentSchema.name);
}
return [
`static void ${identPrefix}_load_resources(void)`,
`static void ${getResourceLoaderName(
fileName,
Object.keys(schemas).length === 1 && currentSchema.name
)}(void)`,
"{",
...resourceLines.map((line) => (line ? `${indentStr}${line}` : line)),
"}",
Expand Down Expand Up @@ -306,7 +302,7 @@ async function compile(rootNode, context, { filePath, indent = 8 }) {
"",
globalLines.join("\n"),
"",
Object.values(schemas).map(compileSchema).join('\n'),
Object.values(schemas).map(compileSchema).join("\n"),
generateResourceFunc(),
].join("\n");
}
Expand Down
59 changes: 59 additions & 0 deletions lib/compiler/ui-plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import fs from "fs-extra";
import path from "path";

const mainFileContent = `#include "main.h"
int main(int argc, char *argv[])
{
lcui_app_init();
// Write code here to initialize your application,
// such as loading configuration files, initializing functional modules
// ...
return lcui_app_run();
}
`;

export default class UIPlugin {
constructor() {
this.name = "UIPlugin";
Expand All @@ -14,6 +28,7 @@ export default class UIPlugin {
compiler.options.buildDirPath,
"components.json"
);
/** @type {Record<string, ComponentConfig>} */
let components = {};
if (fs.existsSync(dataFile)) {
components = fs.readJSONSync(dataFile);
Expand All @@ -27,7 +42,51 @@ export default class UIPlugin {
}
});
compiler.hooks.done.tap(this.name, () => {
const sourceDir = path.join(compiler.options.rootContext, "src");
const mainHeaderFile = path.join(sourceDir, "main.h");
const mainSourceFile = path.join(sourceDir, "main.c");
const componentList = Object.keys(components)
.sort()
.map((key) => components[key]);

fs.writeJSONSync(dataFile, components, { spaces: 2 });
if (!fs.existsSync(sourceDir)) {
fs.mkdirpSync(sourceDir);
}
if (!fs.existsSync(mainSourceFile)) {
fs.writeFileSync(mainSourceFile, mainFileContent);
}
fs.writeFileSync(
mainHeaderFile,
`#include <LCUI.h>
#include <LCUI/app.h>
${componentList.map(
(c) =>
`#include "${path.relative(
sourceDir,
path.join(compiler.options.rootContext, c.headerFilePath)
)}"`
)}
static void lcui_app_init(void)
{
lcui_init();
${componentList
.filter((c) => c.resourceLoaderName)
.map((c) => ` ${c.resourceLoaderName}();`)}
${componentList.map((c) =>
c.components.map((name) => ` ui_register_${name}();`)
)}
}
static int lcui_app_run(void)
{
return lcui_run();
}
`
);
});
}
}
9 changes: 9 additions & 0 deletions lib/compiler/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

export function toIdent(str) {
return str.replace(/[^a-zA-Z0-9]/g, "_");
}

export function getResourceLoaderName(fileName, defaultComponentName) {
const ident = toIdent(defaultComponentName || fileName);
return `ui_load_${ident}_resources`;
}
7 changes: 7 additions & 0 deletions lib/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ interface CompilerInstance {
};
}

interface ComponentConfig {
headerFilePath: string;
resourceLoaderName: string;
assets: Module[];
components: string[];
}

interface ResourceNode {
name: string;
text?: string;
Expand Down

0 comments on commit 7ee7065

Please sign in to comment.