Skip to content

Commit

Permalink
Merge pull request #392 from testmycode/script-changes
Browse files Browse the repository at this point in the history
Script changes
  • Loading branch information
Salmela authored Sep 4, 2016
2 parents 45ab76e + cd2b080 commit 34261e1
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 97 deletions.
6 changes: 4 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ install:
- cmd: SET PATH=C:\maven\apache-maven-3.2.5\bin;%JAVA_HOME%\bin;%PATH%
- cmd: SET MAVEN_OPTS=-XX:MaxPermSize=2g -Xmx4g
- cmd: SET JAVA_OPTS=-XX:MaxPermSize=2g -Xmx4g
build_script:
- mvn clean install --batch-mode
build: off
test_script:
- mvn -Dsurefire.rerunFailingTestsCount=3 test
- mvn checkstyle:check
cache:
- C:\maven\
- C:\Users\appveyor\.m2
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fi.helsinki.cs.tmc.cli</groupId>
<artifactId>tmc-cli</artifactId>
<version>0.7.6</version>
<version>0.8.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -85,6 +85,11 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scripts/autocompletion.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# SCRIPT_PATH will be filled by stub script
alias tmc="$SCRIPT_PATH/tmc"
alias tmc="$SCRIPT_PATH"

TMC_COMMANDS=( "\$(tmc -d shell-helper -c)" )

Expand Down
46 changes: 18 additions & 28 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,44 +1,34 @@
#!/bin/bash

RCFILE=".bashrc"
RCPATH="$HOME/$RCFILE"
set -euo pipefail

echo "~ Installing TMC-CLI ~"
echo "This install script assumes you are using Bash."
echo "If you are using another shell, please add 'source $HOME/.tmc-autocomplete.sh' in your shell's rc file after TMC-CLI has been installed."
echo ""

if [ ! -f $RCPATH ]; then
echo -e "$RCPATH not found, creating"
echo ""
touch $RCPATH
fi

if [ -f $HOME/.tmc-autocomplete.sh ]; then
# If .tmc-autocomplete.sh is already in user's home dir, don't download it
if ! ((grep -Fxq "source $HOME/.tmc-autocomplete.sh" $RCPATH) \
|| (grep -Fxq "source \$HOME/.tmc-autocomplete.sh" $RCPATH) \
|| (grep -Fxq "source ~/.tmc-autocomplete.sh" $RCPATH)); then
echo -e ".tmc-autocomplete already exists in $HOME, but is not sourced in $RCFILE"
echo -e "Adding to $RCPATH"
echo "" >> $RCPATH
echo "source $HOME/.tmc-autocomplete.sh" >> $RCPATH
exit
else
echo "TMC-CLI is already installed. Did you forget to source your rc file?"
echo -e "Try 'source $RCPATH'."
exit
fi
echo -e ".tmc-autocomplete is already installed try adding the following line to your bashrc file."
echo "source $HOME/.tmc-autocomplete.sh"
exit
fi

