Created
July 10, 2015 11:44
-
-
Save bchase/97a47721c588697fc1b2 to your computer and use it in GitHub Desktop.
create a new namespace (module/class) with spec from gem root
This file contains hidden or 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 | |
| # ### USAGE ### | |
| # | |
| # $ ./new_namespace foo/bar_baz [class] | |
| # | |
| # defaults to generating a module | |
| # | |
| # expects existing structure to include... | |
| # | |
| # ./lib/your_gem/foo.rb | |
| # | |
| # generates... | |
| # | |
| # ### lib/your_gem/foo/bar_baz.rb ### | |
| # class YourGem::Foo::BarBaz | |
| # end | |
| # | |
| # ### spec/foo/bar_baz_spec.rb ### | |
| # require 'spec_helper' | |
| # | |
| # describe YourGem::Foo::BarBaz do | |
| # let(:klass) { YourGem::Foo::BarBaz } | |
| # | |
| # describe 'foo' do | |
| # it 'bar' do | |
| # baz | |
| # end | |
| # end | |
| # end | |
| # | |
| # will also append `require`... | |
| # | |
| # ### lib/your_gem/foo/bar_baz.rb ### | |
| # | |
| # module Foo | |
| # # ... | |
| # end | |
| # | |
| # require 'your_gem/foo/bar_baz' | |
| require 'active_support/all' | |
| require 'pathname' | |
| include FileUtils | |
| namespace_path = ARGV[0] # lib/foo/(bar/baz_boo) \1 | |
| gem_namespace = Dir['lib/*'].grep(/.rb/).first[%r{lib/(\w+).rb},1] | |
| full_namespace = gem_namespace + '/' + namespace_path | |
| namespace = full_namespace.classify | |
| lib_path = Pathname.new('lib').join(full_namespace + '.rb') | |
| spec_path = Pathname.new('spec').join(namespace_path + '_spec.rb') | |
| puts lib_path, spec_path | |
| puts | |
| puts namespace | |
| namespace.split('::').each_with_index do |n,i| | |
| puts " (#{i}) #{n}" | |
| end | |
| puts | |
| puts "Any of these need capitalizing?" | |
| puts "(space separated nums, ^D for ENTER)" | |
| puts | |
| upcase_ns = STDIN.gets.strip.split(' ').map(&:to_i) rescue nil | |
| puts | |
| puts | |
| if upcase_ns | |
| namespace = namespace.split('::') | |
| .map.with_index{|n,i| upcase_ns.include?(i) ? n.upcase : n } | |
| .join('::') | |
| end | |
| puts namespace | |
| keyword = ARGV[1] || 'module' | |
| lib = <<-LIB | |
| #{keyword} #{namespace} | |
| end | |
| LIB | |
| spec = <<-SPEC | |
| require 'spec_helper' | |
| describe #{namespace} do | |
| let(:klass) { #{namespace} } | |
| describe 'foo' do | |
| it 'bar' do | |
| baz | |
| end | |
| end | |
| end | |
| SPEC | |
| puts | |
| puts | |
| puts | |
| puts "### #{lib_path} ###" | |
| puts | |
| puts lib | |
| puts | |
| puts | |
| puts | |
| puts "### #{spec_path} ###" | |
| puts | |
| puts spec | |
| # raise 'remove this to keep going' | |
| puts | |
| puts "Create these dirs and files? (Y/n)" | |
| puts "(^D for ENTER)" | |
| puts | |
| doit = STDIN.gets | |
| puts | |
| puts | |
| if doit.nil? || doit.match(/^y/i) | |
| raise "File `#{lib_path}` already exists" if lib_path.exist? | |
| raise "File `#{spec_path}` already exists" if spec_path.exist? | |
| mkdir_p lib_path.to_s.chomp('.rb') | |
| lib_path.write lib | |
| mkdir_p spec_path.to_s.chomp('_spec.rb') | |
| spec_path.write spec | |
| end | |
| parent_path = Pathname lib_path.to_s.split(?/)[0..-2].join(?/) + '.rb' | |
| require_str = "require '#{lib_path.to_s.split(?/)[1..-1].join(?/).chomp('.rb')}'" | |
| puts | |
| puts "Require in parent? (Y/n)" | |
| puts | |
| puts "### #{parent_path} ###" | |
| puts require_str | |
| puts | |
| puts "(^D for ENTER)" | |
| puts | |
| doit = nil | |
| doit = STDIN.gets | |
| puts | |
| puts | |
| if doit.nil? || doit.match(/^y/i) | |
| raise "`#{parent_path}` doesn't exist" unless parent_path.exist? | |
| File.open(parent_path, 'a') {|f| f.write "\n\n#{require_str}"} | |
| end | |
| puts spec_path.to_s + ' ' + lib_path.to_s | |
| puts | |
| puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment