rails-mermaid_erd_markdown 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ca036f9a93d9694b096ad1bce2d2f2ed21a46fa9b2e042a2a6324522301743be
4
+ data.tar.gz: e518276d23ef0e0b8efa1d68a2d36c2528b5c3f5297414e6bef8b9429c742640
5
+ SHA512:
6
+ metadata.gz: 8c0791403b7e7c91c35b3c5fb8b17f94fded6b414bf2c98d2f8fed34dd5e75e9b16615b6997bcec2b15c4f962902c5949de26cf69c346fffcee239374d3c9346
7
+ data.tar.gz: 685f21a2e320ceb5283c92f2f841c777032a97d8f621be3edbd08585b0fdc3b5966d5f05f84a563d4bd58ec3e3b19f81052d216d562843eac4dc9ec5902f2b9c
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # rails-mermaid_erd_markdown
2
+
3
+ This is a rails gem that extends the rails-mermaid_erd gem to generate a mermaid ERD for Rails Models in markdown directly in source code. This avoids the need to download the ERD through a html page, which can be difficult for scenarios where one would like to programmatically generate an ERD.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rails-mermaid_erd_markdown'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rails-mermaid_erd_markdown
20
+
21
+ ## Usage
22
+
23
+ To generate a mermaid ERD in markdown, run `rails generate_erd` or `rake generate_erd` from the terminal
24
+
25
+ The default path for the generated ERD is `app/models/ERD.md`. You can modify this by creating an `erd.yml` file in the root directory
26
+ and modifying the `result_path` as seen in the example at `docs/examples/erd.yml`. Make sure to include the markdown file name you
27
+ wish to generate in the path.
28
+
29
+ If an ERD already exists at the path specified, it will be parsed to determine if it is up to date. If it is, nothing happens. If it
30
+ is not, the ERD is ed.
31
+
32
+ One can create self-updating, living documentation by integrating this rake task into their CI. This ensures that the ERD is always up
33
+ to date and accurately describes the latest state of the models and their relationships.
34
+
35
+ You can view the ERD by navigating to the file in Github, which supports rendering mermaid diagrams from code. If you are a Visual
36
+ Studio Code user, you can use the [Markdown Preview Enhanced](https://marketplace.visualstudio.com/items?itemName=shd101wyy.markdown-preview-enhanced) extension to view the ERD directly in your IDE.
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails-mermaid_erd_markdown. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rails-mermaid_erd_markdown/blob/master/CODE_OF_CONDUCT.md).
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
51
+
52
+ ## Code of Conduct
53
+
54
+ Everyone interacting in the rails-mermaid_erd_markdown project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rails-mermaid_erd_markdown/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/setup"
4
+ require "bundler/gem_tasks"
5
+ require "rake/testtask"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.libs << "lib"
10
+ t.test_files = FileList["test/**/test_*.rb"]
11
+ end
12
+
13
+ require "rubocop/rake_task"
14
+
15
+ RuboCop::RakeTask.new
16
+
17
+ task default: %i[test rubocop]
@@ -0,0 +1,4 @@
1
+ # This is the path and filename of the generated ERD. You can modify it to change where the ERD is stored/updated.
2
+ # Default output path: 'app/ERD.md'
3
+ erd:
4
+ output_path: 'app/ERD.md'
@@ -0,0 +1,20 @@
1
+ require "yaml"
2
+ require "logger"
3
+
4
+ class MermaidErdMarkdown::Configuration
5
+ attr_accessor :output_path
6
+
7
+ def initialize
8
+ config = {
9
+ output_path: "app/models/ERD.md"
10
+ }
11
+ erd_config_path = "erd.yml"
12
+
13
+ begin
14
+ erd_yml = YAML.safe_load File.open(erd_config_path)
15
+ @output_path = erd_yml["erd"]["output_path"]
16
+ rescue StandardError
17
+ @output_path = config[:output_path]
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MermaidErdMarkdown
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rails-mermaid_erd_markdown/configuration"
4
+ require "digest/md5"
5
+ require "rake"
6
+ require "rake/dsl_definition"
7
+ require "pathname"
8
+ require "logger"
9
+
10
+ module MermaidErdMarkdown
11
+ extend Rake::DSL
12
+
13
+ class << self
14
+ attr_writer :logger
15
+
16
+ def logger
17
+ @logger ||= Logger.new($stdout).tap do |log|
18
+ log.progname = name
19
+ end
20
+ end
21
+
22
+ def configuration
23
+ @configuration ||= MermaidErdMarkdown::Configuration.new
24
+ end
25
+
26
+ def perform
27
+ new_erd = generate_mermaid_erd
28
+ existing_erd_path = Pathname.new(configuration.output_path)
29
+
30
+ unless existing_erd_path.exist?
31
+ logger.info("ERD does not currently exist at result path. Creating...")
32
+ begin
33
+ create_erd_file(new_erd)
34
+ logger.info("ERD successfully created at #{configuration.output_path}")
35
+ rescue StandardError
36
+ logger.info("Could not create ERD. Output path is invalid.")
37
+ end
38
+
39
+ return
40
+ end
41
+
42
+ existing_erd = File.read(existing_erd_path)
43
+ update_erd_file(existing_erd, new_erd)
44
+ end
45
+
46
+ def update_erd_file(current_erd, new_erd)
47
+ # check if two diagrams are the same by comparing their MD5 hashes
48
+ if Digest::MD5.hexdigest(current_erd) == Digest::MD5.hexdigest(new_erd)
49
+ logger.info("ERD already exists and is up to date. Skipping...")
50
+ return
51
+ end
52
+
53
+ logger.info("ERD already exists but is out of date. Overwriting...")
54
+ File.write(configuration.output_path, new_erd)
55
+ logger.info("ERD successfully updated")
56
+
57
+ nil
58
+ end
59
+
60
+ def create_erd_file(erd)
61
+ File.write(configuration.output_path, erd)
62
+
63
+ nil
64
+ end
65
+
66
+ def generate_mermaid_erd
67
+ data = RailsMermaidErd::Builder.model_data
68
+
69
+ is_show_key = true
70
+ is_show_relation_comment = true
71
+
72
+ lines = ["```mermaid"]
73
+ lines << "erDiagram"
74
+ lines << " %% --------------------------------------------------------"
75
+ lines << " %% Entity-Relationship Diagram"
76
+ lines << " %% --------------------------------------------------------"
77
+ lines << ""
78
+
79
+ data[:Models].each do |model|
80
+ lines << " %% table name: #{model[:TableName]}"
81
+ lines << " #{model[:ModelName].tr(":", "-")}{"
82
+
83
+ model[:Columns].each do |column|
84
+ key = is_show_key ? column[:key] : ""
85
+ lines << " #{column[:type]} #{column[:name]} #{key} "
86
+ end
87
+
88
+ lines << " }"
89
+ lines << ""
90
+ end
91
+
92
+ data[:Relations].each do |relation|
93
+ left_model_name = relation[:LeftModelName].tr(":", "-")
94
+ right_model_name = relation[:RightModelName].tr(":", "-")
95
+ comment = is_show_relation_comment ? ": \"#{relation[:Comment]}\"" : ": \"\""
96
+
97
+ lines << " #{left_model_name} #{relation[:LeftValue]}#{relation[:Line]}#{relation[:RightValue]} #{right_model_name} #{comment}"
98
+ end
99
+
100
+ lines << "```"
101
+ lines.join("\n")
102
+ end
103
+ end
104
+
105
+ desc "Generate/update mermaid ERD diagram for database Models."
106
+ task generate_erd: :environment do
107
+ perform
108
+ end
109
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-mermaid_erd_markdown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - humzahkiani
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-05-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ description:
28
+ email:
29
+ - 89326566+humzahkiani@users.noreply.github.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - Rakefile
36
+ - docs/examples/erd.yml
37
+ - lib/rails-mermaid_erd_markdown.rb
38
+ - lib/rails-mermaid_erd_markdown/configuration.rb
39
+ - lib/rails-mermaid_erd_markdown/version.rb
40
+ homepage: https://github.com/humzahkiani/rails-mermaid_erd_markdown
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://github.com/humzahkiani/rails-mermaid_erd_markdown
45
+ source_code_uri: https://github.com/humzahkiani/rails-mermaid_erd_markdown
46
+ changelog_uri: https://github.com/humzahkiani/rails-mermaid_erd_markdown/blob/main/CHANGELOG.md
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.6.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.3.3
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: This is a rails gem that extends the rails-mermaid_erd gem to generate a
66
+ mermaid ERD for Rails Models in markdown directly in source code.
67
+ test_files: []