Skip to content

Commit

Permalink
refactoring clients
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Sep 22, 2015
1 parent 77c15b1 commit 2766e32
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 237 deletions.
23 changes: 1 addition & 22 deletions src/clients/migration_finish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,6 @@
# ------------------------------------------------------------------------------
#

require "yast"
require "registration/ui/migration_finish_workflow"

module Yast
class MigrationFinishClient < Client
Yast.import "Wizard"

def main
textdomain "registration"

# create the Wizard dialog if needed
wizard_present = Wizard.IsWizardDialog
Wizard.CreateDialog unless wizard_present

begin
::Registration::UI::MigrationFinishWorkflow.run
ensure
Wizard.CloseDialog unless wizard_present
end
end
end unless defined?(YaST::MigrationFinishClient)
end

Yast::MigrationFinishClient.new.main
Registration::UI::MigrationFinishWorkflow.new.main
23 changes: 1 addition & 22 deletions src/clients/migration_repos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,6 @@
# ------------------------------------------------------------------------------
#

require "yast"
require "registration/ui/migration_repos_workflow"

module Yast
class MigrationReposClient < Client
Yast.import "Wizard"

def main
textdomain "registration"

# create the Wizard dialog if needed
wizard_present = Wizard.IsWizardDialog
Wizard.CreateDialog unless wizard_present

begin
::Registration::UI::MigrationReposWorkflow.run
ensure
Wizard.CloseDialog unless wizard_present
end
end
end unless defined?(YaST::MigrationReposClient)
end

Yast::MigrationReposClient.new.main
Registration::UI::MigrationReposWorkflow.new.main
23 changes: 1 addition & 22 deletions src/clients/registration_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,6 @@
# ------------------------------------------------------------------------------
#

require "yast"
require "registration/ui/registration_sync_workflow"

module Yast
class RegistrationSyncClient < Client
Yast.import "Wizard"

def main
textdomain "registration"

# create the Wizard dialog if needed
wizard_present = Wizard.IsWizardDialog
Wizard.CreateDialog unless wizard_present

begin
::Registration::UI::RegistrationSyncWorkflow.run
ensure
Wizard.CloseDialog unless wizard_present
end
end
end unless defined?(YaST::RegistrationSyncClient)
end

Yast::RegistrationSyncClient.new.main
Registration::UI::RegistrationSyncWorkflow.new.main
56 changes: 3 additions & 53 deletions src/lib/registration/ui/migration_finish_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,69 +12,19 @@
# ------------------------------------------------------------------------------
#

require "yast"

require "registration/repo_state"
require "registration/ui/wizard_client"

module Registration
module UI
# This class handles the workflow for finishing the online migration
class MigrationFinishWorkflow
include Yast::Logger
include Yast::I18n
include Yast::UIShortcuts

Yast.import "Report"
Yast.import "Sequencer"

# run workflow for adding the migration services
# @return [Symbol] the UI symbol
def self.run
workflow = MigrationFinishWorkflow.new
workflow.run
end

# the constructor
def initialize
textdomain "registration"
end

class MigrationFinishWorkflow < WizardClient
# Run the workflow.
# The migration finish workflow is:
# - restore the saved repository states (i.e. enable the Updates
# repositories when they were disabled during migration)
def run
run_sequence
rescue => e
log.error "Caught error: #{e.class}: #{e.message.inspect}, #{e.backtrace}"
# TRANSLATORS: error message, %s are details
Yast::Report.Error(_("Internal error: %s") % e.message)
return :abort
end

private

WORKFLOW_SEQUENCE = {
"ws_start" => "restore_repos",
"restore_repos" => {
abort: :abort,
next: :next
}
}

# run the workflow
# @return [Symbol] the UI symbol
def run_sequence
aliases = {
"restore_repos" => ->() { restore_repos }
}

ui = Yast::Sequencer.Run(aliases, WORKFLOW_SEQUENCE)
log.info "Workflow result: #{ui}"
ui
end

# restore all saved repository states
def restore_repos
log.info "Restoring the original repository setup..."
repo_state = RepoStateStorage.instance
repo_state.read
Expand Down
57 changes: 16 additions & 41 deletions src/lib/registration/ui/migration_repos_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,18 @@
require "registration/releasever"
require "registration/sw_mgmt"
require "registration/url_helpers"
require "registration/ui/wizard_client"
require "registration/ui/migration_selection_dialog"
require "registration/ui/migration_repos_selection_dialog"

module Registration
module UI
# This class handles workflow for adding migration services
class MigrationReposWorkflow
include Yast::Logger
include Yast::I18n
class MigrationReposWorkflow < WizardClient
include Yast::UIShortcuts

Yast.import "Report"
Yast.import "Sequencer"

# run workflow for adding the migration services
# @return [Symbol] the UI symbol
def self.run
workflow = MigrationReposWorkflow.new
workflow.run
end

# the constructor
def initialize
textdomain "registration"
Expand Down Expand Up @@ -68,20 +59,23 @@ def initialize
# - return the user input symbol (:next or :abort) to the caller
# @return [Symbol] the UI symbol
#
def run
def run_sequence
log.info "Starting migration repositories sequence"

