Class: RailsCrudTools::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/crud/tools/cli.rb

Overview

The CLI class provides command-line interface methods for generating CRUD files and configurations. It includes methods to generate CRUD files, generate configuration files, and initialize the application.

Class Method Summary collapse

Class Method Details

.generate_crud_configObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rails/crud/tools/cli.rb', line 128

def generate_crud_config
  load_application unless @application_loaded

  table_names = ActiveRecord::Base.connection.tables.sort
  table_start_col = table_names.any? ? table_names.first : "active_admin_comments"

  config_content = <<~CONFIG
    enabled: true
    base_dir: doc
    crud_file:
      file_name: crud.xlsx
      sheet_name: CRUD
      header_bg_color: 00FFCC
      font_name: Arial
    method_col: Verb
    action_col: Controller#Action
    table_start_col: #{table_start_col}
    sql_logging_enabled: true
  CONFIG

  File.write(".crudconfig.yml", config_content)
  puts "Generated .crudconfig.yml file"
end

.generate_crud_fileObject



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
67
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
93
94
95
96
97
98
99
100
101
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
# File 'lib/rails/crud/tools/cli.rb', line 16

def generate_crud_file
  load_application unless @application_loaded

  # 1. `bundle exec rails routes --expanded`の結果を取得
  routes_output = `bundle exec rails routes --expanded`
  config = Rails::Crud::Tools::CrudConfig.instance.config
  font_name = config.crud_file.font_name

  # 2. 取得した結果を区切り文字で分割
  routes_lines = routes_output.split("\n").reject(&:empty?)
  routes_data = []
  current_route = {}

  routes_lines.each do |line|
    if line.start_with?("--[ Route")
      routes_data << current_route unless current_route.empty?
      current_route = {}
    else
      key, value = line.split("|").map(&:strip)
      current_route[key] = value
    end
  end
  routes_data << current_route unless current_route.empty?

  # 3. 全テーブル名を取得し、アルファベット順にソート
  table_names = ActiveRecord::Base.connection.tables.sort

  # 4. `rubyXL`を使って`xlsx`ファイルに書き込み
  workbook = RubyXL::Workbook.new
  sheet = workbook[0]
  sheet.sheet_name = config.crud_file.sheet_name

  # ヘッダー行を追加
  headers = %w[Prefix Verb URI Controller#Action crud_count] + table_names

  headers.each_with_index do |header, index|
    cell = sheet.add_cell(0, index, header)
    cell.change_fill("00FFCC")
    cell.change_font_name(font_name)
    cell.change_font_bold(true)
    apply_borders(cell)
  end

  start_col = "F"
  end_col = ("A".."ZZ").to_a[table_names.length + 4] # 'F'から始まる列の範囲を計算

  # データ行を追加
  routes_data.each_with_index do |route, row_index|
    headers.each_with_index do |header, col_index|
      cell = sheet.add_cell(row_index + 1, col_index, route[header])
      cell.change_font_name(font_name)
      apply_borders(cell)
    end

    # 追加: crud_count列に式を設定
    crud_count_formula = "=SUMPRODUCT(LEN(#{start_col}#{row_index + 2}:#{end_col}#{row_index + 2}))"
    crud_count_cell = sheet.add_cell(row_index + 1, 4, "", crud_count_formula)
    crud_count_cell.change_font_name(font_name)
    apply_borders(crud_count_cell)
  end

  # app/jobs ディレクトリ内のジョブ名を取得
  job_files = Dir.glob("app/jobs/**/*.rb")
  job_classes = job_files.map do |file|
    File.basename(file, ".rb").camelize
  end.reject { |job| job == "ApplicationJob" }.sort

  # ジョブ名を Controller#Action 列に追加
  job_classes.each_with_index do |job, index|
    headers.each_with_index do |header, col_index|
      if header == "Controller#Action"
        cell = sheet.add_cell(routes_data.length + 1 + index, col_index, job)
        cell.change_font_name(font_name)
      else
        cell = sheet.add_cell(routes_data.length + 1 + index, col_index, nil)
      end
      apply_borders(cell)
    end

    # 追加: crud_count列に式を設定
    crud_count_formula = "=SUMPRODUCT(LEN(#{start_col}#{routes_data.length + 2 + index}:#{end_col}#{routes_data.length + 2 + index}))"
    crud_count_cell = sheet.add_cell(routes_data.length + 1 + index, 4, "", crud_count_formula)
    crud_count_cell.change_font_name(font_name)
    apply_borders(crud_count_cell)
  end

  # ヘッダーの背景色を設定
  (0..headers.length - 1).each do |col_index|
    sheet[0][col_index].change_fill(config.crud_file.header_bg_color)
  end

  # 列幅を設定
  headers.each_with_index do |header, col_index|
    max_length = header.length
    (1..routes_data.length).each do |row_index|
      cell_value = sheet[row_index][col_index].value.to_s
      max_length = [max_length, cell_value.length].max
    end
    sheet.change_column_width(col_index, max_length + 2)
  end

  # ファイルを保存
  crud_file = config.crud_file_path
  base_dir = File.dirname(crud_file)

  # base_dirが存在しなければ作成
  FileUtils.mkdir_p(base_dir) unless Dir.exist?(base_dir)
  workbook.write(crud_file)

  puts "Output: #{crud_file}"
end

.initObject



152
153
154
155
# File 'lib/rails/crud/tools/cli.rb', line 152

def init
  generate_crud_config
  generate_crud_file
end