Class: RailsEngineToolkit::Actions::UpdateEngineReadme

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_engine_toolkit/actions/update_engine_readme.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdout:, root:, stdin: nil, stderr: nil) ⇒ UpdateEngineReadme

Returns a new instance of UpdateEngineReadme.



6
7
8
9
10
11
12
13
# File 'lib/rails_engine_toolkit/actions/update_engine_readme.rb', line 6

def initialize(argv, stdout:, root:, stdin: nil, stderr: nil)
  @engine_name = argv[0]
  @stdout = stdout
  @stdin = stdin
  @stderr = stderr
  @root = Pathname(root)
  @project = Project.new(@root)
end

Instance Method Details

#callObject

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rails_engine_toolkit/actions/update_engine_readme.rb', line 15

def call
  @project.validate_root!
  validate_args!

  engine_path = @project.engine_path(@engine_name)
  readme = engine_path.join('README.md')
  migrations_dir = engine_path.join('db/migrate')

  raise ValidationError, "README not found: #{readme}" unless readme.file?

  owned_tables_block = tables_from(migrations_dir).then do |tables|
    tables.empty? ? '- None defined yet.' : tables.map { |table| "- #{table}" }.join("\n")
  end

  content = readme.read
  unless content.match?(/^## Owned tables$/)
    raise ValidationError,
          "Could not find '## Owned tables' section in #{readme}"
  end

  updated = content.sub(/^## Owned tables$.*?(?=^## |\z)/m, "## Owned tables\n#{owned_tables_block}\n\n")
  readme.write(updated)

  @stdout.puts("Updated README: #{readme}")
end