Skip to content

Instantly share code, notes, and snippets.

@FeepingCreature
Created March 26, 2021 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FeepingCreature/1d36973be5facde89fa7e2e338245713 to your computer and use it in GitHub Desktop.
Save FeepingCreature/1d36973be5facde89fa7e2e338245713 to your computer and use it in GitHub Desktop.
def patch_argv
# remove everything after -- so rake doesn't get confused and think it's tasks
# save it in EXTRA_ARGS
sep = ARGV.index("--") || ARGV.length
real_args, extra_args = ARGV.take(sep), ARGV.drop(sep + 1)
ARGV.clear
real_args.each { |arg| ARGV << arg }
# erroneously, rake will have interpreted the words after -- as tasks
# so now we get to remove them manually
rake_tasks = Rake.application.top_level_tasks
extra_args.each do |arg|
# remove from back to arg
pos = rake_tasks.rindex arg
if pos
rake_tasks = rake_tasks.take(pos)
end
end
Rake.application.instance_variable_set(:@top_level_tasks, rake_tasks)
extra_args
end
EXTRA_ARGS = patch_argv
@Nakilon
Copy link

Nakilon commented Mar 26, 2021

def patch_argv
  sep = ARGV.index("--") || ARGV.length
  real_args, extra_args = ARGV.take(sep), ARGV.drop(sep + 1)
  ARGV.replace real_args

  rake_tasks = Rake.application.top_level_tasks
  Rake.application.instance_variable_set(
    :@top_level_tasks,
    rake_tasks.take extra_args.map{ |arg| rake_tasks.rindex arg }.min
  )
  extra_args
end
EXTRA_ARGS = patch_argv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment