Navigation Menu

Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Make it possible to run schism from another directory
Browse files Browse the repository at this point in the history
I wanted to symlink to `schism.sh` in my PATH so that I could play
around with it in another project folder. This makes that work, fixing
all of the pathing that was using `pwd`.
  • Loading branch information
matthewpblog committed Jun 28, 2018
1 parent d8b3732 commit 8a666d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
19 changes: 12 additions & 7 deletions run-utils.mjs
Expand Up @@ -19,6 +19,9 @@ import * as Schism from './rt/rt';
import child_process from 'child_process';
import fs from 'fs';
import util from 'util';
import { URL } from 'url';

const compilerPath = new URL('./schism/compiler.ss', import.meta.url).pathname;

export const OPTIONS = {
use_snapshot: true, // load schism-stage0.wasm if true instead of
Expand Down Expand Up @@ -70,30 +73,32 @@ function make_cache(thunk) {
export const stage0_bytes = make_cache(() =>
OPTIONS.stage0 ? (async function() {
return OPTIONS.use_snapshot
? fs.readFileSync('schism-stage0.wasm')
? fs.readFileSync(new URL('./schism-stage0.wasm', import.meta.url).pathname)
: await compileBootstrap()
})() : undefined);
// Compile bytes using the stage0 compiler.
export const stage0_compile = OPTIONS.stage0 ? make_compiler(stage0_bytes) : undefined;
export const stage1_bytes = make_cache(async () => {
if (!OPTIONS.stage1) { return undefined; }
const bytes = await stage0_compile(fs.readFileSync('./schism/compiler.ss'));
fs.writeFileSync('schism-stage1.wasm', bytes);
const bytes = await stage0_compile(fs.readFileSync(compilerPath));
fs.writeFileSync(new URL('./schism-stage1.wasm', import.meta.url).pathname,
bytes);
return bytes;
});

export const stage1_compile = OPTIONS.stage1 ? make_compiler(stage1_bytes) : undefined;
export const stage2_bytes = make_cache(async () => {
if (!OPTIONS.stage2) { return undefined; }
const bytes = await stage1_compile(fs.readFileSync('./schism/compiler.ss'));
fs.writeFileSync('schism-stage2.wasm', bytes);
const bytes = await stage1_compile(fs.readFileSync(compilerPath));
fs.writeFileSync(new URL('./schism-stage2.wasm', import.meta.url).pathname,
bytes);
return bytes;
});

export const stage2_compile = OPTIONS.stage2 ? make_compiler(stage2_bytes) : undefined;
export const stage3_bytes = make_cache(async () => {
if (!OPTIONS.stage3) { return undefined; }
const bytes = await stage2_compile(fs.readFileSync('./schism/compiler.ss'));
fs.writeFileSync('schism-stage3.wasm', bytes);
const bytes = await stage2_compile(fs.readFileSync(compilerPath));
fs.writeFileSync(new URL('./schism-stage3.wasm', import.meta.url).pathname, bytes);
return bytes;
});
12 changes: 11 additions & 1 deletion schism.sh
Expand Up @@ -14,4 +14,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

node --experimental-modules run-schism.mjs "$@"
# Get the current directory. Thanks to
# https://gist.github.com/TheMengzor/968e5ea87e99d9c41782
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

node --experimental-modules $DIR/run-schism.mjs "$@"

0 comments on commit 8a666d0

Please sign in to comment.