Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions share/chxcode/auto
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
# shellcheck shell=bash

# https://github.com/asdf-vm/asdf/blob/bd21c99a0a04018b51e528acff8d237bff8780c4/lib/utils.bash#L626-L638
strip_tool_version_comments() {
local tool_version_path="$1"

while IFS= read -r tool_line || [ -n "$tool_line" ]; do
# Remove whitespace before pound sign, the pound sign, and everything after it
new_line="$(cut -f1 -d"#" <<<"$tool_line" | sed -e 's/[[:space:]]*$//')"

# Only print the line if it is not empty
if [[ -n "$new_line" ]]; then
printf "%s\\n" "$new_line"
fi
done <"$tool_version_path"
}

function version_from_file() {
local version

if { read -r version <"$1/.xcode-version"; } 2>/dev/null; then
version="${version%%[[:space:]]}"
elif [ -f "$1/.tool-versions" ]; then
version=$(strip_tool_version_comments "$1/.tool-versions" |\
grep "^xcode " |\
sed -e "s/^xcode //")
fi

printf '%s' "$version"
}

function chxcode_auto() {
local dir="$PWD/" version

until [[ -z "$dir" ]]; do
dir="${dir%/*}"
if { read -r version <"$dir/.xcode-version"; } 2>/dev/null || [[ -n "$version" ]]; then
version="${version%%[[:space:]]}"

version=$(version_from_file "$dir")
if [[ -n "$version" ]]; then
if [[ "$version" == "$XCODE_AUTO_VERSION" ]]; then
return
else
Expand Down
6 changes: 6 additions & 0 deletions share/chxcode/chxcode
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ system_xcode_version() {

xcode_versions() {
system_version=$(system_xcode_version)
versions=()
for xcode in ${XCODES[*]}; do
version=$(xcode_version "$xcode")
versions+=("$version")
done

IFS=$'\n' versions=("$(sort -n <<<"${versions[*]}")"); unset IFS

for version in ${versions[*]}; do
if [ "$version" = "$system_version" ]; then
echo "* $version"
else
Expand Down
21 changes: 21 additions & 0 deletions test/chxcode_auto_test
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ test_auto_enter_versioned_directory() {
"$DEVELOPER_DIR"
}

test_auto_enter_tool_versions_directory() {
echo "xcode 9.3\nruby 2.6" > "$test_project_dir/.tool-versions"

cd "$test_project_dir" && chxcode_auto

assertEquals "should switch Xcode when entering a .tool-versions directory" \
"$search_path/Xcode.app/Contents/Developer" \
"$DEVELOPER_DIR"
}

test_auto_enter_versioned_subdirectory() {
echo "9.3" > "$test_project_dir/.xcode-version"
mkdir "$test_project_dir/subdir"
Expand All @@ -50,6 +60,17 @@ test_auto_enter_versioned_subdirectory() {
"$DEVELOPER_DIR"
}

test_auto_enter_tool_versions_subdirectory() {
echo "xcode 9.3\nruby2.6" > "$test_project_dir/.tool-versions"
mkdir "$test_project_dir/subdir"

cd "$test_project_dir/subdir" && chxcode_auto

assertEquals "should switch Xcode when entering a subdirectory of a .tool-versions directory" \
"$search_path/Xcode.app/Contents/Developer" \
"$DEVELOPER_DIR"
}

test_auto_enter_subdir() {
mkdir -p "$test_project_dir/subdir"
echo "9.3" > "$test_project_dir/.xcode-version"
Expand Down
2 changes: 1 addition & 1 deletion test/chxcode_test
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
. ./test/helper

test_exec_with_no_arguments() {
export DEVELOPER_DIR="$search_path/Xcode.app/Contents/Developer"
export DEVELOPER_DIR="$search_path/Xcode.app/Contents/Developer"

xcodes=$(chxcode | tr '\n' ' ' | xargs)

Expand Down