This repository has been archived by the owner on Nov 24, 2024. It is now read-only.
generated from aubreypwd/zsh-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-plugin-bruse.plugin.zsh
116 lines (92 loc) · 2.61 KB
/
zsh-plugin-bruse.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/zsh
###
# Install version specified in .bruse.json.
#
# {
# "node": "10",
# "php": "7.2"
# }
#
# E.g. this will:
#
# bruse node@10
# bruse [email protected]
#
# @since 1.1.0
##
function bruse_json() {
if ! [[ -f ".bruse.json" ]]; then
return
fi
local package=0
for package in $( jq -r 'keys | .[]' '.bruse.json' ); do
local version=$( jq -r ".$package" '.bruse.json' | xargs );
bruse $(echo "$package" | xargs) "$version"
done
}
###
# Detect version of node in .nvmrc.
#
# If a version is specified in a .nvmrc file, it will
# try and automatically install that version.
#
# @since 1.1.0
##
function bruse_nvmrc() {
if ! [[ -x $(command -v xargs) ]]; then
return;
fi
if ! [[ -f ".nvmrc" ]]; then
return;
fi
local version=$(cat .nvmrc | xargs)
if ! [[ $version =~ '([0-9]+\.?)' ]]; then
echo "Cannot install node at version '$version' as it is not numeric."
return
fi
bruse node "$version"
}
###
# Bruse
#
# E.g: bruse php 7.4
#
# @since 1.0.0
# @since 10/2/20
#
# @author Aubrey Portwood <[email protected]>
##
function bruse {
if [[ "" = "$1" ]]; then
bruse_nvmrc
if [[ -x $(command -v jq) ]]; then
bruse_json
return
fi
echo "Install jq () for .bruse.json and .nvmrc support."
return;
fi
if ! [[ -x $(command -v brew) ]]; then >&2 echo "This helps out Homebrew (https://brew.sh), we couldn't find the 'brew' command." && return; fi
if ! [[ -x $(command -v xargs) ]]; then >&2 echo "This requires 'xargs' command which was not found." && return; fi
local package=$( echo "$1" | xargs )
local version=$( echo "$2" | xargs )
echo "\nUsing $package@$version..."
if [[ -z $( brew ls --versions "$package@$version" ) ]]; then
echo " - Not installed, trying to install..."
brew install "$package@$version"
fi
if [[ -z $( brew ls --versions "$package@$version" ) ]]; then
echo "- Unable to install or use $package@$version."
return
fi
local try_link_results=$( brew unlink "$package@$version" && brew link "$package@$version" --force --overwrite )
if [[ "$try_link_results" == *"relink:"* ]]; then # Try and link the new version, unless it tells us we need to relink...
local relink_command=$( echo "$try_link_results" | grep -Eo ' (.*)' ) # Get the relink command that brew is giving us.
local relink_command=$( echo "$relink_command" | xargs ) # Trim the command.
local relink_command="$relink_command --overwrite" # Make sure we overwrite anything.
if [[ $relink_command == *"brew unlink"* ]]; then # Confirm we have an unlink command.
echo "- Linking using '$relink_command'..."
eval "$relink_command"
fi
fi
}