Skip to content

Commit 03997a9

Browse files
committed
refactor: Git履歴抽出タスクを1つに統合して冪等性を実装
- 引数なし: 全ての非アクティブDojoを処理してYAMLを更新 - 引数あり: 特定のDojoの情報を表示のみ(読み取り専用) - 既に inactivated_at が設定されている場合はスキップ - コードの重複を排除してメンテナンス性を向上
1 parent 11c3d41 commit 03997a9

File tree

1 file changed

+38
-66
lines changed

1 file changed

+38
-66
lines changed

lib/tasks/dojos_inactivated_at.rake

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace :dojos do
2-
desc 'Git履歴からinactivated_at日付を抽出してYAMLファイルに反映'
3-
task extract_inactivated_at_from_git: :environment do
2+
desc 'Git履歴からinactivated_at日付を抽出してYAMLファイルに反映(引数でDojo IDを指定可能)'
3+
task :extract_inactivated_at_from_git, [:dojo_id] => :environment do |t, args|
44
require 'git'
55

66
yaml_path = Rails.root.join('db', 'dojos.yaml')
@@ -9,16 +9,23 @@ namespace :dojos do
99
# YAMLファイルの内容を行番号付きで読み込む
1010
yaml_lines = File.readlines(yaml_path)
1111

12-
# 非アクティブなDojoを取得
13-
inactive_dojos = Dojo.inactive.where(inactivated_at: nil)
12+
# 対象Dojoを決定(引数があれば特定のDojo、なければ全ての非アクティブDojo)
13+
target_dojos = if args[:dojo_id]
14+
dojo = Dojo.find(args[:dojo_id])
15+
puts "=== 特定のDojoのinactivated_at を抽出 ==="
16+
puts "対象Dojo: #{dojo.name} (ID: #{dojo.id})"
17+
[dojo]
18+
else
19+
inactive_dojos = Dojo.inactive.where(inactivated_at: nil)
20+
puts "=== Git履歴から inactivated_at を抽出 ==="
21+
puts "対象となる非アクティブDojo数: #{inactive_dojos.count}"
22+
inactive_dojos
23+
end
1424

15-
puts "=== Git履歴から inactivated_at を抽出 ==="
16-
puts "対象となる非アクティブDojo数: #{inactive_dojos.count}"
1725
puts ""
18-
1926
updated_count = 0
2027

21-
inactive_dojos.each do |dojo|
28+
target_dojos.each do |dojo|
2229
puts "処理中: #{dojo.name} (ID: #{dojo.id})"
2330

2431
# is_active: false が記載されている行を探す
@@ -54,6 +61,15 @@ namespace :dojos do
5461
commit = git.gcommit(commit_id)
5562
inactivated_date = commit.author_date
5663

64+
# 特定Dojoモードの場合は情報表示のみ
65+
if args[:dojo_id]
66+
puts "✓ is_active: false に設定された日時: #{inactivated_date.strftime('%Y-%m-%d %H:%M:%S')}"
67+
puts " コミット: #{commit_id[0..7]}"
68+
puts " 作者: #{commit.author.name}"
69+
puts " メッセージ: #{commit.message.lines.first.strip}"
70+
next
71+
end
72+
5773
# YAMLファイルのDojoブロックを見つけて更新
5874
yaml_updated = false
5975
yaml_lines.each_with_index do |line, index|
@@ -63,23 +79,30 @@ namespace :dojos do
6379
while insert_index < yaml_lines.length && !yaml_lines[insert_index].match?(/^- id:/)
6480
# is_active: false の次の行に挿入したい
6581
if yaml_lines[insert_index - 1].match?(/is_active: false/)
82+
# 既に inactivated_at がある場合はスキップ(冪等性)
83+
if yaml_lines[insert_index].match?(/^\s*inactivated_at:/)
84+
puts " - inactivated_at は既に設定されています"
85+
yaml_updated = false
86+
break
87+
end
88+
6689
yaml_lines.insert(insert_index,
6790
" inactivated_at: '#{inactivated_date.strftime('%Y-%m-%d %H:%M:%S')}'\n")
6891
yaml_updated = true
6992
break
7093
end
7194
insert_index += 1
7295
end
73-
break if yaml_updated
96+
break
7497
end
7598
end
7699

