Skip to content

Commit

Permalink
Merge pull request teracyhq#573 from hoatle/improvements/teracyhq#572-…
Browse files Browse the repository at this point in the history
…add-location-manager.add_sync

@ teracyhq#572 | should add Location::Manager.add_synch for extension support
  • Loading branch information
hoatle authored Dec 3, 2018
2 parents 06e3baf + 2d86294 commit 97f1a49
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
3 changes: 3 additions & 0 deletions lib/teracy-dev.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
require_relative 'teracy-dev/logging'
require_relative 'teracy-dev/loader'
require_relative 'teracy-dev/extension/manager'
require_relative 'teracy-dev/location/manager'

require_relative 'teracy-dev/logging/progname_acceptor'
require_relative 'teracy-dev/logging/mask_filter'
require_relative 'teracy-dev/location/git_synch'


# define public APIs here
Expand Down Expand Up @@ -41,6 +43,7 @@ def self.register_configurator(configurator, weight = 5)
def self.init
Logging.add_acceptor(Logging::PrognameAcceptor.new)
Logging.add_filter(Logging::MaskFilter.new)
Location::Manager.add_synch(Location::GitSynch.new)
@@loader.start
end

Expand Down
14 changes: 7 additions & 7 deletions lib/teracy-dev/location/git_synch.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
require_relative '../logging'
require 'open3'

require_relative '../logging'
require_relative './synch'


module TeracyDev
module Location
class GitWarn < StandardError
end

class GitSynch
class GitSynch < Synch

def initialize
@logger = TeracyDev::Logging.logger_for(self.class.name)
end

def sync(location, sync_existing)
def sync(location_conf, sync_existing)
begin
start(location, sync_existing)
start(location_conf, sync_existing)
rescue GitWarn => e
@logger.warn(e)

Expand Down
21 changes: 15 additions & 6 deletions lib/teracy-dev/location/manager.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
require_relative '../logging'

require_relative 'git_synch'

module TeracyDev
module Location
class Manager
@@synch_list = [GitSynch.new]
@@synch_list = []
@@logger = TeracyDev::Logging.logger_for(self)

# return true if sync action is carried out, otherwise, return false
def self.sync(location, sync_existing = true, force = false)
def self.sync(location_conf, sync_existing = true, force = false)
updated = false
timer_start = Time.now
if !force && !sync_required?
return false
end
@@synch_list.each do |synch|
if synch.sync(location, sync_existing) == true
if synch.sync(location_conf, sync_existing) == true
updated = true
end
end
timer_end = Time.now
@@logger.debug("sync finished in #{timer_end - timer_start}s with updated: #{updated}, location: #{location}")
@@logger.debug("sync finished in #{timer_end - timer_start}s with updated: #{updated}, location_conf: #{location_conf}")
updated
end

# add synchronization which implements sync method
# @since v0.6.0-b1
def self.add_synch(synch)
if synch.respond_to?(:sync)
@@synch_list << synch
@@logger.debug("synch: #{synch} added")
else
@@logger.warn("#{synch} ignored, must implement sync method")
end
end

def self.sync_required?
return ARGV.include?('up') || ARGV.include?('status') || ARGV.include?('reload')
end
Expand Down
18 changes: 18 additions & 0 deletions lib/teracy-dev/location/synch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require_relative '../logging'

module TeracyDev
module Location
class Synch
def initialize
@logger = TeracyDev::Logging.logger_for(self.class.name)
end

protected
# to be implemented by subclass
# return true if sync action is carried out, otherwise, return false
def sync(location_conf, sync_existing)
return false
end
end
end
end

0 comments on commit 97f1a49

Please sign in to comment.