Skip to content

Commit

Permalink
Setup asdf and direnv managed development environment (#44)
Browse files Browse the repository at this point in the history
* Setup asdf and direnv managed development environment

Now `cd` into the project directory would activate correct versions of build
dependencies.
  • Loading branch information
iilyak authored Nov 22, 2023
1 parent 663a6b8 commit af7e555
Show file tree
Hide file tree
Showing 5 changed files with 461 additions and 4 deletions.
41 changes: 41 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
tools::has() {
type "$1" &>/dev/null
}

if ! tools::has tput; then
echo "'tput' tool is missing"
fi

console::bold() {
tput bold
}

console::red() {
tput setaf 1
}

console::reset() {
tput sgr0
}

die() {
echo -e "$(console::red)$(console::bold)FATAL: ${*}$(console::reset)" >&2
exit 1
}


asdf::has() {
asdf current $1 >>/dev/null 2>&1
}

use asdf

while read asdf_tool; do
if ! asdf::has ${asdf_tool}; then asdf direnv local ${asdf_tool}; fi
done < <(cat .tool-versions | grep -v '\#')

watch_file ".asdf"
dotenv_if_exists .envrc.local

export JAVA_HOME=$(dirname $(dirname $(expand_path $(asdf which java) "/")))
export ASDF_DIRENV_CLOUSEAU=true
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ build.log
build.log
.cache
clouseau.iml
dependency-reduced-pom.xml


# direnv & asdf
.asdf
.envrc.local
4 changes: 4 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
direnv 2.32.3
maven 3.8.2
java zulu-7.48.0.11
scala 2.9.1.final
286 changes: 286 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
instances = clouseau1 clouseau2 clouseau3
ifneq ($(WITH_COOKIE),)
with-cookie := -Dlauncher=with-cookie -Dcookie=$(WITH_COOKIE)
endif

.PHONY: help
# target: help - Print this help
help:
@egrep "^# target: " Makefile \
| sed -e 's/^# target: //g' \
| sort \
| awk '{printf(" %-20s", $$1); $$1=$$2=""; print "-" $$0}'

.PHONY: build
# target: build - Build package, run tests and create distribution
build: .asdf check-epmd
@mvn

# target: clouseau1 - Start local inistance of clouseau1 node
# target: clouseau2 - Start local inistance of clouseau2 node
# target: clouseau3 - Start local inistance of clouseau3 node
$(instances): .asdf check-epmd
@mvn scala:run -Dname=$@ $(with-cookie)

.PHONY: clean
# target: clean - Remove build artifacts
clean:
@mvn clean

check-epmd:
@if ! pgrep epmd >>/dev/null 2>&1 ; then echo "Error: Please start 'epmd' first"; exit 1; fi

# ==========================
# Setting up the environment
# --------------------------

define CURRENT_SHELL
"$(shell basename $${SHELL})"
endef

define CURRENT_OS
"$(shell uname -s | tr '[:upper:]' '[:lower:]')"
endef

# Testing from fish
# set SHELL bash && make .asdf
# set SHELL fish && make .asdf
# set SHELL zsh && make .asdf
# set SHELL unsupported && make .asdf
# Testing from other shells
# SHELL=bash && make .asdf
# SHELL=fish && make .asdf
# SHELL=zsh && make .asdf
# SHELL=unsupported && make .asdf

ifndef ASDF_DIRENV_BIN
.asdf:
@case $(CURRENT_SHELL)-$(CURRENT_OS) in \
fish-darwin) echo "$$FISH_DARWIN_HELP" ;; \
bash-darwin) echo "$$BASH_DARWIN_HELP" ;; \
zsh-darwin) echo "$$ZSH_DARWIN_HELP" ;; \
*) echo "$$CONTRIBUTE_SHELL_SETUP" ;; \
esac
else
.asdf:
@touch $@
@touch .envrc
endif

define FISH_DARWIN_HELP
There are two steps which need to be done to set up environment.

- The asdf tool manager would need to be installed
- The direnv plugin for asdf manager would need to be installed


= Setting up asdf tool manager

You can install asdf from brew by following command.

```
brew install asdf
```

Once you finish installation add following line to ~/.config/fish/config.fish

```
source (brew --prefix asdf)/libexec/asdf.fish
```

You can do it by copy/paste of the following line

