Skip to content

Commit

Permalink
Refine install.sh
Browse files Browse the repository at this point in the history
- can work without sourcing (problematic in zsh)
- installs bash completion
- do not create directories (duplicate with the main)
- can specify installing version
  • Loading branch information
umireon authored and DhavalKapil committed Jun 13, 2017
1 parent 99be897 commit 5912b9c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 109 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Expand Up @@ -15,12 +15,13 @@ cache:
before_install:
- wget --version
- curl --version
- export __luaver_env="testing"
- if [[ -n $SHELLCHECK ]]; then cabal update && cabal install transformers-0.4.3.0 ShellCheck && shellcheck --version; fi

install:
- if [[ $SHELL == bash ]]; then git clone --depth 1 https://github.com/sstephenson/bats.git; fi
- . ./install.sh
- touch ~/.bashrc
- ./install.sh
- . ~/.bashrc

script:
- if [[ -n $SHELLCHECK ]]; then shellcheck -s bash luaver && shellcheck -s dash luaver && shellcheck -s ksh luaver && shellcheck -s sh -e SC2039 luaver; fi
Expand Down
9 changes: 5 additions & 4 deletions README.md
Expand Up @@ -36,10 +36,10 @@ sudo apt-get install lib32ncurses5-dev
You can install from the script directly:

```sh
curl https://raw.githubusercontent.com/dhavalkapil/luaver/v1.0.0/install.sh -o install.sh && . ./install.sh
curl -fsSL https://raw.githubusercontent.com/dhavalkapil/luaver/master/install.sh | sh -- -r v1.0.0
```

_Note: It will overwrite any `install.sh` in your current directory. Also, this file is no longer needed after the installation is complete._
Follow the instructions which appears after the luaver installation.

### Install using `git`

