-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkotlins
executable file
·198 lines (166 loc) · 6.05 KB
/
kotlins
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash --posix
#
##############################################################################
# Copyright 2002-2011, LAMP/EPFL
# Copyright 2011, JetBrains
#
# This is free software; see the distribution for copying conditions.
# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
##############################################################################
cygwin=false;
case "`uname`" in
CYGWIN*) cygwin=true ;;
esac
# Convert a file to an absolute path for the native platform
function nativePath() {
local npath=`dirname "$1"`
npath=`cd "$npath"; pwd -P`
npath="$npath/`basename "$1"`"
if $cygwin; then
npath=`cygpath --windows "$npath" | tr '\\\\' '/'`
fi
echo $npath
}
# Finding the root folder for this Kotlin distribution
SOURCE=`which kotlinc-jvm`;
SCRIPT=`basename "$SOURCE"`;
while [ -h "$SOURCE" ]; do
SCRIPT=`basename "$SOURCE"`;
LOOKUP=`ls -ld "$SOURCE"`;
TARGET=`expr "$LOOKUP" : '.*-> \(.*\)$'`;
if expr "${TARGET:-.}/" : '/.*/$' > /dev/null; then
SOURCE=${TARGET:-.};
else
SOURCE=`dirname "$SOURCE"`/${TARGET:-.};
fi;
done;
KOTLIN_HOME=`dirname "$SOURCE"`
KOTLIN_HOME=`cd "$KOTLIN_HOME"; pwd -P`
KOTLIN_HOME=`cd "$KOTLIN_HOME"/..; pwd`
# Remove spaces from KOTLIN_HOME on windows
if $cygwin; then
KOTLIN_HOME=`cygpath --windows --short-name "$KOTLIN_HOME"`
KOTLIN_HOME=`cygpath --unix "$KOTLIN_HOME"`
fi
# echo "KOTLIN_HOME=$KOTLIN_HOME"
# Constructing the extension classpath
TOOL_CLASSPATH="$KOTLIN_HOME"/lib/kotlin-runtime.jar
[ -n "$JAVA_OPTS" ] || JAVA_OPTS="-Xmx256M -Xms32M"
# break out -D and -J options and add them to JAVA_OPTS as well
# so they reach the underlying JVM in time to do some good. The
# -D options will be available as system properties.
declare -a java_args
declare -a kotlin_args
cmd=""
scriptCp=""
while [ $# -gt 0 ]; do
case "$1" in
-D*)
# pass to kotlin as well: otherwise we lose it sometimes when we
# need it, e.g. communicating with a server compiler.
java_args=("${java_args[@]}" "$1")
# kotlin_args=("${kotlin_args[@]}" "$1")
shift
;;
-cp)
scriptCp=$2
shift 2
;;
-J*)
# as with -D, pass to kotlin even though it will almost
# never be used.
java_args=("${java_args[@]}" "${1:2}")
# kotlin_args=("${kotlin_args[@]}" "$1")
shift
;;
*)
# First non-option is the executable. If it's a file then we need to replace it with 'MainKt' as it will need to be compiled
if [ "" = "$cmd" ]; then
cmd="$1"
if [ -f "$cmd" ]; then
kotlin_args=("${kotlin_args[@]}" "MainKt")
else
kotlin_args=("${kotlin_args[@]}" "$1")
fi
else
kotlin_args=("${kotlin_args[@]}" "$1")
fi
shift
;;
esac
done
if [ "" = "$cmd" ]; then
echo "Usage: kotlin [-cp <classpath>] <script|source|module|className> [args...]"
exit 1
fi
# reset "$@" to the remaining args
set -- "${kotlin_args[@]}"
if [ -z "$JAVACMD" -a -n "$JAVA_HOME" -a -x "$JAVA_HOME/bin/java" ]; then
JAVACMD="$JAVA_HOME/bin/java"
fi
# OS X uses TMPDIR, other variants use TMP, others none
[ -z "$TMPDIR" ] || temp="$TMPDIR/"
[ -z "$TMP" ] || temp="$TMP/"
[ -n "$temp" ] || temp="/tmp/"
# If the jar doesn't exist, compile it
if [ -f "$cmd" ]; then
[ -n "$KOTLIN_SCRIPT_CACHE" ] || KOTLIN_SCRIPT_CACHE="$HOME/.kotlin-script-cache"
fullPath=`dirname "$cmd"`
fullPath=`cd "$fullPath"; pwd -P`
jarDir="$KOTLIN_SCRIPT_CACHE$fullPath"
fullPath="$fullPath/`basename "$cmd"`"
jar="$KOTLIN_SCRIPT_CACHE$fullPath.jar"
# If the jar doesn't exist, or the source is newer than the jar
if [[ (! -f "$jar") || ("$cmd" -nt "$jar") ]]; then
module="$cmd"
# If the command isn't a module, create one automatically
if [ `grep -c '</modules>' "$cmd"` -eq 0 ]; then
line=`head -1 "$cmd"`
# If the command is a script, strip the script header and create a main method if necessary
src="${temp}main.kt"
if [ "#!" = "${line:0:2}" ]; then
sedOpts="1d"
[ "#!/bin/sh" != "${line:0:9}" ] || sedOpts="/#!/,/!#/d"
if [ `grep -c 'fun main(' "$cmd"` -ne 0 ]; then
sed "$sedOpts" "$cmd" >> "$src"
else
echo 'fun main(args: Array<String>) {' > "$src"
sed "$sedOpts" "$cmd" >> "$src"
echo ' }' >> "$src"
fi
# Packages are necessary to develop multiple scripts in the IDE without
# the main method clashing in the default package. However, they aren't
# useful from a script execution point of view.
sed -ie '/^package /d' "$src"
else
cp "$cmd" "$src"
fi
delSrc="\"$src\""
# cat "$src"
# Create a module, so the classpath can be applied
module="${temp}kotlin$$.xml"
echo "<modules>" > $module
echo " <module name=\"mymodule\" outputDir=\"${temp}\" type=\"TYPE_PRODUCTION\">" >> $module
echo " <sources path=\"$(nativePath "$src")\"/>" >> $module
for i in $(echo $scriptCp | tr ":" "\n"); do
i=$(nativePath "$i")
echo " <classpath path=\"$i\"/>" >> $module
done
echo " </module>" >> $module
echo "</modules>" >> $module
# cat "$module"
trap "rm \""$module\"" "$delSrc"" EXIT
fi
set -e
mkdir -p "$jarDir"
# echo kotlinc-jvm -module "$(nativePath "$module")" -d "$(nativePath "$jar")"
kotlinc-jvm -module "$(nativePath "$module")" -d "$(nativePath "$jar")"
fi
fi
classpath="${TOOL_CLASSPATH}:$jar:${scriptCp}"
if $cygwin; then
classpath=`cygpath --windows --path "$classpath"`
fi
# echo "${JAVACMD:=java}" $JAVA_OPTS "${java_args[@]}" -cp "$classpath" "$@"
"${JAVACMD:=java}" $JAVA_OPTS "${java_args[@]}" -cp "$classpath" "$@"