```
echo -e "\\nsource (brew --prefix asdf)/libexec/asdf.fish" >> ~/.config/fish/config.fish
```

= Setting up direnv

Run following commands to install direnv plugin

```
asdf plugin-add direnv
asdf install direnv latest
asdf global direnv latest
```

Once you have done with above commands add following to the ~/.config/fish/config.fish

```
# Hook direnv into your shell.
asdf exec direnv hook fish | source

# A shortcut for asdf managed direnv.
function direnv
asdf exec direnv "$$argv"
end
```

You can do it by copy/paste of the following

```
printf "\
# Hook direnv into your shell.
asdf exec direnv hook fish | source

# A shortcut for asdf managed direnv.
function direnv
asdf exec direnv \\"\$$argv\\"
end
" >> ~/.config/fish/config.fish
```

= Enabling direnv integration

- After finishing the above two steps restart your shell.
- Run following `asdf direnv setup --shell fish --version latest`
- Restart your shell once again

endef
export FISH_DARWIN_HELP

define BASH_DARWIN_HELP

There are two steps which need to be done to set up environment.

- The asdf tool manager would need to be installed
- The direnv plugin for asdf manager would need to be installed


= Setting up asdf tool manager

You can install asdf from brew by following command.

```
brew install asdf
```

Once you finish installation add following line to ~/.bash_profile

```
source $$(brew --prefix asdf)/libexec/asdf.sh
```

You can do it by copy/paste of the following line
```
echo -e "\n. $$(brew --prefix asdf)/libexec/asdf.sh" >> ~/.bash_profile
```

= Setting up direnv

Run following commands to install direnv plugin

```
asdf plugin-add direnv
asdf install direnv latest
asdf global direnv latest
```

Once you have done with above commands add following to the ~/.bash_profile

```
# Hook direnv into your shell.
eval "$$(asdf exec direnv hook bash)"

# A shortcut for asdf managed direnv.
direnv() { asdf exec direnv "$$@"; }
```

You can do it by copy/paste of the following

```
cat << EOF >> ~/.bashrc
# Hook direnv into your shell.
eval "$$(asdf exec direnv hook bash)"

# A shortcut for asdf managed direnv.
direnv() { asdf exec direnv "$$@"; }
EOF
```

= Enabling direnv integration

- After finishing the above two steps restart your shell.
- Run following `asdf direnv setup --shell bash --version latest`
- Restart your shell once again

endef
export BASH_DARWIN_HELP

define ZSH_DARWIN_HELP

There are two steps which need to be done to set up environment.

- The asdf tool manager would need to be installed
- The direnv plugin for asdf manager would need to be installed


= Setting up asdf tool manager

You can install asdf from brew by following command.

```
brew install asdf
```

Once you finish installation add following line to ~/.zshrc
(or ${ZDOTDIR}/.zshrc if you use custom location).

```
source $$(brew --prefix asdf)/libexec/asdf.sh
```

You can do it by copy/paste of the following line
```
echo -e "\n. $$(brew --prefix asdf)/libexec/asdf.sh" >> $${ZDOTDIR:-~}/.zshrc
```

= Setting up direnv

Run following commands to install direnv plugin

```
asdf plugin-add direnv
asdf install direnv latest
asdf global direnv latest
```

Once you have done with above commands add following to the ~/.zshrc
(or ${ZDOTDIR}/.zshrc if you use custom location).


```
# Hook direnv into your shell.
eval "$$(asdf exec direnv hook zsh)"

# A shortcut for asdf managed direnv.
direnv() { asdf exec direnv "$$@"; }
```

You can do it by copy/paste of the following

```
cat << EOF >> ~/.zshrc
# Hook direnv into your shell.
eval "$$(asdf exec direnv hook zsh)"

# A shortcut for asdf managed direnv.
direnv() { asdf exec direnv "$$@"; }
EOF
```

= Enabling direnv integration

- After finishing the above two steps restart your shell.
- Run following `asdf direnv setup --shell zsh --version latest`
- Restart your shell once again

endef
export ZSH_DARWIN_HELP


define CONTRIBUTE_SHELL_SETUP
We detected $(CURRENT_SHELL) running on $(CURRENT_OS). Unfortunately
documentation for this combination is not available. Contributions are welcome.
endef
export CONTRIBUTE_SHELL_SETUP
Loading

0 comments on commit af7e555

Please sign in to comment.