ruby / rake

A make-like build utility for Ruby.

Home Page:https://ruby.github.io/rake

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why does Rake not allow exclamation points (bang: !) in the description?

esotericpig opened this issue · comments

In 2016, a pull request pull/134 was made to not allow exclamation points, but why was this?

The following won't work:

desc 'Benchmark ?: vs !!'
task :benchmark do |task|
end

desc 'Overwrite files; dangerous!'
task :overwrite do |task|
end

rake -T produces:

rake benchmark        # Benchmark ?: vs
rake overwrite        # Overwrite files; dangerous

Is there a workaround for this? A way to escape them?

Thanks.

System

  • ruby 2.7.0p0 (2019-12-25 revision 647ee6f091)
  • rake (13.0.1)

I found the problem here: https://github.com/ruby/rake/blob/master/lib/rake/task.rb#L338-L344
In my case, the task name has "!" like "pause!" which is a valid task name but in the description I can't use it.

I found the problem here: https://github.com/ruby/rake/blob/master/lib/rake/task.rb#L338-L344 In my case, the task name has "!" like "pause!" which is a valid task name but in the description I can't use it.

Yes, not to be rude, but I already mentioned the pull request, which points to that:

https://github.com/ruby/rake/pull/134/files

But I don't know why this was designed this way.

However, this did remind me that there is a workaround:

rake -D

This shows the full description.

Here's my re-design proposal:

  1. Change first_sentence to split only on newlines and then strip (the method, not you).
  2. If the length is longer than 50 or something (because I assume this was implemented due to the line being longer than the width of the terminal), then either split the current way or just remove trailing words until fits.

Here's example code. It might not be the fastest way, but just a proposal.

MAX_LEN = 50

def first_sentence(str)
  str = str.split("\n").first.rstrip
  len = str.length

  if len > MAX_LEN
    # First, try to remove trailing words.
    words = str.split(/(\s+)/) # Grouping () keeps the spaces.
    index = words.length

    # Do ">= 1" not 0, else empty string "" because chops off all words.
    while len > MAX_LEN && (index -= 1) >= 1
      len -= words[index].length
    end

    if len <= MAX_LEN
      # It worked, so glue the correct number of words/spaces back together.
      str = words[0...index].join
    else
      # It didn't work, so just slice it.
      str = str[0...MAX_LEN]
    end

    # Strip away any spacing leftover between words.
    str = str.rstrip

    # Just some styling (optional).
    str = "#{str}..."
  end

  str
end

#=> hello
puts first_sentence("hello\nworld")
#=> hello world!
puts first_sentence("hello world!")
#=> hello world this is a long sentence that will get...
puts first_sentence("hello world this is a long sentence that will get cut off")
#=> hellowordlthisisalongwordthathasnospacesidontknoww...
puts first_sentence("hellowordlthisisalongwordthathasnospacesidontknowwhywhatwasithinking")