Class: Gitingest::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/gitingest/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Generator

Returns a new instance of Generator.



10
11
12
13
14
15
16
17
# File 'lib/gitingest/generator.rb', line 10

def initialize(options = {})
  @options = options
  @repo_files = []
  setup_logger
  validate_options
  configure_client
  @exclusion_filter = ExclusionFilter.new(@options[:exclude])
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/gitingest/generator.rb', line 8

def client
  @client
end

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/gitingest/generator.rb', line 8

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/gitingest/generator.rb', line 8

def options
  @options
end

#repo_filesObject (readonly)

Returns the value of attribute repo_files.



8
9
10
# File 'lib/gitingest/generator.rb', line 8

def repo_files
  @repo_files
end

Instance Method Details

#excluded_patternsObject

Exposed for testing



58
59
60
61
62
63
64
# File 'lib/gitingest/generator.rb', line 58

def excluded_patterns
  # This is a bit of a hack to maintain backward compatibility with tests
  # that check for excluded_patterns. In the new design, this is handled
  # by ExclusionFilter.
  @exclusion_filter.instance_variable_get(:@default_patterns) +
    @exclusion_filter.instance_variable_get(:@custom_glob_patterns).map { |p| Regexp.new(p.gsub("*", ".*")) }
end

#generate_directory_structureObject



48
49
50
51
52
53
54
55
# File 'lib/gitingest/generator.rb', line 48

def generate_directory_structure
  fetch_repository_contents if @repo_files.empty?
  @logger.info "Generating directory structure for #{@options[:repository]}"
  repo_name = @options[:repository].split("/").last
  structure = DirectoryStructureBuilder.new(repo_name, @repo_files).build
  @logger.info "\n"
  structure
end

#generate_fileObject



28
29
30
31
32
33
34
35
36
# File 'lib/gitingest/generator.rb', line 28

def generate_file
  fetch_repository_contents if @repo_files.empty?
  @logger.info "Generating file for #{@options[:repository]}"
  File.open(@options[:output_file], "w") do |file|
    process_content_to_output(file)
  end
  @logger.info "Prompt generated and saved to #{@options[:output_file]}"
  @options[:output_file]
end

#generate_promptObject



38
39
40
41
42
43
44
45
46
# File 'lib/gitingest/generator.rb', line 38

def generate_prompt
  @logger.info "Generating in-memory prompt for #{@options[:repository]}"
  fetch_repository_contents if @repo_files.empty?
  content = StringIO.new
  process_content_to_output(content)
  result = content.string
  @logger.info "Generated #{result.size} bytes of content in memory"
  result
end

#runObject



19
20
21
22
23
24
25
26
# File 'lib/gitingest/generator.rb', line 19

def run
  fetch_repository_contents
  if @options[:show_structure]
    puts generate_directory_structure
    return
  end
  generate_file
end