Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cli scripts #4

Merged
merged 9 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/.helper/package
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

set -e

# shellcheck disable=SC1091
# shellcheck source=app/.helper/system
# shellcheck disable=SC1090
source "$ROOT_DIR/app/.helper/system"

LATEST_VERSION="latest"
Expand Down
14 changes: 8 additions & 6 deletions app/.helper/system
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ function kernel() {
# - Normal Linux and Chromebook: linux-x86_64
# - Mac OS: darwin-x86_64
function platform() {
local kern="$(kernel | tr '[:upper:]' '[:lower:]')"
local arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
kern="$(kernel | tr '[:upper:]' '[:lower:]')"
local kern
arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
local arch

echo "${kern}-${arch}"
}

# _readlink wraps readlink in Linux or greadlink in Mac OS
function _readlink() {
if [ $(kernel) == "Linux" ]; then
if [ "$(kernel)" == "Linux" ]; then
readlink "$@"
return $?
elif [ $(kernel) == "Darwin" ]; then
elif [ "$(kernel)" == "Darwin" ]; then
greadlink "$@"
return $?
fi
Expand All @@ -35,10 +37,10 @@ function _readlink() {

# _grep wraps grep in Linux or ggrep in Mac OS
function _grep() {
if [ $(kernel) == "Linux" ]; then
if [ "$(kernel)" == "Linux" ]; then
grep "$@"
return $?
elif [ $(kernel) == "Darwin" ]; then
elif [ "$(kernel)" == "Darwin" ]; then
ggrep "$@"
return $?
fi
Expand Down
12 changes: 7 additions & 5 deletions app/create
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/usr/bin/env bash

__parameters_count="$#"
if [ $# == 0 ]; then
exit 3
fi

APP_DIR=`pwd`
if [[ -d "$APP_DIR/app" && -f "$APP_DIR/app/.bash_cli" ]]; then
APP_DIR=$(pwd)
if [[ -d "$APP_DIR/app" && -f "$APP_DIR/app/.version" ]]; then
APP_DIR="$APP_DIR/app"
fi

if [[ ! -f "$APP_DIR/.bash_cli" ]]; then
if [[ ! -f "$APP_DIR/.version" ]]; then
>&2 echo -e "\033[31mYou are not within a Bash CLI project\033[39m"
>&2 echo "Please change your directory to a valid project or run the init command to set one up."
exit 1
Expand All @@ -18,7 +19,8 @@ fi
CMD_DIR="$APP_DIR"

if [[ $# -gt 1 ]]; then
for i in $(($#-1)); do
__parameters_count=$((__parameters_count-1))
for i in $__parameters_count ; do
DIR=${!i}
CMD_DIR="$CMD_DIR/$DIR"
if [[ ! -d "$CMD_DIR" ]]; then
Expand Down Expand Up @@ -52,4 +54,4 @@ ARGS - The arguments you wish to provide to this command
TODO: Fill out the help information for this command.
EOT

echo -e "\033[32mCommand \033[36m$@\033[32m created successfully\033[39m"
echo -e "\033[32mCommand \033[36m$*\033[32m created successfully\033[39m"
31 changes: 13 additions & 18 deletions app/framework/apply
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ if [ $# == 0 ]; then
exit 3
fi

CUR_DIR=`pwd`
CUR_DIR=$(pwd)
APP_DIR="$CUR_DIR"
PRJ_NAME="${!#}"
SUFFIX="ctl"
AUTOCOMPLETE_DIR="$CUR_DIR/$PRJ_NAME/app/autocomplete"
__bcl_path=$( ls -alh "$HOME"/.local/bin/bcl )

if [[ ! -d "$CUR_DIR/$PRJ_NAME" ]]; then
echo "Project $PRJ_NAME is not found."
Expand All @@ -31,25 +31,19 @@ if [[ ! -d "$CUR_DIR/$PRJ_NAME/playbook" ]]; then
mkdir -p "$CUR_DIR/$PRJ_NAME/playbook"
fi

cp $(ls -alh $HOME/.local/bin/bcl | awk '{print $11}' | sed 's/\/cli/\/framework\//g')* "$CUR_DIR/$PRJ_NAME/framework"
__bcl_framework_path=$( echo "$__bcl_path" | awk '{print $11}' | sed 's/\/cli/\/framework\//g')
cp "$__bcl_framework_path"* "$CUR_DIR/$PRJ_NAME/framework"

if [[ ! -f "$CUR_DIR/$PRJ_NAME/BUILD" ]]; then
cat > "$CUR_DIR/$PRJ_NAME/BUILD" <<EOC
$PRJ_NAME-v0.0.0-release
[email protected]:sample-organization/sample-package.git
EOC
fi

sed -i.bak "s/{{ function_name }}/_bash_cli_$PRJ_NAME$SUFFIX/g" "$CUR_DIR/$PRJ_NAME/framework/complete"
sed -i.bak "s/function_name/_bash_cli_$PRJ_NAME$SUFFIX/g" "$CUR_DIR/$PRJ_NAME/framework/complete"
rm "$CUR_DIR/$PRJ_NAME/framework/complete.bak"

sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/complete"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/complete"
rm "$CUR_DIR/$PRJ_NAME/framework/complete.bak"

sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/help"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/help"
rm "$CUR_DIR/$PRJ_NAME/framework/help.bak"

sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/cli"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/cli"
rm "$CUR_DIR/$PRJ_NAME/framework/cli.bak"

if [[ -d "$CUR_DIR/$PRJ_NAME/app" ]]; then
Expand All @@ -63,17 +57,18 @@ if [[ ! -d "$APP_DIR/autocomplete" ]]; then
mkdir -p "$APP_DIR/autocomplete"
fi

cp $(ls -alh $HOME/.local/bin/bcl | awk '{print $11}' | sed 's/\/cli/\/autocomplete\//g')* "$APP_DIR/autocomplete"
__bcl_autocomplete_path=$( echo "$__bcl_path" | awk '{print $11}' | sed 's/\/cli/\/autocomplete\//g')
cp "$__bcl_autocomplete_path"* "$APP_DIR/autocomplete"

sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$APP_DIR/autocomplete/apply"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$APP_DIR/autocomplete/apply"
rm "$APP_DIR/autocomplete/apply.bak"

sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$APP_DIR/autocomplete/remove"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$APP_DIR/autocomplete/remove"
rm "$APP_DIR/autocomplete/remove.bak"

# Render the *ctl entrypoint
mv "$CUR_DIR/$PRJ_NAME/framework/entrypoint" "$CUR_DIR/$PRJ_NAME$SUFFIX"
sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME$SUFFIX"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME$SUFFIX"
rm "$CUR_DIR/$PRJ_NAME$SUFFIX.bak"

chmod +x "$CUR_DIR/$PRJ_NAME$SUFFIX"
Expand Down
8 changes: 4 additions & 4 deletions app/framework/upgrade
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env bash
echo -e "\033[36mUpgrading BCL framework\033[39m"
BCL_INSTALLATION_DIR=$(dirname $(dirname $(dirname ${BASH_SOURCE[0]})))
BCL_INSTALLATION_DIR=$(dirname "$(dirname "$(dirname "${BASH_SOURCE[0]}")")")

pushd $BCL_INSTALLATION_DIR
pushd "$BCL_INSTALLATION_DIR" || exit
git reset --hard HEAD
git pull https://github.com/cermati/bcl.git master
popd
git pull https://github.com/BrunIF/bcl.git master
popd || exit

echo -e "\033[36mBCL framework is upgraded to latest\033[39m"
57 changes: 31 additions & 26 deletions app/init
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,39 @@ if [ $# == 0 ]; then
exit 3
fi

CUR_DIR=`pwd`
CUR_DIR=$(pwd)
APP_DIR="$CUR_DIR"
PRJ_NAME="${!#}"
SUFFIX="ctl"
AUTOCOMPLETE_DIR="$CUR_DIR/$PRJ_NAME/app/autocomplete"
AUTHOR=`whoami`
__bcl_path=$( ls -alh "$HOME"/.local/bin/bcl )

# get_value_from_user "Question for a user" "default answere"
# User value in variable "$_user_value"
function get_value_from_user () {
_message_to_user="$1"
_default_value="$2"
printf "%s [%s]: " "$_message_to_user" "$_default_value"
read -r _user_value
if [[ -z $_user_value ]]; then
_user_value="$_default_value"
fi
}

mkdir -p "$CUR_DIR/$PRJ_NAME/blueprint"
mkdir -p "$CUR_DIR/$PRJ_NAME/files"
mkdir -p "$CUR_DIR/$PRJ_NAME/framework"
mkdir -p "$CUR_DIR/$PRJ_NAME/playbook"
cp $(ls -alh $HOME/.local/bin/bcl | awk '{print $11}' | sed 's/\/cli/\/framework\//g')* "$CUR_DIR/$PRJ_NAME/framework"
__bcl_framework_path=$( echo "$__bcl_path" | awk '{print $11}' | sed 's/\/cli/\/framework\//g')
cp "$__bcl_framework_path"* "$CUR_DIR/$PRJ_NAME/framework"

cat > "$CUR_DIR/$PRJ_NAME/BUILD" <<EOC
$PRJ_NAME-v0.0.0-release
[email protected]:sample-organization/sample-package.git
EOC

sed -i.bak "s/{{ function_name }}/_bash_cli_$PRJ_NAME$SUFFIX/g" "$CUR_DIR/$PRJ_NAME/framework/complete"
sed -i.bak "s/function_name/_bash_cli_$PRJ_NAME$SUFFIX/g" "$CUR_DIR/$PRJ_NAME/framework/complete"
rm "$CUR_DIR/$PRJ_NAME/framework/complete.bak"

sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/complete"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/complete"
rm "$CUR_DIR/$PRJ_NAME/framework/complete.bak"

sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/help"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/help"
rm "$CUR_DIR/$PRJ_NAME/framework/help.bak"

sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/cli"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME/framework/cli"
rm "$CUR_DIR/$PRJ_NAME/framework/cli.bak"

if [[ -d "$CUR_DIR/$PRJ_NAME/app" ]]; then
Expand All @@ -42,28 +47,28 @@ else
fi

mkdir -p "$APP_DIR/autocomplete"
cp $(ls -alh $HOME/.local/bin/bcl | awk '{print $11}' | sed 's/\/cli/\/autocomplete\//g')* "$APP_DIR/autocomplete"
__bcl_autocomplete_path=$( echo "$__bcl_path" | awk '{print $11}' | sed 's/\/cli/\/autocomplete\//g')
cp "$__bcl_autocomplete_path"* "$APP_DIR/autocomplete"

sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$APP_DIR/autocomplete/apply"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$APP_DIR/autocomplete/apply"
rm "$APP_DIR/autocomplete/apply.bak"

sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$APP_DIR/autocomplete/remove"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$APP_DIR/autocomplete/remove"
rm "$APP_DIR/autocomplete/remove.bak"

cat > "$APP_DIR/.bash_cli" <<EOT
v1
EOT

get_value_from_user "Enter Author name" "$(whoami)"
cat > "$APP_DIR/.author" <<EOT
$AUTHOR
$_user_value
EOT

get_value_from_user "Version of package" "0.1"
cat > "$APP_DIR/.version" <<EOT
0.1
$_user_value
EOT

get_value_from_user "Short information about a BCL package" "BCL package. Short info"
cat > "$APP_DIR/.help" <<EOT
LOREM IPSUM DOLOR SIT AMET
$_user_value
EOT

cat > "$HOME/.bash_completion.d/$PRJ_NAME$SUFFIX" <<EOC
Expand All @@ -79,7 +84,7 @@ fi

# Render the *ctl entrypoint
mv "$CUR_DIR/$PRJ_NAME/framework/entrypoint" "$CUR_DIR/$PRJ_NAME$SUFFIX"
sed -i.bak "s/{{ project_name }}/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME$SUFFIX"
sed -i.bak "s/__project_name__/$PRJ_NAME/g" "$CUR_DIR/$PRJ_NAME$SUFFIX"
rm "$CUR_DIR/$PRJ_NAME$SUFFIX.bak"

chmod +x "$CUR_DIR/$PRJ_NAME$SUFFIX"
4 changes: 3 additions & 1 deletion app/package/cache-clear
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env bash

# shellcheck disable=SC1090

set -e

source $ROOT_DIR/app/.helper/package
source "$ROOT_DIR/app/.helper/package"

clear_package_cache
14 changes: 8 additions & 6 deletions app/package/global-install
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env bash

# shellcheck disable=SC1090

set -e

source $ROOT_DIR/app/.helper/package
source $ROOT_DIR/app/.helper/system
source "$ROOT_DIR/app/.helper/package"
source "$ROOT_DIR/app/.helper/system"

INSTALL_DIRECTORY="$HOME/.bcl"

Expand All @@ -24,10 +26,10 @@ parse_args() {
main() {
parse_args "$@"

package_name=$(get_package_name $PACKAGE)
package_version=$(get_package_version $PACKAGE)
package_channel=$(get_package_channel $PACKAGE)
package_platform=$(get_package_platform $PACKAGE)
package_name=$(get_package_name "$PACKAGE")
package_version=$(get_package_version "$PACKAGE")
package_channel=$(get_package_channel "$PACKAGE")
package_platform=$(get_package_platform "$PACKAGE")

# Fetch appropriate version of the package from git
echo "Fetching available versions of $package_name..."
Expand Down
24 changes: 12 additions & 12 deletions app/package/publish
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fi
PACKAGE_NAME="$1"
TARGET_PLATFORM="$2"

APP_DIR=`pwd`
APP_DIR=$(pwd)
PACKAGE_RELEASE_TAG=''
RELEASE_REPOSITORY=''
EXECUTABLE_SUFFIX='ctl'
Expand All @@ -21,13 +21,13 @@ if [[ ! -d "$APP_DIR/$PACKAGE_NAME" ]]; then
fi

if [[ -f "$APP_DIR/$PACKAGE_NAME/BUILD" ]]; then
PACKAGE_RELEASE_TAG=`head -n 1 $APP_DIR/$PACKAGE_NAME/BUILD`
PACKAGE_RELEASE_TAG=$(head -n 1 "$APP_DIR/$PACKAGE_NAME/BUILD")

if [[ ! -z "$TARGET_PLATFORM" ]]; then
if [[ -n "$TARGET_PLATFORM" ]]; then
PACKAGE_RELEASE_TAG="$PACKAGE_RELEASE_TAG-$TARGET_PLATFORM"
fi

RELEASE_REPOSITORY=`tail -n 1 $APP_DIR/$PACKAGE_NAME/BUILD`
RELEASE_REPOSITORY=$(tail -n 1 "$APP_DIR/$PACKAGE_NAME/BUILD")
fi

if [[ -d "$TMP_DIR" ]]; then
Expand All @@ -38,14 +38,14 @@ fi

echo "Publishing $PACKAGE_RELEASE_TAG..."

git clone $RELEASE_REPOSITORY --depth 1 $TMP_DIR
cd $TMP_DIR
git clone "$RELEASE_REPOSITORY" --depth 1 "$TMP_DIR"
cd "$TMP_DIR"
git update-ref -d HEAD
git checkout -b $PACKAGE_RELEASE_TAG
rm -rf $TMP_DIR/*
cp $APP_DIR/$PACKAGE_NAME$EXECUTABLE_SUFFIX $TMP_DIR
cp -R $APP_DIR/$PACKAGE_NAME $TMP_DIR
git checkout -b "$PACKAGE_RELEASE_TAG"
rm -rf "${TMP_DIR:?}/"*
cp "$APP_DIR/$PACKAGE_NAME$EXECUTABLE_SUFFIX" "$TMP_DIR"
cp -R "$APP_DIR/$PACKAGE_NAME" "$TMP_DIR"
git add -A
git commit -m "Publishing $PACKAGE_RELEASE_TAG"
git push $RELEASE_REPOSITORY $PACKAGE_RELEASE_TAG
rm -rf $TMP_DIR
git push "$RELEASE_REPOSITORY" "$PACKAGE_RELEASE_TAG"
rm -rf "$TMP_DIR"
Loading