forked from oracle-devrel/cool.devo.build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_modified.rb
executable file
·45 lines (35 loc) · 1.17 KB
/
update_modified.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
43
44
45
#!/usr/bin/env ruby
# Update modified dates of changed Markdown files
class ::String
def file_has_yaml?
raise "Invalid file: #{self}" unless File.exist? self
parts = IO.read(self).split(/^---/)
return parts.count > 2
end
def get_yaml
raise "Invalid file: #{self}" unless File.exist? self
parts = IO.read(self).split(/^---/)
raise "Invalid YAML in #{self}" unless parts.count > 2
header = parts[1]
body = parts[2..-1].join('---')
[header, body]
end
end
staged = `git diff-index --name-status --cached HEAD`.strip.split(/\n/).map {|f| f.sub(/^(A|M)\s*/, '')}
partial = `git status --porcelain --untracked-files=no`.strip.split(/\n/).map {|f| f.sub(/^(A|M)\s*/, '')}
changed = staged.concat(partial).sort.uniq
changed.select! {|f| f =~ /(md|markdown)$/ && f.file_has_yaml? }
modified = Time.now.strftime('%Y-%m-%d %H:%M:%S %z')
puts "Modified: #{modified}"
changed.each do |file|
warn "Updating modified date: #{file}"
header, body = file.get_yaml
header.sub!(/^modified: .*?\n/, '')
header += "modified: #{modified}"
File.open(file, 'w+') do |f|
f.puts '---'
f.puts header.strip
f.puts '---'
f.puts body.strip
end
end