Class: Bddgenx::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/bddgenx/generators/runner.rb

Overview

Classe principal de execução da gem BDDGenX. Controla o fluxo de leitura de histórias, geração de artefatos BDD e exportação de relatórios. Suporta execução via terminal.

Class Method Summary collapse

Class Method Details

.choose_files(input_dir) ⇒ Array<String>

Retorna a lista de arquivos ‘.txt` a processar.

  • Se ARGV contiver argumentos, usa esses nomes como arquivos ‘.txt`

  • Caso contrário, entra em modo interativo para seleção manual

Parameters:

  • input_dir (String)

    Caminho do diretório com arquivos ‘.txt`

Returns:

  • (Array<String>)

    Lista de caminhos de arquivos ‘.txt`



34
35
36
# File 'lib/bddgenx/generators/runner.rb', line 34

def self.choose_files(input_dir)
  ARGV.any? ? selecionar_arquivos_txt(input_dir) : choose_input(input_dir)
end

.choose_input(input_dir) ⇒ Array<String>

Modo interativo para o usuário escolher o arquivo ‘.txt` a ser processado.

Exibe uma lista numerada com os arquivos disponíveis no diretório. O usuário pode selecionar um específico ou pressionar ENTER para todos.

Parameters:

  • input_dir (String)

    Caminho do diretório com arquivos ‘.txt`

Returns:

  • (Array<String>)

    Arquivo selecionado ou todos disponíveis



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bddgenx/generators/runner.rb', line 67

def self.choose_input(input_dir)
  files = Dir.glob(File.join(input_dir, '*.txt'))
  if files.empty?
    warn "❌ Não há arquivos .txt no diretório #{input_dir}"
    exit 1
  end

  puts "Selecione o arquivo de história para processar:"
  files.each_with_index { |f, i| puts "#{i+1}. #{File.basename(f)}" }
  print "Digite o número correspondente (ou ENTER para todos): "
  choice = STDIN.gets.chomp

  return files if choice.empty?
  idx = choice.to_i - 1
  unless idx.between?(0, files.size - 1)
    warn "❌ Escolha inválida."
    exit 1
  end
  [files[idx]]
end

.executevoid

This method returns an undefined value.

Executa o fluxo principal de geração dos artefatos BDD.

Etapas executadas:

  • Detecta o modo de execução via ENV (static/chatgpt/gemini/copilot)

  • Carrega e valida histórias de usuário

  • Gera arquivos ‘.feature` e seus respectivos steps

  • Executa geração via IA (quando aplicável)

  • Exporta arquivos em PDF

  • Gera rastreabilidade via CSV

Exibe no final um resumo das operações executadas.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
# File 'lib/bddgenx/generators/runner.rb', line 102

def self.execute
  modo = ENV['BDDGENX_MODE'] || 'static'
  input_dir = 'input'
  Dir.mkdir(input_dir) unless Dir.exist?(input_dir)

  arquivos = choose_files(input_dir)
  if arquivos.empty?
    warn I18n.t('messages.no_files')
    exit 1
  end

  # Contadores
  total = features = steps = ignored = 0
  skipped_steps = []
  generated_pdfs = []
  skipped_pdfs = []

  arquivos.each do |arquivo|
    total += 1
    puts "\n🔍 #{I18n.t('messages.processing')}: #{arquivo}"

    historia = Parser.ler_historia(arquivo)
    idioma = Utils.obter_idioma_do_arquivo(arquivo) || historia[:idioma]
    historia[:idioma] = idioma

    unless Validator.validar(historia)
      ignored += 1
      puts "#{I18n.t('messages.invalid_story')}: #{arquivo}"
      next
    end

    # IA: geração de cenários com Gemini, ChatGPT ou Copilot
    if %w[gemini chatgpt copilot].include?(modo)
      puts I18n.t('messages.start_ia', modo: modo.capitalize)

      feature_text = Support::Loader.run(I18n.t('messages.ia_waiting'), :default) do
        case modo
        when 'gemini' then IA::GeminiCliente.gerar_cenarios(historia, idioma)
        when 'chatgpt' then IA::ChatGptCliente.gerar_cenarios(historia, idioma)
        when 'copilot' then IA::MicrosoftCopilotCliente.gerar_cenarios(historia, idioma)
        end
      end

      if feature_text
        feature_path = Generator.path_para_feature(arquivo)
        feature_content = Utils.limpar(feature_text)
      else
        ignored += 1
        puts I18n.t('messages.feature_fail', arquivo: arquivo)
        next
      end
    else
      # Geração local (modo static)
      feature_path, feature_content = Support::Loader.run(I18n.t('messages.start_static'), :dots) do
        sleep(2)
        Generator.gerar_feature(historia)
      end
    end

    # Salva versão antiga se existir
    Backup.salvar_versao_antiga(feature_path)

    features += 1 if Generator.salvar_feature(feature_path, feature_content)

    if StepsGenerator.gerar_passos(feature_path)
      steps += 1
    else
      skipped_steps << feature_path
    end

    FileUtils.mkdir_p('reports')
    result = PDFExporter.exportar_todos(only_new: true)
    Tracer.adicionar_entrada(historia, feature_path)

    generated_pdfs.concat(result[:generated])
    skipped_pdfs.concat(result[:skipped])
  end

  # Resumo final
  puts "\n#{I18n.t('messages.processing_done')}"
  puts "- #{I18n.t('messages.total_histories')}:    #{total}"
  puts "- #{I18n.t('messages.features_generated')}: #{features}"
  puts "- #{I18n.t('messages.steps_generated')}:    #{steps}"
end

.selecionar_arquivos_txt(input_dir) ⇒ Array<String>

Processa os argumentos da linha de comando (ARGV) e gera caminhos completos para arquivos ‘.txt` no diretório informado.

  • Se a extensão ‘.txt` estiver ausente, ela é adicionada.

  • Arquivos inexistentes são ignorados com aviso.

Parameters:

  • input_dir (String)

    Diretório base onde estão os arquivos ‘.txt`

Returns:

  • (Array<String>)

    Lista de arquivos válidos encontrados



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bddgenx/generators/runner.rb', line 47

def self.selecionar_arquivos_txt(input_dir)
  ARGV.map do |arg|
    nome = arg.end_with?('.txt') ? arg : "#{arg}.txt"
    path = File.join(input_dir, nome)
    unless File.exist?(path)
      warn "⚠️  Arquivo não encontrado: #{path}"
      next
    end
    path
  end.compact
end