-
Notifications
You must be signed in to change notification settings - Fork 369
/
tasks.rake
85 lines (71 loc) · 2.47 KB
/
tasks.rake
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
require 'graphviz/utils'
module ErdRakeHelper
def say(message)
puts message unless Rake.application.options.silent
end
end
namespace :erd do
task :check_dependencies do
if RailsERD.options.generator == :graphviz
include GraphViz::Utils
unless find_executable("dot", nil)
raise "#{RailsERD.options.generator} Unable to find GraphViz's \"dot\" executable. Please " \
"visit https://voormedia.github.io/rails-erd/install.html for installation instructions."
end
end
end
task :options do
(RailsERD.options.keys.map(&:to_s) & ENV.keys).each do |option|
RailsERD.options[option.to_sym] = case ENV[option]
when "true", "yes" then true
when "false", "no" then false
when /,/ then ENV[option].split(/\s*,\s*/)
when /^\d+$/ then ENV[option].to_i
else
if option == 'only'
[ENV[option]]
else
ENV[option].to_sym
end
end
end
end
task :load_models do
include ErdRakeHelper
say "Loading application environment..."
Rake::Task[:environment].invoke
say "Loading code in search of Active Record models..."
begin
Rails.application.eager_load!
if Rails.application.respond_to?(:config) && !Rails.application.config.nil?
Rails.application.config.eager_load_namespaces.each(&:eager_load!) if Rails.application.config.respond_to?(:eager_load_namespaces)
end
rescue Exception => err
if Rake.application.options.trace
raise
else
trace = Rails.backtrace_cleaner.clean(err.backtrace)
error = (["Loading models failed!\nError occurred while loading application: #{err} (#{err.class})"] + trace).join("\n ")
raise error
end
end
raise "Active Record was not loaded." unless defined? ActiveRecord
end
task :generate => [:options, :check_dependencies, :load_models] do
include ErdRakeHelper
say "Generating Entity-Relationship Diagram for #{ActiveRecord::Base.descendants.length} models..."
file = case RailsERD.options.generator
when :mermaid
require "rails_erd/diagram/mermaid"
RailsERD::Diagram::Mermaid.create
when :graphviz
require "rails_erd/diagram/graphviz"
RailsERD::Diagram::Graphviz.create
else
raise "Unknown generator: #{RailsERD.options.generator}"
end
say "Done! Saved diagram to ./#{file}"
end
end
desc "Generate an Entity-Relationship Diagram based on your models"
task :erd => "erd:generate"