From 0ca1707e1034cac0d062ed68a20613b6c2d43a9f Mon Sep 17 00:00:00 2001 From: iberianpig Date: Wed, 19 Feb 2025 00:29:03 +0900 Subject: [PATCH] feat: Enhance Gnome Extensions Installer - 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: https://github.com/iberianpig/fusuma/pull/332 closes: https://github.com/iberianpig/fusuma/issues/323 --- lib/fusuma/plugin/appmatcher.rb | 13 ++++- .../appmatcher/gnome_extensions/installer.rb | 43 ++++++++------- .../plugin/appmatcher/unsupported_backend.rb | 3 - .../gnome_extensions/installer_spec.rb | 55 +++++++++++++++++-- spec/fusuma/plugin/appmatcher_spec.rb | 10 ++-- 5 files changed, 88 insertions(+), 36 deletions(-) diff --git a/lib/fusuma/plugin/appmatcher.rb b/lib/fusuma/plugin/appmatcher.rb index 94d488f..ce3426d 100644 --- a/lib/fusuma/plugin/appmatcher.rb +++ b/lib/fusuma/plugin/appmatcher.rb @@ -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 diff --git a/lib/fusuma/plugin/appmatcher/gnome_extensions/installer.rb b/lib/fusuma/plugin/appmatcher/gnome_extensions/installer.rb index f2d78ae..543a5cf 100644 --- a/lib/fusuma/plugin/appmatcher/gnome_extensions/installer.rb +++ b/lib/fusuma/plugin/appmatcher/gnome_extensions/installer.rb @@ -11,24 +11,6 @@ module GnomeExtensions class Installer include UserSwitcher - EXTENSION = "./appmatcher@iberianpig.dev" - - 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 - "./appmatcher45@iberianpig.dev" - else - "./appmatcher@iberianpig.dev" - end - else - "./appmatcher@iberianpig.dev" - end - end - def install pid = as_user(proctitle: self.class.name.underscore) do |user| FileUtils.cp_r(source_path, install_path(user.username)) @@ -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 = "appmatcher@iberianpig.dev" + EXTENSION45 = "appmatcher45@iberianpig.dev" 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 diff --git a/lib/fusuma/plugin/appmatcher/unsupported_backend.rb b/lib/fusuma/plugin/appmatcher/unsupported_backend.rb index 9bfa3a3..8c49f10 100644 --- a/lib/fusuma/plugin/appmatcher/unsupported_backend.rb +++ b/lib/fusuma/plugin/appmatcher/unsupported_backend.rb @@ -29,9 +29,6 @@ def watch_start end class Matcher - def initialize - end - def running_applications warn nil diff --git a/spec/fusuma/plugin/appmatcher/gnome_extensions/installer_spec.rb b/spec/fusuma/plugin/appmatcher/gnome_extensions/installer_spec.rb index 074b225..47f57b3 100644 --- a/spec/fusuma/plugin/appmatcher/gnome_extensions/installer_spec.rb +++ b/spec/fusuma/plugin/appmatcher/gnome_extensions/installer_spec.rb @@ -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("['appmatcher@iberianpig.dev']") + 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 diff --git a/spec/fusuma/plugin/appmatcher_spec.rb b/spec/fusuma/plugin/appmatcher_spec.rb index edea2f0..958c3ba 100644 --- a/spec/fusuma/plugin/appmatcher_spec.rb +++ b/spec/fusuma/plugin/appmatcher_spec.rb @@ -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