Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools(foreach): replace relative to absolute paths in build output #2109

Merged
merged 1 commit into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion scripts/foreach.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions scripts/path-prefix
Original file line number Diff line number Diff line change
@@ -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;
}
}