Skip to content

Commit

Permalink
Merge pull request #132 from jdno/refactor-dev-desktop-scripts
Browse files Browse the repository at this point in the history
Refactor scripts to set up Rust on dev desktops
  • Loading branch information
jdno authored Nov 2, 2022
2 parents 8877977 + 0da6994 commit 78613eb
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 29 deletions.
6 changes: 3 additions & 3 deletions ansible/roles/dev-desktop/files/scripts/help.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env bash

echo "# Available scripts for managing your Rust checkouts"
echo "init.sh | first time setup, you should only have to execute this once on a new machine"
echo "setup_rustup.sh | first time setup, you should only have to execute this once on a new machine"
echo "status.sh | list the branches and git status of all copies of the Rust repo"
echo "new_worktree.sh | creates a worktree (shallow copy of the main git checkout of Rust, sharing the .git folder)"
echo "detach_merged_prs.sh | invokes \"git pull --fast-forward-only\" on all worktrees and detaches those that are equal to the \"master\" branch"
echo ""
echo "# Rarer commands:"
echo "set_defaults.sh | connects the global config.toml with all worktrees. Use this when your setup is broken"

echo "set_defaults.sh | connects the global config.toml with all worktrees. Use this when your setup is broken"
echo "setup_rust.sh | Clone your fork of rust-lang/rust, compile, and then link it"
14 changes: 4 additions & 10 deletions ansible/roles/dev-desktop/files/scripts/init.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
#!/usr/bin/env bash

username=`id -u -n`
gh_name=${username#"gh-"}

# Using https instead of git urls because vscode only handles login on push/pull
git clone https://github.com/$gh_name/rust.git
pushd rust
git remote add upstream https://github.com/rust-lang/rust.git
git fetch upstream
git checkout upstream/master
popd
# Enable strict mode for Bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'

init_git.py
setup_rustup.sh
46 changes: 46 additions & 0 deletions ansible/roles/dev-desktop/files/scripts/link_rust.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

# Enable strict mode for Bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'

# Discover target triple (e.g. "aarch64-unknown-linux-gnu")
target="$(rustc -vV | awk '/host/ { print $2 }')"

rustc_dummy=$(
cat <<EOF
#!/usr/bin/env bash
echo "This is a dummy file to trick rustup into thinking this is a sysroot"
echo 'Run "x.py build --stage 1 library/std" to create a real sysroot you can use with rustup'
EOF
)

for D in rust*; do
if [ -d "$D" ]; then
pushd "$D"

bootstrap_version=$(grep 'pub const VERSION' src/bootstrap/lib.rs | grep -o '[0-9]*')

if [ "$bootstrap_version" -gt 2 ]; then
stages=(stage1-sysroot stage2-sysroot)
else
stages=(stage1 stage2)
fi

for stage in "${stages[@]}"; do
directory="build/${target}/${stage}"

if [ ! -d "$directory" ]; then
mkdir -p "${directory}/lib"
mkdir -p "${directory}/bin"
echo "$rustc_dummy" >> "${directory}/bin/rustc"
chmod +x "${directory}/bin/rustc"
fi

rustup toolchain link "${D}_${stage}" "$directory"
done

popd
fi
done
2 changes: 1 addition & 1 deletion ansible/roles/dev-desktop/files/scripts/new_worktree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ git checkout upstream/master
ln -s ../config.toml
popd

setup_rustup.sh
link_rust.sh
13 changes: 10 additions & 3 deletions ansible/roles/dev-desktop/files/scripts/set_defaults.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#!/usr/bin/env bash

# Enable strict mode for Bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'

for D in rust*; do
if [ -d "${D}" ]; then
pushd $D
ln -s ../config.toml
popd
pushd "${D}" || exit
if [[ ! -f config.toml ]]; then
ln -s ~/config.toml .
fi
popd || exit
fi
done
29 changes: 29 additions & 0 deletions ansible/roles/dev-desktop/files/scripts/setup_rust.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

# Enable strict mode for Bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'

username=$(id -u -n)
gh_name=${username#"gh-"}

set -x

if [[ ! -d "rust" ]]; then
# Using https instead of git urls because vscode only handles login on push/pull
git clone "https://github.com/${gh_name}/rust.git"
fi

pushd rust

if ! git remote | grep upstream; then
git remote add upstream https://github.com/rust-lang/rust.git
fi

git fetch upstream
git checkout upstream/master
popd

set_defaults.sh
link_rust.sh
25 changes: 13 additions & 12 deletions ansible/roles/dev-desktop/files/scripts/setup_rustup.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env bash

set -x

rustup --version || curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y

source "$HOME/.cargo/env"

for D in rust*; do
if [ -d "${D}" ]; then
rustup toolchain link "$D"_stage1 "$D/build/x86_64-unknown-linux-gnu/stage1"
rustup toolchain link "$D"_stage2 "$D/build/x86_64-unknown-linux-gnu/stage2"
fi
done
# Enable strict mode for Bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'

# Check if rustup is already installed and exit if that's the case.
if command -v rustup &>/dev/null; then
rustup --version
exit 0
fi

echo "Installing rustup..."
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y

0 comments on commit 78613eb

Please sign in to comment.