blob: 8d2442f979ab1d715d1af3d6da9db6a4094acd58 [file] [log] [blame]
#!/bin/bash
#
# Compiles code with DDC and runs the resulting code in node.js with devtool,
# an Electron-based debugger and browser environment.
#
# The first script supplied should be the one with `main()`.
#
# Saves the output in the same directory as the sources for convenient
# inspection, modification or rerunning the code.
set -e
DDC_PATH=$( cd $( dirname "${BASH_SOURCE[0]}" )/.. && pwd )
KERNEL=false
if [ "$1" = "-k" ]; then
KERNEL=true
shift
fi
BASENAME=$( basename "${1%.*}")
LIBROOT=$(cd $( dirname "${1%.*}") && pwd)
# TODO(vsm): Change this to use the regular built version of the summaries
# and the SDK.
export NODE_PATH=$DDC_PATH/gen/sdk/common:$LIBROOT:$NODE_PATH
# Build the SDK in a place where we can find it if it's not already there.
if [ ! -e $DDC_PATH/gen/sdk/ddc_sdk.sum ]; then
$DDC_PATH/tool/build_sdk.sh
fi
if [ "$KERNEL" = true ]; then
dart -c $DDC_PATH/bin/dartdevk.dart \
-o $BASENAME.js $*
else
dart -c $DDC_PATH/bin/dartdevc.dart --modules=node --library-root=$LIBROOT \
--dart-sdk-summary=$DDC_PATH/gen/sdk/ddc_sdk.sum \
-o $LIBROOT/$BASENAME.js $*
fi
pushd $LIBROOT > /dev/null
echo "
// Fix the node.js search paths that Electron cleared out.
const Module = require('module');
const originalResolveFilename = Module._resolveFilename;
Module._resolveFilename = function (request, parent, isMain) {
let paths = parent.paths;
const ddcPath = \"$DDC_PATH/gen/sdk/common\";
if (paths[0] != ddcPath) {
paths.splice(0, 0, ddcPath, \"$LIBROOT\");
}
return originalResolveFilename(request, parent, isMain);
};
let sdk = require(\"dart_sdk\");
let main = require(\"$BASENAME\").$BASENAME.main;
sdk._isolate_helper.startRootIsolate(main, []);" \
> $LIBROOT/$BASENAME.run.js
devtool $LIBROOT/$BASENAME.run.js
popd > /dev/null