Use cat
instead of read -r
to stream stdin to file (#120)
* Use `cat` instead of `read -r` to stream stdin to file * Cleanup dmd-wrapper * Fix typo * Make wrapper work on macOS * Use fifo instead of temporary file * Fix stdin-wrapper * Use `awk` instead of `read` hackery * Finish refactoring * Fix `exec` issue * Add myself as an coauthor of wrapper scripts (no shame at all :P) * Fix dmd-wrapper * Extract check_dubfile
This commit is contained in:
parent
216eadbcbe
commit
e293e0b5ab
57
dmd-wrapper
57
dmd-wrapper
@ -1,40 +1,41 @@
|
|||||||
#!/bin/bash -eu
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# Author: w0rp <devw0rp@gmail.com>
|
# Author: w0rp <devw0rp@gmail.com>, hauleth <lukasz@niemier.pl>
|
||||||
# Description: This script wraps DMD so we can get something which is capable of reading
|
# Description: This script wraps DMD so we can get something which is capable of reading
|
||||||
# D code from stdin.
|
# D code from stdin.
|
||||||
|
|
||||||
temp_file=`mktemp`
|
set -eu
|
||||||
mv "$temp_file" "$temp_file".d
|
|
||||||
temp_file="$temp_file".d
|
|
||||||
|
|
||||||
trap "rm $temp_file" EXIT
|
check_dubfile() {
|
||||||
|
[[ -e "$1/dub.json" || -e "$1/dub.sdl" || -e "$1/package.json" ]]
|
||||||
|
}
|
||||||
|
|
||||||
while read; do
|
traverse() {
|
||||||
echo "$REPLY" >> "$temp_file"
|
path=$(pwd)
|
||||||
done
|
while [ "$path" != "/" ] \
|
||||||
|
&& ! check_dubfile "$path"
|
||||||
|
do
|
||||||
|
path=$(dirname "$path")
|
||||||
|
done
|
||||||
|
|
||||||
# Read imports from DUB.
|
echo "$path"
|
||||||
original_path="$(pwd)"
|
}
|
||||||
path="$original_path"
|
|
||||||
import_line_options=''
|
|
||||||
|
|
||||||
# We need to look for variable configuration files in parent directories.
|
import_line_options() {
|
||||||
while [ "$path" != '/' ]; do
|
root="$(traverse)"
|
||||||
if [ -f "$path/dub.sdl" ] || [ -f "$path/dub.json" ] || [ -f "$path/package.json" ]; then
|
|
||||||
|
|
||||||
cd "$path"
|
if check_dubfile "$root"
|
||||||
|
then
|
||||||
|
dub describe --root="$root" --import-paths | awk '{ print "-I" $0 }'
|
||||||
|
else
|
||||||
|
echo -n
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
while read import_line; do
|
temp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'ale_linters')
|
||||||
import_line_options="$import_line_options -I$import_line"
|
temp_file="$temp_dir/file.d"
|
||||||
done <<< "$(dub describe --import-paths)"
|
trap 'rm -r "$temp_dir"' EXIT
|
||||||
|
|
||||||
cd "$original_path"
|
cp /dev/stdin "$temp_file"
|
||||||
|
|
||||||
break
|
dmd $(import_line_options) "$@" "$temp_file"
|
||||||
fi
|
|
||||||
|
|
||||||
path="$(dirname "$path")"
|
|
||||||
done
|
|
||||||
|
|
||||||
dmd $import_line_options "$@" "$temp_file"
|
|
||||||
|
@ -1,20 +1,28 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# Author: w0rp <devw0rp@gmail.com>
|
# Authors: w0rp <devw0rp@gmail.com>, hauleth <lukasz@niemier.pl>
|
||||||
# Description: This script implements a wrapper for any program which does not accept
|
# Description: This script implements a wrapper for any program which does not accept
|
||||||
# stdin input on most Unix machines. The input to the script is read to a
|
# stdin input on most Unix machines. The input to the script is read to a
|
||||||
# temporary file, and the first argument sets a particular file extension
|
# temporary file, and the first argument sets a particular file extension
|
||||||
# for the temporary file.
|
# for the temporary file.
|
||||||
|
|
||||||
|
set -eu
|
||||||
|
|
||||||
# All of the following arguments are read as command to run.
|
# All of the following arguments are read as command to run.
|
||||||
file_extension="$1"
|
file_extension="$1"
|
||||||
shift
|
shift
|
||||||
|
|
||||||
temp_file=$(mktemp --tmpdir "ale-XXXXXXXXX$file_extension")
|
temp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'ale_linter')
|
||||||
trap 'rm $temp_file' EXIT
|
temp_file="$temp_dir/file$file_extension"
|
||||||
|
trap 'rm -r "$temp_dir"' EXIT
|
||||||
|
|
||||||
while read -r; do
|
# In perfect world it wouldn't be needed, but some tools (`go vet`, I am looking
|
||||||
echo "$REPLY" >> "$temp_file"
|
# at you) do not fit in line and require filename ending. Otherwise it would be
|
||||||
done
|
# simple as
|
||||||
|
#
|
||||||
|
# "$@" /dev/stdin
|
||||||
|
#
|
||||||
|
# without all that hackery with copying `/dev/stdin`
|
||||||
|
cp /dev/stdin "$temp_file"
|
||||||
|
|
||||||
"$@" "$temp_file"
|
"$@" "$temp_file"
|
||||||
|
Loading…
Reference in New Issue
Block a user