ret = nil
begin
ret = run_sequence
rescue => e
log.error "Caught error: #{e.class}: #{e.message.inspect}, #{e.backtrace}"
# TRANSLATORS: error message, %s are details
Yast::Report.Error(_("Internal error: %s") % e.message)
ret = :abort
end
aliases = {
"find_products" => [->() { find_products }, true],
"load_migration_products" => [->() { load_migration_products }, true],
"select_migration_products" => ->() { select_migration_products },
"update_releasever" => ->() { update_releasever },
"register_migration_products" => [->() { register_migration_products }, true],
"activate_migration_repos" => [->() { activate_migration_repos }, true],
"select_migration_repos" => ->() { select_migration_repos },
"store_repos_state" => ->() { store_repos_state }
}

ret
ui = Yast::Sequencer.Run(aliases, WORKFLOW_SEQUENCE)
log.info "User input: #{ui}"
ui
end

private
Expand Down Expand Up @@ -131,25 +125,6 @@ def run
}
}

# run the workflow
# @return [Symbol] the UI symbol
def run_sequence
aliases = {
"find_products" => [->() { find_products }, true],
"load_migration_products" => [->() { load_migration_products }, true],
"select_migration_products" => ->() { select_migration_products },
"update_releasever" => ->() { update_releasever },
"register_migration_products" => [->() { register_migration_products }, true],
"activate_migration_repos" => [->() { activate_migration_repos }, true],
"select_migration_repos" => ->() { select_migration_repos },
"store_repos_state" => ->() { store_repos_state }
}

ui = Yast::Sequencer.Run(aliases, WORKFLOW_SEQUENCE)
log.info "User input: #{ui}"
ui
end

# find all installed products
# @return [Symbol] workflow symbol (:next or :abort)
def find_products
Expand Down
35 changes: 8 additions & 27 deletions src/lib/registration/ui/registration_sync_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,22 @@

require "yast"

require "registration/registration"
require "registration/registration_ui"
require "registration/sw_mgmt"
require "registration/url_helpers"
require "registration/ui/wizard_client"

module Registration
module UI
# This class handles synchronizing the repositories/services after abort
class RegistrationSyncWorkflow
include Yast::Logger
include Yast::I18n
class RegistrationSyncWorkflow < WizardClient
include Yast::UIShortcuts

Yast.import "Report"
Yast.import "Pkg"
Yast.import "Update"
Yast.import "Installation"

# run the registration synchronization
# @return [Symbol] the UI symbol (:next on sucess, :abort on error)
def self.run
workflow = RegistrationSyncWorkflow.new
workflow.run
end

# the constructor
def initialize
textdomain "registration"
Expand All @@ -46,24 +38,9 @@ def initialize
self.registration_ui = RegistrationUI.new(registration)
end

# run the registration synchronization
# @return [Symbol] the UI symbol (:next on sucess, :abort on error)
def run
rollback
rescue => e
log.error "Caught error: #{e.class}: #{e.message.inspect}, #{e.backtrace}"
# TRANSLATORS: error message, %s are details
Yast::Report.Error(_("Internal error: %s") % e.message)
return :abort
end

private

attr_accessor :registration_ui

# restore the registration status
# @return [Symbol] :next on sucess, :abort on error
def rollback
def run_sequence
log.info "Restoring the original repository and registration status..."

restore_repos
Expand All @@ -81,6 +58,10 @@ def rollback
registration_ui.synchronize_products(products) ? :next : :abort
end

private

attr_accessor :registration_ui

# restore the repositpories from the backup archive
def restore_repos
# finish the sources and the target to reload the repositories from the backup
Expand Down
68 changes: 68 additions & 0 deletions src/lib/registration/ui/wizard_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# ------------------------------------------------------------------------------
# Copyright (c) 2015 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# ------------------------------------------------------------------------------
#

require "yast"

module Registration
module UI
# The base class for several clients using the Wizard module
# with exception handling.
class WizardClient
include Yast::I18n
include Yast::Logger

Yast.import "Wizard"
Yast.import "Report"

def initialize
textdomain "registration"
end

# Run the main workflow, opens a Wizard dialog if not present
# and handles raised exceptions
# @return [Symbol] resulting workflow symbol (:next, :abort, ...)
def main
# create the Wizard dialog if needed
wizard_present = Yast::Wizard.IsWizardDialog
Yast::Wizard.CreateDialog unless wizard_present

begin
run
ensure
Yast::Wizard.CloseDialog unless wizard_present
end
end

# Run the main workflow, handles exceptions raised during the client call.
# @return [Symbol] resulting workflow symbol (:next, :abort, ...), :abort
# is returned when an exception is raised.
def run
ret = run_sequence
log.info "Sequence result: #{ret}"
ret
rescue => e
log.error "Caught error: #{e.class}: #{e.message.inspect}, #{e.backtrace}"
# TRANSLATORS: error message, %s are details
Yast::Report.Error(_("Internal error: %s") % e.message)
return :abort
end

# This is main workflow sequence, it needs to be redefined in the derived class.
# @return [Symbol] resulting workflow symbol (:next, :abort, ...)
def run_sequence
raise NotImplementedError, "method run_sequence() must be defined in the derived class"
end
end
end
end
Loading

2 comments on commit 2766e32

@imobachgs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, nice refactoring :)

@lslezak
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, CodeClimate complained about the code duplication quite a lot so I was kind of forced to improve it... 😉

Please sign in to comment.