Class: Vial::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/vial/compiler.rb

Defined Under Namespace

Classes: CleanResult, CompileResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, source_paths: nil, output_path: nil, seed: nil) ⇒ Compiler

Returns a new instance of Compiler.



9
10
11
12
13
14
# File 'lib/vial/compiler.rb', line 9

def initialize(config:, source_paths: nil, output_path: nil, seed: nil)
  @config = config
  @source_paths = Array(source_paths || config.source_paths)
  @output_path = output_path || config.output_path
  @seed = seed || config.seed
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/vial/compiler.rb', line 7

def config
  @config
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



7
8
9
# File 'lib/vial/compiler.rb', line 7

def output_path
  @output_path
end

#seedObject (readonly)

Returns the value of attribute seed.



7
8
9
# File 'lib/vial/compiler.rb', line 7

def seed
  @seed
end

#source_pathsObject (readonly)

Returns the value of attribute source_paths.



7
8
9
# File 'lib/vial/compiler.rb', line 7

def source_paths
  @source_paths
end

Instance Method Details

#clean!Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vial/compiler.rb', line 68

def clean!
  Vial.load_sources!(source_paths: @source_paths)
  definitions = Vial.registry.definitions
  expected = expected_fixture_paths(definitions)
  manifest = read_manifest

  if manifest.empty?
    warn "Vial: no manifest found; nothing to clean"
    return CleanResult.new(status: :no_manifest, removed: [])
  end

  removed = []
  manifest.each do |relative_path|
    next if expected.include?(relative_path)

    file_path = File.join(@output_path.to_s, relative_path)
    next unless File.exist?(file_path)

    File.delete(file_path)
    removed << file_path
  end

  write_manifest(expected)
  CleanResult.new(status: :cleaned, removed: removed)
end

#compile!(dry_run: false, only: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
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
# File 'lib/vial/compiler.rb', line 16

def compile!(dry_run: false, only: nil)
  previous_seed = srand(@seed)
  Vial.load_sources!(source_paths: @source_paths)

  definitions = Vial.registry.definitions
  if definitions.empty?
    warn "Vial: no vial definitions found in #{@source_paths.join(', ')}"
    return CompileResult.new(status: :no_definitions, files: [], output_path: @output_path.to_s)
  end

  selected_definitions = filter_definitions(definitions, only)
  if selected_definitions.empty?
    warn "Vial: no matching vials to compile"
    return CompileResult.new(status: :no_definitions, files: [], output_path: @output_path.to_s)
  end

  warn_about_unused_sequences(definitions)

  records_by_definition = definitions.each_with_object([]) do |definition, entries|
    next if definition.abstract?
    entries << { definition: definition, records: definition.build_records }
  end

  total_records = records_by_definition.sum { |entry| entry[:records].size }
  if total_records == 0
    warn "Vial: no records generated (all definitions are abstract or missing generate calls)"
    return CompileResult.new(status: :no_records, files: [], output_path: @output_path.to_s)
  end

  Validator.new(records_by_definition).validate!

  warn_if_output_path_unconfigured

  selected_names = selected_definitions.map(&:name).to_set
  selected_records = records_by_definition.select { |entry| selected_names.include?(entry[:definition].name) }
  files_to_write = selected_records.map { |entry| fixture_path_for(entry[:definition]) }

  if dry_run
    return CompileResult.new(status: :dry_run, files: files_to_write, output_path: @output_path.to_s)
  end

  selected_records.each do |entry|
    write_fixture(entry[:definition], entry[:records])
  end

  write_manifest(expected_fixture_paths(definitions))

  CompileResult.new(status: :compiled, files: files_to_write, output_path: @output_path.to_s)
ensure
  srand(previous_seed) unless previous_seed.nil?
end