deflopt: remove tmp file handling

This commit is contained in:
Christopher Monsanto 2020-09-15 07:39:36 -04:00
parent 4b0cf1db3b
commit da3f222d7f
6 changed files with 16 additions and 62 deletions

View File

@ -71,7 +71,6 @@ Build settings are configurable in `tup.config`.
- `CONFIG_DEFAULT_OPTIPNG`: Command line to pass to `optipng`.
- `CONFIG_DEFAULT_ADVPNG`: Command line to pass to `advpng`.
- `CONFIG_DEFLOPT_PATH`: Path to `DeflOpt.exe`. If using Wine, you have to copy it somewhere outside the Tup tree.
- `CONFIG_DEFAULT_DEFLOPT`: `true`, `false`, or blank
There are src-specific versions of these settings:
@ -86,7 +85,6 @@ For example, these settings reflect the compression settings for the files chaos
```
CONFIG_DEFAULT_OPTIPNG=-o7
CONFIG_DEFAULT_ADVPNG=-z4 -i5000
CONFIG_DEFLOPT_PATH=/home/monsanto/.local/bin/DeflOpt.exe
CONFIG_DEFAULT_DEFLOPT=true
```

View File

@ -48,12 +48,8 @@ function compresspng(opts)
cmds += rep{"advpng -q ${opts} ${output}", opts=advpng, output=output}
end
if deflopt then
if DEFLOPT_PATH == nil then
error("Need to set CONFIG_DEFLOPT_PATH")
end
cmds += rep{"node ${root}/tools/deflopt ${deflopt} ${output}",
cmds += rep{"node ${root}/tools/deflopt ${output}",
root=ROOTDIR,
deflopt=DEFLOPT_PATH,
output=output}
end

View File

@ -1,5 +1,6 @@
import cp from 'child_process';
import root from '@smogon/sprite-root';
/*
Output looks something like:
@ -29,18 +30,14 @@ function parse(re : RegExp, output : string) {
return num;
}
export function deflopt(exe : string, file : string) {
const DEFLOPT_EXE = `${root}/vendor/DeflOpt.exe`;
export function deflopt(file : string) {
// DeflOpt doesn't like absolute paths, it thinks they are Windows-style
// command line switches.
file = file.replace(/\//g, "\\");
const output = cp.execFileSync(
"wine",
[exe, file],
{encoding: 'utf8',
// Wine doesn't even like it when the current directory is on fuse!
// What a persnickety program!
cwd: '/'});
const output = cp.execFileSync("wine", [DEFLOPT_EXE, file], {encoding: 'utf8'});
const processed = parse(PROCESSED_RE, output);
const rewritten = parse(REWRITTEN_RE, output);
return {processed, rewritten};

View File

@ -1,22 +1,14 @@
// Run DeflOpt.exe.
// Wine and Tup's fuse filesystem don't play nicely together.
// Copy inputs to /tmp first.
import {asTmp} from './tmp.js';
import {deflopt} from './deflopt.js';
const exe = process.argv[2];
const src = process.argv[3];
if (src === undefined || exe === undefined) {
throw new Error('tools/deflopt <path to DeflOpt.exe> <file>');
const src = process.argv[2];
if (src === undefined) {
throw new Error('tools/deflopt <file>');
}
const ret = asTmp("deflopt", src, (dst : string) => {
const {processed, rewritten} = deflopt(exe, dst);
return {changed: rewritten > 0, ret : processed === 1};
});
const {processed, rewritten} = deflopt(src);
if (!ret) {
if (processed !== 1) {
console.error(`Didn't process any files.`);
process.exit(1);
}

View File

@ -1,31 +0,0 @@
import pathlib from 'path';
import fs from 'fs';
import crypto from 'crypto';
import os from 'os';
function join(...paths : string[]) {
return pathlib.join(os.tmpdir(), ...paths);
}
// Adapted from https://stackoverflow.com/a/61312694
function mktemp(prefix : string) {
return join(`${prefix}.${crypto.randomBytes(6).readUIntLE(0,6).toString(36)}`);
}
export function asTmp<T>(prefix : string, src : string, f : (tmpName: string) => {changed : boolean, ret : T}) {
const parsed = pathlib.parse(src);
const dst = mktemp(prefix + "." + parsed.name) + parsed.ext;
// This has an astronomically low chance of throwing, but if it does, you
// can just restart the build.
fs.copyFileSync(src, dst, fs.constants.COPYFILE_EXCL);
try {
const {changed, ret} = f(dst);
if (changed) {
fs.copyFileSync(dst, src);
}
return ret;
} finally {
fs.unlinkSync(dst);
}
}

View File

@ -1,7 +1,9 @@
{
"extends": "../../tsconfig-base",
"compilerOptions": {
"outDir": "dist",
"allowJs": true
}
"outDir": "dist"
},
"references": [
{"path": "../../lib/root"}
]
}