forked from endoflife-date/endoflife.date
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-icalendar-files.rb
124 lines (107 loc) · 3.36 KB
/
create-icalendar-files.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
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
#!/usr/bin/env ruby
# This script creates an calendar/[product].ics file
# in each markdown source file, where [product] is the permalink value and
require 'fileutils'
require 'icalendar'
require 'yaml'
CALENDAR_DIR = 'calendar'.freeze
def load_yaml(file)
if YAML.respond_to?(:unsafe_load)
YAML.unsafe_load_file(file)
else
YAML.load_file(self[:encoded_value])
end
end
class Product
attr_reader :hash
def initialize(markdown_file)
@hash = load_yaml(markdown_file)
end
def permalink
hash.fetch('permalink').sub('/', '')
end
def link
"https://endoflife.date/#{permalink}"
end
def title
hash.fetch('title')
end
def release_cycles
hash.fetch('releases').map do |release|
name = release.delete('releaseCycle')
{ 'name' => name, 'data' => release }
end
end
end
# return a icalendar output filename, including the directory name. Any / characters
# in the name are replaced with - to avoid file errors.
def icalendar_filename(output_dir, name)
filename = name.to_s.tr('/', '-') + '.ics'
File.join(output_dir, filename)
end
def notification_message(product, cycle, type)
message = "#{product} #{cycle}"
case type
when 'eol' then
message += ' will become End-of-life.'
when 'support' then
message += ' will end active development.'
when 'release' then
message += ' will be released.'
when 'extendedSupport' then
message += ' will end extended support.'
end
end
def process_product(product)
FileUtils.mkdir_p(CALENDAR_DIR)
cal = Icalendar::Calendar.new
product.release_cycles.each do |cycle|
cycle.fetch('data').each do |key, item|
next if !['release', 'support', 'eol', 'extendedSupport'].include?(key) || !item.instance_of?(Date)
event = cal.event
event.dtstart = Icalendar::Values::Date.new(item)
event.dtend = Icalendar::Values::Date.new(item + 1)
event.summary = "#{product.title} #{cycle.fetch('name')} #{key.upcase}"
event.summary.ical_params = { 'altrep' => product.link }
event.description = notification_message(product.title, cycle.fetch('name'), key)
event.categories = [key]
event.url = product.link
next if key != 'eol'
event.alarm do |a|
a.action = 'DISPLAY'
a.trigger = Icalendar::Values::DateTime.new((item << 12).to_datetime + Rational(9, 24))
end
event.alarm do |a|
a.action = 'DISPLAY'
a.trigger = Icalendar::Values::DateTime.new((item << 6).to_datetime + Rational(9, 24))
end
event.alarm do |a|
a.action = 'DISPLAY'
a.trigger = Icalendar::Values::DateTime.new((item << 3).to_datetime + Rational(9, 24))
end
event.alarm do |a|
a.action = 'DISPLAY'
a.trigger = Icalendar::Values::DateTime.new((item << 1).to_datetime + Rational(9, 24))
end
event.alarm do |a|
a.action = 'DISPLAY'
a.trigger = '-P6DT9H'
end
event.alarm do |a|
a.action = 'DISPLAY'
a.trigger = 'PT9H'
end
end
end
output_file = icalendar_filename(CALENDAR_DIR, product.permalink)
File.open(output_file, 'w') { |f| f.puts cal.to_ical }
end
# each file is something like 'products/foo.md'
def process_all_files()
Dir['products/*.md'].each do |file|
product = Product.new(file)
process_product(product)
end
end
############################################################
process_all_files()