-
Notifications
You must be signed in to change notification settings - Fork 1
/
to_md
executable file
·121 lines (106 loc) · 3.43 KB
/
to_md
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
#!/usr/bin/ruby
####################################################################################
#
# This script parses ActiveRecord schema.rb files into markdown tables to
# document your database structure.
#
# It uses rails-erd gem to generate uml diagrams of the database.
#
# The table of contents is generated by the gen-toc script then inserted
# into the markdown file.
#
####################################################################################
require "net/http"
require "uri"
def remove_unwanted_characters(param)
param.gsub(',','').gsub('"','')
end
def to_human_uppercase(param)
param = param.split(' ')[1]
parts = remove_unwanted_characters(param).gsub('_',' ').split(' ')
uppercase_parts = Array.new
parts.each do |part|
uppercase_parts.push part.capitalize
end
uppercase_parts.join(' ')
end
def attribute_parts(params)
name = nil
value = nil
default = '~'
allow_null = '~'
comment = ''
parts = params.split(' ')
name = remove_unwanted_characters(parts[1])
value = parts.first.gsub('t.','')
if params.include? 'default: '
index = parts.index('default:') + 1
default = "`#{parts[index].gsub(',','').strip}`"
end
if params.include? 'null: '
index = parts.index('null:') + 1
allow_null = "`#{parts[index].gsub(',','').strip}`"
end
if params.include? 'comment: '
comment = " #{params.split('comment: ').last.gsub('"','').strip}"
end
{
name: name,
value: value,
default: default,
allow_null: allow_null,
comment: comment
}
end
def generate_images
system "rake erd warn=false filetype='png' notation=bachman filename='erd_simple'"
system "rake erd warn=false filetype='png' notation=bachman filename='erd_complex' polymorphism=true"
system "mkdir -p docs/images; mv erd_simple.png erd_complex.png docs/images"
end
def generate_tables
File.open( 'db/schema.rb' ).each do |line|
line = line.strip()
unless line.start_with? '#'
if line.include? 'create_table'
@file.puts ""
@file.puts "### #{to_human_uppercase(line)}"
@file.puts ""
@file.puts "| Name | Value | Default | Allow Null? | Description |"
@file.puts "| ------------------ | ----------------- | ------- | ----------- | ----------- |"
elsif line.start_with? 't.'
unless line.include? 't.index'
parts = attribute_parts(line)
@file.puts "| #{parts[:name]} | #{parts[:value]} | #{parts[:default]} | #{parts[:allow_null]} |#{parts[:comment]}"
end
end
end
end
end
def generate_header
@file.puts "# Database Schema"
@file.puts "> This is a complete guide of the database structure"
@file.puts ""
@file.puts "<!--ts-->"
@file.puts "<!--te-->"
@file.puts ""
@file.puts "## Database Diagram"
@file.puts ""
@file.puts "![alt text](images/erd_simple.png 'Simple ERD Diagram')"
@file.puts "*Generated by [Rails ERD](https://voormedia.github.io/rails-erd/)"
@file.puts ""
@file.puts "> A more detailed diagram can be found [here](images/erd_complex.png)"
@file.puts ""
@file.puts "## Table Descriptions"
end
def generate_toc
system "gen-toc docs/db_schema.md"
end
############################START OF SCRIPT#########################################
generate_images
@file = File.open('docs/db_schema.md', 'w')
generate_header
generate_tables
@file.close
generate_toc
# to_kramdown
############################END OF SCRIPT###########################################