1
+ namespace :dojos do
2
+ desc 'Git履歴からinactivated_at日付を抽出してYAMLファイルに反映'
3
+ task extract_inactivated_at_from_git : :environment do
4
+ require 'git'
5
+
6
+ yaml_path = Rails . root . join ( 'db' , 'dojos.yaml' )
7
+ git = Git . open ( Rails . root )
8
+
9
+ # YAMLファイルの内容を行番号付きで読み込む
10
+ yaml_lines = File . readlines ( yaml_path )
11
+
12
+ # 非アクティブなDojoを取得
13
+ inactive_dojos = Dojo . inactive . where ( inactivated_at : nil )
14
+
15
+ puts "=== Git履歴から inactivated_at を抽出 ==="
16
+ puts "対象となる非アクティブDojo数: #{ inactive_dojos . count } "
17
+ puts ""
18
+
19
+ updated_count = 0
20
+
21
+ inactive_dojos . each do |dojo |
22
+ puts "処理中: #{ dojo . name } (ID: #{ dojo . id } )"
23
+
24
+ # is_active: false が記載されている行を探す
25
+ target_line_number = nil
26
+ in_dojo_block = false
27
+
28
+ yaml_lines . each_with_index do |line , index |
29
+ # Dojoブロックの開始を検出
30
+ if line . match? ( /^- id: #{ dojo . id } $/ )
31
+ in_dojo_block = true
32
+ elsif line . match? ( /^- id: \d +$/ )
33
+ in_dojo_block = false
34
+ end
35
+
36
+ # 該当Dojoブロック内で is_active: false を見つける
37
+ if in_dojo_block && line . match? ( /^\s *is_active: false/ )
38
+ target_line_number = index + 1 # git blameは1-indexedなので+1
39
+ break
40
+ end
41
+ end
42
+
43
+ if target_line_number
44
+ # git blame を使って該当行の最新コミット情報を取得
45
+ # --porcelain で解析しやすい形式で出力
46
+ blame_cmd = "git blame #{ yaml_path } -L #{ target_line_number } ,+1 --porcelain"
47
+ blame_output = `#{ blame_cmd } ` . strip
48
+
49
+ # コミットIDを抽出(最初の行の最初の要素)
50
+ commit_id = blame_output . lines [ 0 ] . split . first
51
+
52
+ if commit_id && commit_id . match? ( /^[0-9a-f]{40}$/ )
53
+ # コミット情報を取得
54
+ commit = git . gcommit ( commit_id )
55
+ inactivated_date = commit . author_date
56
+
57
+ # YAMLファイルのDojoブロックを見つけて更新
58
+ yaml_updated = false
59
+ yaml_lines . each_with_index do |line , index |
60
+ if line . match? ( /^- id: #{ dojo . id } $/ )
61
+ # 該当Dojoブロックの最後に inactivated_at を追加
62
+ insert_index = index + 1
63
+ while insert_index < yaml_lines . length && !yaml_lines [ insert_index ] . match? ( /^- id:/ )
64
+ # is_active: false の次の行に挿入したい
65
+ if yaml_lines [ insert_index - 1 ] . match? ( /is_active: false/ )
66
+ yaml_lines . insert ( insert_index ,
67
+ " inactivated_at: '#{ inactivated_date . strftime ( '%Y-%m-%d %H:%M:%S' ) } '\n " )
68
+ yaml_updated = true
69
+ break
70
+ end
71
+ insert_index += 1
72
+ end
73
+ break if yaml_updated
74
+ end
75
+ end
76
+
77
+ if yaml_updated
78
+ updated_count += 1
79
+ puts " ✓ inactivated_at を追加: #{ inactivated_date . strftime ( '%Y-%m-%d %H:%M:%S' ) } "
80
+ puts " コミット: #{ commit_id [ 0 ..7 ] } by #{ commit . author . name } "
81
+ else
82
+ puts " ✗ YAMLファイルの更新に失敗"
83
+ end
84
+ else
85
+ puts " ✗ コミット情報の取得に失敗"
86
+ end
87
+ else
88
+ puts " ✗ YAMLファイル内で 'is_active: false' 行が見つかりません"
89
+ end
90
+
91
+ puts ""
92
+ end
93
+
94
+ if updated_count > 0
95
+ # YAMLファイルを書き戻す
96
+ File . write ( yaml_path , yaml_lines . join )
97
+
98
+ puts "=== 完了 ==="
99
+ puts "合計 #{ updated_count } 個のDojoに inactivated_at を追加しました"
100
+ puts ""
101
+ puts "次のステップ:"
102
+ puts "1. db/dojos.yaml の変更内容を確認"
103
+ puts "2. rails dojos:update_db_by_yaml を実行してDBに反映"
104
+ puts "3. 変更をコミット"
105
+ else
106
+ 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' 行が見つかりません"
159
+ end
160
+ end
161
+ end
0 commit comments