-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsexec
executable file
·99 lines (81 loc) · 2.53 KB
/
csexec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
#
# csexec
#
# Author:
# Roman M. Yagodin <[email protected]>
#
# Copyright (c) 2015-2019 Roman M. Yagodin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# set TMPDIR
if [ -z "$TMPDIR" ]; then
export TMPDIR="/tmp/csexec"
# create temp directory, if not exists
mkdir -p "$TMPDIR"
fi
# copy (update) assemblies to temp directory
cp -u $HOME/.config/csharp/*.dll "$TMPDIR"
# use temp directory to get referenced assemblies
COMPILER_ARGS="/nologo -lib:/$TMPDIR"
# compiler args should start with "-",
# else the arg contains script file name
if [[ ${1:0:1} == "-" ]]; then
CSEXEC_ARGS=$1
# need to run in terminal?
if [[ ${CSEXEC_ARGS:0:2} == "-t" || ${CSEXEC_ARGS:0:2} == "-T" ]]; then
TERMINAL_COMMAND="x-terminal-emulator -e"
CSEXEC_ARGS=${CSEXEC_ARGS:2}
fi
# get additional compiler args
if [ -n "$CSEXEC_ARGS" ]; then
COMPILER_ARGS="$COMPILER_ARGS $CSEXEC_ARGS"
fi
shift
fi
# script file
SCRIPT_FILE="$1"
# get temporary filenames
SOURCE_FILE="$(mktemp --tmpdir=$TMPDIR)"
BINARY_FILE="$(mktemp --tmpdir=$TMPDIR)"
CSEXEC_LOG="$(mktemp --tmpdir=$TMPDIR)"
# remove first (hashbang) line
tail -n +2 "$SCRIPT_FILE" > $SOURCE_FILE
pushd . > /dev/null
cd "$TMPDIR"
# build
csc $COMPILER_ARGS $SOURCE_FILE -out:$BINARY_FILE &> $CSEXEC_LOG
COMPILER_EXIT_CODE=$?
popd > /dev/null
# check working directory path length
LANG_OLD=$LANG
LANG=C
if [ ${#PWD} -gt 260 ]; then
COMPILER_EXIT_CODE=1
echo "Error: Cannot run the script - path is too long." >> $CSEXEC_LOG
fi
LANG=$LANG_OLD
if [ ! -s $CSEXEC_LOG ]; then
# if compiler log is empty, remove it from the working directory (if it exists)
rm -f "csexec.log"
else
# otherwise, copy log to the working directory
cp -f $CSEXEC_LOG "csexec.log"
fi
# run
if [ $COMPILER_EXIT_CODE -eq 0 ]; then
$TERMINAL_COMMAND mono $BINARY_FILE "$@"
fi
# remove temporary files
rm -f $SOURCE_FILE $BINARY_FILE $CSEXEC_LOG