Skip to content

Commit

Permalink
Allow installing dub in standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
wilzbach committed Mar 12, 2018
1 parent 0d6c917 commit ee24519
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ matrix:
- cmake -DCMAKE_INSTALL_PREFIX=$HOME/.local .
- make install -j$(nproc)
- popd
script: kcov --exclude-pattern=travis.sh $PWD/coverage ./travis.sh
script: kcov --exclude-pattern=travis.sh $PWD/coverage ./test/all.sh
after_success: bash <(curl -s https://codecov.io/bash) -s $PWD/coverage
- os: osx
script: ./travis.sh
script: ./test/all.sh
147 changes: 118 additions & 29 deletions script/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ COMMAND=
COMPILER=dmd
VERBOSITY=1
ROOT=~/dlang
DUB_VERSION=
DUB_BIN_PATH=
case $(uname -s) in
Darwin) OS=osx;;
Expand Down Expand Up @@ -309,7 +310,7 @@ parse_args() {
COMMAND=uninstall
;;

dmd | dmd-* | gdc | gdc-* | ldc | ldc-*)
dmd | dmd-* | gdc | gdc-* | ldc | ldc-* | dub | dub-* )
COMPILER=$1
;;

Expand Down Expand Up @@ -354,16 +355,19 @@ run_command() {
install_compiler "$2"
fi

local -r binpath=$(binpath_for_compiler "$2")
if [ -f "$ROOT/$2/$binpath/dub" ]; then
if [[ $("$ROOT/$2/$binpath/dub" --version) =~ ([0-9]+\.[0-9]+\.[0-9]+(-[^, ]+)?) ]]; then
log "Using dub ${BASH_REMATCH[1]} shipped with $2"

if ! [[ "$2" =~ ^dub ]]; then
local -r binpath=$(binpath_for_compiler "$2")
if [ -f "$ROOT/$2/$binpath/dub" ]; then
if [[ $("$ROOT/$2/$binpath/dub" --version) =~ ([0-9]+\.[0-9]+\.[0-9]+(-[^, ]+)?) ]]; then
log "Using dub ${BASH_REMATCH[1]} shipped with $2"
else
log "Using dub shipped with $2"
fi
else
log "Using dub shipped with $2"
DUB_BIN_PATH="${ROOT}/dub"
install_dub
fi
else
DUB_BIN_PATH="${ROOT}/dub"
install_dub
fi

write_env_vars "$2"
Expand Down Expand Up @@ -419,7 +423,8 @@ Run \`$ROOT/install.sh --help\` for usage information.
}

resolve_latest() {
case $COMPILER in
local compiler=${1:-$COMPILER}
case $compiler in
dmd)
local mirrors=(
"http://downloads.dlang.org/releases/LATEST"
Expand All @@ -440,11 +445,11 @@ resolve_latest() {
# dmd-nightly, dmd-master, dmd-branch
# but not: dmd-2016-10-19 or dmd-branch-2016-10-20
# dmd-2.068.0 or dmd-2.068.2-5
if [[ ! $COMPILER =~ -[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] &&
[[ ! $COMPILER =~ -[0-9][.][0-9]{3}[.][0-9]{1,3}(-[0-9]{1,3})? ]]
if [[ ! $compiler =~ -[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] &&
[[ ! $compiler =~ -[0-9][.][0-9]{3}[.][0-9]{1,3}(-[0-9]{1,3})? ]]
then
local url=http://nightlies.dlang.org/$COMPILER/LATEST
logV "Determing latest $COMPILER version ($url)."
local url=http://nightlies.dlang.org/$compiler/LATEST
logV "Determing latest $compiler version ($url)."
COMPILER="dmd-$(fetch "$url")"
fi
;;
Expand All @@ -458,11 +463,31 @@ resolve_latest() {
logV "Determining latest ldc-beta version ($url)."
COMPILER="ldc-$(fetch $url)"
;;
ldc-*)
;;
gdc)
local url=http://gdcproject.org/downloads/LATEST
logV "Determing latest gdc version ($url)."
COMPILER="gdc-$(fetch $url)"
;;
gdc-*)
;;
dub)
local mirrors=(
"http://code.dlang.org/download/LATEST"
"http://dlang.github.io/dub/LATEST"
)
logV "Determining latest dub version (${mirrors[0]})."
DUB_VERSION="$(fetch "${mirrors[@]}")"
local DUB="dub-${DUB_VERSION}"
if [ -z "$1" ] ; then
COMPILER="$DUB"
fi
;;
dub-*)
;;
*)
fatal "Invalid compiler: $COMPILER"
esac
}

Expand Down Expand Up @@ -542,6 +567,13 @@ install_compiler() {
download_without_verify "$ROOT/$1/bin/gdmd" "$url"
chmod +x "$ROOT/$1/bin/gdmd"

elif [[ $1 =~ ^dub-([0-9]+\.[0-9]+(\.[0-9]+)?(-.*)?)$ ]]; then
local ver=${BASH_REMATCH[1]}
local mirrors=(
"http://code.dlang.org/files/dub-$ver-$OS-$ARCH.tar.gz"
"https://github.com/dlang/dub/releases/download/$ver/dub-$DUB_VERSION-$OS-$ARCH.tar.gz"
)
download_and_unpack_without_verify "$ROOT/$compiler" "${mirrors[@]}"
else
fatal "Unknown compiler '$compiler'"
fi
Expand Down Expand Up @@ -580,6 +612,8 @@ download_and_unpack() {
download "$tmp/$name" "$do_verify" "${urls[@]}"
if [[ $name =~ \.tar\.xz$ ]]; then
tar --strip-components=1 -C "$tmp" -Jxf "$tmp/$name"
elif [[ $name =~ \.tar\.gz$ ]]; then
tar -C "$tmp" -xf "$tmp/$name"
else
unzip -q -d "$tmp" "$tmp/$name"
mv "$tmp/dmd2"/* "$tmp/"
Expand Down Expand Up @@ -636,6 +670,9 @@ binpath_for_compiler() {
gdc*)
local -r binpath=bin
;;
dub*)
local -r binpath=
;;
esac
echo "$binpath"
}
Expand Down Expand Up @@ -667,67 +704,120 @@ write_env_vars() {
local dc=gdc
local dmd=gdmd
;;
dub*)
local libpath=
local dc=
local dmd=
;;
esac

logV "Writing environment variables to $ROOT/$1/activate"
cat > "$ROOT/$1/activate" <<EOF
deactivate() {
export PATH="\$_OLD_D_PATH"
EOF
if [ -n "$libpath" ] ; then
cat >> "$ROOT/$1/activate" <<EOF
export LIBRARY_PATH="\$_OLD_D_LIBRARY_PATH"
export LD_LIBRARY_PATH="\$_OLD_D_LD_LIBRARY_PATH"
export PS1="\$_OLD_D_PS1"
unset _OLD_D_PATH
unset _OLD_D_LIBRARY_PATH
unset _OLD_D_LD_LIBRARY_PATH
unset _OLD_D_PS1
EOF
fi
if [ -n "$dmd" ] ; then
cat >> "$ROOT/$1/activate" <<EOF
unset DMD
unset DC
EOF
fi

cat >> "$ROOT/$1/activate" <<EOF
export PS1="\$_OLD_D_PS1"
unset _OLD_D_PATH
unset _OLD_D_PS1
unset -f deactivate
}
_OLD_D_PATH="\${PATH:-}"
EOF

if [ -n "$libpath" ] ; then
cat >> "$ROOT/$1/activate" <<EOF
_OLD_D_LIBRARY_PATH="\${LIBRARY_PATH:-}"
_OLD_D_LD_LIBRARY_PATH="\${LD_LIBRARY_PATH:-}"
_OLD_D_PS1="\${PS1:-}"
export PATH="${DUB_BIN_PATH}${DUB_BIN_PATH:+:}$ROOT/$1/$binpath\${PATH:+:}\${PATH:-}"
export LIBRARY_PATH="$ROOT/$1/$libpath\${LIBRARY_PATH:+:}\${LIBRARY_PATH:-}"
export LD_LIBRARY_PATH="$ROOT/$1/$libpath\${LD_LIBRARY_PATH:+:}\${LD_LIBRARY_PATH:-}"
EOF
fi

cat >> "$ROOT/$1/activate" <<EOF
_OLD_D_PATH="\${PATH:-}"
_OLD_D_PS1="\${PS1:-}"
export PS1="($1)\${PS1:-}"
export PATH="${DUB_BIN_PATH}${DUB_BIN_PATH:+:}$ROOT/$1/$binpath\${PATH:+:}\${PATH:-}"
EOF

if [ -n "$dmd" ] ; then
cat >> "$ROOT/$1/activate" <<EOF
export DMD=$dmd
export DC=$dc
export PS1="($1)\${PS1:-}"
EOF
fi

logV "Writing environment variables to $ROOT/$1/activate.fish"
cat > "$ROOT/$1/activate.fish" <<EOF
function deactivate
set -gx PATH \$_OLD_D_PATH
EOF

if [ -n "$libpath" ] ; then
cat >> "$ROOT/$1/activate.fish" <<EOF
set -gx LIBRARY_PATH \$_OLD_D_LIBRARY_PATH
set -gx LD_LIBRARY_PATH \$_OLD_D_LD_LIBRARY_PATH
set -e _OLD_D_LIBRARY_PATH
set -e _OLD_D_LD_LIBRARY_PATH
EOF
fi

cat >> "$ROOT/$1/activate.fish" <<EOF
functions -e fish_prompt
functions -c _old_d_fish_prompt fish_prompt
functions -e _old_d_fish_prompt
set -e _OLD_D_PATH
set -e _OLD_D_LIBRARY_PATH
set -e _OLD_D_LD_LIBRARY_PATH
EOF

if [ -n "$dmd" ] ; then
cat >> "$ROOT/$1/activate.fish" <<EOF
set -e DMD
set -e DC
EOF
fi
cat >> "$ROOT/$1/activate.fish" <<EOF
functions -e deactivate
end
set -g _OLD_D_PATH \$PATH
set -g _OLD_D_LIBRARY_PATH \$LIBRARY_PATH
set -g _OLD_D_LD_LIBRARY_PATH \$LD_LIBRARY_PATH
set -g _OLD_D_PS1 \$PS1
set -gx PATH ${DUB_BIN_PATH:+\'}${DUB_BIN_PATH}${DUB_BIN_PATH:+\' }'$ROOT/$1/$binpath' \$PATH
EOF
if [ -n "$libpath" ] ; then
cat >> "$ROOT/$1/activate.fish" <<EOF
set -g _OLD_D_LIBRARY_PATH \$LIBRARY_PATH
set -g _OLD_D_LD_LIBRARY_PATH \$LD_LIBRARY_PATH
set -gx LIBRARY_PATH '$ROOT/$1/$libpath' \$LIBRARY_PATH
set -gx LD_LIBRARY_PATH '$ROOT/$1/$libpath' \$LD_LIBRARY_PATH
EOF
fi

if [ -n "$dmd" ] ; then
cat >> "$ROOT/$1/activate.fish" <<EOF
set -gx DMD $dmd
set -gx DC $dc
EOF
fi
cat >> "$ROOT/$1/activate.fish" <<EOF
functions -c fish_prompt _old_d_fish_prompt
function fish_prompt
printf '($1)%s' (_old_d_fish_prompt)
Expand Down Expand Up @@ -763,9 +853,8 @@ install_dub() {
log "no dub binaries available for $OS"
return
fi
local url=http://code.dlang.org/download/LATEST
logV "Determining latest dub version ($url)."
dub="dub-$(fetch $url)"
resolve_latest dub
dub="dub-${DUB_VERSION}"
if [ -d "$ROOT/$dub" ]; then
log "$dub already installed"
return
Expand Down
12 changes: 12 additions & 0 deletions test/all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

set -eu -o pipefail

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. $DIR/common.sh

for file in $(find "$DIR" -name "*.sh") ; do
if ! ( [[ "$file" == */all.sh ]] || [[ "$file" == */common.sh ]] ) ; then
$file
fi
done
File renamed without changes.
6 changes: 6 additions & 0 deletions test/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

set -eu -o pipefail

ROOT="$DIR/../"
INSTALLER="$ROOT/script/install.sh"
32 changes: 32 additions & 0 deletions test/dub.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

set -uexo pipefail

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. $DIR/common.sh

compilers=(
dub-1.2.0
dub-1.5.0
dub-1.6.0
)

versions=(
'DUB version 1.2.0, built on Jan 22 2017'
'DUB version 1.5.0, built on Sep 1 2017'
'DUB version 1.6.0, built on Nov 8 2017'
)

for idx in "${!compilers[@]}"
do
compiler="${compilers[$idx]}"
echo "Testing: $compiler"
"$INSTALLER" $compiler

. ~/dlang/$compiler/activate
compilerVersion=$(dub --version | sed -n 1p)
test "$compilerVersion" = "${versions[$idx]}"
deactivate

"$INSTALLER" uninstall $compiler
done

0 comments on commit ee24519

Please sign in to comment.