Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
Zemnmez committed May 24, 2022
1 parent 343bfce commit 3b015a7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 44 deletions.
13 changes: 8 additions & 5 deletions cc/inkscape/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ alias(

# Required to bypass FUSE restrictions on bazel.
sh_binary(
name = "bin",
srcs = [ "run.sh" ],
data = [ ":app_image" ],
env = { "APP_IMAGE": "$(location :app_image)" },
deps = ["@bazel_tools//tools/bash/runfiles"],
name = "bin",
srcs = ["run.sh"],
data = [":app_image"],
env = {"APP_IMAGE": "$(location :app_image)"},
visibility = [
"//ts/cmd/svgshot:__subpackages__",
],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
4 changes: 3 additions & 1 deletion cc/inkscape/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
Expand All @@ -10,4 +12,4 @@ source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/
# --- end runfiles.bash initialization v2 ---

# bypasses FUSE issues on bazel https://github.com/AppImage/AppImageKit/pull/842
$(rlocation $APP_IMAGE) --appimage-extract-and-run $@
$(rlocation inkscape_linux/file/bin) --appimage-extract-and-run $@
19 changes: 8 additions & 11 deletions ts/cmd/svgshot/BUILD
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
load("//:rules.bzl", "nodejs_binary", "ts_project", "jest_test")
load("//:rules.bzl", "jest_test", "nodejs_binary", "ts_project")

ts_project(
name = "project",
srcs = [
"index.ts",
"lib.ts",
"svgshot_test.ts"
"svgshot_test.ts",
],
deps = [
"@npm//@bazel/runfiles",
"@npm//@types/jest",
"@npm//@types/node",
"@npm//@types/svgo",
"@npm//@types/tmp",
"@npm//@types/jest",
"@npm//commander",
"@npm//puppeteer",
"@npm//svgo",
"@npm//tmp",
"@npm//@bazel/runfiles"
],
)

Expand All @@ -27,20 +27,17 @@ nodejs_binary(
"$(location //cc/inkscape:bin)",
],
data = [
"//cc/inkscape:bin",
":project_ts",
"//cc/inkscape:bin",
],
entry_point = "index.ts",
)

jest_test(
name = "tests",
srcs = [ "svgshot_test.js" ],
project_deps = [ ":project" ],
srcs = ["svgshot_test.js"],
data = [
"//cc/inkscape:bin",
],
env = {
"INKSCAPE_BIN": "$(rootpath //cc/inkscape:bin)",
},
)
project_deps = [":project"],
)
51 changes: 38 additions & 13 deletions ts/cmd/svgshot/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ const main = async (argv: string[] = process.argv) => {
});

if (out !== undefined && args.length > 1) {
throw new Error(`Out file specified and more than one URL (${args}) to load.`);
throw new Error(
`Out file specified and more than one URL (${args}) to load.`
);
}

