Skip to content

Commit

Permalink
feat: Support configurable ASDF_CONCURRENCY (#1532)
Browse files Browse the repository at this point in the history
Co-authored-by: James Hegedus <jthegedus@hey.com>
  • Loading branch information
hyperupcall and jthegedus authored Apr 19, 2023
1 parent a1e858d commit 684f4f0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 15 deletions.
1 change: 1 addition & 0 deletions defaults
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ use_release_candidates = no
always_keep_download = no
plugin_repository_last_check_duration = 60
disable_plugin_short_name_repository = no
concurrency = auto
56 changes: 48 additions & 8 deletions docs/manage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,45 +134,85 @@ Disabling the plugin short-name repository does not remove plugins previously in

:::

### `concurrency`

The default number of cores to use during compilation.

| Options | Description |
| :------ | :--------------------------------------------------------------------------------------------------- |
| integer | Number of cores to use when compiling the source code |
| `auto` | Calculate the number of cores using `nproc`, then `sysctl hw.ncpu`, then `/proc/cpuinfo` or else `1` |

Note: the environment variable `ASDF_CONCURRENCY` take precedence if set.

## Environment Variables

Setting environment variables varies depending on your system and Shell. Default locations depend upon your installation location and method (Git clone, Homebrew, AUR).

Environment variables should generally be set before sourcing `asdf.sh`/`asdf.fish` etc. For Elvish set above `use asdf`.

The following examples assume:

- an installation to `$HOME/.asdf`
- a Bash Shell
The following describe usage with a Bash Shell.

### `ASDF_CONFIG_FILE`

Path to the `.asdfrc` configuration file. Can be set to any location. Must be an absolute path.

- Default: `$HOME/.asdfrc`
- If Unset: `$HOME/.asdfrc` will be used.
- Usage: `export ASDF_CONFIG_FILE=/home/john_doe/.config/asdf/.asdfrc`

### `ASDF_DEFAULT_TOOL_VERSIONS_FILENAME`

The filename of the file storing the tool names and versions. Can be any valid filename. Typically, you should not set this value unless you want to ignore `.tool-versions` files.

- Default: `.tool-versions`
- If Unset: `.tool-versions` will be used.
- Usage: `export ASDF_DEFAULT_TOOL_VERSIONS_FILENAME=tool_versions`

### `ASDF_DIR`

The location of `asdf` core scripts. Can be set to any location. Must be an absolute path.

- Default: `$HOME/.asdf` (always the parent directory of the `bin/asdf` executable)
- If Unset: the parent directory of the `bin/asdf` executable is used.
- Usage: `export ASDF_DIR=/home/john_doe/.config/asdf`

### `ASDF_DATA_DIR`

The location where `asdf` will install plugins, shims and tool versions. Can be set to any location. Must be an absolute path.

- Default: `$HOME/.asdf`
- If Unset: `$HOME/.asdf` if it exists, or else the value of `ASDF_DIR`
- Usage: `export ASDF_DATA_DIR=/home/john_doe/.asdf`

### `ASDF_CONCURRENCY`

Number of cores to use when compiling the source code. If set, this value takes precedence over the asdf config `concurrency` value.

- If Unset: the asdf config `concurrency` value is used.
- Usage: `export ASDF_CONCURRENCY=32`

## Full Configuration Example

Following a simple asdf setup with:

- a Bash Shell
- an installation location of `$HOME/.asdf`
- installed via Git
- NO environment variables set
- NO custom `.asdfrc` file

would result in the following outcomes:

| Configuration | Value | Calculated by |
| :------------------------------------ | :--------------- | :------------------------------------------------------------------------------------------------------------------------------------------------- |
| config file location | `$HOME/.asdfrc` | `ASDF_CONFIG_FILE` is empty, so use `$HOME/.asdfrc` |
| default tool versions filename | `.tool-versions` | `ASDF_DEFAULT_TOOL_VERSIONS_FILENAME` is empty, so use `.tool-versions` |
| asdf dir | `$HOME/.asdf` | `ASDF_DIR` is empty, so use parent dir of `bin/asdf` |
| asdf data dir | `$HOME/.asdf` | `ASDF_DATA_DIR` is empty so use `$HOME/.asdf` as `$HOME` exists. |
| concurrency | `auto` | `ASDF_CONCURRENCY` is empty, so rely on `concurrency` value from the [default configuration](https://github.com/asdf-vm/asdf/blob/master/defaults) |
| legacy_version_file | `no` | No custom `.asdfrc`, so use the [default configuration](https://github.com/asdf-vm/asdf/blob/master/defaults) |
| use_release_candidates | `no` | No custom `.asdfrc`, so use the [default configuration](https://github.com/asdf-vm/asdf/blob/master/defaults) |
| always_keep_download | `no` | No custom `.asdfrc`, so use the [default configuration](https://github.com/asdf-vm/asdf/blob/master/defaults) |
| plugin_repository_last_check_duration | `60` | No custom `.asdfrc`, so use the [default configuration](https://github.com/asdf-vm/asdf/blob/master/defaults) |
| disable_plugin_short_name_repository | `no` | No custom `.asdfrc`, so use the [default configuration](https://github.com/asdf-vm/asdf/blob/master/defaults) |

## Internal Configuration

Users should not worry about this section as it describes configuration internal to `asdf` useful for Package Managers and integrators.
Expand Down
26 changes: 19 additions & 7 deletions lib/functions/installs.bash
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,27 @@ install_command() {
}

get_concurrency() {
if command -v nproc &>/dev/null; then
nproc
elif command -v sysctl &>/dev/null && sysctl hw.ncpu &>/dev/null; then
sysctl -n hw.ncpu
elif [ -f /proc/cpuinfo ]; then
grep -c processor /proc/cpuinfo
local asdf_concurrency=

if [ -n "$ASDF_CONCURRENCY" ]; then
asdf_concurrency="$ASDF_CONCURRENCY"
else
printf "1\n"
asdf_concurrency=$(get_asdf_config_value 'concurrency')
fi

if [ "$asdf_concurrency" = 'auto' ]; then
if command -v nproc &>/dev/null; then
asdf_concurrency=$(nproc)
elif command -v sysctl &>/dev/null && sysctl hw.ncpu &>/dev/null; then
asdf_concurrency=$(sysctl -n hw.ncpu)
elif [ -f /proc/cpuinfo ]; then
asdf_concurrency=$(grep -c processor /proc/cpuinfo)
else
asdf_concurrency="1"
fi
fi

printf "%s\n" "$asdf_concurrency"
}

install_one_local_tool() {
Expand Down
19 changes: 19 additions & 0 deletions test/install_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ teardown() {
[ "$status" -eq 0 ]
}

@test "install_command set ASDF_CONCURRENCY via env var" {
ASDF_CONCURRENCY=-1 run asdf install dummy 1.0.0
[ "$status" -eq 0 ]
[ -f "$ASDF_DIR/installs/dummy/1.0.0/env" ]
run grep ASDF_CONCURRENCY=-1 "$ASDF_DIR/installs/dummy/1.0.0/env"
[ "$status" -eq 0 ]
}

@test "install_command set ASDF_CONCURRENCY via asdfrc" {
cat >"$HOME/.asdfrc" <<-'EOM'
concurrency = -2
EOM
run asdf install dummy 1.0.0
[ "$status" -eq 0 ]
[ -f "$ASDF_DIR/installs/dummy/1.0.0/env" ]
run grep ASDF_CONCURRENCY=-2 "$ASDF_DIR/installs/dummy/1.0.0/env"
[ "$status" -eq 0 ]
}

@test "install_command without arguments should work in directory containing whitespace" {
WHITESPACE_DIR="$PROJECT_DIR/whitespace\ dir"
mkdir -p "$WHITESPACE_DIR"
Expand Down

0 comments on commit 684f4f0

Please sign in to comment.