Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 195 lines (172 sloc) 4.337 kb
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
#!/bin/bash

# This program contains parts of narwhal's "sea" program,
# as well as bits borrowed from Tim Caswell's "nvm"

# nave install <version>
# Fetch the version of node and install it in nave's folder.

# nave use <version>
# Install the <version> if it isn't already, and then start
# a subshell with that version's folder at the start of the
# $PATH

# nave --version <version> program.js
# Like "nave use", but have the subshell start the program.js
# immediately.

# When told to use a version:
# Ensure that the version exists, install it, and
# then add its prefix to the PATH, and start a subshell.

main () {
  # get the absolute path of the executable
  SELF_PATH=$( cd -P -- "$(dirname -- "$0")" \
            && pwd -P \
            ) && SELF_PATH=$SELF_PATH/$(basename -- "$0")

  # resolve symlinks
  while [ -h "$SELF_PATH" ]; do
    DIR=$(dirname -- "$SELF_PATH")
    SYM=$(readlink -- "$SELF_PATH")
    SELF_PATH=$( cd -- "$DIR" \
              && cd -- $(dirname -- "$SYM") \
              && pwd \
              )/$(basename -- "$SYM")
  done

  export NAVE_SRC="$(dirname -- "$SELF_PATH")/src"
  export NAVE_ROOT="$(dirname -- "$SELF_PATH")/installed"
  ensure_dir "$NAVE_SRC"
  ensure_dir "$NAVE_ROOT"
  
  local cmd="$1"
  shift
case $cmd in
    install | fetch | use | install | clean | test | ls )
      cmd="nave_$cmd"
      ;;
    * )
      cmd="nave_help"
      ;;
  esac
  $cmd "$@"
  exit 0
}

function enquote_all () {
  ARGS=""
  for ARG in "$@"; do
    [ -n "$ARGS" ] && ARGS="$ARGS "
    ARGS="$ARGS'""$( echo " $ARG" \
| cut -c 2- \
| sed 's/'"'"'/'"'"'"'"'"'"'"'"'/g' \
)""'"
  done
  echo "$ARGS"
}

ensure_dir () {
  if ! [ -d "$1" ]; then
    mkdir -p -- "$1" || fail "couldn't create $1"
  fi
}
remove_dir () {
  if [ -d "$1" ]; then
    rm -rf -- "$1" || fail "Could not remove $1"
  fi
}

fail () {
  echo "$@" >&2
  exit 1
}

nave_fetch () {
  version="$1"
  version="${version/v/}"
  if nave_has "$version"; then
    echo "already fetched $version" >&2
    return 0
  fi

  src="$NAVE_SRC/$version"
  remove_dir "$src"
  ensure_dir "$src"
  if [ "$version" == "HEAD" ]; then
    url="http://github.com/ry/node/tarball/master"
  else
    url="http://nodejs.org/dist/node-v$version.tar.gz"
  fi
  curl -L "$url" \
    | tar xzv -C "$src" --strip 1 \
    || fail "Couldn't fetch $version"
  return 0
}

nave_install () {
  version="$1"
  version="${version/v/}"

  if nave_installed "$version"; then
    echo "Already installed: $version" >&2
    return 0
  fi
  nave_fetch "$version"

  src="$NAVE_SRC/$version"
  install="$NAVE_ROOT/$version"
  remove_dir "$install"
  ensure_dir "$install"
  ( cd -- "$src"
    JOBS=8 ./configure --prefix="$install" || fail "Failed to configure $version"
    JOBS=8 make || fail "Failed to make $version"
    make install || fail "Failed to install $version"
  ) || fail "fail"
}
nave_test () {
  version="$1"
  version="${version/v/}"
  nave_fetch "$version"
  src="$NAVE_SRC/$version"
  ( cd -- "$src"
    ./configure --debug || fail "failed to ./configure --debug"
    make test-all || fail "Failed tests"
  ) || fail "failed"
}

nave_ls () {
  ( cd -- "$(dirname -- "$SELF_PATH")"
    ls -- {"$(basename "$NAVE_SRC")","$(basename "$NAVE_ROOT")"}
  )
}

nave_has () {
  version="$1"
  version="${version/v/}"
  [ "$version" == "HEAD" ] && return 1
  [ -d "$NAVE_SRC/$version" ] || return 1
}
nave_installed () {
  version="$1"
  version="${version/v/}"
  [ "$version" == "HEAD" ] && return 1
  [ -d "$NAVE_ROOT/$version/bin" ] || return 1
}

nave_use () {
  version="$1"
  version="${version/v/}"
  nave_install "$version"
  bin="$NAVE_ROOT/$version/bin"
  if [ "$version" == "$NAVE" ]; then
    echo "already using $NAVE"
    if [ $# -gt 1 ]; then
      shift
node "$@"
    fi
    return
  fi
  lvl=$[ ${NAVELVL-0} + 1 ]
  echo "using $version"
  if [ $# -gt 1 ]; then
    shift
PATH="$bin:$PATH" NAVELVL=$lvl NAVE="$version" "$SHELL" -c "$(enquote_all node "$@")"
  else
    PATH="$bin:$PATH" NAVELVL=$lvl NAVE="$version" "$SHELL"
  fi
}

nave_clean () {
  version="$1"
  version="${version/v/}"
  remove_dir "$NAVE_SRC/$version"
}
nave_uninstall () {
  version="$1"
  version="${version/v/}"
  remove_dir "$NAVE_ROOT/$version"
}

nave_help () {
  echo "HELP! $@"
}

main "$@"
Something went wrong with that request. Please try again.