-
Notifications
You must be signed in to change notification settings - Fork 2
/
erd_for.rb
executable file
·149 lines (103 loc) · 3.87 KB
/
erd_for.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
#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true
##########################################################
###
## File: erd_for.rb
## Desc: Create ERD files for given database models
## By: Dewayne VanHoozer ([email protected])
#
require 'pathname'
RAILS_ROOT = Pathname.new( ENV['RR'] || ENV['RAILS_ROOT'] || ENV['PWD'] )
DEFAULT_OUT_DIR = RAILS_ROOT + 'etc/doc'
# Get String#snakecase from here:
require 'mods/string_mods' # from lib/ruby
require 'amazing_print'
require 'debug_me'
include DebugMe
require 'cli_helper'
include CliHelper
configatron.version = '0.0.2'
HELP = <<EOHELP
Important:
Use --group to generate one ERD that has all the models listed.
Without --group individual ERD files will be created for each model.
If --group then --title should also be used otherwise the title
will be the name of the first model in the list.
Note that --exclude will not automatically exclude models that are
"downstream" from the excluded models. i.e. the excluded model's
relationships. To exclude those models as well they MUST be in
the list of models excluded.
EOHELP
cli_helper("Create ERD files for given database models") do |o|
o.string '-t', '--type', 'Type of Output File', default: 'pdf'
o.int '-r', '--recursion_depth', 'Recursive Depth of Relationships', default: 1
o.array '-e', '--exclude', 'Exclude model names separated by comma', default: []
o.array '-m', '--models', 'Model names separated by comma'
o.string '-T', '--title', 'Title defaults to Model Name', default: ''
o.boolean '-g', '--group', 'Group models together into one ERD', default: false
o.path '-o', '--outdir', 'Directory to save ERD files', default: DEFAULT_OUT_DIR
o.string '-f', '--filename', 'Base name of the output file', default: ''
end
# Display the usage info
if ARGV.empty?
show_usage
exit
end
# Error check your stuff; use error('some message') and warning('some message')
unless configatron.outdir.exist?
puts "Warning: Directory does not exist - #{configatron.outdir}"
print "Do you want to create #{configatron.outdir}? (y/N) "
answer = (STDIN.gets).chomp.strip.downcase
if answer.size > 0 && answer.start_with?('y')
configatron.outdir.mkpath
end
end
# At this point we know outdir exists but it had better be a directory!
unless configatron.outdir.directory?
error "Not a directory: #{configatron.outdir}"
end
abort_if_errors
######################################################
# Local methods
######################################################
# Main
at_exit do
puts
puts "Done."
puts
end
ap configatron.to_h if verbose? || debug?
base_command = %w[
bundle exec erd
--orientation=vertical
--cluster
--prepend_primary=true
--sort=true
--direct
].join(' ') + " --only_recursion_depth=#{configatron.recursion_depth}"
base_command += " --exclude #{configatron.exclude.join(',')}," unless configatron.exclude.empty?
configatron.models.each do |model|
command = base_command.dup
puts
puts "============================================================"
if group?
puts "Generating ERD for #{configatron.models.join(', ')} ... "
all_models = configatron.models.join(',')
command += " --only='#{all_models},'"
else
puts "Generating ERD for #{model} ... "
command += " --only='#{model},'"
end
command += configatron.title.empty? ? " --title='#{model} ERD\n#{Time.now}'" : " --title='#{configatron.title}\n#{Time.now}'"
if configatron.filename.empty?
model_filename = model.snakecase.gsub('/', '_')
command += " --filename=#{configatron.outdir}/#{model_filename}_erd"
else
command += " --filename=#{configatron.outdir}/#{configatron.filename}"
end
command += " --filetype=#{configatron.type}"
puts "Command: #{command}"
`#{command}`
break if group?
end