From bff53522985f78df8243efa80953a0b78740a9d3 Mon Sep 17 00:00:00 2001 From: James O'Beirne Date: Mon, 15 Feb 2016 13:23:26 -0800 Subject: [PATCH 1/2] Implement Deskfile recognition --- Dockerfile | 6 +++++- README.md | 30 ++++++++++++++++++++++++------ desk | 34 +++++++++++++++++++++++++++++++--- test/run_tests.sh | 30 ++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 442f70e..d75f327 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,10 @@ ADD test/run_tests.sh run_tests.sh ADD test/run_tests.fish run_tests.fish ADD examples examples RUN mkdir -p .config/fish && touch .config/fish/config.fish -RUN chown -R $USERNAME:$USERNAME .zshrc examples run_tests.fish run_tests.sh .bashrc .config + +# Set up test Deskfile +RUN mkdir -p example-project && cp examples/python_project.sh example-project/Deskfile + +RUN chown -R $USERNAME:$USERNAME .zshrc example-project examples run_tests.fish run_tests.sh .bashrc .config USER $USERNAME diff --git a/README.md b/README.md index d3f4a07..cd5f9d9 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ There are no dependencies other than `bash`. Desk is explicitly tested with `bas `zsh`, and `fish`. ```sh -◲ desk 0.4.1 +◲ desk 0.5.0 Usage: @@ -44,8 +44,10 @@ Usage: Initialize desk configuration. desk (list|ls) List all desks along with a description. - desk (.|go) [shell-args...] - Activate a desk. Extra arguments are passed onto shell. + desk (.|go) [ [shell-args...]] + Activate a desk. Extra arguments are passed onto shell. If called with + no arguments, look for a Deskfile in the current directory. If not a + recognized desk, try as a path to directory containing a Deskfile. desk run Run a command within a desk's environment then exit. Think '$SHELL -c'. desk edit [desk-name] @@ -95,9 +97,9 @@ $ desk tf desk for doing work on a terraform repo - set_aws_env - Set up AWS env variables: - plan - Run `terraform plan` with proper AWS var config - apply - Run `terraform apply` with proper AWS var config + set_aws_env Set up AWS env variables: + plan Run `terraform plan` with proper AWS var config + apply Run `terraform apply` with proper AWS var config ``` Basically, desk just associates a shell script (`name.sh`) with a name. When @@ -179,7 +181,23 @@ Desk does pay attention to certain kinds of comments, though. - *alias and function docs*: if the line above an alias or function is a comment, it will be used as documentation. + +### Deskfiles +Deskfiles are just shell scripts at the root of a project directory that +adhere to the conventions above. These can be put into version control to +formalize and ease common development tasks like running tests or doing +local builds. + +For example, if we have some directory or repository called `myproject`, if +we create `myproject/Deskfile`, we'll be able to do any of the following: +```sh +$ desk go /path/to/myproject/ +$ desk go /path/to/myproject/Deskfile +myproject/ $ desk go . +myproject/ $ desk go +``` + ### Sharing deskfiles across computers Of course, the desk config directory (by default `~/.desks`) can be a symlink diff --git a/desk b/desk index ada70f2..becad18 100755 --- a/desk +++ b/desk @@ -3,12 +3,13 @@ PREFIX="${DESK_DIR:-$HOME/.desk}" DESKS="${DESK_DESKS_DIR:-$PREFIX/desks}" +DESKFILE_NAME=Deskfile ## Commands cmd_version() { - echo "◲ desk 0.4.1" + echo "◲ desk 0.5.0" } @@ -25,8 +26,10 @@ Usage: Initialize desk configuration. $PROGRAM (list|ls) List all desks along with a description. - $PROGRAM (.|go) [shell-args...] - Activate a desk. Extra arguments are passed onto shell. + $PROGRAM (.|go) [ [shell-args...]] + Activate a desk. Extra arguments are passed onto shell. If called with + no arguments, look for a Deskfile in the current directory. If not a + recognized desk, try as a path to directory containing a Deskfile. $PROGRAM run Run a command within a desk's environment then exit. Think '\$SHELL -c'. $PROGRAM edit [desk-name] @@ -90,10 +93,35 @@ cmd_init() { cmd_go() { + # TODESK ($1) may either be + # + # - the name of a desk in $DESKS/ + # - a path to a Deskfile + # - a directory containing a Deskfile + # - empty -> `./Deskfile` + # local TODESK="$1" local DESKEXT=$(get_deskfile_extension) local DESKPATH="$(find "${DESKS}/" -name "${TODESK}${DESKEXT}")" + local POSSIBLE_DESKFILE_DIR="${TODESK%$DESKFILE_NAME}" + if [ -z "$POSSIBLE_DESKFILE_DIR" ]; then + POSSIBLE_DESKFILE_DIR="." + fi + + # If nothing could be found in $DESKS/, check to see if this is a path to + # a Deskfile. + if [[ -z "$DESKPATH" && -d "$POSSIBLE_DESKFILE_DIR" ]]; then + if [ ! -f "${POSSIBLE_DESKFILE_DIR}/${DESKFILE_NAME}" ]; then + echo "No Deskfile found in '${POSSIBLE_DESKFILE_DIR}'" + exit 1 + fi + + local REALPATH=$( cd $POSSIBLE_DESKFILE_DIR && pwd ) + DESKPATH="${REALPATH}/${DESKFILE_NAME}" + TODESK="$(basename $REALPATH)" + fi + # Shift desk name so we can forward on all arguments to the shell. shift; diff --git a/test/run_tests.sh b/test/run_tests.sh index b99f628..206c1a3 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -129,4 +129,34 @@ RAN=$(desk run hello 'hi j') echo "$RAN" | grep 'hi, j!' >/dev/null ensure $? "Run in desk 'hello' didn't work with hi function" +## `desk go` + +RAN=$(desk go example-project/Deskfile -c 'desk ; exit') +echo "$RAN" | grep "python_project - desk for working on a Python project" >/dev/null +ensure $? "Deskfile invocation didn't work (example-project/Deskfile)" +echo "$RAN" | grep -E "req\s+Install requirements" >/dev/null +ensure $? "Deskfile invocation didn't work (example-project/Deskfile)" +echo "$RAN" | grep -E "t\s+Run unittests with nose" >/dev/null +ensure $? "Deskfile invocation didn't work (example-project/Deskfile)" + +RAN=$(desk go example-project/ -c 'desk ; exit') +echo "$RAN" | grep "python_project - desk for working on a Python project" >/dev/null +ensure $? "Deskfile invocation didn't work (example-project/)" +echo "$RAN" | grep -E "req\s+Install requirements" >/dev/null +ensure $? "Deskfile invocation didn't work (example-project/)" +echo "$RAN" | grep -E "t\s+Run unittests with nose" >/dev/null +ensure $? "Deskfile invocation didn't work (example-project/)" + +pushd example-project + +RAN=$(desk go . -c 'desk ; exit') +echo "$RAN" | grep "python_project - desk for working on a Python project" >/dev/null +ensure $? "Deskfile invocation didn't work (./)" +echo "$RAN" | grep -E "req\s+Install requirements" >/dev/null +ensure $? "Deskfile invocation didn't work (./)" +echo "$RAN" | grep -E "t\s+Run unittests with nose" >/dev/null +ensure $? "Deskfile invocation didn't work (./)" + +popd + echo "tests pass." From 67f60a52cd7e3d95d90b69a4a1b35b6bfa99f000 Mon Sep 17 00:00:00 2001 From: James O'Beirne Date: Mon, 15 Feb 2016 19:58:46 -0800 Subject: [PATCH 2/2] Fix tests --- Dockerfile | 2 +- desk | 6 ++---- test/run_tests.sh | 28 ++++++++++++++-------------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index d75f327..1f4fa25 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,7 +15,7 @@ ADD examples examples RUN mkdir -p .config/fish && touch .config/fish/config.fish # Set up test Deskfile -RUN mkdir -p example-project && cp examples/python_project.sh example-project/Deskfile +RUN mkdir -p example-project && cp examples/hello.sh example-project/Deskfile RUN chown -R $USERNAME:$USERNAME .zshrc example-project examples run_tests.fish run_tests.sh .bashrc .config diff --git a/desk b/desk index becad18..8f25232 100755 --- a/desk +++ b/desk @@ -102,7 +102,7 @@ cmd_go() { # local TODESK="$1" local DESKEXT=$(get_deskfile_extension) - local DESKPATH="$(find "${DESKS}/" -name "${TODESK}${DESKEXT}")" + local DESKPATH="$(find "${DESKS}/" -name "${TODESK}${DESKEXT}" 2>/dev/null)" local POSSIBLE_DESKFILE_DIR="${TODESK%$DESKFILE_NAME}" if [ -z "$POSSIBLE_DESKFILE_DIR" ]; then @@ -119,7 +119,7 @@ cmd_go() { local REALPATH=$( cd $POSSIBLE_DESKFILE_DIR && pwd ) DESKPATH="${REALPATH}/${DESKFILE_NAME}" - TODESK="$(basename $REALPATH)" + TODESK=$(basename "$REALPATH") fi # Shift desk name so we can forward on all arguments to the shell. @@ -193,8 +193,6 @@ cmd_current() { fi local DESKPATH=$DESK_ENV - local DESK_NAME=${DESKPATH##*/} - local DESK_NAME=${DESK_NAME%.*} local CALLABLES=$(get_callables "$DESKPATH") local AUTO_ALIGN len longest out diff --git a/test/run_tests.sh b/test/run_tests.sh index 206c1a3..263b6e1 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -132,30 +132,30 @@ ensure $? "Run in desk 'hello' didn't work with hi function" ## `desk go` RAN=$(desk go example-project/Deskfile -c 'desk ; exit') -echo "$RAN" | grep "python_project - desk for working on a Python project" >/dev/null -ensure $? "Deskfile invocation didn't work (example-project/Deskfile)" -echo "$RAN" | grep -E "req\s+Install requirements" >/dev/null -ensure $? "Deskfile invocation didn't work (example-project/Deskfile)" -echo "$RAN" | grep -E "t\s+Run unittests with nose" >/dev/null -ensure $? "Deskfile invocation didn't work (example-project/Deskfile)" +echo "$RAN" | grep "example-project - simple desk that says hello" >/dev/null +ensure $? "Deskfile invocation didn't work (example-project/)" +echo "$RAN" | grep -E "hi\s+" >/dev/null +ensure $? "Deskfile invocation didn't work (example-project/)" +echo "$RAN" | grep -E "howdy\s+" >/dev/null +ensure $? "Deskfile invocation didn't work (example-project/)" RAN=$(desk go example-project/ -c 'desk ; exit') -echo "$RAN" | grep "python_project - desk for working on a Python project" >/dev/null +echo "$RAN" | grep "example-project - simple desk that says hello" >/dev/null ensure $? "Deskfile invocation didn't work (example-project/)" -echo "$RAN" | grep -E "req\s+Install requirements" >/dev/null +echo "$RAN" | grep -E "hi\s+" >/dev/null ensure $? "Deskfile invocation didn't work (example-project/)" -echo "$RAN" | grep -E "t\s+Run unittests with nose" >/dev/null +echo "$RAN" | grep -E "howdy\s+" >/dev/null ensure $? "Deskfile invocation didn't work (example-project/)" pushd example-project RAN=$(desk go . -c 'desk ; exit') -echo "$RAN" | grep "python_project - desk for working on a Python project" >/dev/null -ensure $? "Deskfile invocation didn't work (./)" -echo "$RAN" | grep -E "req\s+Install requirements" >/dev/null -ensure $? "Deskfile invocation didn't work (./)" -echo "$RAN" | grep -E "t\s+Run unittests with nose" >/dev/null +echo "$RAN" | grep "example-project - simple desk that says hello" >/dev/null ensure $? "Deskfile invocation didn't work (./)" +echo "$RAN" | grep -E "hi\s+" >/dev/null +ensure $? "Deskfile invocation didn't work (example-project/)" +echo "$RAN" | grep -E "howdy\s+" >/dev/null +ensure $? "Deskfile invocation didn't work (example-project/)" popd