const captures = map(args, async (url, i) => {
Expand Down Expand Up @@ -154,41 +156,64 @@ const main = async (argv: string[] = process.argv) => {
const [pdfFile, svgFile] = await Promise.all(
['.pdf', '.svg'].map(async (extension): Promise<string> => {
return new Promise((ok, err) => {
tmp.file({ postfix: extension }, (error, path) => {
if (error) return err(error);
return ok(path);
});
tmp.file(
{
tmpdir: process.env['TEST_TMPDIR'] || undefined,
postfix: extension,
},
(error, path) => {
if (error) return err(error);
return ok(path);
}
);
});
})
);

if (pdf.length === 0) {
throw new Error('Failed to generate PDF.');
}

await writeFile(pdfFile, pdf);

const line = `${inkscapeBin} --without-gui ${pdfFile} --export-plain-svg ${svgFile}`;
const line =
`${inkscapeBin} --without-gui ${pdfFile} ` +
`--export-type=svg --export-plain-svg --export-filename=${svgFile}`;
console.warn('running', line);
try {
await promisify(exec)(line);
const result = await promisify(exec)(line);
if (result.stderr.length > 0) console.warn(result.stderr);
console.info(result.stdout);
} catch (e) {
throw new Error(
`failed to run ${line} with ${e} -- make sure you have inkscape installed and in your PATH`
);
}

const title =
const fileName =
out === undefined
? ((await page.title()).trim() || page.url()).replace(
/[^A-z_-]/g,
'_'
)
) + '.svg'
: out;

const fileName = title + '.svg';
const svgContents = (await readFile(svgFile, 'utf8')).toString();

const svgContents = await readFile(svgFile, 'utf8');
const optimSvg = await svgo.optimize(svgContents.toString(), {
if (svgContents.length === 0) {
throw new Error('Failed to generate SVG.');
}

const optimSvg = await svgo.optimize(svgContents, {
multipass: true,
path: svgFile,
});

if (optimSvg.error !== undefined) throw optimSvg.modernError;
if (optimSvg.error !== undefined) throw optimSvg.error;

if (!optimSvg.data) {
throw new Error('Failed to optimize SVG.');
}

console.warn(
`writing ${i + 1}/${args.length} ${fileName} (${width} x ${height})`
Expand Down
45 changes: 31 additions & 14 deletions ts/cmd/svgshot/svgshot_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,46 @@ import main from './lib';
import tmp from 'tmp';
import fs from 'fs/promises';
import { runfiles } from '@bazel/runfiles';
import s from 'child_process';

describe('svgshot', () => {
it('should render a test URL', async () => {
const target = await new Promise<string>((ok, err) =>
tmp.file({ postfix: '.svg' }, (error, path) => {
if (error) return err(error);
return ok(path);
})
tmp.file(
{
// https://docs.bazel.build/versions/main/test-encyclopedia.html#test-interaction-with-the-filesystem
tmpdir: process.env['TEST_TMPDIR'] || undefined,
postfix: '.svg',
},
(error, path) => {
if (error) return err(error);
return ok(path);
}
)
);

const inkscape = runfiles.resolveWorkspaceRelative(process.env["INKSCAPE_BIN"]!);
const inkscape =
runfiles.resolveWorkspaceRelative('cc/inkscape/run.sh');

// something weird happens here :(
await main([
"fake123",
"fake1234",
"--inkscapeBin", inkscape,
'data:text/plain,Hello, world!', '--out', target
]);
await expect(
main([
'fake123',
'fake1234',
'--inkscapeBin',
inkscape,
'data:text/plain,Hello, world!',
'--out',
target,
])
).resolves.toBeUndefined();

const result = (await fs.readFile(target)).toString();

expect(result).not.toEqual('');
expect(result).not.toBeUndefined();
expect(result).not.toBeNull();

expect(await fs.readFile(target).toString()).toEqual('');
expect(result).toEqual(
`<svg xml:space="preserve" width="1000" height="1000" xmlns="http://www.w3.org/2000/svg"><defs><clipPath clipPathUnits="userSpaceOnUse" id="a"><path d="M0 0h3125v3125H0Z" clip-rule="evenodd"/></clipPath></defs><g clip-path="url(#a)" transform="scale(.32)"><path d="M0 0h1000v1000H0Z" style="fill:#fff;fill-opacity:1;fill-rule:nonzero;stroke:none" transform="scale(3.125)"/><text transform="translate(25 75) scale(3.125)" style="font-variant:normal;font-weight:400;font-size:13px;font-family:'Liberation Mono';-inkscape-font-specification:LiberationMono;writing-mode:lr-tb;fill:#000;fill-opacity:1;fill-rule:nonzero;stroke:none"><tspan x="0 7.8012695 15.602539 23.403809 31.205078 39.006348 46.807617 54.608887 62.410156 70.211426 78.012695 85.813965 93.615234" y="0">Hello, world!</tspan></text></g></svg>`
);
});
});

0 comments on commit 3b015a7

Please sign in to comment.