From e4ebaa2cf4588ec6971a1dd20b59bfed8a4ce081 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sun, 1 Jan 2023 11:28:25 -0800 Subject: [PATCH] Fixes #107 --- build.ts | 4 +--- common/markdown_parser/parse_tree.ts | 2 ++ plugos/asset_bundle/bundle.ts | 2 ++ plugos/bin/plugos-bundle.ts | 6 ++++-- plugos/compile.ts | 5 +++-- plugos/runtime.test.ts | 4 +++- plugos/syscalls/fs.deno.test.ts | 3 ++- plugos/util.ts | 5 +++++ plugs/markdown/markdown_render.test.ts | 3 ++- 9 files changed, 24 insertions(+), 10 deletions(-) diff --git a/build.ts b/build.ts index a5ff2d1d..710c1eb1 100644 --- a/build.ts +++ b/build.ts @@ -47,9 +47,7 @@ async function prepareAssets(dist: string) { `${dist}/web/main.css`, compiler.to_string("expanded") as string, ); - const globalManifest = await plugOsBundle( - new URL(`./plugs/global.plug.yaml`, import.meta.url).pathname, - ); + const globalManifest = await plugOsBundle("./plugs/global.plug.yaml"); await Deno.writeTextFile( `${dist}/web/global.plug.json`, JSON.stringify(globalManifest, null, 2), diff --git a/common/markdown_parser/parse_tree.ts b/common/markdown_parser/parse_tree.ts index 23eef1d6..13e02de6 100644 --- a/common/markdown_parser/parse_tree.ts +++ b/common/markdown_parser/parse_tree.ts @@ -59,6 +59,8 @@ export function lezerToParseTree( } export function parse(language: Language, text: string): ParseTree { + // Remove \r for Windows before parsing + text = text.replaceAll("\r", ""); const tree = lezerToParseTree(text, language.parser.parse(text).topNode); return tree; } diff --git a/plugos/asset_bundle/bundle.ts b/plugos/asset_bundle/bundle.ts index fb615f65..7b18b034 100644 --- a/plugos/asset_bundle/bundle.ts +++ b/plugos/asset_bundle/bundle.ts @@ -57,6 +57,8 @@ export class AssetBundle { } writeFileSync(path: string, data: Uint8Array) { + // Replace \ with / for windows + path = path.replaceAll("\\", "/"); const mimeType = mime.getType(path) || "application/octet-stream"; this.bundle[path] = base64EncodedDataUrl(mimeType, data); } diff --git a/plugos/bin/plugos-bundle.ts b/plugos/bin/plugos-bundle.ts index 21b4de3c..ef9f0914 100755 --- a/plugos/bin/plugos-bundle.ts +++ b/plugos/bin/plugos-bundle.ts @@ -76,11 +76,13 @@ export async function bundle( continue; } let jsFunctionName = "default", - filePath = path.join(rootPath, def.path); + filePath: string = def.path; if (filePath.indexOf(":") !== -1) { [filePath, jsFunctionName] = filePath.split(":"); } - + // Resolve path + filePath = path.join(rootPath, filePath); + def.code = await compile( filePath, jsFunctionName, diff --git a/plugos/compile.ts b/plugos/compile.ts index 87592b4d..5ffee6a8 100644 --- a/plugos/compile.ts +++ b/plugos/compile.ts @@ -49,8 +49,9 @@ export async function compile( inFile = await Deno.makeTempFile({ suffix: ".ts" }); await Deno.writeTextFile( inFile, - `import {${functionName}} from "${ - path.resolve(filePath) + `import {${functionName}} from "file://${ + // Replacaing \ with / for Windows + path.resolve(filePath).replaceAll("\\", "\\\\") }";export default ${functionName};`, ); } diff --git a/plugos/runtime.test.ts b/plugos/runtime.test.ts index 9fa680c9..c0b6e2f7 100644 --- a/plugos/runtime.test.ts +++ b/plugos/runtime.test.ts @@ -130,10 +130,12 @@ Deno.test("Run a deno sandbox", async () => { import { bundle as plugOsBundle } from "./bin/plugos-bundle.ts"; import { esbuild } from "./compile.ts"; +import { urlToPathname } from "./util.ts"; -const __dirname = new URL(".", import.meta.url).pathname; +const __dirname = urlToPathname(new URL(".", import.meta.url)); Deno.test("Preload dependencies", async () => { + const globalModules = await plugOsBundle( `${__dirname}../plugs/global.plug.yaml`, ); diff --git a/plugos/syscalls/fs.deno.test.ts b/plugos/syscalls/fs.deno.test.ts index a26ae0b4..62fb5a63 100644 --- a/plugos/syscalls/fs.deno.test.ts +++ b/plugos/syscalls/fs.deno.test.ts @@ -2,11 +2,12 @@ import { assert } from "../../test_deps.ts"; import { FileMeta } from "../../common/types.ts"; import { path } from "../deps.ts"; import fileSystemSyscalls from "./fs.deno.ts"; +import { urlToPathname } from "../util.ts"; const fakeCtx = {} as any; Deno.test("Test FS operations", async () => { - const thisFolder = path.dirname(new URL(import.meta.url).pathname); + const thisFolder = path.resolve(path.dirname(urlToPathname(new URL(import.meta.url)))); const syscalls = fileSystemSyscalls(thisFolder); const allFiles: FileMeta[] = await syscalls["fs.listFiles"]( fakeCtx, diff --git a/plugos/util.ts b/plugos/util.ts index 67284acd..a8906cbd 100644 --- a/plugos/util.ts +++ b/plugos/util.ts @@ -5,3 +5,8 @@ export function safeRun(fn: () => Promise) { // throw e; }); } + +export function urlToPathname(url: URL) { + // For Windows, remove prefix / + return url.pathname.replace(/^\/(\w:)/, "$1"); +} \ No newline at end of file diff --git a/plugs/markdown/markdown_render.test.ts b/plugs/markdown/markdown_render.test.ts index a7638bee..fb625b85 100644 --- a/plugs/markdown/markdown_render.test.ts +++ b/plugs/markdown/markdown_render.test.ts @@ -12,6 +12,7 @@ import { createSandbox } from "../../plugos/environments/deno_sandbox.ts"; import { loadMarkdownExtensions } from "../../common/markdown_parser/markdown_ext.ts"; import { renderMarkdownToHtml } from "./markdown_render.ts"; import { assertEquals } from "../../test_deps.ts"; +import { urlToPathname } from "../../plugos/util.ts"; Deno.test("Markdown render", async () => { const system = new System("server"); @@ -19,7 +20,7 @@ Deno.test("Markdown render", async () => { await system.load(tasksPlug, createSandbox); const lang = buildMarkdown(loadMarkdownExtensions(system)); const testFile = Deno.readTextFileSync( - new URL("test/example.md", import.meta.url).pathname, + urlToPathname(new URL("test/example.md", import.meta.url)), ); const tree = parse(lang, testFile); renderMarkdownToHtml(tree, {