Skip to content

Commit

Permalink
Use String#start_with? in OrganizeGemfile
Browse files Browse the repository at this point in the history
Closes #1204

It seems `active_support/core_ext/string` is not loaded when calling
`suspenders:cleanup:organize_gemfile` on applications **not** created
with Suspenders's [application template][].

We could require this file in the class, but instead, we simply use
`String#start_with?` to keep things simple.

[application template]: https://raw.githubusercontent.com/thoughtbot/suspenders/main/lib/install/web.rb
  • Loading branch information
stevepolitodesign committed May 15, 2024
1 parent 17c3745 commit df87122
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions lib/suspenders/cleanup/organize_gemfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ def remove_line_breaks

def sort_gems_and_groups
current_lines.each do |line|
if line.starts_with?(/group/)
if line.start_with?("group")
@current_group = line
end

# Consolidate gem groups
if current_group
if line.starts_with?(/end/)
if line.start_with?("end")
@current_group = nil
elsif !line.starts_with?(/group/)
elsif !line.start_with?("group")
gem_groups[current_group] ||= []
gem_groups[current_group] << line
end
# Add non-grouped gems
elsif !line.starts_with?(/\n/)
elsif !line.start_with?("\n")
new_lines << line
@current_group = nil
end
Expand Down Expand Up @@ -82,35 +82,35 @@ def add_line_breaks
marker = index + 1

# Add line break if it's a gem and the next line is commented out
if (line.starts_with?(/\s*gem/) || line.starts_with?(/\s*\#\s*gem/)) && next_line&.starts_with?(/\s*\#/)
if line.start_with?(/\s*gem/, /\s*\#\s*gem/) && next_line&.start_with?(/\s*\#/)
new_line_markers << marker
end

# Add line break if it's a commented out gem and the next line is a gem
if line.starts_with?(/\s*\#\s*gem/) && next_line&.starts_with?(/\s*gem/)
if line.start_with?(/\s*\#\s*gem/) && next_line&.start_with?(/\s*gem/)
new_line_markers << marker
end

# Add line break if it's a gem with a comment and the next line is a gem
if previous_line&.starts_with?(/\s*\#/) \
&& line.starts_with?(/\s*gem/) \
&& next_line&.starts_with?(/\s*gem/) \
&& !previous_line.starts_with?(/\s*\#\s*gem/)
if previous_line&.start_with?(/\s*\#/) \
&& line.start_with?(/\s*gem/) \
&& next_line&.start_with?(/\s*gem/) \
&& !previous_line.start_with?(/\s*\#\s*gem/)
new_line_markers << marker
end

# Add a line break if it's /end/
if line.starts_with?(/end/)
if line.start_with?("end")
new_line_markers << marker
end

# Add a line break if it's a gem and the next line is a group
if line.starts_with?(/gem/) && next_line&.starts_with?(/group/)
if line.start_with?("gem") && next_line&.start_with?("group")
new_line_markers << marker
end

# Add line break if it's /source/ or /ruby/
if line.starts_with?(/\w/) && !line.starts_with?(/\s*(gem|group|end)/)
if line.start_with?(/\w/) && !line.start_with?(/\s*(gem|group|end)/)
new_line_markers << marker
end
end
Expand Down

0 comments on commit df87122

Please sign in to comment.