Class: MangUpdate::Dsl

Inherits:
Object
  • Object
show all
Defined in:
lib/MangUpdate/dsl.rb

Constant Summary collapse

@@options =
nil
@@configs =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDsl

Returns a new instance of Dsl.



88
89
90
# File 'lib/MangUpdate/dsl.rb', line 88

def initialize
  @tmp = {}
end

Class Method Details

.evaluate_mupfile(options = {}) ⇒ Object

Evaluate the DSL methods in the ‘Mupfile`.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/MangUpdate/dsl.rb', line 11

def evaluate_mupfile(options = {})
  raise ArgumentError.new('No option hash passed to evaluate_mupfile!') unless options.is_a?(Hash)

  @@options = options.dup
  @@configs = {}

  fetch_mupfile_contents
  instance_eval_mupfile(mupfile_contents)
  
  @@configs.each do |key, value|
    @@options[key] = value unless @@options.key?(key)
  end
  @@options
end

.fetch_mupfile_contentsObject

Get the content to evaluate and stores it into the options as ‘:mupfile_contents`.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/MangUpdate/dsl.rb', line 45

def fetch_mupfile_contents
  if @@options[:mupfile_contents]
    UI.info 'Using inline Mupfile.'
    @@options[:mupfile_path] = 'Inline Mupfile'

  elsif @@options[:mupfile]
    if File.exist?(@@options[:mupfile])
      read_mupfile(@@options[:mupfile])
      UI.info "Using Mupfile at #{ @@options[:mupfile] }."
    else
      UI.error "No Mupfile exists at #{ @@options[:mupfile] }."
      exit 1
    end

  else
    if File.exist?(mupfile_default_path)
      read_mupfile(mupfile_default_path)
    else
      UI.error 'No Mupfile found, please create one with `mangupdate init`.'
      exit 1
    end
  end

  if mupfile_contents.empty?
    UI.error "The command file(#{ @@options[:mupfile] }) seems to be empty."
    exit 1
  end
end

.instance_eval_mupfile(contents) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/MangUpdate/dsl.rb', line 26

def instance_eval_mupfile(contents)
  new.instance_eval(contents, @@options[:mupfile_path], 1)
rescue
  UI.error "Invalid Mupfile, original error is:\n#{ $! }"
  raise if @@options[:debug]
  exit 1
end

.mupfile_contentsObject



74
75
76
# File 'lib/MangUpdate/dsl.rb', line 74

def mupfile_contents
  @@options ? @@options[:mupfile_contents] : ''
end

.mupfile_default_pathObject



82
83
84
# File 'lib/MangUpdate/dsl.rb', line 82

def mupfile_default_path
  File.join(Dir.pwd, 'Mupfile')
end

.mupfile_pathObject



78
79
80
# File 'lib/MangUpdate/dsl.rb', line 78

def mupfile_path
  @@options ? @@options[:mupfile_path] : ''
end

.read_mupfile(mupfile_path) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/MangUpdate/dsl.rb', line 34

def read_mupfile(mupfile_path)
  @@options[:mupfile_path]     = mupfile_path
  @@options[:mupfile_contents] = File.read(mupfile_path)
rescue
  UI.error("Error reading file #{ mupfile_path }")
  exit 1
end

Instance Method Details

#apply(options = {}) ⇒ Object



136
137
138
139
# File 'lib/MangUpdate/dsl.rb', line 136

def apply(options = {})
  raise "apply must be called inside of a job block" unless [:job, :filtered].member?(@tmp[:type])  
  @tmp[:applies] << [:list, options, @tmp[:filters] || []]
end

#build_listsObject



153
154
155
# File 'lib/MangUpdate/dsl.rb', line 153

def build_lists
  MangUpdate.build_lists
end

#config(*args) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/MangUpdate/dsl.rb', line 141

def config(*args)
  name = args.shift
  case args.size
  when 0
    @@configs[name.downcase.to_sym]
  when 1
    @@configs[name.downcase.to_sym] = args[0]
  else
    raise ArgumentError, "Wrong number of arguments!"
  end
end

#filter(&block) ⇒ Object



131
132
133
134
# File 'lib/MangUpdate/dsl.rb', line 131

def filter(&block)
  raise "filter must be called inside of a filtered block" unless @tmp[:type] == :filtered
  @tmp[:filters] << block
end

#filtered(options = { :clear => false }) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/MangUpdate/dsl.rb', line 122

def filtered(options = { :clear => false })
  raise "filtered must be called inside of a job block" unless [:job, :filtered].member?(@tmp[:type])
  filters_new = []
  filters_new = @tmp[:filters] if @tmp[:filters] && !options[:clear]      
  tmp_block({ :type => :filtered, :filters => filters_new, :jobname => @tmp[:jobname] }) do
    yield if block_given?
  end
end

#job(name) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/MangUpdate/dsl.rb', line 114

def job(name)
  # jobs are just for some extra groupping
  tmp_block({ :type => :job, :applies => [] }) do
    yield if block_given?
    MangUpdate.add_job(name, @tmp[:applies])
  end
end

#list(name) ⇒ Object



102
103
104
105
106
107
# File 'lib/MangUpdate/dsl.rb', line 102

def list(name)
  tmp_block({ :type => :list, :paths => [] }) do
    yield if block_given?
    MangUpdate.add_list(name, @tmp[:paths])
  end
end

#path(path, options = {}) ⇒ Object



109
110
111
112
# File 'lib/MangUpdate/dsl.rb', line 109

def path(path, options = {})
  raise "path must be called inside of a list block" unless @tmp[:type] == :list
  @tmp[:paths] << [path, options]
end

#tmp_block(merge_options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/MangUpdate/dsl.rb', line 92

def tmp_block(merge_options = {})
  return unless block_given?
  preserve_tmp = @tmp
  @tmp = {} if @tmp.nil? && merge_options
  @tmp = @tmp.merge(merge_options) if @tmp.respond_to?(:merge)
  val = yield
  @tmp = preserve_tmp
  val
end

#upload_file(filename, database, rev = nil) ⇒ Object



157
158
159
160
# File 'lib/MangUpdate/dsl.rb', line 157

def upload_file(filename, database, rev = nil)
  raise "upload_file must be called inside of a job block" unless @tmp[:type] == :job
  @tmp[:applies] << [:file, filename, database, rev]
end

#upload_sql_string(sql, database, rev = nil) ⇒ Object



162
163
164
165
# File 'lib/MangUpdate/dsl.rb', line 162

def upload_sql_string(sql, database, rev = nil)
  raise "upload_sql_string must be called inside of a job block" unless @tmp[:type] == :job
  @tmp[:applies] << [:inline, sql, database, rev]
end