Skip to content

Instantly share code, notes, and snippets.

@elgalu
Last active December 14, 2015 06:09
Show Gist options
  • Save elgalu/5040554 to your computer and use it in GitHub Desktop.
Save elgalu/5040554 to your computer and use it in GitHub Desktop.
Sample thor task to create custom generators
source 'https://rubygems.org'
# Specify your gem's dependencies in <%= gem_name %>.gemspec
gemspec

Copyright (c) 2013 MIT License Permission is hereby granted, free of charge, to any person...

#!/usr/bin/env ruby
require 'thor'
class Newgem < Thor::Group
include Thor::Actions
# Define arguments and options
argument :gem_name
class_option :test_framework, :default => :test_unit
def self.source_root
File.dirname(__FILE__)
end
def create_gemfile_file
template('Gemfile.tt', "#{gem_name}/Gemfile")
end
def create_test_file
test = options[:test_framework].to_s == "rspec" ? :spec : :test
create_file "#{gem_name}/#{test}/#{gem_name}_#{test}.rb"
end
def copy_licence
copy_file "LICENSE.md", "#{gem_name}/LICENSE.md"
end
end
#!/usr/bin/env ruby
require_relative 'newgem.thor'
# From the command-line you would invoke this task like this:
# $ thor newgem some_name --test-framework rspec
#
# This is how ARGV would look like after that invocation:
# ["newgem", "some_name", "--test-framework", "rspec"]
#
# This is how you should pass the arguments directly to your script
#
# If you use start() pass options like this:
# args = ["some_name", "--test-framework", "rspec"]
#
# Newgem.start(args)
#
#
# If you use invoke() pass options like this:
# args = ["some_name"]
# opts = {:test_framework => 'rspec'}
#
# script = Newgem.new(args, opts)
# script.invoke_all
#
# Using invoke() is the suggested technique.
args = ["some_name"]
opts = {:test_framework => 'rspec'}
script = Newgem.new(args, opts)
script.invoke_all
@elgalu
Copy link
Author

elgalu commented Feb 26, 2013

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