Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
rebrand to typst-preview and adjust cli args (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enter-tainer authored Aug 6, 2023
1 parent 46c226b commit a208689
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 147 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ jobs:
sudo apt-get install gcc-arm-linux-gnueabihf
- shell: pwsh
run: |
cargo build --release -p typst-ws --target ${{ matrix.rust-target }}
cp "target/${{ matrix.rust-target }}/release/typst-ws$(If ('${{ matrix.platform }}' -eq 'win32') { '.exe' } else { '' } )" "addons/vscode/out/"
cp "target/${{ matrix.rust-target }}/release/typst-ws$(If ('${{ matrix.platform }}' -eq 'win32') { '.exe' } else { '' } )" "typst-ws-${{ env.target }}$(If ('${{ matrix.platform }}' -eq 'win32') { '.exe' } else { '' } )"
cargo build --release -p typst-preview --target ${{ matrix.rust-target }}
cp "target/${{ matrix.rust-target }}/release/typst-preview$(If ('${{ matrix.platform }}' -eq 'win32') { '.exe' } else { '' } )" "addons/vscode/out/"
cp "target/${{ matrix.rust-target }}/release/typst-preview$(If ('${{ matrix.platform }}' -eq 'win32') { '.exe' } else { '' } )" "typst-preview-${{ env.target }}$(If ('${{ matrix.platform }}' -eq 'win32') { '.exe' } else { '' } )"
- shell: pwsh
run: yarn run package -- --target ${{ env.target }} -o typst-preview-${{ env.target }}.vsix
working-directory: ./addons/vscode
Expand All @@ -93,8 +93,8 @@ jobs:
path: addons/vscode/typst-preview-${{ env.target }}.vsix
- uses: actions/upload-artifact@v2
with:
name: typst-ws-${{ env.target }}
path: typst-ws-${{ env.target }}${{ fromJSON('["", ".exe"]')[matrix.platform == 'win32'] }}
name: typst-preview-${{ env.target }}
path: typst-preview-${{ env.target }}${{ fromJSON('["", ".exe"]')[matrix.platform == 'win32'] }}

build_alpine:
name: build (x86_64-unknown-linux-musl)
Expand Down Expand Up @@ -122,10 +122,10 @@ jobs:
working-directory: ./addons/vscode
- name: build server binary
run: |
cargo build --release -p typst-ws --target $RUST_TARGET
cargo build --release -p typst-preview --target $RUST_TARGET
mkdir -p addons/vscode/out
cp "target/$RUST_TARGET/release/typst-ws" "addons/vscode/out/"
cp "target/$RUST_TARGET/release/typst-ws" "typst-ws-alpine-x64"
cp "target/$RUST_TARGET/release/typst-preview" "addons/vscode/out/"
cp "target/$RUST_TARGET/release/typst-preview" "typst-preview-alpine-x64"
- name: package extension
run: yarn run package -- --target ${{ env.target }} -o typst-preview-${{ env.target }}.vsix
working-directory: ./addons/vscode
Expand All @@ -135,8 +135,8 @@ jobs:
path: addons/vscode/typst-preview-${{ env.target }}.vsix
- uses: actions/upload-artifact@v2
with:
name: typst-ws-${{ env.target }}
path: typst-ws-${{ env.target }}
name: typst-preview-${{ env.target }}
path: typst-preview-${{ env.target }}

release:
runs-on: ubuntu-latest
Expand Down
6 changes: 3 additions & 3 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@
"windows": {
"command": "cp",
"args": [
"${workspaceFolder}\\target\\release\\typst-ws.exe",
"${workspaceFolder}\\target\\release\\typst-preview.exe",
"${workspaceFolder}\\addons\\vscode\\out\\"
]
},
"linux": {
"command": "cp",
"args": [
"${workspaceFolder}/target/release/typst-ws",
"${workspaceFolder}/target/release/typst-preview",
"${workspaceFolder}/addons/vscode/out/"
]
},
"osx": {
"command": "cp",
"args": [
"${workspaceFolder}/target/release/typst-ws",
"${workspaceFolder}/target/release/typst-preview",
"${workspaceFolder}/addons/vscode/out/"
]
}
Expand Down
70 changes: 35 additions & 35 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "typst-ws"
name = "typst-preview"
version = "0.6.3"
authors = ["The Typst Project Developers"]
edition = "2021"

[[bin]]
name = "typst-ws"
name = "typst-preview"
path = "src/main.rs"
test = false
doctest = false
Expand Down
8 changes: 4 additions & 4 deletions addons/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
{
"command": "typst-preview.preview",
"title": "Typst Preview: Preview current file",
"description": "Launch typst-ws server",
"description": "Launch typst-preview server",
"icon": "$(open-preview)"
},
{
"command": "typst-preview.browser",
"title": "Typst Preview: Preview current file in browser",
"description": "Launch typst-ws server and open the preview in your browser",
"description": "Launch typst-preview server and open the preview in your browser",
"icon": "$(open-preview)"
},
{
Expand All @@ -60,7 +60,7 @@
"typst-preview.executable": {
"type": "string",
"default": "",
"description": "Path to typst-ws executable. If not set, the extension will use bundled typst-ws. If bundled binary is not found, it will use typst-ws installed in PATH"
"description": "Path to typst-preview executable. If not set, the extension will use bundled typst-preview. If bundled binary is not found, it will use typst-preview installed in PATH"
},
"typst-preview.fontPaths": {
"type": "array",
Expand All @@ -70,7 +70,7 @@
"description": "Absolute path to a directory or file containing font assets."
},
"default": [],
"description": "List of *additional* paths to font assets used by typst-ws."
"description": "List of *additional* paths to font assets used by typst-preview."
},
"typst-preview.refresh": {
"title": "Refresh preview",
Expand Down
36 changes: 18 additions & 18 deletions addons/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ async function loadHTMLFile(context: vscode.ExtensionContext, relativePath: stri
return fileContents;
}

export async function getTypstWsPath(extensionPath?: string): Promise<string> {
const state = getTypstWsPath as unknown as any;
(!state.BINARY_NAME) && (state.BINARY_NAME = "typst-ws");
export async function getCliPath(extensionPath?: string): Promise<string> {
const state = getCliPath as unknown as any;
(!state.BINARY_NAME) && (state.BINARY_NAME = "typst-preview");
(!state.getConfig) && (state.getConfig = (
() => vscode.workspace.getConfiguration().get<string>('typst-preview.executable')));

const bundledPath = path.resolve(extensionPath || path.join(__dirname, ".."), "out", state.BINARY_NAME);
const configPath = state.getConfig();

if (state.bundledPath === bundledPath && state.configPath === configPath) {
// console.log('getTypstWsPath cached', state.resolved);
// console.log('getCliPath cached', state.resolved);
return state.resolved;
}
state.bundledPath = bundledPath;
Expand All @@ -46,7 +46,7 @@ export async function getTypstWsPath(extensionPath?: string): Promise<string> {
};

const resolvePath = async () => {
console.log('getTypstWsPath resolving', bundledPath, configPath);
console.log('getCliPath resolving', bundledPath, configPath);

if (configPath?.length) {
return configPath;
Expand All @@ -65,12 +65,12 @@ export async function getTypstWsPath(extensionPath?: string): Promise<string> {
return (state.resolved = await resolvePath());
}

export function getTypstWsFontArgs(fontPaths?: string[]): string[] {
export function getCliFontArgs(fontPaths?: string[]): string[] {
return (fontPaths ?? []).flatMap((fontPath) => ["--font-path", fontPath]);
}

export function codeGetTypstWsFontArgs(): string[] {
return getTypstWsFontArgs(vscode.workspace.getConfiguration().get<string[]>(
export function codeGetCliFontArgs(): string[] {
return getCliFontArgs(vscode.workspace.getConfiguration().get<string[]>(
'typst-preview.fontPaths'));
}

Expand Down Expand Up @@ -109,7 +109,7 @@ const panelScrollTo = async (bindDocument: vscode.TextDocument, activeEditor: vs
interface TaskControlBlock {
/// related panel
panel?: vscode.WebviewPanel;
/// channel to communicate with typst-ws
/// channel to communicate with typst-preview
addonΠserver: WebSocket;
}

Expand Down Expand Up @@ -160,7 +160,7 @@ function runServer(command: string, args: string[], outputChannel: vscode.Output
});
serverProcess.on('error', (err: any) => {
console.error('Failed to start server process');
vscode.window.showErrorMessage(`Failed to start typst-ws(${command}) process: ${err}`);
vscode.window.showErrorMessage(`Failed to start typst-preview(${command}) process: ${err}`);
});
serverProcess.stdout.on('data', (data: Buffer) => {
outputChannel.append(data.toString());
Expand All @@ -170,7 +170,7 @@ function runServer(command: string, args: string[], outputChannel: vscode.Output
});
serverProcess.on('exit', (code: any) => {
if (code !== null && code !== 0) {
vscode.window.showErrorMessage(`typst-ws process exited with code ${code}`);
vscode.window.showErrorMessage(`typst-preview process exited with code ${code}`);
}
console.log(`child process exited with code ${code}`);
});
Expand Down Expand Up @@ -228,7 +228,7 @@ const launchPreview = async (task: LaunchInBrowserTask | LaunchInWebViewTask) =>
const scrollSyncMode = vscode.workspace.getConfiguration().get<ScrollSyncMode>('typst-preview.scrollSync') || "never";
const fontendPath = path.resolve(context.extensionPath, "out/frontend");
await watchEditorFiles();
const { serverProcess, controlPlanePort, dataPlanePort } = await launchTypstWs(task.kind === 'browser');
const { serverProcess, controlPlanePort, dataPlanePort } = await launchCli(task.kind === 'browser');

const addonΠserver = new WebSocket(`ws://127.0.0.1:${controlPlanePort}`);
addonΠserver.addEventListener("message", async (message) => {
Expand Down Expand Up @@ -284,7 +284,7 @@ const launchPreview = async (task: LaunchInBrowserTask | LaunchInWebViewTask) =>
const basename = path.basename(activeEditor.document.fileName);
// Create and show a new WebView
const panel = vscode.window.createWebviewPanel(
'typst-ws-preview', // 标识符
'typst-preview', // 标识符
`${basename} (Preview)`, // 面板标题
vscode.ViewColumn.Beside, // 显示在编辑器的哪一侧
{
Expand Down Expand Up @@ -343,21 +343,21 @@ const launchPreview = async (task: LaunchInBrowserTask | LaunchInWebViewTask) =>
}
};

async function launchTypstWs(serveStaticFile: boolean) {
const serverPath = await getTypstWsPath(context.extensionPath);
async function launchCli(openInBrowser: boolean) {
const serverPath = await getCliPath(context.extensionPath);
console.log(`Watching ${filePath} for changes`);
const projectRoot = getProjectRoot(filePath);
const rootArgs = projectRoot ? ["--root", projectRoot] : [];
const staticFileArgs = serveStaticFile ? ["--serve-static-file"] : [];
const staticFileArgs = openInBrowser ? ["--open-in-browser", "--open-in-browser-host", "127.0.0.1:0"] : [];
const partialRenderingArgs = vscode.workspace.getConfiguration().get<boolean>('typst-preview.partialRendering') ? ["--partial-rendering"] : [];
const [dataPlanePort, controlPlanePort, serverProcess] = await runServer(serverPath, [
"--data-plane-host", "127.0.0.1:0",
"--control-plane-host", "127.0.0.1:0",
...rootArgs,
...staticFileArgs,
...partialRenderingArgs,
...codeGetTypstWsFontArgs(),
"watch", filePath,
...codeGetCliFontArgs(),
filePath,
], outputChannel);
console.log(`Launched server, data plane port:${dataPlanePort}, control plane port:${controlPlanePort}`);
// window.typstWebsocket.send("current");
Expand Down
34 changes: 17 additions & 17 deletions addons/vscode/src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,36 @@ suite('Extension Test Suite', () => {
test('Executable Configuration Test', async () => {
assert.strictEqual('', vscode.workspace.getConfiguration().get<string>('typst-preview.executable'), 'default path');

assert.notStrictEqual('', await ext.getTypstWsPath(), 'never resolve empty string');
assert.notStrictEqual(undefined, await ext.getTypstWsPath(), 'never resolve undefined');
assert.notStrictEqual('', await ext.getCliPath(), 'never resolve empty string');
assert.notStrictEqual(undefined, await ext.getCliPath(), 'never resolve undefined');

const state = ext.getTypstWsPath as unknown as any;
const state = ext.getCliPath as unknown as any;
let resolved: string;

const BINARY_NAME = state.BINARY_NAME;
assert.strictEqual('typst-ws', BINARY_NAME, 'default binary path is typst-ws');
assert.strictEqual('typst-preview', BINARY_NAME, 'default binary path is typst-preview');

resolved = await ext.getTypstWsPath();
resolved = await ext.getCliPath();
assert.strictEqual(state.bundledPath, resolved, 'the bundle path exists and detected');

state.BINARY_NAME = 'bad-typst-ws';
assert.strictEqual('bad-typst-ws', await ext.getTypstWsPath(), 'fallback to binary name if not exists');
state.BINARY_NAME = 'bad-typst-preview';
assert.strictEqual('bad-typst-preview', await ext.getCliPath(), 'fallback to binary name if not exists');

const oldGetConfig = state.getConfig;
state.getConfig = () => 'config-typst-ws';
assert.strictEqual('config-typst-ws', await ext.getTypstWsPath(), 'use config if set');
state.getConfig = () => 'config-typst-preview';
assert.strictEqual('config-typst-preview', await ext.getCliPath(), 'use config if set');

state.BINARY_NAME = 'typst-ws';
state.BINARY_NAME = 'typst-preview';
state.getConfig = oldGetConfig;
resolved = await ext.getTypstWsPath();
resolved = await ext.getCliPath();
assert.strictEqual(state.bundledPath, resolved, 'reactive state');

resolved = await ext.getTypstWsPath();
resolved = await ext.getCliPath();
assert.strictEqual(true, resolved.endsWith(state.BINARY_NAME), 'exact file suffix');

/// fast path should hit
for (let i = 0; i < 1000; i++) {
await ext.getTypstWsPath();
await ext.getCliPath();
}
});

Expand All @@ -61,16 +61,16 @@ suite('Extension Test Suite', () => {
);

jsonIs(assert.strictEqual)(
[], ext.getTypstWsFontArgs(undefined));
[], ext.getCliFontArgs(undefined));

jsonIs(assert.strictEqual)(
[], ext.getTypstWsFontArgs([]));
[], ext.getCliFontArgs([]));

jsonIs(assert.strictEqual)(
[], ext.codeGetTypstWsFontArgs());
[], ext.codeGetCliFontArgs());

jsonIs(assert.strictEqual)(
["--font-path", "/path/to/font1", "--font-path", "/path/to/font2"],
ext.getTypstWsFontArgs(["/path/to/font1", "/path/to/font2"]));
ext.getCliFontArgs(["/path/to/font1", "/path/to/font2"]));
});
});
Loading

0 comments on commit a208689

Please sign in to comment.