ahnnotate 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +55 -0
- data/LICENSE.txt +21 -0
- data/README.md +118 -0
- data/Rakefile +10 -0
- data/ahnnotate.gemspec +33 -0
- data/bin/console +14 -0
- data/bin/rake +29 -0
- data/bin/setup +8 -0
- data/exe/ahnnotate +14 -0
- data/lib/ahnnotate/cli.rb +116 -0
- data/lib/ahnnotate/column.rb +60 -0
- data/lib/ahnnotate/config.rb +67 -0
- data/lib/ahnnotate/error.rb +8 -0
- data/lib/ahnnotate/facet/models/main.rb +49 -0
- data/lib/ahnnotate/facet/models/module_node.rb +123 -0
- data/lib/ahnnotate/facet/models/processor.rb +91 -0
- data/lib/ahnnotate/facet/models/resolve_active_record_models.rb +42 -0
- data/lib/ahnnotate/facet/models/resolve_class_relationships.rb +67 -0
- data/lib/ahnnotate/facet/models/standin.rb +41 -0
- data/lib/ahnnotate/facet/models.rb +10 -0
- data/lib/ahnnotate/function/format.rb +36 -0
- data/lib/ahnnotate/function/main.rb +32 -0
- data/lib/ahnnotate/function/run.rb +24 -0
- data/lib/ahnnotate/function/tabularize.rb +53 -0
- data/lib/ahnnotate/index.rb +26 -0
- data/lib/ahnnotate/options.rb +48 -0
- data/lib/ahnnotate/rails.rake +44 -0
- data/lib/ahnnotate/railtie.rb +7 -0
- data/lib/ahnnotate/table.rb +44 -0
- data/lib/ahnnotate/tables.rb +55 -0
- data/lib/ahnnotate/version.rb +3 -0
- data/lib/ahnnotate/vfs.rb +69 -0
- data/lib/ahnnotate/vfs_driver/filesystem.rb +68 -0
- data/lib/ahnnotate/vfs_driver/hash.rb +59 -0
- data/lib/ahnnotate/vfs_driver/read_only_filesystem.rb +8 -0
- data/lib/ahnnotate.rb +32 -0
- metadata +202 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8c69ae3868972bae9fd2390bf305aba574c1a9522049ed1ce203b25b8423b9cb
|
4
|
+
data.tar.gz: dd3d1e477e1351de18d99f54d1471ac1fc03c5a815a4eee0c78d104c3486aacd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6469c1d8fe2603f98757c149aa0d7e16e3a9460d635c8395a8a548268b743ae2c16bd3a958f34acf9f19cc4dd072c579bcae34f207953977d59483bd207fc9ff
|
7
|
+
data.tar.gz: f2b2e026256a531b29eb2d3dc6d9b8950fa1ef90303b923bacdaa4f0e21d1d7d471d73e2be072058b5530a9e06e9c5bb8a0e62e574c45db2f46dfaa0e3260231
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ahnnotate (0.2.0)
|
5
|
+
activerecord (>= 4.0.0, < 6)
|
6
|
+
parser
|
7
|
+
proc_party
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activemodel (5.2.1.1)
|
13
|
+
activesupport (= 5.2.1.1)
|
14
|
+
activerecord (5.2.1.1)
|
15
|
+
activemodel (= 5.2.1.1)
|
16
|
+
activesupport (= 5.2.1.1)
|
17
|
+
arel (>= 9.0)
|
18
|
+
activesupport (5.2.1.1)
|
19
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
20
|
+
i18n (>= 0.7, < 2)
|
21
|
+
minitest (~> 5.1)
|
22
|
+
tzinfo (~> 1.1)
|
23
|
+
arel (9.0.0)
|
24
|
+
ast (2.4.0)
|
25
|
+
coderay (1.1.2)
|
26
|
+
concurrent-ruby (1.1.3)
|
27
|
+
i18n (1.1.1)
|
28
|
+
concurrent-ruby (~> 1.0)
|
29
|
+
method_source (0.9.2)
|
30
|
+
minitest (5.11.3)
|
31
|
+
parser (2.5.3.0)
|
32
|
+
ast (~> 2.4.0)
|
33
|
+
proc_party (0.2.0)
|
34
|
+
pry (0.12.2)
|
35
|
+
coderay (~> 1.1.0)
|
36
|
+
method_source (~> 0.9.0)
|
37
|
+
rake (10.5.0)
|
38
|
+
sqlite3 (1.3.13)
|
39
|
+
thread_safe (0.3.6)
|
40
|
+
tzinfo (1.2.5)
|
41
|
+
thread_safe (~> 0.1)
|
42
|
+
|
43
|
+
PLATFORMS
|
44
|
+
ruby
|
45
|
+
|
46
|
+
DEPENDENCIES
|
47
|
+
bundler (~> 1.17)
|
48
|
+
ahnnotate!
|
49
|
+
minitest (~> 5.0)
|
50
|
+
pry
|
51
|
+
rake (~> 10.0)
|
52
|
+
sqlite3
|
53
|
+
|
54
|
+
BUNDLED WITH
|
55
|
+
1.17.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Zach Ahn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# Ahnnotate
|
2
|
+
|
3
|
+
Ahnnotate comments on your ActiveRecord models with their respective schemas!
|
4
|
+
|
5
|
+
Ahnnotate performs static analysis on your files to determine which files
|
6
|
+
should be annotated. The primary goals of ahnnotate are ease of configuration
|
7
|
+
and correctness.
|
8
|
+
|
9
|
+
It's very similar to [annotate][annotate] and was inspired heavily by it.
|
10
|
+
Annotate has more features than ahnnotate does; it may fit your needs much
|
11
|
+
better.
|
12
|
+
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem "ahnnotate"
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
```
|
25
|
+
bundle
|
26
|
+
```
|
27
|
+
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Please use source control management software like git, mercurial, etc! The
|
32
|
+
purpose of this software is to overwrite your source files. Although I'm pretty
|
33
|
+
comfortable running this in my own projects, I've definitely made a couple bugs
|
34
|
+
in my lifetime!
|
35
|
+
|
36
|
+
|
37
|
+
### In a Rails app
|
38
|
+
|
39
|
+
To run it manually, run:
|
40
|
+
|
41
|
+
```bash
|
42
|
+
bundle exec ahnnotate --fix
|
43
|
+
# OR
|
44
|
+
bundle exec rake ahnnotate
|
45
|
+
```
|
46
|
+
|
47
|
+
(Leaving out the `--fix` argument runs the command but doesn't make any changes
|
48
|
+
to your filesystem. The rake task assumes that you do want to fix by default.)
|
49
|
+
|
50
|
+
Ahnnotate automatically runs after running migrations. This can be disabled,
|
51
|
+
though by creating a `.ahnnotate.yml` configuration file and setting
|
52
|
+
`rake_db_autorun: false`.
|
53
|
+
|
54
|
+
See `bundle exec ahnnotate --help` for some more help.
|
55
|
+
|
56
|
+
|
57
|
+
### In apps using ActiveRecord but not Rails
|
58
|
+
|
59
|
+
You'll need some initial configuration to get things working. But I'm sure
|
60
|
+
you're used to that since you aren't using Rails! 😝
|
61
|
+
|
62
|
+
You'll need a `.ahnnotate.yml` file at the root of your project with one key,
|
63
|
+
`boot:`. Ahnnotate will `eval` the contents, and it really just needs it to
|
64
|
+
connect to your database. It might look something like the following, I tested
|
65
|
+
it with one of my Rails apps:
|
66
|
+
|
67
|
+
```yaml
|
68
|
+
---
|
69
|
+
boot: |
|
70
|
+
require "dotenv/load" # If you use something like dotenv to load ENV variables
|
71
|
+
require "yaml"
|
72
|
+
require "erb"
|
73
|
+
require "active_record"
|
74
|
+
|
75
|
+
config_file = File.read("config/database.yml")
|
76
|
+
config_file = ERB.new(config_file).result(binding)
|
77
|
+
config = YAML.load(config_file)
|
78
|
+
|
79
|
+
environment = ENV.fetch("RAILS_ENV", "development")
|
80
|
+
database_config = config[environment]
|
81
|
+
|
82
|
+
ActiveRecord::Base.establish_connection(database_config)
|
83
|
+
```
|
84
|
+
|
85
|
+
You may also need to configure the location of your models.
|
86
|
+
|
87
|
+
```yaml
|
88
|
+
annotate:
|
89
|
+
models:
|
90
|
+
path: path/to/your/models # also accepts an array of paths
|
91
|
+
```
|
92
|
+
|
93
|
+
It doesn't officially support automatically running after migrations on
|
94
|
+
non-Rails apps.
|
95
|
+
|
96
|
+
See `bundle exec ahnnotate --help` for some more help.
|
97
|
+
|
98
|
+
|
99
|
+
## Development
|
100
|
+
|
101
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
102
|
+
`rake test` to run the tests. You can also run `bin/console` for an interactive
|
103
|
+
prompt that will allow you to experiment.
|
104
|
+
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
Bug reports and pull requests are welcome on its [project page][github].
|
109
|
+
|
110
|
+
|
111
|
+
## License
|
112
|
+
|
113
|
+
The gem is available as open source under the terms of the [MIT License][mit].
|
114
|
+
|
115
|
+
|
116
|
+
[annotate]: https://github.com/ctran/annotate_models
|
117
|
+
[github]: https://github.com/zachahn/ahnnotate
|
118
|
+
[mit]: https://opensource.org/licenses/MIT
|
data/Rakefile
ADDED
data/ahnnotate.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "ahnnotate/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ahnnotate"
|
7
|
+
spec.version = Ahnnotate::VERSION
|
8
|
+
spec.authors = ["Zach Ahn"]
|
9
|
+
spec.email = ["engineering@zachahn.com"]
|
10
|
+
|
11
|
+
spec.summary = %("Annotate" your models)
|
12
|
+
spec.description = %(Heavily inspired by the `annotate` gem)
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
spec.add_development_dependency "sqlite3"
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
|
30
|
+
spec.add_runtime_dependency "activerecord", ">= 4.0.0", "< 6"
|
31
|
+
spec.add_runtime_dependency "parser"
|
32
|
+
spec.add_runtime_dependency "proc_party"
|
33
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ahnnotate"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rake' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("rake", "rake")
|
data/bin/setup
ADDED
data/exe/ahnnotate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
if File.exist?(File.expand_path("../.git", __dir__))
|
4
|
+
lib = File.expand_path("../lib", __dir__)
|
5
|
+
|
6
|
+
if !$LOAD_PATH.include?(lib)
|
7
|
+
$LOAD_PATH.unshift(lib)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
require "ahnnotate"
|
12
|
+
require "ahnnotate/cli"
|
13
|
+
|
14
|
+
Ahnnotate::Cli.new(name: File.basename(__FILE__)).run(ARGV)
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require "optparse"
|
2
|
+
require "rubygems/text"
|
3
|
+
|
4
|
+
module Ahnnotate
|
5
|
+
class Cli
|
6
|
+
def initialize(name:)
|
7
|
+
@name = name
|
8
|
+
@options = Options.new(fix: false)
|
9
|
+
end
|
10
|
+
|
11
|
+
def run(argv, config = nil)
|
12
|
+
argv = argv.dup
|
13
|
+
|
14
|
+
debug_options = argv.delete("--debug-opts") || argv.delete("--debug-options")
|
15
|
+
|
16
|
+
parser.parse(argv)
|
17
|
+
|
18
|
+
if debug_options
|
19
|
+
puts @options
|
20
|
+
end
|
21
|
+
|
22
|
+
if @options.exit?
|
23
|
+
return
|
24
|
+
end
|
25
|
+
|
26
|
+
root = Pathname.new(Dir.pwd)
|
27
|
+
config ||= Config.load(root: root)
|
28
|
+
|
29
|
+
Function::Main.new(root, @options, config).call
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def parser
|
35
|
+
if instance_variable_defined?(:@parser)
|
36
|
+
return @parser
|
37
|
+
end
|
38
|
+
|
39
|
+
@parser = OptionParser.new do |opts|
|
40
|
+
opts.banner = "Usage: #{@name} [options]"
|
41
|
+
opts.separator ""
|
42
|
+
opts.separator "Command line options:"
|
43
|
+
opts.separator ""
|
44
|
+
|
45
|
+
opts.on("--[no-]fix", "Actually modify files") do |fix|
|
46
|
+
@options.fix = fix
|
47
|
+
end
|
48
|
+
|
49
|
+
opts.on("-h", "--help", "Prints this help message") do
|
50
|
+
@options.exit = true
|
51
|
+
puts opts
|
52
|
+
end
|
53
|
+
|
54
|
+
opts.on("--version", "Print version") do
|
55
|
+
@options.exit = true
|
56
|
+
puts Ahnnotate::VERSION
|
57
|
+
end
|
58
|
+
|
59
|
+
opts.separator ""
|
60
|
+
opts.separator ""
|
61
|
+
opts.separator "Configuration file:"
|
62
|
+
opts.separator ""
|
63
|
+
|
64
|
+
# The gsub converts all non-consecutive newlines into a space.
|
65
|
+
# Consecutive newlines are left alone.
|
66
|
+
configuration_file_help = <<-MSG.gsub(/(?<!\n)\n(?!\n)/, " ")
|
67
|
+
The configuration file (`.ahnnotate.yml`) must be placed at the root of your
|
68
|
+
project, or wherever you will be calling this script. Any unset config option
|
69
|
+
will fall back to the following default configuration:
|
70
|
+
|
71
|
+
%{default}
|
72
|
+
|
73
|
+
In Rails projects (projects which explicitly use the `Rails` gem), ahnnotate
|
74
|
+
merges the following configs in with the defaults. This should allow ahnnotate
|
75
|
+
to work out of the box.
|
76
|
+
|
77
|
+
%{rails_additions}
|
78
|
+
|
79
|
+
(It should generally be possible to speed up the "boot" process by only loading
|
80
|
+
ActiveRecord, custom inflections, etc. Note though that the actual models do
|
81
|
+
not need to be loaded into the runtime; ahnnotate will read them as needed)
|
82
|
+
MSG
|
83
|
+
|
84
|
+
output = format(
|
85
|
+
configuration_file_help,
|
86
|
+
default: yaml_dump_and_indent(Ahnnotate::Config.default, indent: 4),
|
87
|
+
rails_additions: yaml_dump_and_indent(Ahnnotate::Config.rails_additions, indent: 4)
|
88
|
+
)
|
89
|
+
|
90
|
+
opts.separator wrap_and_indent(text: output, width: 72 + 4, indent: 4)
|
91
|
+
end
|
92
|
+
|
93
|
+
@parser
|
94
|
+
end
|
95
|
+
|
96
|
+
def yaml_dump_and_indent(object, indent:)
|
97
|
+
YAML.dump(object).gsub(/^/, " " * indent)
|
98
|
+
end
|
99
|
+
|
100
|
+
def wrap_and_indent(text:, width:, indent:)
|
101
|
+
GemTextWrapper.format_text(text, width, indent)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# I really had a feeling Ruby would have a "word wrap" feature somewhere.
|
106
|
+
# Somewhere in the Rubygems code was not my first guess lol.
|
107
|
+
module GemTextWrapper
|
108
|
+
class << self
|
109
|
+
include Gem::Text
|
110
|
+
|
111
|
+
def clean_text(text)
|
112
|
+
text
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Ahnnotate
|
2
|
+
class Column
|
3
|
+
attr_reader :name
|
4
|
+
attr_reader :type
|
5
|
+
|
6
|
+
def initialize(name:, type:, nullable:, primary_key:, default:)
|
7
|
+
@name = name
|
8
|
+
@type = type
|
9
|
+
@nullable = nullable
|
10
|
+
@primary_key = primary_key
|
11
|
+
@default = default
|
12
|
+
end
|
13
|
+
|
14
|
+
def details
|
15
|
+
if @details
|
16
|
+
return @details
|
17
|
+
end
|
18
|
+
|
19
|
+
details = []
|
20
|
+
|
21
|
+
if !nullable?
|
22
|
+
details.push("not null")
|
23
|
+
end
|
24
|
+
|
25
|
+
if has_default?
|
26
|
+
details.push("default (#{default.inspect})")
|
27
|
+
end
|
28
|
+
|
29
|
+
if primary_key?
|
30
|
+
details.push("primary key")
|
31
|
+
end
|
32
|
+
|
33
|
+
@details = details.join(", ")
|
34
|
+
end
|
35
|
+
|
36
|
+
def default
|
37
|
+
if @default.nil?
|
38
|
+
return nil
|
39
|
+
end
|
40
|
+
|
41
|
+
if type == "boolean"
|
42
|
+
return !ActiveModel::Type::Boolean::FALSE_VALUES.include?(@default)
|
43
|
+
end
|
44
|
+
|
45
|
+
@default
|
46
|
+
end
|
47
|
+
|
48
|
+
def has_default?
|
49
|
+
!default.nil?
|
50
|
+
end
|
51
|
+
|
52
|
+
def nullable?
|
53
|
+
!!@nullable
|
54
|
+
end
|
55
|
+
|
56
|
+
def primary_key?
|
57
|
+
!!@primary_key
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Ahnnotate
|
2
|
+
class Config
|
3
|
+
def self.load(root:)
|
4
|
+
config_path = root.join(".ahnnotate.yml")
|
5
|
+
|
6
|
+
if !config_path.exist?
|
7
|
+
return new({})
|
8
|
+
end
|
9
|
+
|
10
|
+
loaded_config = YAML.safe_load(File.read(config_path))
|
11
|
+
new(loaded_config)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.default
|
15
|
+
@default ||= {
|
16
|
+
"boot" => nil,
|
17
|
+
"rake_db_autorun" => false,
|
18
|
+
"annotate" => {
|
19
|
+
"models" => {
|
20
|
+
"enabled" => true,
|
21
|
+
"path" => "app/models",
|
22
|
+
},
|
23
|
+
},
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.rails_additions
|
28
|
+
@rails_additions ||= {
|
29
|
+
"boot" => %(require File.expand_path("config/environment").to_s),
|
30
|
+
"rake_db_autorun" => true,
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.effective_default
|
35
|
+
if @effective_default
|
36
|
+
return @effective_default
|
37
|
+
end
|
38
|
+
|
39
|
+
@effective_default ||= YAML.load(YAML.dump(default)) # deep dup
|
40
|
+
|
41
|
+
if Gem.loaded_specs.key?("rails")
|
42
|
+
@effective_default.merge!(rails_additions)
|
43
|
+
end
|
44
|
+
|
45
|
+
@effective_default
|
46
|
+
end
|
47
|
+
|
48
|
+
def initialize(config)
|
49
|
+
@config =
|
50
|
+
if config.is_a?(Hash)
|
51
|
+
config
|
52
|
+
else
|
53
|
+
{}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def [](*args)
|
58
|
+
specified_config = @config.dig(*args)
|
59
|
+
|
60
|
+
if specified_config.nil?
|
61
|
+
self.class.effective_default.dig(*args)
|
62
|
+
else
|
63
|
+
specified_config
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Ahnnotate
|
2
|
+
module Facet
|
3
|
+
module Models
|
4
|
+
class Main
|
5
|
+
def initialize(config, tables, vfs)
|
6
|
+
@config = config
|
7
|
+
@tables = tables
|
8
|
+
@vfs = vfs
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
formatter = Function::Format.new(comment: "#")
|
13
|
+
|
14
|
+
model_nodes.each do |model_node|
|
15
|
+
table = @tables[model_node.table_name]
|
16
|
+
|
17
|
+
if table.nil?
|
18
|
+
next
|
19
|
+
end
|
20
|
+
|
21
|
+
@vfs[model_node.path] =
|
22
|
+
formatter.call(table, @vfs[model_node.path])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def model_nodes
|
27
|
+
@model_nodes ||=
|
28
|
+
begin
|
29
|
+
model_path = @config["annotate", "models", "path"]
|
30
|
+
model_files = @vfs.each_in(model_path)
|
31
|
+
processor = Processor.new
|
32
|
+
models = model_files.map do |path, contents|
|
33
|
+
module_nodes = processor.call(contents)
|
34
|
+
module_nodes.each { |node| node.path = path }
|
35
|
+
module_nodes
|
36
|
+
end
|
37
|
+
|
38
|
+
models
|
39
|
+
.flatten
|
40
|
+
.yield_self(&ResolveClassRelationships.new)
|
41
|
+
.yield_self(&ResolveActiveRecordModels.new)
|
42
|
+
.select(&:is_a_kind_of_activerecord_base?)
|
43
|
+
.reject(&:abstract_class?)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|