From 7001f7723cde611f0d8cd9c182f92120b09579c3 Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Thu, 28 Mar 2019 12:10:16 +0200 Subject: [PATCH] tools(foreach): replace relative to absolute paths in build output (#2109) this allows running "foreach" from the IDE and have problems discovered globally. --- scripts/foreach.sh | 6 +++++- scripts/path-prefix | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100755 scripts/path-prefix diff --git a/scripts/foreach.sh b/scripts/foreach.sh index 27b2db682dfb8..514064f4c633f 100755 --- a/scripts/foreach.sh +++ b/scripts/foreach.sh @@ -19,9 +19,11 @@ # # -------------------------------------------------------------------------------------------------- set -euo pipefail +scriptdir=$(cd $(dirname $0) && pwd) statefile="${HOME}/.foreach.state" commandfile="${HOME}/.foreach.command" command_arg="${@:-}" +base=$PWD function heading { printf "\e[38;5;81m$@\e[0m\n" @@ -70,7 +72,9 @@ heading "${next}: ${command} (${remaining} remaining)" ( cd ${next} - ${command} || { + ${command} &> /tmp/foreach.stdio || { + cd ${base} + cat /tmp/foreach.stdio | ${scriptdir}/path-prefix ${next} error "error: last command failed. fix problem and resume by executing: $0" error "directory: ${next}" exit 1 diff --git a/scripts/path-prefix b/scripts/path-prefix new file mode 100755 index 0000000000000..42749114f8b01 --- /dev/null +++ b/scripts/path-prefix @@ -0,0 +1,38 @@ +#!/usr/bin/env node +// converts relative file paths at the beginning of each input line to absolute file paths +const path = require('path'); +const fs = require('fs'); +const rl = require('readline'); + +const REMOVE_COLORS = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; + +const dir = process.argv[2]; +if (!dir) { + throw new Error(`usage: path-prefix DIR`); +} + +const reldir = path.relative(process.cwd(), dir); + +const ifc = rl.createInterface(process.stdin); +ifc.on('line', line => { + line = line.toString(); + const [ relative, ...rest ] = line.split(':'); + const rel = relative.replace(REMOVE_COLORS, ''); + const absolute = path.join(dir, rel); + if (relative && fs.existsSync(absolute)) { + process.stdout.write(path.join(reldir, rel) + ':' + rest.join(':') + '\n'); + } else { + process.stdout.write(line + '\n'); + } +}); + +process.stdin.resume(); + +function exists(p) { + try { + fs.readFileSync(p); + return true; + } catch (e) { + return false; + } +} \ No newline at end of file