Sunday, May 1, 2016

Configuring Gnome-Shell from the command line

Here's some useful tricks to automate the configuration of the gnome shell

Get your current config

Installed Extensions

So you have a bunch of extensions installed and configured how you like them.. but now you need to reinstall, got a new computer, whatever — you need to back it all up so you can get back to the way it was right away!

First get a list of all your extensions by uuid:

# all installed extensions
ls -1 ~/.local/share/gnome-shell/extensions/

# only the ones that are enabled and not installed at
# /usr/share/gnome-shell/extensions
diff --new-line-format="%L" --old-line-format="" \
    --unchanged-line-format="" \
    <(ls /usr/share/gnome-shell/extensions) \
    <(gsettings get org.gnome.shell enabled-extensions \
      | sed -e "s/\['\(.*\)'\]/\1/" -e "s/', '/\n/g") || true

gsettings get org.gnome.shell enabled-extensions | sed -e "s/\['\(.*\)'\]/\1/" -e "s/', '/\n/g"

Extension Settings

Next you'll need the settings for your extensions as well. To get a list of all extension settings do the following:

dconf dump /org/gnome/shell/extensions/

This list can then be piped back into dconf load /org/gnome/shell/extensions/ later to restore all your settings.

Export extensions and settings to file

Run this to get a backup of your extension setup:

diff --new-line-format="%L" --old-line-format="" \
    --unchanged-line-format="" \
    <(ls /usr/share/gnome-shell/extensions) \
    <(gsettings get org.gnome.shell enabled-extensions \
    | sed -e "s/\['\(.*\)'\]/\1/" -e "s/', '/\n/g") \
    | sort >extensions-enabled \
&& ls -1 ~/.local/share/gnome-shell/extensions/ >extensions-installed \
&& dconf dump /org/gnome/shell/extensions/ >extensions-settings

Creates three files for installed extensions, enabled extensions and settings.

Install extensions

Having to find and click on all your extensions to install them from the website can be a real pain in the ass. Luckily, Nicolas Bernaerts has a great little write up that can get us up to speed on how to do what we want.. With some minor tweaking of course.

Let's start with some simple bash functions to help us out here. The first one we'll need should get all the info for the extension that we'll need to install it — namely the download link.

gv=$(gnome-shell --version | cut -d' ' -f3)
base_url="https://extensions.gnome.org"

get_dl_link () {
    # args: the uuid of the extension (eg removeaccesibility@lomegor)
    info_url="${base_url}/extension-info/?uuid=${1}&shell_version=${gv}"
    echo "${base_url}$(curl "$info_url" \
        | sed -e 's/.*"download_url": "\([^"]*\)".*/\1/')"
}

# test with our uuid:
get_dl_link removeaccesibility@lomegor

Now that we have an url, we can download it and unzip the files to our extensions folder:

install () {
    # args: the uuid to download to
    temp=$(mktemp -d)
    uuid="$1"
    url=$(get_dl_link "$uuid")
    dest="$HOME/.local/share/gnome-shell/extensions/$uuid"
    curl -L "$url" > "$temp/e.zip"
    unzip "$temp/e.zip" -d "$dest"
    rm -rfv "$temp"
    gnome-shell-extension-tool --enable-extension="$uuid"
}

Next we'll need to enable the extension and then restart gnome shell:

gnome-shell-extension-tool --enable-extension=UUID
gnome-shell --replace &>/dev/null & disown

Removing an extenstion is also pretty easy. We just need to disable it, delete the files and optionally delete the settings:

remove () {
    # args: the uuid to remove
    uuid="$1"
    gnome-shell-extension-tool --disable-extension="$uuid"
    rm -rfv "$HOME/.local/share/gnome-shell/extensions/$uuid"
}

Here's the whole setup:

gv=$(gnome-shell --version | cut -d' ' -f3)
base_url="https://extensions.gnome.org"

get_dl_link () {
    # args: the uuid of the extension (eg removeaccesibility@lomegor)
    info_url="${base_url}/extension-info/?uuid=${1}&shell_version=${gv}"
    echo "${base_url}$(curl "$info_url" \
        | sed -e 's/.*"download_url": "\([^"]*\)".*/\1/')"
}

install () {
    # args: the uuid to download to
    temp=$(mktemp -d)
    uuid="$1"
    url=$(get_dl_link "$uuid")
    dest="$HOME/.local/share/gnome-shell/extensions/$uuid"
    curl -L "$url" > "$temp/e.zip"
    unzip "$temp/e.zip" -d "$dest"
    rm -rfv "$temp"
    gnome-shell-extension-tool --enable-extension="$uuid"
}

remove () {
    # args: the uuid to remove
    uuid="$1"
    gnome-shell-extension-tool -d "$uuid"
    rm -rfv "$HOME/.local/share/gnome-shell/extensions/$uuid"
}

# test with our uuid:
tdir=$(mktemp -d)
install files-menu
rm -rf "$tdir"
gnome-shell --replace &>/dev/null & disown
echo "press anykey to remove.."; read
remove files-menu
gnome-shell --replace &>/dev/null & disown

No comments :