Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify template hierarchy - enable multiple versions #145

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 4 additions & 3 deletions lib/config/cucumber-java.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ fallback_template = 'empty'

[features]
node_name = folders
template_dirs = gherkin/inlined_uids, gherkin/java, gherkin, common
template_dirs = gherkin, common
gherkin_flavors = inlined_uids, no_empty_scenarios
named_filename = '%s.feature'
indentation = ' '
renderer_addons = 'GherkinAddon'

[step_definitions]
node_name = actionwords
template_dirs = cucumber/java, java, common
template_dirs = cucumber-java, java, common
filename = 'StepDefinitions.java'
naming_convention = 'camelize_lower'
call_prefix = 'actionwords'
renderer_addons = 'GherkinAddon'

[actionwords]
template_dirs = cucumber/java/actionwords, java, common
template_dirs = cucumber-java, java, common
filename = 'Actionwords.java'
naming_convention = 'camelize_lower'
2 changes: 1 addition & 1 deletion lib/config/cucumber-ruby.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ renderer_addons = 'GherkinAddon'

[step_definitions]
node_name = actionwords
template_dirs = cucumber, common
template_dirs = cucumber-ruby, common
filename = 'step_definitions.rb'
naming_convention = 'underscore'
renderer_addons = 'GherkinAddon'
Expand Down
2 changes: 1 addition & 1 deletion lib/config/python-unittest.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[_common]
template_dirs = python/unittest, python, common
template_dirs = python-unittest, python, common
indentation = " "

[tests]
Expand Down
2 changes: 1 addition & 1 deletion lib/config/ruby-minitest.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[_common]
template_dirs = ruby/minitest, ruby, common
template_dirs = minitest, ruby, common

[tests]
filename = 'project_test.rb'
Expand Down
2 changes: 1 addition & 1 deletion lib/config/ruby-rspec.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[_common]
template_dirs = ruby, common
template_dirs = rspec, ruby, common

[tests]
filename = 'project_spec.rb'
Expand Down
106 changes: 22 additions & 84 deletions lib/hiptest-publisher/options_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

require 'hiptest-publisher/formatters/console_formatter'
require 'hiptest-publisher/renderer_addons'
require 'hiptest-publisher/template_finder'
require 'hiptest-publisher/utils'
require 'hiptest-publisher/handlebars_helper'


class FileConfigParser
Expand Down Expand Up @@ -359,87 +359,6 @@ def renderer_addons
end
end

class TemplateFinder
attr_reader :template_dirs, :overriden_templates, :forced_templates, :fallback_template

def initialize(
template_dirs: nil,
overriden_templates: nil,
indentation: ' ',
forced_templates: nil,
fallback_template: nil,
**)
@template_dirs = template_dirs || []
@overriden_templates = overriden_templates
@compiled_handlebars = {}
@template_path_by_name = {}
@forced_templates = forced_templates || {}
@fallback_template = fallback_template
@context = {indentation: indentation}
end

def dirs
@dirs ||= begin
search_dirs = []
# search in overriden template base dir first
search_dirs << overriden_templates if overriden_templates
template_dirs.each {|template_dir|
# search template paths in overriden_templates
search_dirs << "#{overriden_templates}/#{template_dir}" if overriden_templates
# search template paths in hiptest_publisher
search_dirs << "#{hiptest_publisher_path}/lib/templates/#{template_dir}"
}
search_dirs
end
end

def get_compiled_handlebars(template_name)
template_path = get_template_path(template_name)
@compiled_handlebars[template_path] ||= handlebars.compile(File.read(template_path))
end

def get_template_by_name(name)
return if name.nil?
name = forced_templates.fetch(name, name)
dirs.each do |path|
template_path = File.join(path, "#{name}.hbs")
return template_path if File.file?(template_path)
end
nil
end

def get_template_path(template_name)
unless @template_path_by_name.has_key?(template_name)
@template_path_by_name[template_name] = get_template_by_name(template_name) || get_template_by_name(@fallback_template)
end
@template_path_by_name[template_name] or raise ArgumentError.new(I18n.t('errors.template_not_found', template_name: template_name, dirs: dirs))
end

def register_partials
dirs.reverse_each do |path|
next unless File.directory?(path)
Dir.entries(path).select do |file_name|
file_path = File.join(path, file_name)
next unless File.file?(file_path) && file_name.start_with?('_')
@handlebars.register_partial(file_name[1..-5], File.read(file_path))
end
end
end

private