Expand All @@ -49,10 +49,11 @@ _Note: It will overwrite any `install.sh` in your current directory. Also, this
$ git clone https://github.com/DhavalKapil/luaver.git ~/.luaver
```

2. Add `. ~/.luaver/luaver` to your `.bashrc` or equivalent:
2. Add `. ~/.luaver/luaver` to your profile such as `.bashrc` or `.zshrc`:

```sh
$ echo ". ~/.luaver/luaver" >> ~/.bashrc
$ echo "[ -s ~/.luaver/luaver ] && . ~/.luaver/luaver" >> ~/.bashrc
$ echo "[ -s ~/.luaver/completions/luaver.bash ] && . ~/.luaver/completions/luaver.bash"" >> ~/.bashrc
```
3. Reload `.bashrc` or restart the shell to load `luaver`:
Expand Down
174 changes: 71 additions & 103 deletions install.sh 100644 → 100755
@@ -1,120 +1,88 @@
PROGRAM="luaver"
SRC_URL="https://raw.githubusercontent.com/dhavalkapil/luaver/v1.0.0/${PROGRAM}"
#!/bin/sh

# Directories to be used
LUAVER_DIR="${HOME}/.luaver" # The luaver directory
SRC_DIR="${LUAVER_DIR}/src" # Where source code is downloaded
LUA_DIR="${LUAVER_DIR}/lua" # Where lua source is built
LUAJIT_DIR="${LUAVER_DIR}/luajit" # Where luajit source is built
LUAROCKS_DIR="${LUAVER_DIR}/luarocks" # Where luarocks source is built
{ # ensure the whole script is loaded

# Present directory
present_dir=$(pwd)
set -eu

# Printing bold text - TODO
print()
print_bold()
{
tput bold
printf "==> %s\n" "${1}"
tput sgr0
tput bold; echo "${1}"; tput sgr0
}

# Initializes directories
init()
download_file()
{
print "Setting up directory structure..."

if [ ! -e "${LUAVER_DIR}" ]
then
mkdir "${LUAVER_DIR}"
fi
if [ ! -e "${SRC_DIR}" ]
then
mkdir "${SRC_DIR}"
fi
if [ ! -e "${LUA_DIR}" ]
then
mkdir "${LUA_DIR}"
fi
if [ ! -e "${LUAJIT_DIR}" ]
then
mkdir "${LUAJIT_DIR}"
fi
if [ ! -e "${LUAROCKS_DIR}" ]
then
mkdir "${LUAROCKS_DIR}"
if 'curl' -V >/dev/null 2>&1
then 'curl' -fsSL "${1}"
else 'wget' -qO- "${1}"
fi
print "Directory structure built..."
}

# Downloads luaver
install()
# install_file baseurl file
install_file()
{
print "Downloading '${PROGRAM}'..."
cd "${LUAVER_DIR}" || exit
if [ -e "${PROGRAM}" ]
if [ -e "${2}" ]
then
print "Existing '${PROGRAM}' detected. Removing it..."
rm "${PROGRAM}"
print "Downloading fresh '${PROGRAM}'"
fi
# This variable is initialized in travis.yml
# shellcheck disable=SC2154
if [ "${__luaver_env}" = "testing" ]
then
cd "${present_dir}" || exit
cp "./${PROGRAM}" "${LUAVER_DIR}/"
cd "${LUAVER_DIR}" || exit
cp "${2}" "${LUAVER_DIR}/${2}"
else
wget "${SRC_URL}"
download_file "${1}/${2}" >"${LUAVER_DIR}/${2}"
fi
chmod 775 "${PROGRAM}"
}

# Inserts path variables inside bash rc
set_up_path()
{
local str="[ -s ${LUAVER_DIR}/${PROGRAM} ] && . ${LUAVER_DIR}/${PROGRAM}"
local shell_type
shell_type=$(basename "${SHELL}")
print "Detected SHELL_TYPE: ${shell_type}"

local profile=""
if [ "${shell_type}" = "bash" ]
then
if [ -f "$HOME/.bashrc" ]
then
profile="$HOME/.bashrc"
fi
elif [ "${shell_type}" = "zsh" ]
then
if [ -f "$HOME/.zshrc" ]
then
profile="$HOME/.zshrc"
fi
fi

if [ "${profile}" = "" ]
then
print "Unable to detect profile(no ~/.bashrc, ~/.zshrc found)"
print "Add the following at the end of the correct file yourself:"
print "${str}"
print "You can start using it in a new terminal or run 'source ~/.bashrc' in present terminal"
else
if ! command grep -qc "${str}" "${profile}"
then
print "Appending '${str}' at the end of ${profile}"
printf "\n%s\n" "${str}" >> "${profile}"
fi
# shellcheck source=/dev/null
source "${profile}"
fi
# shellcheck disable=SC2164
cd "${present_dir}"
}
## Option parsing
LUAVER_DIR=~/.luaver
REVISION=v1.0.0
SHELL_TYPE="$(basename /"${SHELL}")"

while getopts hr:s: OPT
do
case "$OPT" in
r ) REVISION="${OPTARG}" ;;
h )
echo "Usage: ${0} [-r REVISION] [-s SHELL]"
echo " -r luaver reversion [${REVISION}]"
;;
esac
done

print_bold "Installing luaver..."

## Download script
URL="https://raw.githubusercontent.com/DhavalKapil/luaver/${REVISION}"

mkdir -p "${LUAVER_DIR}/completions"

install_file "${URL}" "luaver"
chmod a+x "${LUAVER_DIR}/luaver"

install_file "${URL}" "completions/luaver.bash" || rm "${LUAVER_DIR}/completions/luaver.bash"

print "Installing '${PROGRAM}'..."
init
install
set_up_path
print "Successfully installed '${PROGRAM}'..."
## Set up profile
APPEND_COMMON="[ -s ~/.luaver/luaver ] && . ~/.luaver/luaver"

APPEND_BASH="${APPEND_COMMON}
[ -s ~/.luaver/completions/luaver.bash ] && . ~/.luaver/completions/luaver.bash"

APPEND_ZSH="${APPEND_COMMON}"

case "${SHELL_TYPE}" in
bash ) APPEND="${APPEND_BASH}" ;;
zsh ) APPEND="${APPEND_ZSH}" ;;
* ) APPEND="${APPEND_COMMON}"
esac

if [ -f ~/."${SHELL_TYPE}"rc ]
then
'grep' -qF "${APPEND}" ~/."${SHELL_TYPE}"rc || printf "\n%s\n\n" "${APPEND}" >>~/."${SHELL_TYPE}"rc

print_bold "Appending the following lines at the end of ~/.${SHELL_TYPE}rc if lines not exists:"
printf "\n%s\n\n" "${APPEND}"
print_bold "To use luaver, you must restart the shell or execute '. ~/.${SHELL_TYPE}rc'"
else
print_bold "Add the following lines at the end of your profile (~/.bashrc, ~/.zshrc, etc):"
printf "\n%s\n\n" "${APPEND}"
print_bold "To use luaver, you must restart the shell or execute the above lines"
fi

print_bold "luaver was successfully installed!"

}

0 comments on commit 5912b9c

Please sign in to comment.