Skip to content

Commit

Permalink
feat(compile): merge compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Aug 21, 2023
1 parent 995f0b6 commit 5f2d5e4
Show file tree
Hide file tree
Showing 23 changed files with 198 additions and 370 deletions.
16 changes: 12 additions & 4 deletions bin/lcui.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env node
import fs from "fs-extra";
import path from "path";
import program from "commander";
import { Command } from "commander";
import { fileURLToPath } from "url";
import { create } from "../lib/create.js";
import { generate } from "../lib/generator.js";
import { compile } from "../lib/compiler.js";
import { compile } from "../lib/compiler/index.js";

const { version } = fs.readJSONSync(
path.join(path.dirname(fileURLToPath(import.meta.url)), "../package.json")
Expand All @@ -22,6 +22,8 @@ function wrapAction(action) {
};
}

const program = new Command();

program
.command("create <project-name>")
.description("create a new LCUI project")
Expand All @@ -33,8 +35,14 @@ program
.action(wrapAction(generate));

program
.command("compile <type>")
.description("compile files of the specified type to C code")
.command("compile")
.description("compile resource files into C soruce files")
.argument("<filePath>", "file or directory")
.option(
"--type <name>",
"specify which type of compiler to use to compile files",
"auto"
)
.action(wrapAction(compile));

program.version(version).parse(process.argv);
54 changes: 0 additions & 54 deletions compilers/resource/index.js

This file was deleted.

6 changes: 0 additions & 6 deletions compilers/resource/javascript.js

This file was deleted.

5 changes: 0 additions & 5 deletions compilers/resource/sass.js

This file was deleted.

15 changes: 0 additions & 15 deletions compilers/resource/xml.js

This file was deleted.

13 changes: 0 additions & 13 deletions compilers/resource/yaml.js

This file was deleted.

15 changes: 0 additions & 15 deletions compilers/router/templates/router.c

This file was deleted.

4 changes: 0 additions & 4 deletions compilers/router/templates/router.h

This file was deleted.

22 changes: 0 additions & 22 deletions lib/compiler.js

This file was deleted.

20 changes: 20 additions & 0 deletions lib/compiler/css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let identCount = 0;

export default {
test: /\.css$/,
compile(input) {
const cssCode = JSON.stringify(
input
.split("\n")
.map((line) => line.trimEnd())
.join("\n")
)
.split("\\n")
.join("\\\n");
return [
`const char *css_str_${identCount++} = "\\`,
`${cssCode.substring(1, cssCode.length - 1)}\\`,
'";\n',
].join("\n");
},
};
53 changes: 53 additions & 0 deletions lib/compiler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import path from "path";
import fs from "fs-extra";
import css from "./css.js";
import sass from "./sass.js";
import xml from "./xml.js";
import yaml from "./yaml.js";
import json from "./json.js";
import javascript from "./javascript.js";

const compilers = {
xml,
yaml,
json,
javascript,
sass,
css,
};

function compileFile(filePath, options) {
if (fs.statSync(filePath).isDirectory()) {
fs.readdirSync(filePath).forEach((name) => {
compileFile(path.join(filePath, name), options);
});
return;
}

let type = options.type;
if (type === "auto") {
type = Object.keys(compilers).find((c) => compilers[c].test.test(filePath));
}

const compiler = compilers[type];
if (!compiler) {
return;
}

const content = fs.readFileSync(filePath, { encoding: "utf-8" });
const result = compiler.compile(content, { filePath });
fs.writeFileSync(`${filePath}.c`, result, { encoding: "utf-8" });
}

export function compile(file, options) {
const cwd = process.cwd();

if (fs.existsSync(file)) {
compileFile(file, {
...options,
cwd,
projectDir: cwd,
sourceDir: path.join(cwd, "src"),
});
}
}
10 changes: 10 additions & 0 deletions lib/compiler/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import json from "./json.js";

export default {
test: /\.js$/,
compile(input, options) {
const module = { exports: {} };
eval(input);
return json.compileData(module.exports, options);
}
}
42 changes: 17 additions & 25 deletions compilers/resource/json.js → lib/compiler/json.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import fs from "fs";
import path from "path";
import css from "./css.js";
import sass from "./sass.js";

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

export function compileJson(jsonData, { filePath }) {
function compile(jsonData, { filePath }) {
let count = 0;
let resourceCount = 0;
const { name: fileName, base: fileBase } = path.parse(filePath);
const identPrefix = toIdent(fileName);
const globalLines = [];
Expand Down Expand Up @@ -54,9 +55,12 @@ export function compileJson(jsonData, { filePath }) {
initLines.push(
`pd_font_library_load_file(${JSON.stringify(attrs.src)});`
);
} else if (attrs.type === "text/css") {
return;
}
if (["text/css", "text/scss", "text/sass"].includes(attrs.type)) {
let cssFilePath;
let cssText;

if (attrs.src) {
cssFilePath = path.resolve(filePath, "..", attrs.src);
cssText = fs.readFileSync(cssFilePath, {
Expand All @@ -66,20 +70,12 @@ export function compileJson(jsonData, { filePath }) {
cssFilePath = filePath;
cssText = text;
}
ident = `css_str_${resourceCount++}`;
const cssCode = JSON.stringify(
cssText
.split("\n")
.map((line) => line.trimEnd())
.join("\n")
)
.split("\\n")
.join("\\\n");
globalLines.push(
`const char *${ident} = "\\`,
`${cssCode.substring(1, cssCode.length - 1)}\\`,
'";'
);
const cssCode =
attrs.type === "text/css"
? css.compile(cssText)
: sass.compile(cssText);
ident = cssCode.substring("const char *".length).split(" ")[0];
globalLines.push(cssCode);
initLines.push(
`ui_load_css_string(${ident}, ${JSON.stringify(
path.parse(cssFilePath).base
Expand Down Expand Up @@ -166,11 +162,7 @@ export function compileJson(jsonData, { filePath }) {
].join("\n");
}

export function compileJsonFile(filePath) {
const data = fs.readFileSync(filePath, {
encoding: "utf-8",
});
return compileJson(JSON.parse(data), {
filePath,
});
}
export default {
test: /\.json$/,
compile,
};
Loading

0 comments on commit 5f2d5e4

Please sign in to comment.