77100
if yaml_updated
78101
updated_count += 1
79102
puts " ✓ inactivated_at を追加: #{inactivated_date.strftime('%Y-%m-%d %H:%M:%S')}"
80103
puts " コミット: #{commit_id[0..7]} by #{commit.author.name}"
81-
else
82-
puts " ✗ YAMLファイルの更新に失敗"
104+
elsif !args[:dojo_id]
105+
puts " - スキップ(既に設定済みまたは更新失敗)"
83106
end
84107
else
85108
puts " ✗ コミット情報の取得に失敗"
@@ -91,8 +114,8 @@ namespace :dojos do
91114
puts ""
92115
end
93116

94-
if updated_count > 0
95-
# YAMLファイルを書き戻す
117+
# 全Dojoモードで更新があった場合のみYAMLファイルを書き戻す
118+
if !args[:dojo_id] && updated_count > 0
96119
File.write(yaml_path, yaml_lines.join)
97120

98121
puts "=== 完了 ==="
@@ -102,60 +125,9 @@ namespace :dojos do
102125
puts "1. db/dojos.yaml の変更内容を確認"
103126
puts "2. rails dojos:update_db_by_yaml を実行してDBに反映"
104127
puts "3. 変更をコミット"
105-
else
128+
elsif !args[:dojo_id]
106129
puts "=== 完了 ==="
107-
puts "更新対象のDojoはありませんでした"
108-
end
109-
end
110-
111-
desc '特定のDojoのinactivated_at日付をGit履歴から抽出'
112-
task :extract_inactivated_at_for_dojo, [:dojo_id] => :environment do |t, args|
113-
require 'git'
114-
115-
dojo = Dojo.find(args[:dojo_id])
116-
yaml_path = Rails.root.join('db', 'dojos.yaml')
117-
git = Git.open(Rails.root)
118-
119-
puts "対象Dojo: #{dojo.name} (ID: #{dojo.id})"
120-
121-
# YAMLファイルの内容を読み込む
122-
yaml_lines = File.readlines(yaml_path)
123-
124-
# is_active: false が記載されている行を探す
125-
target_line_number = nil
126-
in_dojo_block = false
127-
128-
yaml_lines.each_with_index do |line, index|
129-
if line.match?(/^- id: #{dojo.id}$/)
130-
in_dojo_block = true
131-
elsif line.match?(/^- id: \d+$/)
132-
in_dojo_block = false
133-
end
134-
135-
if in_dojo_block && line.match?(/^\s*is_active: false/)
136-
target_line_number = index + 1
137-
break
138-
end
139-
end
140-
141-
if target_line_number
142-
blame_cmd = "git blame #{yaml_path} -L #{target_line_number},+1 --porcelain"
143-
blame_output = `#{blame_cmd}`.strip
144-
commit_id = blame_output.lines[0].split.first
145-
146-
if commit_id && commit_id.match?(/^[0-9a-f]{40}$/)
147-
commit = git.gcommit(commit_id)
148-
inactivated_date = commit.author_date
149-
150-
puts "✓ is_active: false に設定された日時: #{inactivated_date.strftime('%Y-%m-%d %H:%M:%S')}"
151-
puts " コミット: #{commit_id[0..7]}"
152-
puts " 作者: #{commit.author.name}"
153-
puts " メッセージ: #{commit.message.lines.first.strip}"
154-
else
155-
puts "✗ コミット情報の取得に失敗しました"
156-
end
157-
else
158-
puts "✗ YAMLファイル内で 'is_active: false' 行が見つかりません"
130+
puts "更新対象のDojoはありませんでした(または既に設定済み)"
159131
end
160132
end
161133
end

0 commit comments

Comments
 (0)