Class: Falsework::Upgrader

Inherits:
Object
  • Object
show all
Defined in:
lib/falsework/upgrader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, note = Mould::NOTE) ⇒ Upgrader

Returns a new instance of Upgrader.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/falsework/upgrader.rb', line 39

def initialize dir, note = Mould::NOTE
  fail UpgradeError, "directory #{dir} is unreadable" unless File.readable?(dir.to_s)
  @dir = Pathname.new File.realpath(dir)

  begin
    @note = Upgrader.noteLoad(@dir + note)
  rescue
    raise UpgradeError, $!
  end

  @mould = Mould.new @note['project']['classy'], @note['template']['name']
  @template_dir = Mould.templates[@note['template']['name']]
  @project = @mould.project

  @batch = false
end

Instance Attribute Details

#batchObject

Returns the value of attribute batch.



56
57
58
# File 'lib/falsework/upgrader.rb', line 56

def batch
  @batch
end

#projectObject (readonly)

Returns the value of attribute project.



57
58
59
# File 'lib/falsework/upgrader.rb', line 57

def project
  @project
end

#template_dirObject (readonly)

Returns the value of attribute template_dir.



58
59
60
# File 'lib/falsework/upgrader.rb', line 58

def template_dir
  @template_dir
end

Class Method Details

.noteLoad(file = Mould::NOTE) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/falsework/upgrader.rb', line 20

def self.noteLoad file = Mould::NOTE
  r = YAML.load_file(file) rescue raise

  ['project', 'template'].each {|idx|
    fail "no #{idx} spec" unless r[idx]
  }
  
  fail 'no project name' unless Utils.all_set?(r['project']['classy'])
  fail "no template version" unless Utils.all_set?(r['template']['version'])
  r['template']['version'] = Gem::Version.new r['template']['version']
  fail "no template name" unless Utils.all_set?(r['template']['name'])

  unless Mould.templates[r['template']['name']]
    fail "unknown template '#{r['template']['name']}'"
  end

  r
end

Instance Method Details

#able?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
# File 'lib/falsework/upgrader.rb', line 64

def able?
  return false unless @mould.conf['upgrade']
  return false if Gem::Version.new(@mould.conf['upgrade']['from']) > @note['template']['version']
  return false unless @mould.conf['upgrade']['files'].is_a?(Array) && @mould.conf['upgrade']['files'].size > 0
  true
end

#filesObject



71
72
73
# File 'lib/falsework/upgrader.rb', line 71

def files
  @mould.conf['upgrade']['files']
end

#getProjectBindingObject



60
61
62
# File 'lib/falsework/upgrader.rb', line 60

def getProjectBinding
  @mould.getBinding
end

#obsoleteObject



75
76
77
# File 'lib/falsework/upgrader.rb', line 75

def obsolete
  @mould.conf['upgrade']['obsolete'] || []
end

#upgrade(save_old = false) ⇒ Object

yield f



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/falsework/upgrader.rb', line 98

def upgrade save_old = false # yield f
  fail UpgradeError, "this project cannot be upgraded" unless able?

  at_least_one = false
  files.each {|idx|
    f = Mould.resolve_filename idx, @mould.getBinding

    next unless userSaidYes 'update', f
    
    FileUtils.mv f, f + '.orig' if save_old && File.exist?(f)
    Mould.extract @template_dir + idx, @mould.getBinding, f

    # say 'opa-popa was updated'
    yield f if block_given?
    at_least_one = true
  }

  # update a note file
  @mould.noteCreate true if at_least_one
end

#userSaidYes(msg, file) ⇒ Object

Return true if user enter ‘y’ or ‘a’, false otherwise. Always return true for non-batch mode.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/falsework/upgrader.rb', line 81

def userSaidYes msg, file
  return true if @batch
  
  print "#{msg} '#{file}'? [y/n/a] "
  asnwer = $stdin.gets
  
  if asnwer =~ /^a/i
    @batch = true
    return true
  end
  
  yes = (asnwer =~ /^y/i)
  return true if yes

  false
end