Skip to content

Commit 02d8ff4

Browse files
committed
- Move functions to lib
1 parent 11fa7e5 commit 02d8ff4

File tree

10 files changed

+293
-289
lines changed

10 files changed

+293
-289
lines changed

rush

Lines changed: 150 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,84 @@ ini_keys() {
10781078
for a in "${keys[@]}"; do echo "$a"; done | sort
10791079
}
10801080

1081+
# src/lib/list_display_item.sh
1082+
list_display_item() {
1083+
local package="$1"
1084+
local infofile="$2"
1085+
local repo="$3"
1086+
local simple width
1087+
1088+
simple=${args[--simple]}
1089+
width=$((${COLUMNS:-80} + 9))
1090+
1091+
[[ "$repo" != "default" ]] && package="$repo:$package"
1092+
if [[ $simple ]]; then
1093+
printf "%s\n" "$package"
1094+
else
1095+
info=$(head -1 "$infofile" 2>/dev/null)
1096+
padded_package=$(printf "%-20s" "$package")
1097+
message="$(green "$padded_package") $info"
1098+
printf "%.${width}s\n" "$message"
1099+
fi
1100+
}
1101+
1102+
# src/lib/list_show_repo.sh
1103+
list_show_repo() {
1104+
local repo_or_package="$1"
1105+
local search="${args[--search]}"
1106+
local simple=${args[--simple]}
1107+
local all=${args[--all]}
1108+
local repo="$repo_or_package"
1109+
local package glob repo_path infofile regex package_name
1110+
1111+
if [[ $repo_or_package =~ (.*):(.*) ]]; then
1112+
repo=${BASH_REMATCH[1]}
1113+
package=${BASH_REMATCH[2]}
1114+
fi
1115+
1116+
repo_path=$(config_get "$repo")
1117+
1118+
if [[ ! $repo_path ]]; then
1119+
package="$repo"
1120+
repo="default"
1121+
repo_path=$(config_get "$repo")
1122+
fi
1123+
1124+
if [[ $package ]]; then
1125+
glob=("$repo_path"/"$package"/**/info)
1126+
else
1127+
if [[ $all ]]; then
1128+
glob_files=$(find "$repo_path" -type f -name 'info' | sort)
1129+
else
1130+
glob_files=$(find "$repo_path" -maxdepth 2 -type f -name 'info' | sort)
1131+
fi
1132+
readarray -t glob < <(echo "${glob_files[@]}")
1133+
fi
1134+
1135+
if [[ ${glob[0]} =~ .*\*.* ]]; then
1136+
infofile="$repo_path/$package/info"
1137+
if [[ -f "$infofile" ]]; then
1138+
list_display_item "$package" "$infofile" "$repo"
1139+
elif [[ ! $simple ]]; then
1140+
red "nothing in $repo repo"
1141+
fi
1142+
1143+
else
1144+
for infofile in "${glob[@]}"; do
1145+
if [[ $search ]]; then
1146+
regex="$repo_path/(.*${search}.*)/info"
1147+
else
1148+
regex="$repo_path/(.*)/info"
1149+
fi
1150+
1151+
if [[ $infofile =~ $regex ]]; then
1152+
package_name="${BASH_REMATCH[1]}"
1153+
list_display_item "$package_name" "$infofile" "$repo"
1154+
fi
1155+
done
1156+
fi
1157+
}
1158+
10811159
# src/lib/print_logo.sh
10821160
# shellcheck disable=SC1003
10831161
# Disabling single quote escape hint
@@ -1091,11 +1169,83 @@ print_logo() {
10911169
echo
10921170
}
10931171

