-
Notifications
You must be signed in to change notification settings - Fork 4
/
pacmanfile
executable file
·114 lines (89 loc) · 2.37 KB
/
pacmanfile
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
#!/bin/bash
set -u
usage_msg='
NAME:
pacmanfile - Manage your pacman packages declaratively
SYNOPSIS:
pacmanfile <operations> [options]
OPERATIONS:
help Show this help
dump Dump your installed packages into a pacmanfile
sync Synchronize your installed packages with your pacmanfile
OPTIONS:
--noconfirm Install and uninstall without asking for confirmation
'
package_manager=pacman
if [ -x "$(command -v yay)" ]; then
package_manager=yay
fi
if [ -x "$(command -v paru)" ]; then
package_manager=paru
fi
config_dir=${XDG_CONFIG_HOME:-$HOME/.config}
pacmanfile_dir="${config_dir}/pacmanfile"
# Parse arguments
operation=help
option_confirm='--confirm'
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
dump)
operation=dump
shift
;;
sync)
operation=sync
shift
;;
--noconfirm)
option_confirm='--noconfirm'
shift
;;
*)
shift
;;
esac
done
function dump {
dump_file="${pacmanfile_dir}/pacmanfile-dumped.txt"
installed_packages=$($package_manager --query --explicit --quiet | sort --unique)
# Create config folder if not present
if [ ! -d "$pacmanfile_dir" ]; then
mkdir --parents "$pacmanfile_dir"
fi
echo "$installed_packages" >"$dump_file"
echo "Installed packages dumped to $dump_file"
}
function sync {
pacmanfile_packages=$(sort --unique "${pacmanfile_dir}"/pacmanfile*.txt | grep -E '^[^#].*')
installed_packages=$($package_manager --query --explicit --quiet | sort --unique)
to_install=$(diff --new-line-format="" --unchanged-line-format="" <(echo "$pacmanfile_packages") <(echo "$installed_packages"))
if [ -n "$to_install" ]; then
# shellcheck disable=SC2086
$package_manager "$option_confirm" --sync --needed $to_install
fi
to_remove=$(diff --new-line-format="" --unchanged-line-format="" <(echo "$installed_packages") <(echo "$pacmanfile_packages"))
if [ -n "$to_remove" ]; then
# shellcheck disable=SC2086
$package_manager "$option_confirm" --remove --nosave --recursive $to_remove
fi
if [ -z "$to_install" ] && [ -z "$to_remove" ]; then
echo "Everything is synced"
fi
}
case $operation in
dump)
dump "$@"
;;
sync)
sync "$@"
;;
help)
echo "$usage_msg"
exit 0
;;
*)
echo "$usage_msg"
exit 1
;;
esac