Copyright (c) 2013 MIT License Permission is hereby granted, free of charge, to any person...
Last active
December 14, 2015 06:09
-
-
Save elgalu/5040554 to your computer and use it in GitHub Desktop.
Sample thor task to create custom generators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
# Specify your gem's dependencies in <%= gem_name %>.gemspec | |
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ref: https://github.com/wycats/thor/wiki/Generators