1
1
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 |
4
4
require 'git'
5
5
6
6
yaml_path = Rails . root . join ( 'db' , 'dojos.yaml' )
@@ -9,16 +9,23 @@ namespace :dojos do
9
9
# YAMLファイルの内容を行番号付きで読み込む
10
10
yaml_lines = File . readlines ( yaml_path )
11
11
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
14
24
15
- puts "=== Git履歴から inactivated_at を抽出 ==="
16
- puts "対象となる非アクティブDojo数: #{ inactive_dojos . count } "
17
25
puts ""
18
-
19
26
updated_count = 0
20
27
21
- inactive_dojos . each do |dojo |
28
+ target_dojos . each do |dojo |
22
29
puts "処理中: #{ dojo . name } (ID: #{ dojo . id } )"
23
30
24
31
# is_active: false が記載されている行を探す
@@ -54,6 +61,15 @@ namespace :dojos do
54
61
commit = git . gcommit ( commit_id )
55
62
inactivated_date = commit . author_date
56
63
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
+
57
73
# YAMLファイルのDojoブロックを見つけて更新
58
74
yaml_updated = false
59
75
yaml_lines . each_with_index do |line , index |
@@ -63,23 +79,30 @@ namespace :dojos do
63
79
while insert_index < yaml_lines . length && !yaml_lines [ insert_index ] . match? ( /^- id:/ )
64
80
# is_active: false の次の行に挿入したい
65
81
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
+
66
89
yaml_lines . insert ( insert_index ,
67
90
" inactivated_at: '#{ inactivated_date . strftime ( '%Y-%m-%d %H:%M:%S' ) } '\n " )
68
91
yaml_updated = true
69
92
break
70
93
end
71
94
insert_index += 1
72
95
end
73
- break if yaml_updated
96
+ break
74
97
end
75
98
end
76
99
77
100
if yaml_updated
78
101
updated_count += 1
79
102
puts " ✓ inactivated_at を追加: #{ inactivated_date . strftime ( '%Y-%m-%d %H:%M:%S' ) } "
80
103
puts " コミット: #{ commit_id [ 0 ..7 ] } by #{ commit . author . name } "
81
- else
82
- puts " ✗ YAMLファイルの更新に失敗 "
104
+ elsif ! args [ :dojo_id ]
105
+ puts " - スキップ(既に設定済みまたは更新失敗) "
83
106
end
84
107
else
85
108
puts " ✗ コミット情報の取得に失敗"
@@ -91,8 +114,8 @@ namespace :dojos do
91
114
puts ""
92
115
end
93
116
94
- if updated_count > 0
95
- # YAMLファイルを書き戻す
117
+ # 全Dojoモードで更新があった場合のみYAMLファイルを書き戻す
118
+ if ! args [ :dojo_id ] && updated_count > 0
96
119
File . write ( yaml_path , yaml_lines . join )
97
120
98
121
puts "=== 完了 ==="
@@ -102,60 +125,9 @@ namespace :dojos do
102
125
puts "1. db/dojos.yaml の変更内容を確認"
103
126
puts "2. rails dojos:update_db_by_yaml を実行してDBに反映"
104
127
puts "3. 変更をコミット"
105
- else
128
+ elsif ! args [ :dojo_id ]
106
129
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はありませんでした(または既に設定済み)"
159
131
end
160
132
end
161
133
end
0 commit comments