Skip to content

Commit

Permalink
feat: Enhance Gnome Extensions Installer
Browse files Browse the repository at this point in the history
- Added `enabled?` method to check if the Gnome Shell extension is enabled.
- Improved `backend_klass` method to log a warning if the Gnome extension is not enabled.
- Updated the installer path handling for better clarity and maintainability.
- Added RSpec tests for the new `enabled?` method, covering scenarios for when the extension is enabled and when it is not.
- Enhanced existing tests for install and uninstall methods to ensure they correctly reflect the extension management logic.

ref: iberianpig/fusuma#332
closes: iberianpig/fusuma#323
  • Loading branch information
iberianpig committed Feb 18, 2025
1 parent 1f34f45 commit 0ca1707
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 36 deletions.
13 changes: 10 additions & 3 deletions lib/fusuma/plugin/appmatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ def backend_klass
when /wayland/
case xdg_current_desktop
when /GNOME/
return GnomeExtension if GnomeExtensions::Installer.new.installed?

return Gnome
if GnomeExtensions::Installer.new.enabled?
return GnomeExtension
else
MultiLogger.warn "Appmatcher Gnome Shell Extension is NOT enabled"
MultiLogger.warn "Please enable it by running the following command:"
MultiLogger.warn ""
MultiLogger.warn "$ fusuma-appmatcher --install-gnome-extension"
MultiLogger.warn ""
end
end
end

MultiLogger.warn "appmatcher doesn't support"
UnsupportedBackend
end

Expand Down
43 changes: 24 additions & 19 deletions lib/fusuma/plugin/appmatcher/gnome_extensions/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@ module GnomeExtensions
class Installer
include UserSwitcher

EXTENSION = "./[email protected]"

def gnome_shell_extension_path
output = `gnome-shell --version`
version = output.match(/GNOME Shell (\d+\.\d+)/)

if version
version_number = version[1].to_f
if version_number >= 45.0
"./[email protected]"
else
"./[email protected]"
end
else
"./[email protected]"
end
end

def install
pid = as_user(proctitle: self.class.name.underscore) do |user|
FileUtils.cp_r(source_path, install_path(user.username))
Expand All @@ -52,18 +34,41 @@ def installed?
File.exist?(install_path)
end

def enabled?
enabled_extensions = `gsettings get org.gnome.shell enabled-extensions`.strip
enabled_extensions.include?(EXTENSION)
end

private

def user_extension_dir(username = login_username)
File.expand_path("#{Dir.home(username)}/.local/share/gnome-shell/extensions/")
end

EXTENSION = "[email protected]"
EXTENSION45 = "[email protected]"
def install_path(username = login_username)
File.expand_path("#{Dir.home(username)}/.local/share/gnome-shell/extensions/#{EXTENSION}")
end

def source_path
File.expand_path(gnome_shell_extension_path, __dir__)
File.expand_path(gnome_shell_extension_filename, __dir__)
end

def gnome_shell_extension_filename
output = `gnome-shell --version`
version = output.match(/GNOME Shell (\d+\.\d+)/)

if version
version_number = version[1].to_f
if version_number >= 45.0
EXTENSION45
else
EXTENSION
end
else
EXTENSION
end
end

def login_username
Expand Down
3 changes: 0 additions & 3 deletions lib/fusuma/plugin/appmatcher/unsupported_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ def watch_start
end

class Matcher
def initialize
end

def running_applications
warn
nil
Expand Down
55 changes: 49 additions & 6 deletions spec/fusuma/plugin/appmatcher/gnome_extensions/installer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,62 @@ module Plugin
module Appmatcher
module GnomeExtensions
RSpec.describe Installer do
let(:installer) { described_class.new }
let(:user) { UserSwitcher::User.new("user") }

describe "#install" do
it "should copy file to users dir"
before do
allow(Process).to receive(:waitpid)
allow(installer).to receive(:as_user).and_yield(user) do |block_context|
allow(block_context).to receive(:source_path).and_return("source_path")
allow(block_context).to receive(:install_path).with(user.username).and_return("install_path")
allow(block_context).to receive(:user_extension_dir).with(user.username)
end
end
it "should copy file to user's dir" do
expect(FileUtils).to receive(:cp_r).with(any_args)
installer.install
end
end

describe "#uninstall" do
context "when extension is NOT installed" do
it "should remove file to users dir"
before do
allow(Process).to receive(:waitpid)
allow(installer).to receive(:as_user).and_yield(user) do |block_context|
allow(block_context).to receive(:source_path).and_return("source_path")
allow(block_context).to receive(:install_path).with(user.username).and_return("install_path")
allow(block_context).to receive(:user_extension_dir).with(user.username)
end
end

context "when extension is installed" do
it "should remove file to user's dir" do
expect(FileUtils).to receive(:rm_r).with(any_args)
installer.uninstall
end
end

context "when extension is NOT installed" do
it "should NOT execute"
before do
allow(installer).to receive(:installed?).and_return(false)
end
it "should NOT execute" do
expect(installer).not_to receive(:as_user)
installer.uninstall
end
end
end
describe "#installed?" do
it "check extension is in the installed path"

describe "#enabled?" do
it "returns true if the gnome extension is enabled" do
allow(installer).to receive(:`).with("gsettings get org.gnome.shell enabled-extensions").and_return("['[email protected]']")
expect(installer.enabled?).to be true
end

it "returns false if the gnome extension is not enabled" do
allow(installer).to receive(:`).with("gsettings get org.gnome.shell enabled-extensions").and_return("[]")
expect(installer.enabled?).to be false
end
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/fusuma/plugin/appmatcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ module Plugin
context "when XDG_CURRENT_DESKTOP is ubuntu:GNOME" do
before { allow(Appmatcher).to receive(:xdg_current_desktop).and_return("ubuntu:GNOME") }

context "when gnome-extension is installed" do
context "when gnome-extension is enabled" do
before do
allow_any_instance_of(Appmatcher::GnomeExtensions::Installer).to receive(:installed?).and_return(true)
allow_any_instance_of(Appmatcher::GnomeExtensions::Installer).to receive(:enabled?).and_return(true)
end
it { is_expected.to eq Appmatcher::GnomeExtension }
end

context "when gnome-extension is NOT installed" do
context "when gnome-extension is NOT enabled" do
before do
allow_any_instance_of(Appmatcher::GnomeExtensions::Installer).to receive(:installed?).and_return(false)
allow_any_instance_of(Appmatcher::GnomeExtensions::Installer).to receive(:enabled?).and_return(false)
end
it { is_expected.to eq Appmatcher::Gnome }
it { is_expected.to eq Appmatcher::UnsupportedBackend }
end
end

Expand Down

0 comments on commit 0ca1707

Please sign in to comment.