#!/usr/bin/env ruby # encoding: UTF-8 require 'thor' require 'thor/group' require 'i18n' require 'gherkin' # Used here as a translation source require 'json' require 'yaml' require 'tempfile' require File.join(File.dirname(__FILE__), '../lib/generate_ruby_tests/generate_ruby_test_helper') require_relative '../lib/generate_ruby_tests/version' module GeneCapybara # Definição de todos os geradores de gemas class Generate < Thor include Thor::Actions desc 'feature [RESOURCE_NAME]', 'Generates a feature' option :lang, banner: 'any of the gherkin supported languages', default: :en def feature(name) I18n.config.default_locale = options[:lang] in_root_project_folder? create_feature_file(name) end desc 'step [RESOURCE_NAME]', 'Generates a step' option :lang, banner: 'any of the gherkin supported languages', default: :en def step(name) I18n.config.default_locale = options[:lang] in_root_project_folder? create_steps_file name end desc 'page [RESOURCE_NAME]', 'Generates pages' option :lang, banner: 'any of the gherkin supported languages', default: :en def page(name) I18n.config.default_locale = options[:lang] in_root_project_folder? create_page_file name end desc 'screens [RESOURCE_NAME]', 'Generates screens' option :lang, banner: 'any of the gherkin supported languages', default: :en def screen(name) I18n.config.default_locale = options[:lang] in_root_project_folder? create_screens_file name end desc 'services [RESOURCE_NAME]', 'Generates services' option :lang, banner: 'any of the gherkin supported languages', default: :en def service(name) I18n.config.default_locale = options[:lang] in_root_project_folder? create_service_file name end desc 'all_web [RESOURCE_NAME]', 'Generates features, steps and pages' option :lang, banner: 'any of the gherkin supported languages', default: :en def all_web(name) I18n.config.default_locale = options[:lang] in_root_project_folder? create_feature_file(name) create_steps_file name create_page_file name end desc 'all_mobile [RESOURCE_NAME]', 'Generates features, steps and screens' option :lang, banner: 'any of the gherkin supported languages', default: :en def all_mobile(name) I18n.config.default_locale = options[:lang] in_root_project_folder? create_feature_file(name) create_steps_file name create_screens_file name end desc 'all_api [RESOURCE_NAME]', 'Generates feature and steps' option :lang, banner: 'any of the gherkin supported languages', default: :en def all_api(name) I18n.config.default_locale = options[:lang] in_root_project_folder? create_feature_file(name) create_steps_file name create_service_file name end def self.source_root File.join(File.dirname(__FILE__), '..', 'lib', 'templates') end end end module GeneCapybara # Definição dos grupos geradores class GeneCapybaraRunner < Thor include Thor::Actions map '-v' => :version map '--version' => :version default_task :help register GeneCapybara::Generate, 'generate', 'generate [GENERATOR] [RESOURCE_NAME]', 'Generates various resources' register GeneCapybara::Generate, 'g', 'g [GENERATOR] [RESOURCE_NAME]', 'Generates various resources' desc 'new web PROJECT_NAME', 'Generates the structure of a new project that uses '\ 'Capybara, SitePrism, Selenium and Cucumber' option :lang, banner: 'any of the gherkin supported languages', default: :en def new_web(name) I18n.config.default_locale = options[:lang] # Thor sera responsavel por procurar # arquivos e possiveis conflitos directory File.join(File.dirname(__FILE__), '../lib/SkeletonWeb'), name end desc 'new api PROJECT_NAME', 'Generates the structure of api' option :lang, banner: 'any of the gherkin supported languages', default: :en def new_api(name) I18n.config.default_locale = options[:lang] # Thor sera responsavel por procurar # arquivos e possiveis conflitos directory File.join(File.dirname(__FILE__), '../lib/SkeletonApi'), name end desc 'new mobile PROJECT_NAME', 'Generates the structure of mobile' option :lang, banner: 'any of the gherkin supported languages', default: :en def new_mobile(name) I18n.config.default_locale = options[:lang] # Thor sera responsavel por procurar # arquivos e possiveis conflitos directory File.join(File.dirname(__FILE__), '../lib/SkeletonMobile'), name end desc 'version', 'Shows the gem version' def version puts "GeneCapybara Version #{GeneCapybara::VERSION}" end def self.source_root File.join(File.dirname(__FILE__), '..', 'lib', 'templates') end # Substituindo o metodo initialize para carregar todos os # traducoes suportadas pelo cucumber gem def initialize(*args) super # Loading gherkin accepted translations translations_file_path = File.join( Gem.loaded_specs['gherkin'].full_gem_path, 'lib', 'gherkin', 'i18n.json') # Analisando o arquivo JSON # Removendo a sequencia * | e toda a alternativa # opcoes para as traducoes do cucumber translations_json = JSON.parse(File.read(translations_file_path) .gsub(/\*\|/, '') .gsub(/\|.*\"/, '"')) # Convertendo traducoes para YAML e armazenando em um arquivo temporario translations_temp_file = Tempfile.new(['translations', '.yml']) File.write(translations_temp_file, translations_json.to_yaml) # Carregando as traducoes do pepino e do # locales pasta desta gem locales_folder_path = File.join(File.dirname(__FILE__), '../lib/locales') I18n.load_path = Dir[translations_temp_file, File.join(locales_folder_path, '*.yml')] I18n.backend.load_translations I18n.config.enforce_available_locales = true end end end GeneCapybara::GeneCapybaraRunner.start