Skip to content

Commit 81e36cb

Browse files
committed
fix: dashed bin names
1 parent 910729c commit 81e36cb

13 files changed

Lines changed: 115 additions & 56 deletions

File tree

src/artifacts-helper/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ Configures Codespace to authenticate with Azure Artifact feeds
2626
| pnpmAlias | Create alias for pnpm | boolean | true |
2727
| targetFiles | Comma separated list of files to write to. Default is '/etc/bash.bashrc,/etc/zsh/zshrc' for root and '~/.bashrc,~/.zshrc' for non-root | string | DEFAULT |
2828
| python | Install Python keyring helper for pip | boolean | false |
29+
| wrapperType | Type of wrapper to use. Options are 'SHELL_FUNCTION' or 'EXECUTABLE' | string | `SHELL_FUNCTION` |
30+
31+
### Wrapper Types
32+
33+
| Wrapper Type | Description |
34+
|-|-|
35+
| `SHELL_FUNCTION` | Configures shell functions that wrap the commands to set the authentication token before calling the actual command. This is the default options. |
36+
| `EXECUTABLE` | Configures a separate executable that wraps the commands to set the authentication token before calling the actual command. Suited for scenarios where the commands need to be invoked from scripts that do not consider shell functions, e.g. `subprocess.Popen(['npx'], shell=False)`. |
2937

3038
## Customizations
3139

src/artifacts-helper/install.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ elif [ "$WRAPPERTYPE" = "EXECUTABLE" ]; then
151151
sudo mkdir -p "${EXECUTABLES_TARGET_DIR}"
152152

153153
for ALIAS in "${WRAPPED_BINS_ARR[@]}"; do
154-
CMD_LINE=(ln -s "/usr/local/bin/run-$ALIAS.sh" "${EXECUTABLES_TARGET_DIR}/$ALIAS")
155-
sudo "${CMD_LINE[@]}"
154+
TARGET_EXECUTABLE_PATH="${EXECUTABLES_TARGET_DIR}/$ALIAS"
155+
TARGET_WRAPPER_PATH="/usr/local/bin/run-$ALIAS.sh"
156+
TARGET_WRAPPER_EXE_ENV_VAR_NAME=$(sed "s|-|_|g" <<<"${ALIAS}_EXE" | tr '[:lower:]' '[:upper:]')
157+
cp ./scripts/executable-wrapper-template.sh "$TARGET_EXECUTABLE_PATH"
158+
sed -i "s|SUBST_TARGET_WRAPPER_PATH|$TARGET_WRAPPER_PATH|g; s|SUBST_TARGET_BIN_NAME|$ALIAS|g; s|SUBST_EXE_ENV_VAR|$TARGET_WRAPPER_EXE_ENV_VAR_NAME|g" "$TARGET_EXECUTABLE_PATH"
159+
chmod +x "$TARGET_EXECUTABLE_PATH"
156160
done
157161
else
158162
echo "Invalid WRAPPERTYPE specified: $WRAPPERTYPE. Must be one of SHELL_FUNCTION or EXECUTABLE."

src/artifacts-helper/scripts/executable-wrapper-template.sh

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,67 @@
33
# Recursively unwrap itself from the path, eventually calling the target binary's
44
# `run-<target>.sh` script, which will inject a feed authentication token.
55

6-
TARGET_BIN_NAME=TARGET_BIN_NAME
6+
TARGET_BIN_NAME=SUBST_TARGET_BIN_NAME
7+
TARGET_WRAPPER_PATH=SUBST_TARGET_WRAPPER_PATH
78

89
log() {
9-
# Print to stderr to avoid interfering with scripts that capture stdout.
10-
# Prefix error messages so the user can quickly determine that it's from our
11-
# artifacts-helper wrapper, not an error originating from the wrapped binary.
12-
printf >&2 "[codespace-features/artifacts-helper] %s" "$1"
10+
# Print to stderr to avoid interfering with scripts that capture stdout.
11+
# Prefix error messages so the user can quickly determine that it's from our
12+
# artifacts-helper wrapper, not an error originating from the wrapped binary.
13+
printf >&2 "[codespace-features/artifacts-helper] %s" "$1"
1314
}
1415

1516
log_line() {
16-
log "$1
17+
log "$1
1718
"
1819
}
1920

