Class: Hiptest::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/hiptest-publisher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, listeners: nil, exit_on_bad_arguments: true) ⇒ Publisher

Returns a new instance of Publisher.



20
21
22
23
24
25
# File 'lib/hiptest-publisher.rb', line 20

def initialize(args, listeners: nil, exit_on_bad_arguments: true)
  @reporter = Reporter.new(listeners)
  @cli_options = OptionsParser.parse(args, reporter)
  # pass false to prevent hiptest-publisher from exiting, useful when used embedded
  @exit_on_bad_arguments = exit_on_bad_arguments
end

Instance Attribute Details

#reporterObject (readonly)

Returns the value of attribute reporter.



18
19
20
# File 'lib/hiptest-publisher.rb', line 18

def reporter
  @reporter
end

Instance Method Details

#add_listener(listener) ⇒ Object



104
105
106
# File 'lib/hiptest-publisher.rb', line 104

def add_listener(listener)
  reporter.add_listener(listener)
end

#exportObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/hiptest-publisher.rb', line 229

def export
  return if @project.nil?

  with_status_message "Analyzing data" do
    @language_config = LanguageConfigParser.new(@cli_options)
    Hiptest::Nodes::ParentAdder.add(@project)
    Hiptest::Nodes::ParameterTypeAdder.add(@project)
    Hiptest::DefaultArgumentAdder.add(@project)
    Hiptest::GherkinAdder.add(@project)
  end

  export_files
  export_actionword_signature if @language_config.include_group?("actionwords")
end

#export_actionword_signatureObject



127
128
129
130
131
132
# File 'lib/hiptest-publisher.rb', line 127

def export_actionword_signature
  write_to_file(
    "#{@cli_options.output_directory}/actionwords_signature.yaml",
    "Exporting actionword signature"
  ) { Hiptest::SignatureExporter.export_actionwords(@project).to_yaml }
end

#export_filesObject



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hiptest-publisher.rb', line 114

def export_files
  @language_config.language_group_configs.each do |language_group_config|
    language_group_config.each_node_rendering_context(@project) do |node_rendering_context|
      write_node_to_file(
        node_rendering_context.path,
        node_rendering_context.node,
        node_rendering_context,
        "Exporting #{node_rendering_context.description}",
      )
    end
  end
end

#fetch_xml_fileObject



69
70
71
72
73
74
75
76
# File 'lib/hiptest-publisher.rb', line 69

def fetch_xml_file
  with_status_message "Fetching data from Hiptest" do
    fetch_project_export(@cli_options)
  end
rescue Exception => err
  puts "Unable to open the file, please check that the token is correct".red
  reporter.dump_error(err)
end

#get_project(xml) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/hiptest-publisher.rb', line 78

def get_project(xml)
  with_status_message "Extracting data" do
    parser = Hiptest::XMLParser.new(xml, reporter)
    return parser.build_project
  end
rescue Exception => err
  reporter.dump_error(err)
end

#mkdirs_for(path) ⇒ Object



98
99
100
101
102
# File 'lib/hiptest-publisher.rb', line 98

def mkdirs_for(path)
  unless Dir.exists?(File.dirname(path))
    FileUtils.mkpath(File.dirname(path))
  end
end

#post_resultsObject



263
264
265
266
267
268
269
# File 'lib/hiptest-publisher.rb', line 263

def post_results
  with_status_message "Posting #{@cli_options.push} to #{@cli_options.site}" do
    push_results(@cli_options)
  end
rescue Exception => err
  reporter.dump_error(err)
end


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/hiptest-publisher.rb', line 244

def print_categories
  language_config = LanguageConfigParser.new(@cli_options)
  group_names = language_config.group_names
  puts "For language #{@cli_options.language}, available file groups are"
  group_names.each do |group_name|
    puts "  - #{group_name}"
  end
  puts [
    "",
    "Usage examples:",
    "",
    "To export only #{group_names.first} files:",
    "    hiptest-publisher --language=#{@cli_options.language} --only=#{group_names.first}",
    "",
    "To export both #{group_names.first} and #{group_names[1]} files:",
    "    hiptest-publisher --language=#{@cli_options.language} --only=#{group_names.take(2).join(",")}"
  ].join("\n")
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hiptest-publisher.rb', line 27

