richmond 0.2.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
+ SHA1:
3
+ metadata.gz: fe3d2cb81c934c3cfb710affd81ab50331357ff2
4
+ data.tar.gz: fcdccef945b2ad8d8db0961a668b4f520abf0d8f
5
+ SHA512:
6
+ metadata.gz: 4b12e08234efb1b9c2028e20ca0271509735fbb0c59ba0be91415487838c819734e87ae543f0152e494a499b7efe718e579f1efa1ebe2994ad9410acaf093775
7
+ data.tar.gz: 1b972cf6666d3f8cd9c389c08d4eb83de34cca9382be74f4e1d2580bef05a7a87ed2fe18dd74f800998446d02ac6c705b04d12fd544e8321662fb23a4a15a986
data/.autotest ADDED
@@ -0,0 +1,20 @@
1
+ require "autotest/bundler"
2
+ require "autotest/restart"
3
+
4
+ Autotest.add_hook :initialize do |autotest|
5
+ %w{.git ._* vendor tmp log doc output}.each do |exception|
6
+ autotest.add_exception(exception)
7
+ end
8
+ end
9
+
10
+ CLEAR = "\e[2J\e[f"
11
+ LINE = "\n"*2 + '-'*80 + "\n"*2
12
+
13
+ ##
14
+ # # From autotest/growl
15
+ # # Set the label and clear the terminal.
16
+ Autotest.add_hook :run_command do
17
+ print LINE
18
+ print CLEAR
19
+ false
20
+ end
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ /output
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in richmond.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Richmond
2
+
3
+ Richmond is a solution for culling documentation from files in a repo.
4
+
5
+ ## What's it for?
6
+
7
+ I was not happy with existing solutions for generating swagger documentation
8
+ from ruby code. For this reason I started hand-editing the swagger JSON files.
9
+ This became tedious over time, and I decided that editing the files in YAML
10
+ would be nicer. This solution worked well, but still required a lot of context-
11
+ switching between the YAML files and the api implementation. I wrote richmond
12
+ so that I could do this:
13
+
14
+ =begin output-file: swagger.yaml
15
+ api: /my/url/{param1}
16
+ parameters
17
+ - name: param1
18
+ ... snipped for brevity
19
+ =end
20
+ get '/my/url/:param1' do
21
+ param1 = params[:param1]
22
+ ... snipped for brevity
23
+ end
24
+
25
+ ## Installation
26
+
27
+ Add this line to your application's Gemfile:
28
+
29
+ ```ruby
30
+ gem 'richmond'
31
+ ```
32
+
33
+ And then execute:
34
+
35
+ $ bundle
36
+
37
+ Or install it yourself as:
38
+
39
+ $ gem install richmond
40
+
41
+ ## Usage
42
+
43
+ Richmond.generate dir
44
+
45
+ This command will cause Richmond to scan all files in dir, recursively.
46
+ The tool looks for ruby comment blocks in the following format:
47
+
48
+ =begin output-file: <output file name>
49
+ some text
50
+ =end
51
+
52
+ Successive blocks targeting the same output file can be written as follows:
53
+
54
+ =begin append
55
+ some more text
56
+ =end
57
+
58
+ Richmond will take any text in the block and insert it into the specified output file.
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it ( https://github.com/crmckenzie/richmond/fork )
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'pry'
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
4
+
5
+ task :default => :spec
data/bin/richmond ADDED
@@ -0,0 +1,6 @@
1
+ require_relative '../lib/richmond.rb'
2
+ dir = ARGV[0]
3
+
4
+ puts "scanning directory #{dir} for files"
5
+ Richmond.generate dir
6
+
@@ -0,0 +1,31 @@
1
+ require 'logger'
2
+
3
+ module Richmond
4
+ module Logging
5
+
6
+ class << self
7
+
8
+ def logger
9
+ @logger ||= Logger.new $stdout
10
+ end
11
+
12
+ def logger=(value)
13
+ @logger = value
14
+ end
15
+
16
+ end
17
+
18
+ # Addition
19
+ def self.included(base)
20
+ class << base
21
+ def logger
22
+ Logging.logger
23
+ end
24
+ end
25
+ end
26
+
27
+ def logger
28
+ Logging.logger
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,125 @@
1
+ require 'find'
2
+ require 'ostruct'
3
+
4
+ module Richmond
5
+
6
+ class RTFM
7
+
8
+ attr_accessor :record_pattern, :end_record_pattern, :output_file_pattern
9
+
10
+ include Richmond::Logging
11
+
12
+ def initialize
13
+ @record_pattern =/^\=begin.*(output-file:\s+.*|append)/i
14
+ @output_file_pattern = /output-file:\s+.*\s*/i
15
+ @end_record_pattern = /^\=end/i
16
+ @mode = :paused
17
+ end
18
+
19
+ def parse_output_file(line)
20
+ match = line.match output_file_pattern
21
+ smatch = match
22
+ .to_s
23
+ .gsub(/output-file:/, '')
24
+ .strip
25
+
26
+ File.expand_path smatch
27
+ end
28
+
29
+ def scan(dir)
30
+ logger.info "beginning scan"
31
+ result = scan_files Richmond.find_files_in_dir dir
32
+ logger.info "scan finished"
33
+ result
34
+ end
35
+
36
+ def scan_files(files)
37
+ @result = ScanResult.new
38
+
39
+ files.each do |file|
40
+ dir = File.dirname file
41
+ @input_filename = File.expand_path file
42
+ @output_filename = default_output_filename dir
43
+
44
+ lines = File.readlines file
45
+ lines.each_with_index do |line, i|
46
+ line.encode!('UTF-8', 'UTF-8', :invalid => :replace)
47
+ #begin
48
+ end_recording! line if end_recording? line
49
+ record! line if recording?
50
+ begin_recording! line if begin_recording? line
51
+ #rescue Exception => error
52
+ # logger.debug "error recording line: #{file}: line: #{i}: #{line} => #{error}"
53
+ #end
54
+ end
55
+ end
56
+
57
+ @result
58
+ end
59
+
60
+ def emit(input)
61
+ logger.info "begin emitting files"
62
+ input.each_pair do |filename, lines|
63
+ assert_directory_exists filename
64
+ logger.info "writing #{filename}"
65
+ File.open(filename, 'w') do |file|
66
+ lines.each {|line| file.write line }
67
+ end
68
+ end
69
+ logger.info "finished emitting files"
70
+ input.keys
71
+ end
72
+
73
+ def begin_recording?(line)
74
+ return false if line.nil?
75
+ return true if line.match record_pattern
76
+ false
77
+ end
78
+
79
+ def end_recording?(line)
80
+ line.match end_record_pattern
81
+ end
82
+
83
+ private
84
+
85
+ def begin_recording!(line)
86
+ @mode = :recording
87
+
88
+ if set_output_file? line
89
+ @output_filename = parse_output_file line
90
+ end
91
+ logger.debug "---#{line}"
92
+ logger.info "---begin recording into #{@output_filename} from #{@input_filename}"
93
+ end
94
+
95
+ def end_recording!(line)
96
+ @mode = :paused
97
+ logger.info "---end recording"
98
+ end
99
+
100
+ def recording?
101
+ @mode == :recording
102
+ end
103
+
104
+ def record! line
105
+ @result.output[@output_filename].push line
106
+ @result.input[@input_filename].push line
107
+ end
108
+
109
+ def set_output_file?(line)
110
+ line.match output_file_pattern
111
+ end
112
+
113
+ def assert_directory_exists(filename)
114
+ dirname = File.dirname(filename)
115
+ unless File.directory?(dirname)
116
+ FileUtils.mkdir_p(dirname)
117
+ end
118
+ end
119
+
120
+ def default_output_filename(dir)
121
+ File.join(dir,'output', 'richmond.output')
122
+ end
123
+
124
+ end
125
+ end
@@ -0,0 +1,16 @@
1
+ class ScanResult
2
+ attr_accessor :input, :output
3
+
4
+ def initialize
5
+ @input = key_array_hash
6
+ @output = key_array_hash
7
+ end
8
+
9
+ private
10
+ def key_array_hash
11
+ Hash.new {|hash, key| hash[key] = [] }
12
+ end
13
+
14
+ end
15
+
16
+
@@ -0,0 +1,3 @@
1
+ module Richmond
2
+ VERSION = "0.2.0"
3
+ end
data/lib/richmond.rb ADDED
@@ -0,0 +1,30 @@
1
+ require "richmond/version"
2
+ require 'richmond/logging'
3
+ require 'richmond/scan_result'
4
+ require 'richmond/rtfm'
5
+ require 'find'
6
+
7
+ module Richmond
8
+
9
+ @@find_files_in_dir = lambda do |dir|
10
+ files = Find.find(dir).to_a
11
+ files.reject! {|f| File.directory? f}
12
+ files.reject! {|f| f.split('/').include? '.git' }
13
+ files
14
+ end
15
+
16
+ def self.filter(&block)
17
+ @@find_files_in_dir = block
18
+ end
19
+
20
+ def self.find_files_in_dir(dir)
21
+ @@find_files_in_dir.call dir
22
+ end
23
+
24
+ def self.generate(dir)
25
+ rtfm = RTFM.new
26
+ scan = rtfm.scan dir
27
+ rtfm.emit scan.output
28
+ end
29
+
30
+ end
data/richmond.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'richmond/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "richmond"
8
+ spec.version = Richmond::VERSION
9
+ spec.authors = ["crmckenzie"]
10
+ spec.email = ["crmckenzie@gmail.com"]
11
+ spec.summary = %q{scrap files for content and emit an aggregate file}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler"
21
+ spec.add_development_dependency 'autotest'
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "rspec-autotest"
24
+ spec.add_development_dependency "pry"
25
+
26
+ end
@@ -0,0 +1,160 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Richmond::RTFM do
4
+
5
+ let!(:dir) {
6
+ file = File.expand_path('../../', __FILE__)
7
+ dir = File.dirname(file)
8
+ dir
9
+ }
10
+
11
+ subject {
12
+ Richmond::RTFM.new
13
+ }
14
+
15
+ describe '.parse_output_file' do
16
+ it 'pulls the output-file out of the start line' do
17
+ line = '=begin richmond output-file: output/testfile.output'
18
+ expect(subject.parse_output_file line).to eq File.expand_path('output/testfile.output')
19
+ end
20
+ end
21
+
22
+ describe '.begin_recording?' do
23
+
24
+ it "standard form" do
25
+ line = '=begin output-file: output/testfile.output'
26
+ expect(subject.begin_recording? line).to eq true
27
+ end
28
+
29
+ it 'append' do
30
+ expect(subject.begin_recording? '=begin append').to eq true
31
+ end
32
+
33
+ it 'rejects nil' do
34
+ expect(subject.begin_recording? nil).to eq false
35
+ end
36
+
37
+ it 'rejects gobbledygook' do
38
+ expect(subject.begin_recording? 'gobbledygook').to eq false
39
+ end
40
+
41
+ it 'rejects standard comment block' do
42
+ expect(subject.begin_recording? '=begin').to eq false
43
+ end
44
+
45
+ end
46
+
47
+ describe '.rtfm' do
48
+ let!(:result) { subject.scan dir }
49
+
50
+ describe 'simple comment block' do
51
+
52
+ =begin output-file: output/richmond.output
53
+ this:
54
+ is: some
55
+ yaml:
56
+ - array item 1
57
+ - array item 2
58
+ =end
59
+
60
+ it 'is not nil' do
61
+ expect(result).to_not eq nil
62
+ end
63
+
64
+ describe 'input' do
65
+ it 'contains a key for the file' do
66
+ filename = File.expand_path __FILE__
67
+ expect(result.input).to have_key(filename)
68
+ end
69
+
70
+ it 'documents which comments came from which file' do
71
+ lines = result.input[File.expand_path __FILE__]
72
+ expect(lines.size).to eq 9
73
+ end
74
+ end
75
+
76
+ describe 'output' do
77
+ it 'has output filename' do
78
+
79
+ default_filename = File.join(File.dirname(File.expand_path('../../', __FILE__)), 'output', 'richmond.output')
80
+ expect(result.output).to have_key(default_filename), result.output.keys
81
+
82
+ lines = result.output[default_filename]
83
+ expect(lines.first).to eq "this:\n"
84
+ expect(lines.last).to eq " - array item 2\n"
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ describe 'comment blocks with identified output files' do
91
+ =begin output-file: output/file1.txt
92
+ file 1 comments
93
+ =end
94
+
95
+ =begin output-file: output/file2.txt
96
+ file 2 comments
97
+ =end
98
+
99
+ =begin output-file: output/file1.txt
100
+ some more file 1 comments
101
+ =end
102
+
103
+ =begin append
104
+ this line should appear in the output/file1.txt
105
+ =end
106
+
107
+ it "should have keys for file1.txt and file2.txt" do
108
+ expect(result.output).to have_key(File.expand_path 'output/file1.txt')
109
+ expect(result.output).to have_key(File.expand_path 'output/file2.txt')
110
+ end
111
+
112
+ it 'should have 1 line for file 2' do
113
+ expect(result.output[File.expand_path 'output/file2.txt']).to include " file 2 comments\n"
114
+ end
115
+
116
+ it 'should merge file 1 lines from all relevant comment blocks' do
117
+ expect(result.output[File.expand_path 'output/file1.txt']).to include(" file 1 comments\n")
118
+ expect(result.output[File.expand_path 'output/file1.txt']).to include(" some more file 1 comments\n")
119
+ expect(result.output[File.expand_path 'output/file1.txt']).to include(" this line should appear in the output/file1.txt\n")
120
+ end
121
+
122
+ end
123
+
124
+ describe 'comment block without richmond identifier' do
125
+ =begin some random comment block
126
+ some random comments
127
+ should not change the test results for the richmond comment blocks
128
+ =end
129
+ end
130
+ end
131
+
132
+ describe '.emit' do
133
+ let!(:input_result) { subject.scan dir }
134
+
135
+ before(:each) do
136
+ input_result.output.each_pair do |file, lines|
137
+ if File.exists? file
138
+ File.delete file
139
+ end
140
+ end
141
+ end
142
+
143
+ let!(:emit_result) { subject.emit input_result.output }
144
+
145
+ describe 'files created' do
146
+ it 'emit_result is not nil' do
147
+ expect(emit_result).to_not be nil
148
+ end
149
+
150
+ it 'creates the output files' do
151
+ input_result.output.keys.each {|file|
152
+ expect(File.exists? file).to eq(true), file
153
+ }
154
+ end
155
+
156
+ end
157
+
158
+ end
159
+
160
+ end
@@ -0,0 +1,2 @@
1
+ require_relative '../lib/richmond.rb'
2
+ require 'pry'
data/tasks/rspec.rake ADDED
@@ -0,0 +1,6 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec) do |t|
4
+ pattern = Dir['spec/**/*_spec.rb']
5
+ t.pattern = pattern
6
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: richmond
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - crmckenzie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-19 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: autotest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-autotest
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
+ description:
84
+ email:
85
+ - crmckenzie@gmail.com
86
+ executables:
87
+ - richmond
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".autotest"
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - Gemfile
95
+ - README.md
96
+ - Rakefile
97
+ - bin/richmond
98
+ - lib/richmond.rb
99
+ - lib/richmond/logging.rb
100
+ - lib/richmond/rtfm.rb
101
+ - lib/richmond/scan_result.rb
102
+ - lib/richmond/version.rb
103
+ - richmond.gemspec
104
+ - spec/lib/rtfm_spec.rb
105
+ - spec/spec_helper.rb
106
+ - tasks/rspec.rake
107
+ homepage: ''
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.5
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: scrap files for content and emit an aggregate file
131
+ test_files:
132
+ - spec/lib/rtfm_spec.rb
133
+ - spec/spec_helper.rb