ahnnotate 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +6 -0
  5. data/Gemfile.lock +55 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +118 -0
  8. data/Rakefile +10 -0
  9. data/ahnnotate.gemspec +33 -0
  10. data/bin/console +14 -0
  11. data/bin/rake +29 -0
  12. data/bin/setup +8 -0
  13. data/exe/ahnnotate +14 -0
  14. data/lib/ahnnotate/cli.rb +116 -0
  15. data/lib/ahnnotate/column.rb +60 -0
  16. data/lib/ahnnotate/config.rb +67 -0
  17. data/lib/ahnnotate/error.rb +8 -0
  18. data/lib/ahnnotate/facet/models/main.rb +49 -0
  19. data/lib/ahnnotate/facet/models/module_node.rb +123 -0
  20. data/lib/ahnnotate/facet/models/processor.rb +91 -0
  21. data/lib/ahnnotate/facet/models/resolve_active_record_models.rb +42 -0
  22. data/lib/ahnnotate/facet/models/resolve_class_relationships.rb +67 -0
  23. data/lib/ahnnotate/facet/models/standin.rb +41 -0
  24. data/lib/ahnnotate/facet/models.rb +10 -0
  25. data/lib/ahnnotate/function/format.rb +36 -0
  26. data/lib/ahnnotate/function/main.rb +32 -0
  27. data/lib/ahnnotate/function/run.rb +24 -0
  28. data/lib/ahnnotate/function/tabularize.rb +53 -0
  29. data/lib/ahnnotate/index.rb +26 -0
  30. data/lib/ahnnotate/options.rb +48 -0
  31. data/lib/ahnnotate/rails.rake +44 -0
  32. data/lib/ahnnotate/railtie.rb +7 -0
  33. data/lib/ahnnotate/table.rb +44 -0
  34. data/lib/ahnnotate/tables.rb +55 -0
  35. data/lib/ahnnotate/version.rb +3 -0
  36. data/lib/ahnnotate/vfs.rb +69 -0
  37. data/lib/ahnnotate/vfs_driver/filesystem.rb +68 -0
  38. data/lib/ahnnotate/vfs_driver/hash.rb +59 -0
  39. data/lib/ahnnotate/vfs_driver/read_only_filesystem.rb +8 -0
  40. data/lib/ahnnotate.rb +32 -0
  41. metadata +202 -0