1172+
# src/lib/pull_repo.sh
1173+
pull_repo() {
1174+
local repo_path="$1"
1175+
local repo="$2"
1176+
1177+
if [[ -d "$repo_path/.git" ]]; then
1178+
say "pull" "$repo"
1179+
(cd "$repo_path" && git pull)
1180+
else
1181+
say "pull" "skipping $repo (not a git repo)"
1182+
fi
1183+
}
1184+
1185+
# src/lib/push_repo.sh
1186+
push_repo() {
1187+
local repo_path="$1"
1188+
local repo="$2"
1189+
1190+
if [[ -d "$repo_path/.git" ]]; then
1191+
say "push" "$repo"
1192+
(
1193+
set -e
1194+
cd "$repo_path"
1195+
1196+
say "push" "$repo: adding files"
1197+
git add . --all
1198+
1199+
if [[ -n "${args[--chmod]}" ]]; then
1200+
say "push" "$repo: applying chmod +x"
1201+
git ls-files | grep -E "undo|main" | xargs -I {} git update-index --chmod +x {}
1202+
fi
1203+
say "push" "$repo: committing"
1204+
git commit -am "$message"
1205+
1206+
say "push" "$repo: pushing"
1207+
git push
1208+
)
1209+
else
1210+
say "push" "$repo: skipping (not a git repo)"
1211+
fi
1212+
}
1213+
10941214
# src/lib/say.sh
10951215
say() {
10961216
printf "%-20s | %s\n" "$(magenta "$1")" "$(bold "${*:2}")"
10971217
}
10981218