echo "Fetching latest release URL"
URL=$(curl -s https://api.github.com/repos/testmycode/tmc-cli/releases/latest | grep '"browser_download_url"' | grep '/tmc"' | head -n 1 | cut -d '"' -f 4)
echo "Downloading TMC-CLI"
curl -LO $URL > ./tmc
if ! PAGE=$(curl -s https://api.github.com/repos/testmycode/tmc-cli/releases/latest); then
echo "Failed to fetch latest release from github api." >&2
fi
URL=$(echo "$PAGE" | grep '"browser_download_url"' | grep '/tmc"' | head -n 1 | cut -d '"' -f 4)

echo "Downloading TMC-CLI from following address"
echo "$URL"
echo

curl -LO "$URL" > ./tmc || true

if [ ! -f ./tmc ]; then
echo "Error downloading TMC-CLI"
exit 1
fi

chmod u+x ./tmc
./tmc > /dev/null

echo "Installation complete"
exit
210 changes: 145 additions & 65 deletions scripts/stub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,83 @@

set -euo pipefail

## Embeded binary magic
if [[ ${TMC_DEBUG-} == 1 ]]; then
tmc_debug() {
(>&2 echo "$*")
}
else
tmc_debug() {
:
}
fi

MYSELF=$(which "$0" 2>/dev/null)
[ $? -gt 0 ] && [ -f "$0" ] && MYSELF="./$0"
tmc_is_native() {
# get the script directory
local DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

## Find the java binary and correct version
TMC_NATIVE_PACKAGE=0

JAVA_BIN=java
JAVA_HOME=${JAVA_HOME-}
if [ -n "$JAVA_HOME" ]; then
JAVA_BIN="$JAVA_HOME/bin/java"
fi
# check if the tmc binary is in read-only directory
if [[ ! -w $DIR ]]; then
TMC_NATIVE_PACKAGE=1
fi

if ! hash "$JAVA_BIN" 2>/dev/null; then
echo "Java not installed. If you have installed it in another directory then set the \$JAVA_HOME variable."
exit 1
fi
if [[ "$TMC_NATIVE_PACKAGE" == "1" ]]; then
tmc_debug "Tmc is installed through package manager"
fi

JAVA_VERSION=$("$JAVA_BIN" -version 2>&1 | awk -F '"' '/version/ {print $2}')
if [ "$JAVA_VERSION" \< "1.7" ]; then
echo "You must have at least Java 1.7 installed."
exit 1
fi
echo "$TMC_NATIVE_PACKAGE"
}

## find the place for running the autocomplete/alias file
TMC_NATIVE_PACKAGE=$(tmc_is_native)

tmc_detect_profile() {
local PROFILE_ENV
local SHELL_ENV
local HOME_ENV
tmc_get_binary() {
tmc_debug "Embeded binary magic"

local MYSELF=$(which "$0" 2>/dev/null)
[ $? -gt 0 ] && [ -f "$0" ] && MYSELF="./$0"

echo "$MYSELF"
}

tmc_find_java_binary() {
tmc_debug "Find the java binary and correct version"

local JAVA_BIN=java
local JAVA_HOME=${JAVA_HOME-}

if [ -n "$JAVA_HOME" ]; then
JAVA_BIN="$JAVA_HOME/bin/java"
fi

if ! hash "$JAVA_BIN" 2>/dev/null; then
echo "Java not installed. If you have installed it" >&2
echo "in another directory then set the \$JAVA_HOME" >&2
echo "variable." >&2
exit 1
fi

local JAVA_VERSION=$("$JAVA_BIN" -version 2>&1 | awk -F '"' '/version/ {print $2}')

if [ "$JAVA_VERSION" \< "1.7" ]; then
echo "You must have at least Java 1.7 installed." >&2
exit 1
fi

echo $JAVA_BIN
}
JAVA_BIN=$(tmc_find_java_binary)

#####
tmc_debug "Find the place for running the autocomplete/alias file"

PROFILE_ENV=${PROFILE-}
SHELL_ENV=${SHELL-}
HOME_ENV=${HOME-}
tmc_detect_profile() {
local PROFILE_ENV=${PROFILE-}
local SHELL_ENV=${SHELL-}
local HOME_ENV=${HOME-}

if [ -n "$HOME_ENV" ] && [ -f "$PROFILE_ENV" ]; then
(>&2 echo "Home environment variable is not set")
echo "Home environment variable is not set" >&2
return
fi

Expand All @@ -47,10 +87,8 @@ tmc_detect_profile() {
return
fi

local DETECTED_PROFILE
DETECTED_PROFILE=''
local SHELLTYPE
SHELLTYPE="$(basename "/$SHELL_ENV")"
local DETECTED_PROFILE=''
local SHELLTYPE="$(basename "/$SHELL_ENV")"

if [ "$SHELLTYPE" = "bash" ]; then
if [ -f "$HOME_ENV/.bashrc" ]; then
Expand Down Expand Up @@ -81,60 +119,69 @@ tmc_detect_profile() {

## Bash autocompletion script extraction

# This is used in autocompletion file
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
tmc_binary_file() {
echo "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
}

tmc_autocomplete_file() {
echo "${TMC_AUTOCOMPLETE_FILE-$HOME/.tmc-autocomplete.sh}"
}

## Create the alias and autocompletion code if tmc alias not set
tmc_update_autocomplete() {
local AUTOCOMPLETE_FILE
local PROFILE_FILE
local AUTOCOMPLETE_FILE="$(tmc_autocomplete_file)"
local INSTALLED=0

AUTOCOMPLETE_FILE="$(tmc_autocomplete_file)"
# This variable is defined for the embeded autocompletion file
SCRIPT_PATH="$(tmc_binary_file)"
if [[ -e "$AUTOCOMPLETE_FILE" ]]; then
INSTALLED=1
fi

cat > "$AUTOCOMPLETE_FILE" <<- EOM
#EMBED_AUTOCOMPLETE_SH
EOM
chmod +x "$AUTOCOMPLETE_FILE"

PROFILE_FILE=$(tmc_detect_profile)

# get the aliases
#TODO remove this and use global variables
set +euo pipefail
source $PROFILE_FILE
set -euo pipefail

if type tmc &> /dev/null; then
exit
if [[ $INSTALLED == 0 ]]; then
tmc_install_hook "$AUTOCOMPLETE_FILE"
fi
}

tmc_install_hook() {
local AUTOCOMPLETE_FILE="$1"
local PROFILE_FILE=$(tmc_detect_profile)

if [ -z "$PROFILE_FILE" ]; then
echo "Profile file not found" >&2
echo "Put the \"source $AUTOCOMPLETE_FILE\" line in somewhere where" >&2
echo "it's run at terminal initialization." >&2
echo "Put the \"source $AUTOCOMPLETE_FILE\" line in" >&2
echo "your shell's rc file." >&2
fi
echo "source $AUTOCOMPLETE_FILE" >> "$PROFILE_FILE"
# The `|| true` structure is used just in case that the rc file
# has strict mode and user has manually removed tmc script
echo "source $AUTOCOMPLETE_FILE || true" >> "$PROFILE_FILE"

echo "To use new autocompletion run the following on command line:" >&2
echo ". ~/.bashrc" >&2
echo "To use new autocompletion run the following on command line:" >&1
echo ". $PROFILE_FILE" >&1
}

## Auto update code

##### If you MODIFY the install script then do the following:
##### Enable the "THE INSTALL SCRIPT DEBUGGING LINE" at [Tmc]CliUpdater.java
##### (It runs the dev script instead of the latest release script)
##### Enable the "THE INSTALL SCRIPT DEBUGGING LINE" at AutoUpdater.java
##### (It runs your script instead of the script from latest github release)
##### And use the --force-update flag in application.

tmc_update() {
tmc_update_autocomplete
}

tmc_install_update() {
if [[ $TMC_NATIVE_PACKAGE == 1 ]]; then
echo "Tmc should be updated by your package manager" >&2
exit 126
fi

echo "Please report any error messages that may come up below." >&2
if [ ! -f tmc.new ]; then
echo "Could not find the updated file." >&2
Expand All @@ -155,9 +202,9 @@ tmc_install_update() {
rm tmc.orig &> /dev/null
echo "Running the new tmc update script..." >&2
echo "" >&2

tmc_update

echo ""
if [ -f tmc ]; then
echo "Tmc cli installation was successful" >&2
else
Expand All @@ -167,18 +214,51 @@ tmc_install_update() {
exit
}

if [ "${1-}" == "++internal-update" ]; then
tmc_install_update
fi
tmc_uninstall() {
local AUTOCOMPLETE_FILE="$(tmc_autocomplete_file)"
local PROFILE_FILE=$(tmc_detect_profile)
local TMC_FILE="$(tmc_binary_file)"

if [ ! -f "$(tmc_autocomplete_file)" ]; then
tmc_update_autocomplete
exit
fi
#remove the include line from rc file

#EMBED_UNIT_TESTS_SH
grep -v "source $AUTOCOMPLETE_FILE || true" "$PROFILE_FILE" > "${PROFILE_FILE}2"
if [[ "$?" == 0 ]]; then
mv "${PROFILE_FILE}2" "$PROFILE_FILE"
fi

export COLUMNS=$(tput cols)
exec "$JAVA_BIN" -jar "$MYSELF" "$@"
rm "$AUTOCOMPLETE_FILE"
rm "$TMC_FILE"
}

tmc_main() {
if [ "${1-}" == "++internal-update" ]; then
tmc_install_update
fi

if [ "${1-}" == "--uninstall" ]; then
tmc_uninstall
exit
fi

# check if this is first time running the tmc
if [ ! -e "$(tmc_autocomplete_file)" ]; then
tmc_update_autocomplete
exit
fi

local TMC_FLAGS=

# disable auto updates if tmc is from native package
if [[ $TMC_NATIVE_PACKAGE == 1 ]]; then
TMC_FLAGS="-d"
fi

#EMBED_UNIT_TESTS_SH

export COLUMNS=$(tput cols)
exec "$JAVA_BIN" -jar "$(tmc_get_binary)" $TMC_FLAGS "$@"

exit 0
}

exit 0
tmc_main $*
Loading

0 comments on commit 34261e1

Please sign in to comment.