-
Notifications
You must be signed in to change notification settings - Fork 72
/
build_events_catalog.rb
164 lines (134 loc) · 3.44 KB
/
build_events_catalog.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
require 'fileutils'
class BuildEventsCatalog
PATH = "./"
SOURCE_PATH = "./ecommerce/"
EVENTS_CATALOG_DIRECTORY_NAME = "events_catalog"
EVENT_TYPE = "Infra::Event"
CONFIG_FILE = "./eventcatalog.config.js"
def call
configure
clear_domains
clear_events
clear_services
read_domains.each do |domain|
domain.capitalize!
domain_directory = create_domain_directory(domain)
create_domain_index(domain, domain_directory)
create_domain_events(domain)
end
end
private
def configure
replace_config_file
end
def read_domains
Dir.entries(SOURCE_PATH).select {|entry| File.directory?("#{SOURCE_PATH}#{entry}") and !(entry =='.' || entry == '..' || entry == 'processes') }
end
def source_domain_directory(domain)
"#{SOURCE_PATH}#{domain.downcase}"
end
def clear_domains
remove_catalog(domains_catalog)
create_catalog(domains_catalog)
end
def clear_events
remove_catalog(events_catalog)
create_catalog(events_catalog)
end
def clear_services
remove_catalog(services_catalog)
create_catalog(services_catalog)
end
def create_catalog(catalog)
FileUtils.mkdir(catalog)
end
def remove_catalog(catalog)
File.exist?(catalog) && File.directory?(catalog)
FileUtils.rm_rf(catalog)
end
def root_catalog
"#{PATH}#{EVENTS_CATALOG_DIRECTORY_NAME}"
end
def domains_catalog
"#{root_catalog}/domains"
end
def events_catalog
"#{root_catalog}/events"
end
def services_catalog
"#{root_catalog}/services"
end
def create_domain_directory(domain)
domain_directory = "#{domains_catalog}/#{domain}"
FileUtils.mkdir(domain_directory)
puts " * domain directory created: #{domain_directory}"
domain_directory
end
def create_domain_index(domain, domain_directory)
File.open("#{domain_directory}/index.md", "w") do |f|
f.write(DomainTemplate.new.render(domain))
end
end
def create_domain_events(domain)
domain_events_directory = "#{domains_catalog}/#{domain}/events"
FileUtils.mkdir(domain_events_directory)
build_events(domain)
end
def build_events(domain)
scan_domain_for_events(domain).each do |event|
domain_event_directory = "#{domains_catalog}/#{domain}/events/#{event}"
FileUtils.mkdir(domain_event_directory)
create_event_index(domain_event_directory, event)
end
end
def create_event_index(event_directory, event)
File.open("#{event_directory}/index.md", "w") do |f|
f.write(EventTemplate.new.render(event))
puts " - event: #{event}"
end
end
def scan_domain_for_events(domain)
files = Dir.glob("#{source_domain_directory(domain)}/**/*").reject { |f| File.directory?(f) || f =='.' || f == '..' }
list = files.map do |file|
IO.read(file).scan(/class (.*?) < #{EVENT_TYPE}/)
end.flatten
.reject {|e| e.empty?}
.compact
end
def replace_config_file
FileUtils.cp(CONFIG_FILE, root_catalog)
end
end
class DomainTemplate
def render(name)
<<~EOS
---
name: #{name}
summary: |
Summary
owners:
- Arkency
---
<Admonition>Domain description</Admonition>
### Details
-
<NodeGraph title="Domain Graph" />
EOS
end
end
class EventTemplate
def render(name)
<<~EOS
---
name: #{name}
version: 0.0.1
summary: |
Summary
owners:
- Arkency
---
#{name}
...
EOS
end
end