Skip to content

Commit ea1ce27

Browse files
committed
Add rake task to scrape rubocop documentation
This adds the `docs:scrape` rake task which will pull down the rubocop repo and scrape all of the cops for class documentation and insert it into markdown files. This uses regular expresses to extract and manipulate the class documentation since working with YARD and RDoc like this isn't straightforward.
1 parent 88552d7 commit ea1ce27

File tree

10 files changed

+51
-82
lines changed

10 files changed

+51
-82
lines changed

.gitignore

Whitespace-only changes.

Dockerfile

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ WORKDIR /usr/src/app
44
COPY Gemfile /usr/src/app/
55
COPY Gemfile.lock /usr/src/app/
66

7-
RUN apk --update add ruby ruby-dev ruby-bundler build-base && \
7+
RUN apk --update add ruby ruby-dev ruby-bundler build-base git && \
88
bundle install -j 4 && \
99
apk del build-base && rm -fr /usr/share/ri
1010

1111
RUN adduser -u 9000 -D app
12+
COPY . /usr/src/app
13+
RUN chown -R app:app /usr/src/app
14+
1215
USER app
1316

14-
COPY . /usr/src/app
17+
RUN cd /usr/src/app && \
18+
rake docs:scrape
1519

1620
CMD ["/usr/src/app/bin/codeclimate-rubocop"]

Gemfile.lock

-3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,3 @@ DEPENDENCIES
5959
rake
6060
rubocop
6161
rubocop-rspec
62-
63-
BUNDLED WITH
64-
1.10.6

Rakefile

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require 'rake/testtask'
22

3+
Rake.add_rakelib 'lib/tasks'
4+
35
Rake::TestTask.new do |t|
46
t.test_files = Dir.glob('spec/**/*_spec.rb')
57
t.libs = %w[lib spec]

circle.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ machine:
77

88
test:
99
override:
10-
- bundle exec rake
1110
- docker build -t=$PRIVATE_REGISTRY/$CIRCLE_PROJECT_REPONAME:b$CIRCLE_BUILD_NUM .
11+
- docker run $PRIVATE_REGISTRY/$CIRCLE_PROJECT_REPONAME:b$CIRCLE_BUILD_NUM bundle exec rake
1212

1313
deployment:
1414
registry:

config/contents/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.md

config/contents/metrics/abc_size.md

-22
This file was deleted.

config/contents/metrics/class_length.md

-27
This file was deleted.

config/contents/metrics/module_length.md

-27
This file was deleted.

lib/tasks/docs.rake

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace :docs do
2+
desc "Scrapes documentation from the rubocop gem"
3+
task :scrape do
4+
TAG = "v0.34.2"
5+
COP_FOLDERS = %w[lint metrics performance rails style]
6+
7+
%x{git clone https://github.com/bbatsov/rubocop.git rubocop-git}
8+
%x{cd rubocop-git && git checkout tags/#{TAG}}
9+
10+
files = Dir.glob("./rubocop-git/lib/rubocop/cop/{#{COP_FOLDERS.join(',')}}/**.rb")
11+
12+
documentation = files.each_with_object({}) do |file, hash|
13+
content = File.read(file)
14+
class_doc = content.match(/(\s+#.*)+/).to_s
15+
doc_lines = class_doc.
16+
gsub(/^\n/,"").
17+
split("\n").
18+
map { |line| line.gsub(/\s+#\s?/, "") }.
19+
join("\n")
20+
hash[file] = doc_lines
21+
end
22+
23+
documentation.each do |file_path, documentation|
24+
namespace = file_path.split('/').slice(-2, 1).join('/')
25+
file_name = File.basename(file_path, '.rb')
26+
27+
folder_path = "./config/contents/#{namespace}"
28+
write_path = "#{folder_path}/#{file_name}.md"
29+
30+
puts "Writing documentation to #{write_path}"
31+
32+
33+
FileUtils.mkdir_p(folder_path)
34+
File.open(write_path, 'w') do |file|
35+
file.write(documentation)
36+
end
37+
end
38+
39+
%x{rm -rf rubocop-git}
40+
end
41+
end

0 commit comments

Comments
 (0)