Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
annotate
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
annotate
Commits
e315405c
Commit
e315405c
authored
Oct 19, 2017
by
Alexander Belozerov
Committed by
Cuong Tran
Oct 18, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Do not remove magic comments (AnnotateRoutes) (#520)
parent
fd67ed46
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
177 additions
and
24 deletions
+177
-24
annotate_routes.rb
lib/annotate/annotate_routes.rb
+31
-2
annotate_routes_spec.rb
spec/annotate/annotate_routes_spec.rb
+146
-22
No files found.
lib/annotate/annotate_routes.rb
View file @
e315405c
...
@@ -38,7 +38,13 @@ module AnnotateRoutes
...
@@ -38,7 +38,13 @@ module AnnotateRoutes
def
header
(
options
=
{})
def
header
(
options
=
{})
routes_map
=
app_routes_map
(
options
)
routes_map
=
app_routes_map
(
options
)
magic_comments_map
,
routes_map
=
extract_magic_comments_from_array
(
routes_map
)
out
=
[]
out
=
[]
magic_comments_map
.
each
do
|
magic_comment
|
out
<<
magic_comment
end
out
+=
[
"#
#{
options
[
:wrapper_open
]
}
"
]
if
options
[
:wrapper_open
]
out
+=
[
"#
#{
options
[
:wrapper_open
]
}
"
]
if
options
[
:wrapper_open
]
out
+=
[
"#
#{
options
[
:format_markdown
]
?
PREFIX_MD
:
PREFIX
}
"
+
(
options
[
:timestamp
]
?
" (Updated
#{
Time
.
now
.
strftime
(
'%Y-%m-%d %H:%M'
)
}
)"
:
''
)]
out
+=
[
"#
#{
options
[
:format_markdown
]
?
PREFIX_MD
:
PREFIX
}
"
+
(
options
[
:timestamp
]
?
" (Updated
#{
Time
.
now
.
strftime
(
'%Y-%m-%d %H:%M'
)
}
)"
:
''
)]
...
@@ -82,6 +88,28 @@ module AnnotateRoutes
...
@@ -82,6 +88,28 @@ module AnnotateRoutes
end
end
end
end
def
self
.
magic_comment_matcher
Regexp
.
new
(
/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/
)
end
# @param [Array<String>] content
# @return [Array<String>] all found magic comments
# @return [Array<String>] content without magic comments
def
self
.
extract_magic_comments_from_array
(
content_array
)
magic_comments
=
[]
new_content
=
[]
content_array
.
map
do
|
row
|
if
row
=~
magic_comment_matcher
magic_comments
<<
row
.
strip
else
new_content
<<
row
end
end
[
magic_comments
,
new_content
]
end
def
self
.
app_routes_map
(
options
)
def
self
.
app_routes_map
(
options
)
routes_map
=
`rake routes`
.
chomp
(
"
\n
"
).
split
(
/\n/
,
-
1
)
routes_map
=
`rake routes`
.
chomp
(
"
\n
"
).
split
(
/\n/
,
-
1
)
...
@@ -143,9 +171,10 @@ module AnnotateRoutes
...
@@ -143,9 +171,10 @@ module AnnotateRoutes
end
end
def
self
.
annotate_routes
(
header
,
content
,
where_header_found
,
options
=
{})
def
self
.
annotate_routes
(
header
,
content
,
where_header_found
,
options
=
{})
magic_comments_map
,
content
=
extract_magic_comments_from_array
(
content
)
if
%w(before top)
.
include?
(
options
[
:position_in_routes
])
if
%w(before top)
.
include?
(
options
[
:position_in_routes
])
header
=
header
<<
''
if
content
.
first
!=
''
header
=
header
<<
''
if
content
.
first
!=
''
new_content
=
header
+
content
new_content
=
magic_comments_map
+
header
+
content
else
else
# Ensure we have adequate trailing newlines at the end of the file to
# Ensure we have adequate trailing newlines at the end of the file to
# ensure a blank line separating the content from the annotation.
# ensure a blank line separating the content from the annotation.
...
@@ -155,7 +184,7 @@ module AnnotateRoutes
...
@@ -155,7 +184,7 @@ module AnnotateRoutes
# the spacer we put in the first time around.
# the spacer we put in the first time around.
content
.
shift
if
where_header_found
==
:before
&&
content
.
first
==
''
content
.
shift
if
where_header_found
==
:before
&&
content
.
first
==
''
new_content
=
content
+
header
new_content
=
magic_comments_map
+
content
+
header
end
end
new_content
new_content
...
...
spec/annotate/annotate_routes_spec.rb
View file @
e315405c
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment