Skip to content

Commit 5c8aa2d

Browse files
authored
Add unix & windows install scripts for smithy-cli (#1657)
1 parent 8c2b05a commit 5c8aa2d

File tree

5 files changed

+396
-0
lines changed

5 files changed

+396
-0
lines changed

smithy-cli/build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ runtime {
187187
}
188188
}
189189

190+
// Add finishing touches to the distributables, such as an install script
191+
tasks.runtime.doLast {
192+
targetPlatforms.each { targetPlatform ->
193+
copy {
194+
from "config"
195+
include targetPlatform.value.name.contains("windows") ? "install.bat" : "install"
196+
into Paths.get(
197+
"${project.buildDir}", "image", "smithy-cli-${targetPlatform.value.name}").toString()
198+
}
199+
}
200+
}
201+
190202
tasks.register("optimize", Exec) {
191203
commandLine("$smithyBinary", "warmup")
192204
}

smithy-cli/config/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Overview
2+
This document describes how to test and lint the ``install`` scripts used
3+
to install the Smithy CLI distributables.
4+
5+
These installers get packaged with the respective distributable (`install` for linux/mac, `install.bat` for windows).
6+
7+
## Shell installer (`install`)
8+
9+
### Running the tests
10+
The test suite utilizes [`bats-core`](https://github.com/bats-core/bats-core) to run
11+
the tests. To run the tests, first make sure you first install `bats-core`:
12+
13+
$ brew install bats-core
14+
15+
16+
And then run bats-core from within this `config` directory:
17+
18+
$ bats .
19+
20+
21+
### Linting
22+
To help catch potential issues in the shell scripts, you should lint the scripts.
23+
To lint the shell scripts, use [`shellcheck`](https://github.com/koalaman/shellcheck).
24+
It can be installed with `brew`:
25+
26+
$ brew install shellcheck
27+
28+
29+
Then can be ran on both the ``install`` shell script and the ``install.bats``
30+
test file::
31+
32+
$ shellcheck install install.bats
33+
34+
35+
## Batch installer (`install.bat`)
36+
Unfortunately, it's not exactly straightforward to test batch (`.bat`) files on linux or mac.
37+
38+
If you do find yourself on a windows machine, you can bootstrap an test installation environment
39+
by creating a folder, which contains `install.bat` and a dummy executable, `bin/smithy`.
40+
41+
Running the installer will perform the installation as if you were installing the real
42+
distributable (which is exactly what `bats` does above).

smithy-cli/config/install

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/bin/sh
2+
3+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
usage() {
7+
cat 1>&2 <<EOF
8+
Installs the Smithy CLI
9+
10+
USAGE:
11+
install [FLAGS] [OPTIONS]
12+
13+
FLAGS:
14+
-u, --update Updates the Smithy CLI if a different version
15+
is previously installed. By default, this script
16+
will not update the Smithy CLI if a previous
17+
installation is detected.
18+
19+
-h, --help Prints help information
20+
21+
OPTIONS:
22+
-i, --install-dir <path> The directory to install the Smithy CLI. By
23+
default, this directory is: /usr/local/smithy
24+
25+
-b, --bin-dir <path> The directory to store symlinks to executables
26+
for the Smithy CLI. By default, the directory
27+
used is: /usr/local/bin
28+
EOF
29+
}
30+
31+
parse_commandline() {
32+
while test $# -gt 0
33+
do
34+
key="$1"
35+
case "$key" in
36+
-i|--install-dir)
37+
PARSED_INSTALL_DIR="$2"
38+
shift
39+
;;
40+
-b|--bin-dir)
41+
PARSED_BIN_DIR="$2"
42+
shift
43+
;;
44+
-u|--update)
45+
PARSED_UPGRADE="yes"
46+
;;
47+
-h|--help)
48+
usage
49+
exit 0
50+
;;
51+
*)
52+
quit "Got an unexpected argument: $1"
53+
;;
54+
esac
55+
shift
56+
done
57+
}
58+
59+
set_global_vars() {
60+
ROOT_INSTALL_DIR=${PARSED_INSTALL_DIR:-/usr/local/smithy}
61+
BIN_DIR=${PARSED_BIN_DIR:-/usr/local/bin}
62+
UPGRADE=${PARSED_UPGRADE:-no}
63+
64+
EXE_NAME="smithy"
65+
INSTALLER_DIR="$( cd "$( dirname "$0" )" >/dev/null 2>&1 && pwd )"
66+
INSTALLER_EXE="$INSTALLER_DIR/bin/$EXE_NAME"
67+
SMITHY_VERSION=$($INSTALLER_EXE --version)
68+
69+
INSTALL_DIR="$ROOT_INSTALL_DIR/$SMITHY_VERSION"
70+
# INSTALL_DIR="$INSTALL_DIR"
71+
72+
CURRENT_INSTALL_DIR="$ROOT_INSTALL_DIR/current"
73+
CURRENT_SMITHY_EXE="$CURRENT_INSTALL_DIR/bin/$EXE_NAME"
74+
75+
BIN_SMITHY_EXE="$BIN_DIR/$EXE_NAME"
76+
}
77+
78+
create_install_dir() {
79+
echo "Installing Smithy CLI to '$INSTALL_DIR'..."
80+
mkdir -p "$INSTALL_DIR" || exit 1
81+
{
82+
cp -R "$INSTALLER_DIR/" "$INSTALL_DIR" &&
83+
ln -snf "$INSTALL_DIR" "$CURRENT_INSTALL_DIR"
84+
} || {
85+
rm -rf "$INSTALL_DIR"
86+
exit 1
87+
}
88+
}
89+
90+
check_preexisting_install() {
91+
echo "Checking for existing installations of Smithy CLI..."
92+
if [ -L "$CURRENT_INSTALL_DIR" ] && [ "$UPGRADE" = "no" ]
93+
then
94+
quit "Found preexisting Smithy CLI installation: $CURRENT_INSTALL_DIR. Please rerun install script with --update flag."
95+
fi
96+
if [ -d "$INSTALL_DIR" ]
97+
then
98+
echo "Found same Smithy CLI version: $INSTALL_DIR. Skipping install."
99+
exit 0
100+
fi
101+
}
102+
103+
create_bin_symlinks() {
104+
echo "Setting up links..."
105+
mkdir -p "$BIN_DIR"
106+
ln -sf "$CURRENT_SMITHY_EXE" "$BIN_SMITHY_EXE"
107+
}
108+
109+
quit() {
110+
err_msg="$1"
111+
echo "$err_msg" >&2
112+
exit 1
113+
}
114+
115+
main() {
116+
parse_commandline "$@"
117+
set_global_vars
118+
check_preexisting_install
119+
create_install_dir
120+
create_bin_symlinks
121+
$BIN_SMITHY_EXE warmup
122+
echo "You can now run: '$BIN_SMITHY_EXE --version' or '$EXE_NAME --version'"
123+
case :$PATH: in
124+
*:$BIN_DIR:*)
125+
;;
126+
*)
127+
echo "$BIN_DIR not detected in \$PATH, you will be unable to use '$EXE_NAME --version'"
128+
;;
129+
esac
130+
exit 0
131+
}
132+
133+
main "$@"

smithy-cli/config/install.bat

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@echo off
2+
3+
:: Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
:: SPDX-License-Identifier: Apache-2.0
5+
6+
set exe_name=smithy
7+
set installer_path=%~dp0
8+
set installer_drive=%~d0
9+
set installer_exe=%installer_path%bin\%exe_name%
10+
set install_path=%installer_drive%\Program Files\Smithy
11+
12+
13+
:: Set the installation path
14+
set choice_path=%install_path%
15+
set /p choice_path=Install path [%install_path%]:
16+
echo Installing Smithy to %choice_path%...
17+
18+
19+
echo Checking for existing installation...
20+
if exist "%choice_path%" goto upgrade
21+
22+
23+
:start
24+
:: Create a wrapper bat
25+
(
26+
echo @echo off
27+
echo "%choice_path%\bin\smithy" %%*
28+
) > "%installer_path%\%exe_name%.bat"
29+
30+
:: Copy the installation
31+
xcopy /i /h /e "%installer_path%\*" "%choice_path%\"
32+
setx path "%path%;%choice_path%\;"
33+
call %exe_name% warmup
34+
echo You may now run '%exe_name% --version'
35+
goto done
36+
37+
38+
:: Check if upgrade is desired
39+
:upgrade
40+
echo Existing Smithy installation found...
41+
set choice_upgrade=''
42+
set /p choice_upgrade=Upgrade Smithy? [y/n]:
43+
if '%choice_upgrade%'=='y' goto yes
44+
if '%choice_upgrade%'=='n' goto no
45+
echo "%choice_upgrade%" is not valid
46+
goto upgrade
47+
48+
49+
:no
50+
echo Not upgrading Smithy...
51+
goto done
52+
53+
:yes
54+
echo Upgrading Smithy...
55+
rmdir "%choice_path%" /s /q
56+
goto start
57+
58+
:done
59+
pause
60+
exit

0 commit comments

Comments
 (0)