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.



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

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.



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

def reporter
  @reporter
end

Instance Method Details

#add_listener(listener) ⇒ Object



106
107
108
# File 'lib/hiptest-publisher.rb', line 106

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

#exportObject



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

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)
    Hiptest::ItemsOrderer.add(@project, @cli_options.sort)
  end

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

#export_actionword_signatureObject



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

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



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

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



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

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



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

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



100
101
102
103
104
# File 'lib/hiptest-publisher.rb', line 100

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

#post_resultsObject



266
267
268
269
270
271
272
# File 'lib/hiptest-publisher.rb', line 266

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


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

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



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
68
69
# File 'lib/hiptest-publisher.rb', line 29

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



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
228
229
# File 'lib/hiptest-publisher.rb', line 136

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



110
111
112
113
114
# File 'lib/hiptest-publisher.rb', line 110

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



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

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