Class: PullBundle::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/pull-bundle/repository.rb

Defined Under Namespace

Classes: CommandException

Constant Summary collapse

ERRORS =
['ERROR', 'fatal']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Repository

Returns a new instance of Repository.



11
12
13
14
# File 'lib/pull-bundle/repository.rb', line 11

def initialize(options = {})
  @name = options[:name]
  @address = "#{options[:base]}/#{@name}"
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/pull-bundle/repository.rb', line 7

def name
  @name
end

Instance Method Details

#include_error_message?(message) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/pull-bundle/repository.rb', line 60

def include_error_message?(message)
  not ERRORS.reduce(true) { |meta, error| meta and not message.include?(error) }
end

#install_gemsObject



38
39
40
# File 'lib/pull-bundle/repository.rb', line 38

def install_gems
  run_command 'bundle install', 'Your bundle is complete!', CommandException.new('Could not install gems.')
end

#is_clean?Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/pull-bundle/repository.rb', line 42

def is_clean?
  run_command 'git status', 'working directory clean', CommandException.new('Not a clean directory.')
  true
end

#is_git_repository?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/pull-bundle/repository.rb', line 34

def is_git_repository?
  Dir.exist?("#{@address}//.git")
end

#is_ruby_repository?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/pull-bundle/repository.rb', line 30

def is_ruby_repository?
  File.exist?("#{@address}//Gemfile")
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pull-bundle/repository.rb', line 16

def run
  if is_git_repository? and is_clean?
    update
  else
    raise CommandException.new('Not a clean git repository.')
  end

  if is_ruby_repository?
    install_gems
  else
    raise CommandException.new('Not a ruby repository.')
  end
end

#run_command(command, condition, exception) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/pull-bundle/repository.rb', line 52

def run_command(command, condition, exception)
  Dir.chdir(@address)
  status = Open4::popen4(command)
  message = status[2].read
  error = status[3].read
  raise exception if not message.include?(condition) or not error.empty? or include_error_message?(message)
end

#updateObject



47
48
49
50
# File 'lib/pull-bundle/repository.rb', line 47

def update
  run_command 'git pull', '', CommandException.new('Could not update repository.')
  run_command 'git status', 'working directory clean', CommandException.new('Could not update repository.')
end