21+
log_debug_line() {
22+
if [ "$ARTIFACTS_HELPER_DEBUG" == "1" ]; then
23+
log_line "$1"
24+
fi
25+
}
26+
2027
call_wrapped_bin() {
21-
CURRENT_SCRIPT_PATH=$(realpath "${BASH_SOURCE[0]}")
22-
if [ -z "$CURRENT_SCRIPT_PATH" ]; then
23-
log_line "Error: Current script path could not be determined! This will result in infinite recursion. Bailing early."
24-
exit 1
25-
fi
26-
27-
# Find the next executable from the PATH order, which would be shadowed by this wrapper
28-
if [ -z "$ARTIFACTS_HELPER_TARGET_BIN_PATHS" ]; then
29-
ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(which -a "$TARGET_BIN_NAME")
30-
fi
31-
32-
ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(sed '/^[[:space:]]*$/d' <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS")
33-
ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(grep --color=never -Fve "$CURRENT_SCRIPT_PATH" <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS")
34-
NEXT_SHADOWED_BIN=$(head -n 1 <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS")
35-
if [ -z "$NEXT_SHADOWED_BIN" ]; then
36-
log_line "Error: The real $TARGET_BIN_NAME could not be found on PATH."
37-
log_line "which -a $TARGET_BIN_NAME:\n%s\n" "$(which -a $TARGET_BIN_NAME)"
38-
log_line "CC_SHADOWED_BINS:\n%s\n" "$ARTIFACTS_HELPER_TARGET_BIN_PATHS"
39-
log_line "CURRENT_SCRIPT_PATH: %s\n" "$CURRENT_SCRIPT_PATH"
40-
exit 1
41-
fi
42-
43-
# Recursively removing the wrapper script(s) from the shadowed bin paths will
44-
# allow us to account for circumstances where the wrapper script appears multiple
45-
# times on the PATH. We will eventually descend to the real target binary.
46-
export ARTIFACTS_HELPER_TARGET_BIN_PATHS
47-
48-
log_line "Running: %s\n" "$NEXT_SHADOWED_BIN"
49-
${NEXT_SHADOWED_BIN} "$@"
50-
return "$?"
28+
CURRENT_SCRIPT_PATH=$(realpath "${BASH_SOURCE[0]}")
29+
if [ -z "$CURRENT_SCRIPT_PATH" ]; then
30+
log_line "Error: Current script path could not be determined! This will result in infinite recursion. Bailing early."
31+
exit 1
32+
fi
33+
34+
# Find the next executable from the PATH order, which would be shadowed by this wrapper
35+
if [ -z "$ARTIFACTS_HELPER_TARGET_BIN_PATHS" ]; then
36+
ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(which -a "$TARGET_BIN_NAME")
37+
fi
38+
39+
ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(sed '/^[[:space:]]*$/d' <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS")
40+
ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(grep --color=never -Fve "$CURRENT_SCRIPT_PATH" <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS")
41+
NEXT_SHADOWED_BIN=$(head -n 1 <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS")
42+
43+
log_debug_line "CURRENT_SCRIPT_PATH=$CURRENT_SCRIPT_PATH"
44+
log_debug_line "TARGET_BIN_NAME=$TARGET_BIN_NAME"
45+
log_debug_line "TARGET_WRAPPER_PATH=$TARGET_WRAPPER_PATH"
46+
log_debug_line "ARTIFACTS_HELPER_TARGET_BIN_PATHS=$ARTIFACTS_HELPER_TARGET_BIN_PATHS"
47+
log_debug_line "NEXT_SHADOWED_BIN=$NEXT_SHADOWED_BIN"
48+
49+
if [ -z "$NEXT_SHADOWED_BIN" ]; then
50+
log_line "Error: The real $TARGET_BIN_NAME could not be found on PATH."
51+
log_line "which -a $TARGET_BIN_NAME:\n%s\n" "$(which -a $TARGET_BIN_NAME)"
52+
log_line "CC_SHADOWED_BINS:\n%s\n" "$ARTIFACTS_HELPER_TARGET_BIN_PATHS"
53+
log_line "CURRENT_SCRIPT_PATH: %s\n" "$CURRENT_SCRIPT_PATH"
54+
exit 1
55+
fi
56+
57+
# Recursively removing the wrapper script(s) from the shadowed bin paths will
58+
# allow us to account for circumstances where the wrapper script appears multiple
59+
# times on the PATH. We will eventually descend to the real target binary.
60+
export ARTIFACTS_HELPER_TARGET_BIN_PATHS
61+
62+
log_debug_line "Running: SUBST_EXE_ENV_VAR=$NEXT_SHADOWED_BIN $TARGET_WRAPPER_PATH"
63+
SUBST_EXE_ENV_VAR="$NEXT_SHADOWED_BIN" "$TARGET_WRAPPER_PATH" "$@"
64+
return "$?"
5165
}
5266

5367
call_wrapped_bin "$@"
54-
exit $?
68+
EXIT_CODE=$?
69+
exit $EXIT_CODE

src/artifacts-helper/scripts/run-dotnet.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ if [ -f "${HOME}/ado-auth-helper" ]; then
1111
fi
1212

1313
# Find the dotnet executable so we do not run the bash alias again
14-
DOTNET_EXE=$(which dotnet)
14+
if [ -z "$DOTNET_EXE" ]; then
15+
DOTNET_EXE=$(which dotnet)
16+
fi
1517

1618
${DOTNET_EXE} "$@"
1719
EXIT_CODE=$?

src/artifacts-helper/scripts/run-npm.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ if [ -f "${HOME}/ado-auth-helper" ]; then
44
export ARTIFACTS_ACCESSTOKEN=$(${HOME}/ado-auth-helper get-access-token)
55
fi
66

7-
# Find the npm executable so we do not run the bash alias again
8-
NPM_EXE=$(which npm)
7+
if [ -z "$NPM_EXE" ]; then
8+
# Find the npm executable so we do not run the bash alias again
9+
NPM_EXE=$(which npm)
10+
fi
911

1012
${NPM_EXE} "$@"
1113
EXIT_CODE=$?

src/artifacts-helper/scripts/run-npx.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ if [ -f "${HOME}/ado-auth-helper" ]; then
44
export ARTIFACTS_ACCESSTOKEN=$(${HOME}/ado-auth-helper get-access-token)
55
fi
66

7-
# Find the npm executable so we do not run the bash alias again
8-
NPX_EXE=$(which npx)
7+
# Find the npx executable so we do not run the bash alias again
8+
if [ -z "$NPX_EXE" ]; then
9+
NPX_EXE=$(which npx)
10+
fi
911

1012
${NPX_EXE} "$@"
1113
EXIT_CODE=$?

src/artifacts-helper/scripts/run-nuget.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ if [ -f "${HOME}/ado-auth-helper" ]; then
1010
export VSS_NUGET_URI_PREFIXES=REPLACE_WITH_AZURE_DEVOPS_NUGET_FEED_URL_PREFIX
1111
fi
1212

13-
# Find the dotnet executable so we do not run the bash alias again
14-
NUGET_EXE=$(which nuget)
13+
# Find the nuget executable so we do not run the bash alias again
14+
if [ -z "$NUGET_EXE" ]; then
15+
NUGET_EXE=$(which nuget)
16+
fi
1517

1618
${NUGET_EXE} "$@"
1719
EXIT_CODE=$?

src/artifacts-helper/scripts/run-pnpm.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ if [ -f "${HOME}/ado-auth-helper" ]; then
55
fi
66

77
# Find the pnpm executable so we do not run the bash alias again
8-
PNPM_EXE=$(which pnpm)
8+
if [ -z "$PNPM_EXE" ]; then
9+
PNPM_EXE=$(which pnpm)
10+
fi
911

1012
${PNPM_EXE} "$@"
1113
EXIT_CODE=$?

src/artifacts-helper/scripts/run-pnpx.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ if [ -f "${HOME}/ado-auth-helper" ]; then
55
fi
66

77
# Find the pnpx executable so we do not run the bash alias again
8-
PNPX_EXE=$(which pnpx)
8+
if [ -z "$PNPX_EXE" ]; then
9+
PNPX_EXE=$(which pnpx)
10+
fi
911

1012
${PNPX_EXE} "$@"
1113
EXIT_CODE=$?

src/artifacts-helper/scripts/run-rush-pnpm.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ if [ -f "${HOME}/ado-auth-helper" ]; then
55
fi
66

77
# Find the rush-pnpm executable so we do not run the bash alias again
8-
RUSH_PNPM_EXE=$(which rush-pnpm)
8+
if [ -z "$RUSH_PNPM_EXE" ]; then
9+
RUSH_PNPM_EXE=$(which rush-pnpm)
10+
fi
911

1012
${RUSH_PNPM_EXE} "$@"
1113
EXIT_CODE=$?

0 commit comments

Comments
 (0)