def run
  puts "URL: #{make_url(@cli_options)}".white if @cli_options.verbose
  begin
    CliOptionsChecker.new(@cli_options, reporter).check!
  rescue CliOptionError => e
    puts e.message
    exit 1 if @exit_on_bad_arguments
    raise
  end

  if @cli_options.only == 'list'
    print_categories
    return
  end

  if push?(@cli_options)
    post_results
    return
  end

  if @cli_options.xml_file
    xml = IO.read(@cli_options.xml_file)
  else
    xml = fetch_xml_file
  end
  return if xml.nil?

  @project = get_project(xml)

  if @cli_options.actionwords_signature
    export_actionword_signature
    return
  end

  if @cli_options.actionwords_diff?
    show_actionwords_diff
    return
  end

  export
end

#show_actionwords_diffObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/hiptest-publisher.rb', line 134

def show_actionwords_diff
  old = nil
  with_status_message "Loading previous definition" do
    old = YAML.load_file("#{@cli_options.output_directory}/actionwords_signature.yaml")
  end

  @language_config = LanguageConfigParser.new(@cli_options)
  Hiptest::Nodes::ParentAdder.add(@project)
  Hiptest::Nodes::ParameterTypeAdder.add(@project)
  Hiptest::DefaultArgumentAdder.add(@project)
  Hiptest::GherkinAdder.add(@project)

  current = Hiptest::SignatureExporter.export_actionwords(@project, true)
  diff =  Hiptest::SignatureDiffer.diff( old, current)

  if @cli_options.aw_deleted
    return if diff[:deleted].nil?

    diff[:deleted].map {|deleted|
      puts @language_config.name_action_word(deleted[:name])
    }
    return
  end

  if @cli_options.aw_created
    return if diff[:created].nil?

    @language_config.language_group_configs.select { |language_group_config|
      language_group_config[:group_name] == "actionwords"
    }.each do |language_group_config|
      diff[:created].each do |created|
        node_rendering_context = language_group_config.build_node_rendering_context(created[:node])
        puts node_rendering_context[:node].render(node_rendering_context)
        puts ""
      end
    end
    return
  end

  if @cli_options.aw_renamed
    return if diff[:renamed].nil?

    diff[:renamed].map {|renamed|
      puts "#{@language_config.name_action_word(renamed[:name])}\t#{@language_config.name_action_word(renamed[:new_name])}"
    }
    return
  end

  if @cli_options.aw_signature_changed
    return if diff[:signature_changed].nil?

    @language_config.language_group_configs.select { |language_group_config|
      language_group_config[:group_name] == "actionwords"
    }.each do |language_group_config|
      diff[:signature_changed].each do |signature_changed|
        node_rendering_context = language_group_config.build_node_rendering_context(signature_changed[:node])
        puts signature_changed[:node].render(node_rendering_context)
        puts ""
      end
    end
    return
  end

  unless diff[:deleted].nil?
    puts "#{pluralize(diff[:deleted].length, "action word")} deleted:"
    puts diff[:deleted].map {|d| "- #{d[:name]}"}.join("\n")
    puts ""
  end

  unless diff[:created].nil?
    puts "#{pluralize(diff[:created].length, "action word")} created:"
    puts diff[:created].map {|c| "- #{c[:name]}"}.join("\n")
    puts ""
  end

  unless diff[:renamed].nil?
    puts "#{pluralize(diff[:renamed].length, "action word")} renamed:"
    puts diff[:renamed].map {|r| "- #{r[:name]} => #{r[:new_name]}"}.join("\n")
    puts ""
  end

  unless diff[:signature_changed].nil?
    puts "#{pluralize(diff[:signature_changed].length, "action word")} which signature changed:"
    puts diff[:signature_changed].map {|c| "- #{c[:name]}"}.join("\n")
    puts ""
  end

  if diff.empty?
    puts "No action words changed"
    puts ""
  end
rescue Exception => err
  reporter.dump_error(err)
end

#write_node_to_file(path, node, context, message) ⇒ Object



108
109
110
111
112
# File 'lib/hiptest-publisher.rb', line 108

def write_node_to_file(path, node, context, message)
  write_to_file(path, message) do
    Hiptest::Renderer.render(node, context)
  end
end

#write_to_file(path, message) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/hiptest-publisher.rb', line 87

def write_to_file(path, message)
  with_status_message "#{message}: #{path}" do
    mkdirs_for(path)
    File.open(path, 'w') do |file|
      file.write(yield)
    end
  end
rescue Exception => err
  reporter.dump_error(err)
end