Skip to content

Commit d59af58

Browse files
committed
Fix for wrapper scripts not being applied in non-interactive environments
These environments do not respect the BASH_ENV variable so installing these alias hooks does not work
1 parent d033b56 commit d59af58

File tree

22 files changed

+92
-245
lines changed

22 files changed

+92
-245
lines changed

src/artifacts-helper/NOTES.md

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
This installs [Azure Artifacts Credential Provider](https://github.com/microsoft/artifacts-credprovider)
2-
and optionally configures functions which shadow `dotnet`, `nuget`, `npm`, `yarn`, `rush`, and `pnpm` which dynamically sets an authentication token
3-
for pulling artifacts from a feed before running the command.
2+
and optionally configures shims which shadow `dotnet`, `nuget`, `npm`, `yarn`, `rush`, and `pnpm`.
3+
These dynamically sets an authentication token for pulling artifacts from a feed before running the command.
44

55
For `npm`, `yarn`, `rush`, and `pnpm` this requires that your `~/.npmrc` file is configured to use the ${ARTIFACTS_ACCESSTOKEN}
66
environment variable for the `authToken`. A helper script has been added that you can use to write your `~/.npmrc`
@@ -40,28 +40,4 @@ to download the package.
4040

4141
## OS Support
4242

43-
This feature is tested to work on Debian/Ubuntu and Mariner CBL 2.0
44-
45-
## Changing where functions are configured
46-
47-
By default, the functions are defined in `/etc/bash.bashrc` and `/etc/zsh/zshrc` if the container user is `root`, otherwise `~/.bashrc` and `~/.zshrc`.
48-
This default configuration ensures that the functions are always available for any interactive shells.
49-
50-
In some cases it can be useful to have the functions written to a non-default location. For example:
51-
- the configuration file of a shell other than `bash` and `zsh`
52-
- a custom file which is not a shell configuration script (so that it can be `source`d in non-interactive shells and scripts)
53-
54-
To do this, set the `targetFiles` option to the path script path where the functions should be written. Note that the default paths WILL NOT be used
55-
if the `targetFiles` option is provided, so you may want to include them in the overridden value, or add `source` the custom script in those configurations:
56-
57-
```bash
58-
# .devcontainer/devcontainer.json
59-
{
60-
// ...
61-
"targetFiles": "/custom/path/to/auth-helper.sh"
62-
}
63-
64-
# ~/.bashrc
65-
66-
source /custom/path/to/auth-helper.sh
67-
```
43+
This feature is tested to work on Debian/Ubuntu and Mariner CBL 2.0

src/artifacts-helper/devcontainer-feature.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Azure Artifacts Credential Helper",
33
"id": "artifacts-helper",
4-
"version": "2.0.3",
4+
"version": "3.0.0",
55
"description": "Configures Codespace to authenticate with Azure Artifact feeds",
66
"options": {
77
"nugetURIPrefixes": {
@@ -49,10 +49,10 @@
4949
"default": true,
5050
"description": "Create alias for pnpm"
5151
},
52-
"targetFiles": {
52+
"shimDirectory": {
5353
"type": "string",
54-
"default": "DEFAULT",
55-
"description": "Comma separated list of files to write to. Default is '/etc/bash.bashrc,/etc/zsh/zshrc' for root and '~/.bashrc,~/.zshrc' for non-root"
54+
"default": "/usr/local/bin",
55+
"description": "Directory where the shims will be installed. This must be in $PATH, and needs to be as early as possible in priority for the scripts to override the base executables."
5656
},
5757
"python": {
5858
"type": "boolean",

src/artifacts-helper/install.sh

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ALIAS_NPX="${NPXALIAS:-"true"}"
1212
ALIAS_RUSH="${RUSHALIAS:-"true"}"
1313
ALIAS_PNPM="${PNPMALIAS:-"true"}"
1414
INSTALL_PIP_HELPER="${PYTHON:-"false"}"
15-
COMMA_SEP_TARGET_FILES="${TARGETFILES:-"DEFAULT"}"
15+
SHIM_DIRECTORY="${SHIMDIRECTORY:-"/usr/local/bin"}"
1616

1717
ALIASES_ARR=()
1818

@@ -78,31 +78,23 @@ cd "$(dirname "$0")"
7878

7979
cp ./scripts/install-provider.sh /tmp
8080
chmod +rx /tmp/install-provider.sh
81+
8182
cp ./scripts/install-python-keyring.sh /tmp
8283
chmod +rx /tmp/install-python-keyring.sh
8384

84-
sed "s|REPLACE_WITH_AZURE_DEVOPS_NUGET_FEED_URL_PREFIX|${PREFIXES}|g" ./scripts/run-dotnet.sh > /usr/local/bin/run-dotnet.sh
85-
chmod +rx /usr/local/bin/run-dotnet.sh
86-
sed "s|REPLACE_WITH_AZURE_DEVOPS_NUGET_FEED_URL_PREFIX|${PREFIXES}|g" ./scripts/run-nuget.sh > /usr/local/bin/run-nuget.sh
87-
chmod +rx /usr/local/bin/run-nuget.sh
88-
cp ./scripts/run-npm.sh /usr/local/bin/run-npm.sh
89-
chmod +rx /usr/local/bin/run-npm.sh
90-
cp ./scripts/run-yarn.sh /usr/local/bin/run-yarn.sh
91-
chmod +rx /usr/local/bin/run-yarn.sh
92-
cp ./scripts/write-npm.sh /usr/local/bin/write-npm.sh
93-
chmod +rx /usr/local/bin/write-npm.sh
94-
cp ./scripts/run-npx.sh /usr/local/bin/run-npx.sh
95-
chmod +rx /usr/local/bin/run-npx.sh
96-
97-
cp ./scripts/run-rush.sh /usr/local/bin/run-rush.sh
98-
chmod +rx /usr/local/bin/run-rush.sh
99-
cp ./scripts/run-rush-pnpm.sh /usr/local/bin/run-rush-pnpm.sh
100-
chmod +rx /usr/local/bin/run-rush-pnpm.sh
101-
102-
cp ./scripts/run-pnpm.sh /usr/local/bin/run-pnpm.sh
103-
chmod +rx /usr/local/bin/run-pnpm.sh
104-
cp ./scripts/run-pnpx.sh /usr/local/bin/run-pnpx.sh
105-
chmod +rx /usr/local/bin/run-pnpx.sh
85+
# Replace AZURE_DEVOPS_NUGET_FEED_URL_PREFIX in scripts that require it
86+
sed -i "s|REPLACE_WITH_AZURE_DEVOPS_NUGET_FEED_URL_PREFIX|${PREFIXES}|g" ./scripts/dotnet
87+
sed -i "s|REPLACE_WITH_AZURE_DEVOPS_NUGET_FEED_URL_PREFIX|${PREFIXES}|g" ./scripts/nuget
88+
89+
# Install helper scripts in ${SHIM_DIRECTORY}
90+
cp "./scripts/auth-ado.sh" "${SHIM_DIRECTORY}"
91+
cp "./scripts/resolve-shim.sh" "${SHIM_DIRECTORY}"
92+
93+
# Install selected shim scripts in ${SHIM_DIRECTORY}
94+
for alias in "${ALIASES_ARR[@]}"; do
95+
chmod +rx "./scripts/${alias}"
96+
cp "./scripts/${alias}" "${SHIM_DIRECTORY}"
97+
done
10698

10799
if [ "${INSTALL_PIP_HELPER}" = "true" ]; then
108100
USER="${_REMOTE_USER}" /tmp/install-python-keyring.sh
@@ -116,28 +108,6 @@ if command -v sudo >/dev/null 2>&1; then
116108
fi
117109
fi
118110

119-
if [ "${COMMA_SEP_TARGET_FILES}" = "DEFAULT" ]; then
120-
if [ "${INSTALL_WITH_SUDO}" = "true" ]; then
121-
COMMA_SEP_TARGET_FILES="~/.bashrc,~/.zshrc"
122-
else
123-
COMMA_SEP_TARGET_FILES="/etc/bash.bashrc,/etc/zsh/zshrc"
124-
fi
125-
fi
126-
127-
IFS=',' read -r -a TARGET_FILES_ARR <<< "$COMMA_SEP_TARGET_FILES"
128-
129-
for ALIAS in "${ALIASES_ARR[@]}"; do
130-
for TARGET_FILE in "${TARGET_FILES_ARR[@]}"; do
131-
CMD="$ALIAS() { /usr/local/bin/run-$ALIAS.sh \"\$@\"; }"
132-
133-
if [ "${INSTALL_WITH_SUDO}" = "true" ]; then
134-
sudo -u ${_REMOTE_USER} bash -c "echo '$CMD' >> $TARGET_FILE"
135-
else
136-
echo $CMD >> $TARGET_FILE || true
137-
fi
138-
done
139-
done
140-
141111
if [ "${INSTALL_WITH_SUDO}" = "true" ]; then
142112
sudo -u ${_REMOTE_USER} bash -c "/tmp/install-provider.sh ${USENET6}"
143113
fi
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
if [ -f "${HOME}/ado-auth-helper" ]; then
3+
ARTIFACTS_ACCESSTOKEN=$(${HOME}/ado-auth-helper get-access-token)
4+
fi
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
source auth-ado.sh
3+
source resolve-shim.sh
4+
5+
# Install artifact credential provider if it is not already installed
6+
if [ ! -d "${HOME}/.nuget/plugins/netcore" ]; then
7+
wget -qO- https://aka.ms/install-artifacts-credprovider.sh | bash
8+
fi
9+
10+
DOTNET_EXE="$(resolve_shim)"
11+
VSS_NUGET_ACCESSTOKEN="${ARTIFACTS_ACCESSTOKEN:-}" VSS_NUGET_URI_PREFIXES=REPLACE_WITH_AZURE_DEVOPS_NUGET_FEED_URL_PREFIX ${NUGET_EXE} "$@"

src/artifacts-helper/scripts/npm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
source auth-ado.sh
3+
source resolve-shim.sh
4+
5+
NPM_EXE="$(resolve_shim)"
6+
ARTIFACTS_ACCESSTOKEN="${ARTIFACTS_ACCESSTOKEN:-}" ${NPM_EXE} "$@"

src/artifacts-helper/scripts/npx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
source auth-ado.sh
3+
source resolve-shim.sh
4+
5+
NPX_EXE="$(resolve_shim)"
6+
ARTIFACTS_ACCESSTOKEN="${ARTIFACTS_ACCESSTOKEN:-}" ${NPX_EXE} "$@"

src/artifacts-helper/scripts/pnpm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
source auth-ado.sh
3+
source resolve-shim.sh
4+
5+
PNPM_EXE="$(resolve_shim)"
6+
ARTIFACTS_ACCESSTOKEN="${ARTIFACTS_ACCESSTOKEN:-}" ${PNPM_EXE} "$@"

src/artifacts-helper/scripts/pnpx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
source auth-ado.sh
3+
source resolve-shim.sh
4+
5+
PNPX_EXE="$(resolve_shim)"
6+
ARTIFACTS_ACCESSTOKEN="${ARTIFACTS_ACCESSTOKEN:-}" ${PNPX_EXE} "$@"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
[[ ${RESOLVE_SHIMS_IMPORTED} == "true" ]] && return
3+
RESOLVE_SHIMS_IMPORTED=true
4+
5+
resolve_shim() {
6+
# Find the next non-shim executable in PATH so we do not run the shim again
7+
shim_file=$(readlink -f "${BASH_SOURCE[1]}")
8+
echo $(which -a dotnet | grep -vx "$shim_file" | head -n 1)
9+
}

0 commit comments

Comments
 (0)