Skip to content

Commit cb78d4f

Browse files
committed
Merge pull request #18 from sbt/wip/cleanup-for-play
Wip/cleanup for play
2 parents d2af5f7 + bd83f04 commit cb78d4f

File tree

28 files changed

+711
-275
lines changed

28 files changed

+711
-275
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
#!/bin/bash
2+
3+
### ------------------------------- ###
4+
### Helper methods for BASH scripts ###
5+
### ------------------------------- ###
6+
7+
realpath () {
8+
(
9+
TARGET_FILE="$1"
10+
11+
cd $(dirname "$TARGET_FILE")
12+
TARGET_FILE=$(basename "$TARGET_FILE")
13+
14+
COUNT=0
15+
while [ -L "$TARGET_FILE" -a $COUNT -lt 100 ]
16+
do
17+
TARGET_FILE=$(readlink "$TARGET_FILE")
18+
cd $(dirname "$TARGET_FILE")
19+
TARGET_FILE=$(basename "$TARGET_FILE")
20+
COUNT=$(($COUNT + 1))
21+
done
22+
23+
# make sure we grab the actual windows path, instead of cygwin's path.
24+
echo $(cygwinpath "$(pwd -P)/$TARGET_FILE")
25+
)
26+
}
27+
28+
# TODO - Do we need to detect msys?
29+
30+
# Uses uname to detect if we're in the odd cygwin environment.
31+
is_cygwin() {
32+
local os=$(uname -s)
33+
case "$os" in
34+
CYGWIN*) return 0 ;;
35+
*) return 1 ;;
36+
esac
37+
}
38+
39+
# This can fix cygwin style /cygdrive paths so we get the
40+
# windows style paths.
41+
cygwinpath() {
42+
local file="$1"
43+
if is_cygwin; then
44+
echo $(cygpath -w $file)
45+
else
46+
echo $file
47+
fi
48+
}
49+
50+
# Make something URI friendly
51+
make_url() {
52+
url="$1"
53+
local nospaces=${url// /%20}
54+
if is_cygwin; then
55+
echo "/${nospaces//\\//}"
56+
else
57+
echo "$nospaces"
58+
fi
59+
}
60+
61+
# Detect if we should use JAVA_HOME or just try PATH.
62+
get_java_cmd() {
63+
if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
64+
echo "$JAVA_HOME/bin/java"
65+
else
66+
echo "java"
67+
fi
68+
}
69+
70+
echoerr () {
71+
echo 1>&2 "$@"
72+
}
73+
vlog () {
74+
[[ $verbose || $debug ]] && echoerr "$@"
75+
}
76+
dlog () {
77+
[[ $debug ]] && echoerr "$@"
78+
}
79+
execRunner () {
80+
# print the arguments one to a line, quoting any containing spaces
81+
[[ $verbose || $debug ]] && echo "# Executing command line:" && {
82+
for arg; do
83+
if printf "%s\n" "$arg" | grep -q ' '; then
84+
printf "\"%s\"\n" "$arg"
85+
else
86+
printf "%s\n" "$arg"
87+
fi
88+
done
89+
echo ""
90+
}
91+
92+
exec "$@"
93+
}
94+
addJava () {
95+
dlog "[addJava] arg = '$1'"
96+
java_args=( "${java_args[@]}" "$1" )
97+
}
98+
addApp () {
99+
dlog "[addApp] arg = '$1'"
100+
sbt_commands=( "${app_commands[@]}" "$1" )
101+
}
102+
addResidual () {
103+
dlog "[residual] arg = '$1'"
104+
residual_args=( "${residual_args[@]}" "$1" )
105+
}
106+
addDebugger () {
107+
addJava "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1"
108+
}
109+
# a ham-fisted attempt to move some memory settings in concert
110+
# so they need not be messed around with individually.
111+
get_mem_opts () {
112+
local mem=${1:-1024}
113+
local perm=$(( $mem / 4 ))
114+
(( $perm > 256 )) || perm=256
115+
(( $perm < 1024 )) || perm=1024
116+
local codecache=$(( $perm / 2 ))
117+
118+
echo "-Xms${mem}m -Xmx${mem}m -XX:MaxPermSize=${perm}m -XX:ReservedCodeCacheSize=${codecache}m"
119+
}
120+
require_arg () {
121+
local type="$1"
122+
local opt="$2"
123+
local arg="$3"
124+
if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then
125+
die "$opt requires <$type> argument"
126+
fi
127+
}
128+
require_arg () {
129+
local type="$1"
130+
local opt="$2"
131+
local arg="$3"
132+
if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then
133+
die "$opt requires <$type> argument"
134+
fi
135+
}
136+
is_function_defined() {
137+
declare -f "$1" > /dev/null
138+
}
139+
140+
# Attempt to detect if the script is running via a GUI or not
141+
# TODO - Determine where/how we use this generically
142+
detect_terminal_for_ui() {
143+
[[ ! -t 0 ]] && [[ "${#residual_args}" == "0" ]] && {
144+
echo "true"
145+
}
146+
# SPECIAL TEST FOR MAC
147+
[[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]] && [[ "${#residual_args}" == "0" ]] && {
148+
echo "true"
149+
}
150+
}
151+
152+
# Processes incoming arguments and places them in appropriate global variables. called by the run method.
153+
process_args () {
154+
while [[ $# -gt 0 ]]; do
155+
case "$1" in
156+
-h|-help) usage; exit 1 ;;
157+
-v|-verbose) verbose=1 && shift ;;
158+
-d|-debug) debug=1 && shift ;;
159+
160+
-mem) require_arg integer "$1" "$2" && app_mem="$2" && shift 2 ;;
161+
-jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;;
162+
163+
-java-home) require_arg path "$1" "$2" && java_cmd="$2/bin/java" && shift 2 ;;
164+
165+
-D*) addJava "$1" && shift ;;
166+
-J*) addJava "${1:2}" && shift ;;
167+
*) addResidual "$1" && shift ;;
168+
esac
169+
done
170+
171+
is_function_defined process_my_args && {
172+
myargs=("${residual_args[@]}")
173+
residual_args=()
174+
process_my_args "${myargs[@]}"
175+
}
176+
}
177+
178+
# Actually runs the script.
179+
run() {
180+
# TODO - check for sane environment
181+
182+
# process the combined args, then reset "$@" to the residuals
183+
process_args "$@"
184+
set -- "${residual_args[@]}"
185+
argumentCount=$#
186+
187+
#check for jline terminal fixes on cygwin
188+
if is_cygwin; then
189+
stty -icanon min 1 -echo > /dev/null 2>&1
190+
addJava "-Djline.terminal=jline.UnixTerminal"
191+
addJava "-Dsbt.cygwin=true"
192+
fi
193+
# run sbt
194+
execRunner "$java_cmd" \
195+
$(get_mem_opts $app_mem) \
196+
${java_opts} \
197+
${java_args[@]} \
198+
-cp "$app_classpath" \
199+
$app_mainclass \
200+
"${app_commands[@]}" \
201+
"${residual_args[@]}"
202+
203+
local exit_code=$?
204+
if is_cygwin; then
205+
stty icanon echo > /dev/null 2>&1
206+
fi
207+
exit $exit_code
208+
}
209+
210+
# Loads a configuration file full of default command line options for this script.
211+
loadConfigFile() {
212+
cat "$1" | sed '/^\#/d'
213+
}
214+
215+
### ------------------------------- ###
216+
### Start of customized settings ###
217+
### ------------------------------- ###
218+
usage() {
219+
cat <<EOM
220+
Usage: $script_name [options]
221+
222+
-h | -help print this message
223+
-v | -verbose this runner is chattier
224+
-d | -debug set sbt log level to debug
225+
-mem <integer> set memory options (default: $sbt_mem, which is $(get_mem_opts $sbt_mem))
226+
-jvm-debug <port> Turn on JVM debugging, open at the given port.
227+
228+
# java version (default: java from PATH, currently $(java -version 2>&1 | grep version))
229+
-java-home <path> alternate JAVA_HOME
230+
231+
# jvm options and output control
232+
JAVA_OPTS environment variable, if unset uses "$java_opts"
233+
-Dkey=val pass -Dkey=val directly to the java runtime
234+
-J-X pass option -X directly to the java runtime
235+
(-J is stripped)
236+
237+
In the case of duplicated or conflicting options, the order above
238+
shows precedence: JAVA_OPTS lowest, command line options highest.
239+
EOM
240+
}
241+
242+
### ------------------------------- ###
243+
### Main script ###
244+
### ------------------------------- ###
245+
246+
declare -a residual_args
247+
declare -a java_args
248+
declare -a app_commands
249+
declare -r app_home="$(realpath "$(dirname "$0")")"
250+
# TODO - Check whether this is ok in cygwin...
251+
declare -r lib_dir="${app_home}/../lib"
252+
${{template_declares}}
253+
declare -r java_cmd=$(get_java_cmd)
254+
255+
# Now check to see if it's a good enough version
256+
# TODO - Check to see if we have a configured default java version, otherwise use 1.6
257+
declare -r java_version=$("$java_cmd" -version 2>&1 | awk -F '"' '/version/ {print $2}')
258+
if [[ "$java_version" == "" ]]; then
259+
echo
260+
echo No java installations was detected.
261+
echo Please go to http://www.java.com/getjava/ and download
262+
echo
263+
exit 1
264+
elif [[ ! "$java_version" > "1.6" ]]; then
265+
echo
266+
echo The java installation you have is not up to date
267+
echo $app_name requires at least version 1.6+, you have
268+
echo version $java_version
269+
echo
270+
echo Please go to http://www.java.com/getjava/ and download
271+
echo a valid Java Runtime and install before running $app_name.
272+
echo
273+
exit 1
274+
fi
275+
276+
277+
# if configuration files exist, prepend their contents to $@ so it can be processed by this runner
278+
[[ -f "$script_conf_file" ]] && set -- $(loadConfigFile "$script_conf_file") "$@"
279+
280+
run "$@"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
@REM @@APP_NAME@@ launcher script
2+
@REM
3+
@REM Envioronment:
4+
@REM JAVA_HOME - location of a JDK home dir (optional if java on path)
5+
@REM CFG_OPTS - JVM options (optional)
6+
@REM Configuration:
7+
@REM @@APP_ENV_NAME@@_config.txt found in the @@APP_ENV_NAME@@_HOME.
8+
@setlocal enabledelayedexpansion
9+
10+
@echo off
11+
if "%@@APP_ENV_NAME@@_HOME%"=="" set "@@APP_ENV_NAME@@_HOME=%~dp0\\.."
12+
set ERROR_CODE=0
13+
14+
set "APP_LIB_DIR=%@@APP_ENV_NAME@@_HOME%\lib\"
15+
16+
rem Detect if we were double clicked, although theoretically A user could
17+
rem manually run cmd /c
18+
for %%x in (%cmdcmdline%) do if %%~x==/c set DOUBLECLICKED=1
19+
20+
rem FIRST we load the config file of extra options.
21+
set "CFG_FILE=%@@APP_ENV_NAME@@_HOME%\@@APP_ENV_NAME@@_config.txt"
22+
set CFG_OPTS=
23+
if exist %CFG_FILE% (
24+
FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%CFG_FILE%") DO (
25+
set DO_NOT_REUSE_ME=%%i
26+
rem ZOMG (Part #2) WE use !! here to delay the expansion of
27+
rem CFG_OPTS, otherwise it remains "" for this loop.
28+
set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
29+
)
30+
)
31+
32+
rem We use the value of the JAVACMD environment variable if defined
33+
set _JAVACMD=%JAVACMD%
34+
35+
if "%_JAVACMD%"=="" (
36+
if not "%JAVA_HOME%"=="" (
37+
if exist "%JAVA_HOME%\bin\java.exe" set "_JAVACMD=%JAVA_HOME%\bin\java.exe"
38+
)
39+
)
40+
41+
if "%_JAVACMD%"=="" set _JAVACMD=java
42+
43+
rem Detect if this java is ok to use.
44+
for /F %%j in ('"%_JAVACMD%" -version 2^>^&1') do (
45+
if %%~j==Java set JAVAINSTALLED=1
46+
)
47+
48+
rem Detect the same thing about javac
49+
if "%_JAVACCMD%"=="" (
50+
if not "%JAVA_HOME%"=="" (
51+
if exist "%JAVA_HOME%\bin\javac.exe" set "_JAVACCMD=%JAVA_HOME%\bin\javac.exe"
52+
)
53+
)
54+
if "%_JAVACCMD%"=="" set _JAVACCMD=javac
55+
for /F %%j in ('"%_JAVACCMD%" -version 2^>^&1') do (
56+
if %%~j==javac set JAVACINSTALLED=1
57+
)
58+
59+
rem BAT has no logical or, so we do it OLD SCHOOL! Oppan Redmond Style
60+
set JAVAOK=true
61+
if not defined JAVAINSTALLED set JAVAOK=false
62+
rem TODO - JAVAC is an optional requirement.
63+
if not defined JAVACINSTALLED set JAVAOK=false
64+
65+
if "%JAVAOK%"=="false" (
66+
echo.
67+
echo A Java JDK is not installed or can't be found.
68+
if not "%JAVA_HOME%"=="" (
69+
echo JAVA_HOME = "%JAVA_HOME%"
70+
)
71+
echo.
72+
echo Please go to
73+
echo http://www.oracle.com/technetwork/java/javase/downloads/index.html
74+
echo and download a valid Java JDK and install before running @@APP_NAME@@.
75+
echo.
76+
echo If you think this message is in error, please check
77+
echo your environment variables to see if "java.exe" and "javac.exe" are
78+
echo available via JAVA_HOME or PATH.
79+
echo.
80+
if defined DOUBLECLICKED pause
81+
exit /B 1
82+
)
83+
84+
85+
rem We use the value of the JAVA_OPTS environment variable if defined, rather than the config.
86+
set _JAVA_OPTS=%JAVA_OPTS%
87+
if "%_JAVA_OPTS%"=="" set _JAVA_OPTS=%CFG_OPTS%
88+
89+
:run
90+
91+
set "APP_CLASSPATH=@@APP_CLASSPATH@@"
92+
rem TODO - figure out how to pass arguments....
93+
"%_JAVACMD%" %_JAVA_OPTS% %@@APP_ENV_NAME@@_OPTS% -cp "%APP_CLASSPATH%" @@APP_MAIN_CLASS@@ %CMDS%
94+
if ERRORLEVEL 1 goto error
95+
goto end
96+
97+
:error
98+
set ERROR_CODE=1
99+
100+
:end
101+
102+
@endlocal
103+
104+
exit /B %ERROR_CODE%

src/main/scala/com/typesafe/sbt/PackagerPlugin.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ object SbtNativePackager extends Plugin
4141
addPackage(UniversalDocs, packageXzTarball in UniversalDocs, "txz")
4242

4343
object packageArchetype {
44+
private[this] def genericMappingSettings: Seq[Setting[_]] = packagerSettings ++ mapGenericFilesToLinux ++ mapGenericFilesToWindows
4445
def java_application: Seq[Setting[_]] =
45-
packagerSettings ++ archetypes.JavaAppPackaging.settings
46+
genericMappingSettings ++ archetypes.JavaAppPackaging.settings
4647
}
4748

4849
// TODO - Add a few targets that detect the current OS and build a package for that OS.

0 commit comments

Comments
 (0)