Class: Bundlegem::CLI::Gem

Inherits:
Object
  • Object
show all
Defined in:
lib/bundlegem/cli/gem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, gem_name) ⇒ Gem

Returns a new instance of Gem.



7
8
9
10
11
12
13
14
15
# File 'lib/bundlegem/cli/gem.rb', line 7

def initialize(options, gem_name)
  @options = options
  @gem_name = resolve_name(gem_name)

  @name = @gem_name
  @target = Pathname.pwd.join(gem_name)

  validate_ext_name if options[:ext]
end

Instance Attribute Details

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



5
6
7
# File 'lib/bundlegem/cli/gem.rb', line 5

def gem_name
  @gem_name
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/bundlegem/cli/gem.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/bundlegem/cli/gem.rb', line 5

def options
  @options
end

#targetObject (readonly)

Returns the value of attribute target.



5
6
7
# File 'lib/bundlegem/cli/gem.rb', line 5

def target
  @target
end

Instance Method Details

#build_interpolation_configObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bundlegem/cli/gem.rb', line 17

def build_interpolation_config
  underscored_name = name.tr('-', '_')
  namespaced_path = name.tr('-', '/')
  constant_name = name.split('_').map{|p| p[0..0].upcase + p[1..-1] unless p.empty?}.join
  constant_name = constant_name.split('-').map{|q| q[0..0].upcase + q[1..-1] }.join('::') if constant_name =~ /-/
  constant_array = constant_name.split('::')
  git_user_name = `git config user.name`.chomp
  git_user_email = `git config user.email`.chomp

  config = {
    :name             => name,
    :underscored_name => underscored_name,
    :namespaced_path  => namespaced_path,
    :makefile_path    => "#{underscored_name}/#{underscored_name}",
    :constant_name    => constant_name,
    :constant_array   => constant_array,
    :author           => git_user_name.empty? ? "TODO: Write your name" : git_user_name,
    :email            => git_user_email.empty? ? "TODO: Write your email address" : git_user_email,
    :git_repo_url     => git_user_name.empty? ? "TODO: set your git username so link to repo is automatic" : "https://github.com/#{git_user_name}/#{underscored_name}",
    :template         => options[:template],
    :test             => options[:test],
    :ext              => options[:ext],
    :bin              => options[:bin],
    :bundler_version  => bundler_dependency_version
  }
end

#configObject



44
45
46
# File 'lib/bundlegem/cli/gem.rb', line 44

def config
  @config ||= build_interpolation_config
end

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bundlegem/cli/gem.rb', line 48

def run
  raise_project_with_that_name_already_exists! if File.exist?(target)

  ensure_safe_gem_name(name, config[:constant_array])

  template_src = match_template_src
  template_directories = dynamically_generate_template_directories
  templates = dynamically_generate_templates

  puts "Creating new project folder '#{name}'\n\n"
  create_template_directories(template_directories, target)

  templates.each do |src, dst|
    template("#{template_src}/#{src}", target.join(dst), config)
  end

  # Bundler.ui.info "Initializing git repo in #{target}"
  Dir.chdir(target) { `git init`; `git add .` }

  # Disabled thanks to removal of thor, might not be helpful...
  #if options[:edit]
  #  # Open gemspec in editor
  #
  #  # thor.run("#{options["edit"]} \"#{target.join("#{name}.gemspec")}\"")
  #end

  puts "\nComplete."
end