1219+
# src/lib/search_repo.sh
1220+
search_repo() {
1221+
repo="$1"
1222+
text="$2"
1223+
set +e
1224+
1225+
repo_path=$(config_get "$repo")
1226+
1227+
prefix=''
1228+
[[ "$repo" != "default" ]] && prefix="$repo:"
1229+
1230+
grep_color_flag=""
1231+
if grep --help 2>&1 | grep -q -- "--color"; then
1232+
grep_color_flag="--color=always"
1233+
fi
1234+
1235+
bold "Matching packages ($repo):\n"
1236+
find "$repo_path" -type f -name main |
1237+
sed "s#${repo_path}/#${prefix}#g; s#/main##" |
1238+
grep $grep_color_flag --ignore-case -- "$text" |
1239+
sort
1240+
1241+
bold "\nMatching info files ($repo):\n"
1242+
find "$repo_path" -type f -name info -exec grep $grep_color_flag --ignore-case -n -- "$text" {} + |
1243+
sed "s#${repo_path}/#${prefix}#g; s#/info##" |
1244+
sort
1245+
1246+
echo
1247+
}
1248+
10991249
# src/lib/send_completions.sh
11001250
send_completions() {
11011251
echo $'# rush completion -*- shell-script -*-'
@@ -1391,18 +1541,6 @@ rush_pull_command() {
13911541
# src/commands/pull.sh
13921542
repo=${args[repo]}
13931543

1394-
pull_repo() {
1395-
local repo_path="$1"
1396-
local repo="$2"
1397-
1398-
if [[ -d "$repo_path/.git" ]]; then
1399-
say "pull" "$repo"
1400-
(cd "$repo_path" && git pull)
1401-
else
1402-
say "pull" "skipping $repo (not a git repo)"
1403-
fi
1404-
}
1405-
14061544
if [[ $repo ]]; then
14071545
repo_path=$(config_get "$repo")
14081546
[[ $repo_path ]] || abort "no such repo: $repo"
@@ -1424,34 +1562,6 @@ rush_push_command() {
14241562
repo=${args[repo]:-default}
14251563
message=${args[--message]:-"automatic commit"}
14261564

1427-
push_repo() {
1428-
local repo_path="$1"
1429-
local repo="$2"
1430-
1431-
if [[ -d "$repo_path/.git" ]]; then
1432-
say "push" "$repo"
1433-
(
1434-
set -e
1435-
cd "$repo_path"
1436-
1437-
say "push" "$repo: adding files"
1438-
git add . --all
1439-
1440-
if [[ -n "${args[--chmod]}" ]]; then
1441-
say "push" "$repo: applying chmod +x"
1442-
git ls-files | grep -E "undo|main" | xargs -I {} git update-index --chmod +x {}
1443-
fi
1444-
say "push" "$repo: committing"
1445-
git commit -am "$message"
1446-
1447-
say "push" "$repo: pushing"
1448-
git push
1449-
)
1450-
else
1451-
say "push" "$repo: skipping (not a git repo)"
1452-
fi
1453-
}
1454-
14551565
if [[ $all ]]; then
14561566
for k in $(config_keys); do
14571567
push_repo "$(config_get "$k")" "$k"
@@ -1706,80 +1816,6 @@ rush_info_command() {
17061816
rush_list_command() {
17071817

17081818
# src/commands/list.sh
1709-
list_display_item() {
1710-
package="$1"
1711-
infofile="$2"
1712-
repo="$3"
1713-
simple=${args[--simple]}
1714-
width=$((${COLUMNS:-80} + 9))
1715-
1716-
[[ "$repo" != "default" ]] && package="$repo:$package"
1717-
if [[ $simple ]]; then
1718-
printf "%s\n" "$package"
1719-
else
1720-
info=$(head -1 "$infofile" 2>/dev/null)
1721-
padded_package=$(printf "%-20s" "$package")
1722-
message="$(green "$padded_package") $info"
1723-
printf "%.${width}s\n" "$message"
1724-
fi
1725-
}
1726-
1727-
list_show_repo() {
1728-
local repo_or_package="$1"
1729-
local search="${args[--search]}"
1730-
local simple=${args[--simple]}
1731-
local all=${args[--all]}
1732-
local repo="$repo_or_package"
1733-
local package glob repo_path infofile regex package_name
1734-
1735-
if [[ $repo_or_package =~ (.*):(.*) ]]; then
1736-
repo=${BASH_REMATCH[1]}
1737-
package=${BASH_REMATCH[2]}
1738-
fi
1739-
1740-
repo_path=$(config_get "$repo")
1741-
1742-
if [[ ! $repo_path ]]; then
1743-
package="$repo"
1744-
repo="default"
1745-
repo_path=$(config_get "$repo")
1746-
fi
1747-
1748-
if [[ $package ]]; then
1749-
glob=("$repo_path"/"$package"/**/info)
1750-
else
1751-
if [[ $all ]]; then
1752-
glob_files=$(find "$repo_path" -type f -name 'info' | sort)
1753-
else
1754-
glob_files=$(find "$repo_path" -maxdepth 2 -type f -name 'info' | sort)
1755-
fi
1756-
readarray -t glob < <(echo "${glob_files[@]}")
1757-
fi
1758-
1759-
if [[ ${glob[0]} =~ .*\*.* ]]; then
1760-
infofile="$repo_path/$package/info"
1761-
if [[ -f "$infofile" ]]; then
1762-
list_display_item "$package" "$infofile" "$repo"
1763-
elif [[ ! $simple ]]; then
1764-
red "nothing in $repo repo"
1765-
fi
1766-
1767-
else
1768-
for infofile in "${glob[@]}"; do
1769-
if [[ $search ]]; then
1770-
regex="$repo_path/(.*${search}.*)/info"
1771-
else
1772-
regex="$repo_path/(.*)/info"
1773-
fi
1774-
1775-
if [[ $infofile =~ $regex ]]; then
1776-
package_name="${BASH_REMATCH[1]}"
1777-
list_display_item "$package_name" "$infofile" "$repo"
1778-
fi
1779-
done
1780-
fi
1781-
}
1782-
17831819
repo_or_package=${args[repo_or_package]}
17841820

17851821
if [[ $repo_or_package ]]; then
@@ -1796,35 +1832,6 @@ rush_list_command() {
17961832
rush_search_command() {
17971833

17981834
# src/commands/search.sh
1799-
search_repo() {
1800-
repo="$1"
1801-
text="$2"
1802-
set +e
1803-
1804-
repo_path=$(config_get "$repo")
1805-
1806-
prefix=''
1807-
[[ "$repo" != "default" ]] && prefix="$repo:"
1808-
1809-
grep_color_flag=""
1810-
if grep --help 2>&1 | grep -q -- "--color"; then
1811-
grep_color_flag="--color=always"
1812-
fi
1813-
1814-
bold "Matching packages ($repo):\n"
1815-
find "$repo_path" -type f -name main |
1816-
sed "s#${repo_path}/#${prefix}#g; s#/main##" |
1817-
grep $grep_color_flag --ignore-case -- "$text" |
1818-
sort
1819-
1820-
bold "\nMatching info files ($repo):\n"
1821-
find "$repo_path" -type f -name info -exec grep $grep_color_flag --ignore-case -n -- "$text" {} + |
1822-
sed "s#${repo_path}/#${prefix}#g; s#/info##" |
1823-
sort
1824-
1825-
echo
1826-
}
1827-
18281835
text=${args[text]}
18291836

18301837
for k in $(config_keys); do

0 commit comments

Comments
 (0)