def handlebars
if !@handlebars
@handlebars = Handlebars::Handlebars.new
@handlebars.set_escaper(Handlebars::Escapers::DummyEscaper)

register_partials
Hiptest::HandlebarsHelper.register_helpers(@handlebars, @context)
end
@handlebars
end
end

class LanguageGroupConfig
@@MAX_FILE_SIZE = 255

Expand Down Expand Up @@ -526,14 +445,33 @@ def template_dirs
end
end

def template_flavors
specified_flavors = {}

template_dirs.each do |template_dir|
flavor_name = "#{template_dir}_flavors".to_sym
flavors = @language_group_params[flavor_name]

next if flavors.nil?
specified_flavors[flavor_name] = flavors.split(',').map(&:strip)
end

specified_flavors
end

def template_finder
@template_finder ||= TemplateFinder.new(
@template_finder ||= TemplateFinder.new(template_finder_create_opts)
end

def template_finder_create_opts
opts = {
template_dirs: template_dirs,
overriden_templates: @language_group_params[:overriden_templates],
indentation: indentation,
forced_templates: forced_templates,
fallback_template: @language_group_params[:fallback_template],
)
language_group: @language_group_params[:group_name]
}.merge(template_flavors)
end

def each_node_rendering_context(project)
Expand Down
150 changes: 150 additions & 0 deletions lib/hiptest-publisher/template_finder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
require 'i18n'

require 'hiptest-publisher/handlebars_helper'

class TemplateFinder
attr_reader :template_dirs, :overriden_templates, :forced_templates, :fallback_template

def initialize(
template_dirs: nil,
overriden_templates: nil,
indentation: ' ',
forced_templates: nil,
fallback_template: nil,
language_group: nil,
**extra)
@template_dirs = template_dirs || []
@overriden_templates = overriden_templates
@compiled_handlebars = {}
@template_path_by_name = {}
@forced_templates = forced_templates || {}
@fallback_template = fallback_template
@language_group = language_group
@extra_params = extra

@context = {indentation: indentation}
end

def dirs
@dirs ||= begin
search_dirs = []
# search in overriden template base dir first
search_dirs << overriden_templates if overriden_templates
template_dirs.each do |template_dir|
# search template paths in overriden_templates
search_dirs << "#{overriden_templates}/#{template_dir}" if overriden_templates
version = required_version(template_dir)
flavors = @extra_params["#{template_dir}_flavors".to_sym] || []

# Search for templates/languages/<language>/version-<language-version>
if languages_dirs.has_key?(template_dir)
language_dir = "#{hiptest_publisher_path}/lib/templates/languages/#{template_dir}/#{find_version(languages_dirs[template_dir], version)}"

if @language_group && File.directory?(File.join(language_dir, @language_group))
search_dirs << "#{language_dir}/#{@language_group}"
end

flavors.each do |flavor|
flavor_dir_name = "#{flavor}_flavor"
if File.directory?(File.join(language_dir, flavor_dir_name))
search_dirs << "#{language_dir}/#{flavor_dir_name}"
end
end

search_dirs << language_dir
# Search for templates/frameworks/<framework>/version-<framework-version>
elsif framework_dirs.has_key?(template_dir)
framework_dir = "#{hiptest_publisher_path}/lib/templates/frameworks/#{template_dir}/#{find_version(framework_dirs[template_dir], version)}"
if @language_group && File.directory?(File.join(framework_dir, @language_group))
search_dirs << "#{framework_dir}/#{@language_group}"
end

search_dirs << framework_dir
else
# Support old location for templates and "common" folder
search_dirs << "#{hiptest_publisher_path}/lib/templates/#{template_dir}/"
end
end

search_dirs
end
end

def required_version(template_dir)
required = @extra_params["#{template_dir}_version".to_sym]
required ? "version-#{required}" : nil
end

def find_version(versions, version)
versions.include?(version) ? version : versions.first
end

def get_compiled_handlebars(template_name)
template_path = get_template_path(template_name)
@compiled_handlebars[template_path] ||= handlebars.compile(File.read(template_path))
end

def get_template_path(template_name)
unless @template_path_by_name.has_key?(template_name)
@template_path_by_name[template_name] = get_template_by_name(template_name) || get_template_by_name(@fallback_template)
end
@template_path_by_name[template_name] or raise ArgumentError.new(I18n.t('errors.template_not_found', template_name: template_name, dirs: dirs))
end

