1# Simply sets up a few useful variables.
2
3SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4
5function relative() {
6  local full_path="${SCRIPT_DIR}/../${1}"
7
8  if [ -d "${full_path}" ]; then
9    # Try to use readlink as a fallback to readpath for cross-platform compat.
10    if command -v realpath >/dev/null 2>&1; then
11      realpath "${full_path}"
12    elif ! (readlink -f 2>&1 | grep illegal > /dev/null); then
13      readlink -f "${full_path}"
14    else
15      echo "Rocket's scripts require 'realpath' or 'readlink -f' support." >&2
16      echo "Install realpath or GNU readlink via your package manager." >&2
17      echo "Aborting." >&2
18      exit 1
19    fi
20  else
21    # when the directory doesn't exist, fallback to this.
22    echo "${full_path}"
23  fi
24}
25
26function future_date() {
27  local days_in_future=`[[ -z "$1" ]] && echo "0" || echo "$1"`
28  if date -v+1d +%Y-%m-%d > /dev/null 2>&1; then
29    echo $(date -v+${days_in_future}d +%Y-%m-%d)
30  elif date -d "+1 day" > /dev/null 2>&1; then
31    echo $(date '+%Y-%m-%d' -d "+${days_in_future} days")
32  else
33    echo "Error: need a 'date' cmd that accepts -v (BSD) or -d (GNU)"
34    exit 1
35  fi
36}
37
38# Versioning information. These are toggled as versions change.
39CURRENT_RELEASE=false
40PRE_RELEASE=true
41
42# A generated codename for this version. Use the git branch for pre-releases.
43case $PRE_RELEASE in
44  true)
45    VERSION_CODENAME="$(git branch --show-current)"
46    ROCKET_VERSION="${VERSION_CODENAME}-$(future_date)"
47    ;;
48  false)
49    ROCKET_VERSION="0.5.0-dev"
50    VERSION_CODENAME="$(echo "v${ROCKET_VERSION}" | cut -d'.' -f1-2)"
51    ;;
52esac
53
54# Root of workspace-like directories.
55PROJECT_ROOT=$(relative "") || exit $?
56CORE_ROOT=$(relative "core") || exit $?
57CONTRIB_ROOT=$(relative "contrib") || exit $?
58SITE_ROOT=$(relative "site") || exit $?
59
60# Root of project-like directories.
61CORE_LIB_ROOT=$(relative "core/lib") || exit $?
62CORE_CODEGEN_ROOT=$(relative "core/codegen") || exit $?
63CORE_HTTP_ROOT=$(relative "core/http") || exit $?
64CONTRIB_LIB_ROOT=$(relative "contrib/lib") || exit $?
65CONTRIB_CODEGEN_ROOT=$(relative "contrib/codegen") || exit $?
66
67# Root of infrastructure directories.
68EXAMPLES_DIR=$(relative "examples") || exit $?
69DOC_DIR=$(relative "target/doc") || exit $?
70
71ALL_PROJECT_DIRS=(
72    "${CORE_HTTP_ROOT}"
73    "${CORE_CODEGEN_ROOT}"
74    "${CORE_LIB_ROOT}"
75    "${CONTRIB_CODEGEN_ROOT}"
76    "${CONTRIB_LIB_ROOT}"
77)
78
79if [ "${1}" = "-p" ]; then
80  echo "ROCKET_VERSION: ${ROCKET_VERSION}"
81  echo "CURRENT_RELEASE: ${CURRENT_RELEASE}"
82  echo "PRE_RELEASE: ${PRE_RELEASE}"
83  echo "VERSION_CODENAME: ${VERSION_CODENAME}"
84  echo "SCRIPT_DIR: ${SCRIPT_DIR}"
85  echo "PROJECT_ROOT: ${PROJECT_ROOT}"
86  echo "CORE_ROOT: ${CORE_ROOT}"
87  echo "CONTRIB_ROOT: ${CONTRIB_ROOT}"
88  echo "SITE_ROOT: ${SITE_ROOT}"
89  echo "CORE_LIB_ROOT: ${CORE_LIB_ROOT}"
90  echo "CORE_CODEGEN_ROOT: ${CORE_CODEGEN_ROOT}"
91  echo "CORE_HTTP_ROOT: ${CORE_HTTP_ROOT}"
92  echo "CONTRIB_LIB_ROOT: ${CONTRIB_LIB_ROOT}"
93  echo "CONTRIB_CODEGEN_ROOT: ${CONTRIB_CODEGEN_ROOT}"
94  echo "EXAMPLES_DIR: ${EXAMPLES_DIR}"
95  echo "DOC_DIR: ${DOC_DIR}"
96  echo "ALL_PROJECT_DIRS: ${ALL_PROJECT_DIRS[*]}"
97  echo "date(): $(future_date)"
98fi
99