8000 Add support for custom configuration by unchidev · Pull Request #13 · koedame/rails-mermaid_erd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for custom configuration #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"customizations": {
"vscode": {
"extensions": [
"GitHub.copilot"
]
}
}
Expand Down
11 changes: 11 additions & 0 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ mermaid_erd
このファイルを共有すれば、Ruby on Rails環境が無くても使用できます。サーバーにアップロードすれば、同じURLを共有することもできます。
CIと連携して生成から共有までを自動化するのはとても有効な手段です。

## 設定

`./config/mermaid_erd.yml` を設置することで設定を変更できます。
設定例は [./docs/example.yml](./docs/example.yml) を参照してください。

設定項目は次のとおりです。

| キー | 説明 | 初期値 |
| --- | --- | --- |
| `result_path` | 生成されるファイルのパス。 | `mermaid_erd/index.html` |

<!--
TODO:
## Contributing
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ If you share this file, it can be used by those who do not have Ruby on Rails en

It would be very smart to generate it automatically using CI.

## Configuration

`./config/mermaid_erd.yml` to customize the configuration.
See [./docs/example.yml](./docs/example.yml) for an example configuration.

The setting items are as follows.

| key | description | default |
| --- | --- | --- |
| `result_path` | Destination of generated files. | `mermaid_erd/index.html` |

<!--
TODO:
## Contributing
Expand Down
3 changes: 3 additions & 0 deletions docs/example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# result path
# default "mermaid_erd/index.html"
result_path: doc/erd.html
12 changes: 10 additions & 2 deletions lib/rails-mermaid_erd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
require "rake"
require "rake/dsl_definition"
require_relative "rails-mermaid_erd/version"
require_relative "rails-mermaid_erd/configuration"

module RailsMermaidErd
extend Rake::DSL

class << self
def configuration
@configuration ||= RailsMermaidErd::Configuration.new
end
end

desc "Generate Mermaid ERD."
task mermaid_erd: :environment do
result = {
Expand Down Expand Up @@ -194,9 +201,10 @@ module RailsMermaidErd
erb = ERB.new(File.read(File.expand_path("./templates/index.html.erb", __dir__)))
result_html = erb.result(binding)

result_dir = Rails.root.join("./mermaid_erd")
result_file = result_dir.join("./index.html")
result_dir = Rails.root.join(File.dirname(RailsMermaidErd.configuration.result_path))
FileUtils.mkdir_p(result_dir)

result_file = Rails.root.join(RailsMermaidErd.configuration.result_path)
File.write(result_file, result_html)
end
end
21 changes: 21 additions & 0 deletions lib/rails-mermaid_erd/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "yaml"

module RailsMermaidErd
class Configuration
attr_accessor :result_path

def initialize
config = {
result_path: "mermaid_erd/index.html"
}

config_file = Rails.root.join("config/mermaid_erd.yml")
if File.exist?(config_file)
custom_config = YAML.load(config_file.read).symbolize_keys
config = config.merge(custom_config)
end

@result_path = config[:result_path]
end
end
end
3 changes: 3 additions & 0 deletions spec/fixtures/files/configurations/valid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# result path
# default "mermaid_erd/index.html"
result_path: doc/erd.html
23 changes: 23 additions & 0 deletions spec/rails-mermaid_erd/configration_spec.rb
Original file line number Diff line number Diff line 5DA8 change
@@ -0,0 +1,23 @@
require "spec_helper"

describe RailsMermaidErd::Configuration do
let(:configuration) { RailsMermaidErd::Configuration.new }

context "when the config file does not exist" do
it "return default config value" do
expect(configuration.result_path).to eq("mermaid_erd/index.html")
end
end

context "when the config file exist" do
before do
FileUtils.cp(file_fixture("configurations/valid.yml").to_s, Rails.root.join("config/mermaid_erd.yml").to_s)
end
after do
FileUtils.rm(Rails.root.join("config/mermaid_erd.yml").to_s)
end
it "return overwrite config value" do
expect(configuration.result_path).to eq("doc/erd.html")
end
end
end
0