def get_template_by_name(name)
return if name.nil?
name = forced_templates.fetch(name, name)
dirs.each do |path|
template_path = File.join(path, "#{name}.hbs")
return template_path if File.file?(template_path)
end
nil
end

def register_partials
dirs.reverse_each do |path|
next unless File.directory?(path)
Dir.entries(path).select do |file_name|
file_path = File.join(path, file_name)
next unless File.file?(file_path) && file_name.start_with?('_')
@handlebars.register_partial(file_name[1..-5], File.read(file_path))
end
end
end

private

def languages_dirs
@languages_dirs ||= two_levels_hierarchy('languages')
end

def framework_dirs
@framework_dirs ||= two_levels_hierarchy('frameworks')
end

def two_levels_hierarchy(path)
hierarchy = {}
list_directories(path).map do |first_level|
hierarchy[first_level] = list_directories("#{path}/#{first_level}")
end
hierarchy
end

def list_directories(path)
Dir.
entries("#{hiptest_publisher_path}/lib/templates/#{path}")
.select {|entry| File.directory? File.join("#{hiptest_publisher_path}/lib/templates/#{path}", entry) and !(entry =='.' || entry == '..') }
.sort
end

def handlebars
if !@handlebars
@handlebars = Handlebars::Handlebars.new
@handlebars.set_escaper(Handlebars::Escapers::DummyEscaper)

register_partials
Hiptest::HandlebarsHelper.register_helpers(@handlebars, @context)
end
@handlebars
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions lib/templates_old/common/_gherkin_pattern.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
^{{#strip}}{{#join chunks context.parameter_delimiter}}{{this.value}}{{/join}}{{#each extra_inlined_parameters}} {{context.parameter_delimiter}}{{this.value}}{{context.parameter_delimiter}}{{/each}}{{/strip}}$
1 change: 1 addition & 0 deletions lib/templates_old/common/booleanliteral.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{to_string rendered_children.value }}
3 changes: 3 additions & 0 deletions lib/templates_old/common/datatable.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each rendered_children.datasets}}
{{{this}}}
{{/each}}
Empty file.
1 change: 1 addition & 0 deletions lib/templates_old/common/nullliteral.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nil
1 change: 1 addition & 0 deletions lib/templates_old/common/stringliteral.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'{{ rendered_children.value }}'
1 change: 1 addition & 0 deletions lib/templates_old/common/symbol.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{to_string rendered_children.value }}
1 change: 1 addition & 0 deletions lib/templates_old/common/unaryexpression.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{ rendered_children.operator }}}{{{ rendered_children.expression }}}
1 change: 1 addition & 0 deletions lib/templates_old/common/variable.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ underscore rendered_children.name }}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions lib/templates_old/cucumber/actionword.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#if rendered_children.gherkin_annotation }}
{{{ rendered_children.gherkin_annotation }}} /{{> gherkin_pattern}}/ do{{#if has_parameters?}} |{{{ join rendered_children.parameters_ordered_by_pattern ', '}}}|{{/if}}
{{#indent}}{{ underscore rendered_children.name }}{{#if has_parameters?}}({{{ join rendered_children.parameters ', '}}}){{/if}}{{/indent}}
end
{{/if}}
5 changes: 5 additions & 0 deletions lib/templates_old/cucumber/actionwords.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# encoding: UTF-8

require_relative 'actionwords'
World(Actionwords)
{{#each rendered_children.actionwords}}{{{this}}}{{/each}}
12 changes: 12 additions & 0 deletions lib/templates_old/cucumber/java/actionwords.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package {{{ context.package }}};

import cucumber.api.DataTable;
import cucumber.api.java.en.*;

public class StepDefinitions {{#curly}}{{#indent}}
public Actionwords {{{ context.call_prefix }}} = new Actionwords();

{{#each rendered_children.actionwords}}{{{this}}}
{{/each}}
{{/indent}}
{{/curly}}
1 change: 1 addition & 0 deletions lib/templates_old/cucumber/java/actionwords/parameter.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#if is_datatable?}}DataTable{{else}}{{#if is_bool?}}boolean{{else}}{{{ node.type }}}{{/if}}{{/if}} {{{ camelize_lower rendered_children.name }}}
1 change: 1 addition & 0 deletions lib/templates_old/cucumber/java/parameter.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#if is_datatable?}}DataTable{{else}}{{#if is_bool?}}boolean{{else}}{{{ node.type }}}{{/if}}{{/if}} {{{ camelize_lower rendered_children.name }}}
Loading