@@ -0,0 +1,55 @@
1
+ module Ahnnotate
2
+ class Tables
3
+ include Enumerable
4
+
5
+ def initialize(connection = ActiveRecord::Base.connection)
6
+ @connection = connection
7
+ end
8
+
9
+ def to_h
10
+ map { |table| [table.name, table] }.to_h
11
+ end
12
+
13
+ def each
14
+ if !block_given?
15
+ enum_for(:each)
16
+ end
17
+
18
+ @connection.tables.each do |table_name|
19
+ primary_key = ActiveRecord::Base.get_primary_key(table_name)
20
+
21
+ columns = @connection.columns(table_name).map do |c|
22
+ is_primary_key =
23
+ if primary_key.is_a?(Array)
24
+ primary_key.include?(c.name)
25
+ else
26
+ primary_key == c.name
27
+ end
28
+
29
+ Column.new(
30
+ name: c.name,
31
+ type: c.type.to_s,
32
+ nullable: c.null,
33
+ primary_key: is_primary_key,
34
+ default: c.default
35
+ )
36
+ end
37
+
38
+ indexes = @connection.indexes(table_name).map do |i|
39
+ Index.new(
40
+ name: i.name,
41
+ columns: i.columns,
42
+ unique: i.unique,
43
+ comment: i.comment
44
+ )
45
+ end
46
+
47
+ yield Table.new(
48
+ name: table_name,
49
+ columns: columns,
50
+ indexes: indexes
51
+ )
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module Ahnnotate
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,69 @@
1
+ module Ahnnotate
2
+ class Vfs
3
+ include Enumerable
4
+
5
+ def initialize(driver)
6
+ @driver = driver
7
+ end
8
+
9
+ def []=(path, content)
10
+ if content.nil?
11
+ return
12
+ end
13
+
14
+ if @driver.dir?(path)
15
+ raise Error::VfsWriteError, "can't write to directory"
16
+ end
17
+
18
+ if !accessible_path?(path)
19
+ raise Error::VfsOutsideOfRoot, "path seems to be outside of root"
20
+ end
21
+
22
+ @driver[path] = content
23
+ end
24
+
25
+ def [](path)
26
+ if @driver.dir?(path)
27
+ raise Error::VfsReadError, "can't read a directory"
28
+ end
29
+
30
+ if !accessible_path?(path)
31
+ raise Error::VfsOutsideOfRoot, "path seems to be outside of root"
32
+ end
33
+
34
+ @driver[path]
35
+ end
36
+
37
+ def each
38
+ if !block_given?
39
+ return enum_for(:each)
40
+ end
41
+
42
+ @driver.each(&Proc.new)
43
+ end
44
+
45
+ def each_in(paths)
46
+ if !block_given?
47
+ return enum_for(:each_in, paths)
48
+ end
49
+
50
+ paths =
51
+ if paths.is_a?(Array)
52
+ paths
53
+ else
54
+ [paths]
55
+ end
56
+
57
+ @driver.each_in(paths, &Proc.new)
58
+ end
59
+
60
+ private
61
+
62
+ def accessible_path?(path)
63
+ random_safe_prefix = "/#{SecureRandom.hex}"
64
+ expanded_path = File.expand_path(path, random_safe_prefix)
65
+
66
+ /\A#{random_safe_prefix}\b/ === expanded_path
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,68 @@
1
+ module Ahnnotate
2
+ module VfsDriver
3
+ class Filesystem
4
+ def initialize(root:)
5
+ @root = root
6
+ end
7
+
8
+ def each
9
+ paths =
10
+ root
11
+ .glob("**/*")
12
+ .select(&:file?)
13
+ .map { |path| path.relative_path_from(root).to_s }
14
+
15
+ root.glob("**/*").select(&:file?).each do |abspath|
16
+ relpath = abspath.relative_path_from(root).to_s
17
+
18
+ yield relpath, File.read(abspath)
19
+ end
20
+ end
21
+
22
+ def each_in(subset_paths)
23
+ subset_paths = subset_paths.map { |path| root.join(path) }
24
+
25
+ subset_paths.each do |subset_path|
26
+ subset_path.glob("**/*").select(&:file?).map do |abspath|
27
+ relpath = abspath.relative_path_from(root).to_s
28
+
29
+ yield relpath, File.read(abspath)
30
+ end
31
+ end
32
+ end
33
+
34
+ def [](path)
35
+ path = root.join(path)
36
+
37
+ if path.exist?
38
+ File.read(path)
39
+ end
40
+ end
41
+
42
+ def []=(path, content)
43
+ path = root.join(path)
44
+ holding_directory = path.dirname
45
+
46
+ if holding_directory.file?
47
+ raise "File is not a directory"
48
+ end
49
+
50
+ if !holding_directory.exist?
51
+ holding_directory.mkpath
52
+ end
53
+
54
+ File.write(path, content)
55
+ end
56
+
57
+ def dir?(path)
58
+ root.join(path).directory?
59
+ end
60
+
61
+ private
62
+
63
+ def root
64
+ Pathname.new(@root)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,59 @@
1
+ module Ahnnotate
2
+ module VfsDriver
3
+ class Hash
4
+ def initialize(files, subdirectories = nil)
5
+ @files = files
6
+ @subdirectories = nil
7
+ end
8
+
9
+ def each
10
+ @files.each do |path, content|
11
+ yield path, content
12
+ end
13
+ end
14
+
15
+ def each_in(subset_paths)
16
+ subset_patterns = subset_paths.map { |path| /\A#{path}\b/ }
17
+
18
+ @files.each do |path, content|
19
+ if subset_patterns.none? { |pattern| pattern === path }
20
+ next
21
+ end
22
+
23
+ yield path, content
24
+ end
25
+ end
26
+
27
+ def [](path)
28
+ @files[path]
29
+ end
30
+
31
+ def []=(path, content)
32
+ if content.nil?
33
+ return
34
+ end
35
+
36
+ @files[path] = content
37
+ end
38
+
39
+ def subset(in: nil)
40
+ subdirectories = binding.local_variable_get(:in)
41
+
42
+ self.class.new(the_subset, subdirectories)
43
+ end
44
+
45
+ def dir?(path)
46
+ path_with_trailing_slash =
47
+ if path[-1] == "/"
48
+ path
49
+ else
50
+ path + "/"
51
+ end
52
+
53
+ @files.any? do |filepath, _|
54
+ filepath.start_with?(path_with_trailing_slash)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,8 @@
1
+ module Ahnnotate
2
+ module VfsDriver
3
+ class ReadOnlyFilesystem < Filesystem
4
+ def []=(_path, _content)
5
+ end
6
+ end
7
+ end
8
+ end
data/lib/ahnnotate.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "active_record"
2
+ require "parser/current"
3
+ require "proc_party"
4
+ require "stringio"
5
+
6
+ require "ahnnotate/column"
7
+ require "ahnnotate/config"
8
+ require "ahnnotate/error"
9
+ require "ahnnotate/vfs"
10
+ require "ahnnotate/vfs_driver/filesystem"
11
+ require "ahnnotate/vfs_driver/hash"
12
+ require "ahnnotate/vfs_driver/read_only_filesystem"
13
+ require "ahnnotate/function/format"
14
+ require "ahnnotate/function/main"
15
+ require "ahnnotate/function/run"
16
+ require "ahnnotate/function/tabularize"
17
+ require "ahnnotate/facet/models"
18
+ require "ahnnotate/facet/models/main"
19
+ require "ahnnotate/facet/models/module_node"
20
+ require "ahnnotate/facet/models/processor"
21
+ require "ahnnotate/facet/models/resolve_active_record_models"
22
+ require "ahnnotate/facet/models/resolve_class_relationships"
23
+ require "ahnnotate/facet/models/standin"
24
+ require "ahnnotate/index"
25
+ require "ahnnotate/options"
26
+ require "ahnnotate/table"
27
+ require "ahnnotate/tables"
28
+ require "ahnnotate/version"
29
+
30
+ if defined?(Rails)
31
+ require "ahnnotate/railtie"
32
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ahnnotate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Zach Ahn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 4.0.0
90
+ - - "<"
91
+ - !ruby/object:Gem::Version
92
+ version: '6'
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 4.0.0
100
+ - - "<"
101
+ - !ruby/object:Gem::Version
102
+ version: '6'
103
+ - !ruby/object:Gem::Dependency
104
+ name: parser
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: proc_party
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :runtime
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ description: Heavily inspired by the `annotate` gem
132
+ email:
133
+ - engineering@zachahn.com
134
+ executables:
135
+ - ahnnotate
136
+ extensions: []
137
+ extra_rdoc_files: []
138
+ files:
139
+ - ".gitignore"
140
+ - ".travis.yml"
141
+ - Gemfile
142
+ - Gemfile.lock
143
+ - LICENSE.txt
144
+ - README.md
145
+ - Rakefile
146
+ - ahnnotate.gemspec
147
+ - bin/console
148
+ - bin/rake
149
+ - bin/setup
150
+ - exe/ahnnotate
151
+ - lib/ahnnotate.rb
152
+ - lib/ahnnotate/cli.rb
153
+ - lib/ahnnotate/column.rb
154
+ - lib/ahnnotate/config.rb
155
+ - lib/ahnnotate/error.rb
156
+ - lib/ahnnotate/facet/models.rb
157
+ - lib/ahnnotate/facet/models/main.rb
158
+ - lib/ahnnotate/facet/models/module_node.rb
159
+ - lib/ahnnotate/facet/models/processor.rb
160
+ - lib/ahnnotate/facet/models/resolve_active_record_models.rb
161
+ - lib/ahnnotate/facet/models/resolve_class_relationships.rb
162
+ - lib/ahnnotate/facet/models/standin.rb
163
+ - lib/ahnnotate/function/format.rb
164
+ - lib/ahnnotate/function/main.rb
165
+ - lib/ahnnotate/function/run.rb
166
+ - lib/ahnnotate/function/tabularize.rb
167
+ - lib/ahnnotate/index.rb
168
+ - lib/ahnnotate/options.rb
169
+ - lib/ahnnotate/rails.rake
170
+ - lib/ahnnotate/railtie.rb
171
+ - lib/ahnnotate/table.rb
172
+ - lib/ahnnotate/tables.rb
173
+ - lib/ahnnotate/version.rb
174
+ - lib/ahnnotate/vfs.rb
175
+ - lib/ahnnotate/vfs_driver/filesystem.rb
176
+ - lib/ahnnotate/vfs_driver/hash.rb
177
+ - lib/ahnnotate/vfs_driver/read_only_filesystem.rb
178
+ homepage:
179
+ licenses:
180
+ - MIT
181
+ metadata: {}
182
+ post_install_message:
183
+ rdoc_options: []
184
+ require_paths:
185
+ - lib
186
+ required_ruby_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ requirements: []
197
+ rubyforge_project:
198
+ rubygems_version: 2.7.6
199
+ signing_key:
200
+ specification_version: 4
201
+ summary: '"Annotate" your models'
202
+ test_files: []