Class: Tumbler::Generate

Inherits:
Object
  • Object
show all
Includes:
Informer, Runner
Defined in:
lib/tumbler/generate.rb

Constant Summary

Constants included from Informer

Informer::Colors

Instance Attribute Summary collapse

Attributes included from Runner

#noop

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Informer

#inform

Methods included from Runner

#dry, #sh, #sh_with_code

Constructor Details

#initialize(dir, name) ⇒ Generate

Returns a new instance of Generate.



31
32
33
34
35
36
37
38
# File 'lib/tumbler/generate.rb', line 31

def initialize(dir, name)
  @base = dir
  @name = name
  @dependencies = []
  @development_dependencies = []
  @version = Manager::Version::INITIAL_VERSION
  @changelog = Manager::Changelog::DEFAULT_FILE
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



28
29
30
# File 'lib/tumbler/generate.rb', line 28

def base
  @base
end

#changelogObject

Returns the value of attribute changelog.



29
30
31
# File 'lib/tumbler/generate.rb', line 29

def changelog
  @changelog
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



28
29
30
# File 'lib/tumbler/generate.rb', line 28

def dependencies
  @dependencies
end

#development_dependenciesObject (readonly)

Returns the value of attribute development_dependencies.



28
29
30
# File 'lib/tumbler/generate.rb', line 28

def development_dependencies
  @development_dependencies
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/tumbler/generate.rb', line 28

def name
  @name
end

#versionObject

Returns the value of attribute version.



29
30
31
# File 'lib/tumbler/generate.rb', line 29

def version
  @version
end

Class Method Details

.app(dir, name, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tumbler/generate.rb', line 10

def self.app(dir, name, opts = {})
  generator = Generate.new(dir, name)
  generator.version = opts[:version] if opts[:version]
  generator.changelog = opts[:changelog] if opts.key?(:changelog)
  generator.development_dependencies << ::Gem::Dependency.new('tumbler')
  if opts[:dependencies]
    Array(opts[:dependencies]).each do |dep|
      generator.dependencies << (dep.is_a?(Array) ? ::Gem::Dependency.new(*dep) : ::Gem::Dependency.new(dep))
    end
  end
  if opts[:development_dependencies]
    Array(opts[:development_dependencies]).each do |dep|
      generator.development_dependencies << (dep.is_a?(Array) ? ::Gem::Dependency.new(*dep) : ::Gem::Dependency.new(dep))
    end
  end
  generator
end

Instance Method Details

#config_fileObject



76
77
78
# File 'lib/tumbler/generate.rb', line 76

def config_file
  File.join(@base, 'Tumbler')
end

#constant_nameObject



40
41
42
43
44
# File 'lib/tumbler/generate.rb', line 40

def constant_name
  result = @name.split('_').map{|p| p.capitalize}.join
  result = result.split('-').map{|q| q.capitalize}.join('::') if result =~ /-/
  result
end

#copy_template(template_file, options = {}) ⇒ Object

copy_template(‘generic.rb.erb’, :to => ‘/path/to/file’)



139
140
141
142
143
144
# File 'lib/tumbler/generate.rb', line 139

def copy_template(template_file, options={})
  FileUtils.mkdir_p(File.dirname(options[:to]))
  template = ERB.new(File.read(template_path(template_file)), 0, '<>')
  contents = template.result(options[:binding] || binding)
  File.open(options[:to], 'w') {|f| f << contents }
end

#gemfile_fileObject



68
69
70
# File 'lib/tumbler/generate.rb', line 68

def gemfile_file
  File.join(@base, 'Gemfile')
end

#gemspec_fileObject



72
73
74
# File 'lib/tumbler/generate.rb', line 72

def gemspec_file
  File.join(@base, "#{@name}.gemspec")
end

#git_emailObject



122
123
124
# File 'lib/tumbler/generate.rb', line 122

def git_email
  sh('git config user.email').strip rescue 'user.email'
end

#git_nameObject



126
127
128
# File 'lib/tumbler/generate.rb', line 126

def git_name
  sh('git config user.name').strip rescue 'user.name'
end

#github_userObject



130
131
132
# File 'lib/tumbler/generate.rb', line 130

def github_user
  sh('git config github.user').strip rescue 'github.user'
end

#initial_commitObject



60
61
62
63
64
65
66
# File 'lib/tumbler/generate.rb', line 60

def initial_commit
  inform "Performing initial commit"
  sh 'git init'
  sh 'git add .'
  sh 'git commit -a -m"Initial commit"'
  sh "git tag #{@version}"
end

#rb_pathObject



84
85
86
# File 'lib/tumbler/generate.rb', line 84

def rb_path
  File.join(@base, 'lib', "#{@name}.rb")
end

#template_path(path) ⇒ Object



134
135
136
# File 'lib/tumbler/generate.rb', line 134

def template_path(path)
  File.join(File.dirname(__FILE__), '..', 'template', path)
end

#version_pathObject



80
81
82
# File 'lib/tumbler/generate.rb', line 80

def version_path
  File.join(@base, 'lib', @name, 'version.rb')
end

#writeObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tumbler/generate.rb', line 46

def write
  inform "Generating gem #{name}" do
    write_gemspec
    write_gemfile
    write_version(@version)
    write_file
    write_changelog
    write_rakefile
    write_tumbler_config
    sh 'git init'
    initial_commit
  end
end

#write_changelogObject



88
89
90
91
# File 'lib/tumbler/generate.rb', line 88

def write_changelog
  inform "Writing changelog file"
  File.open(File.join(@base, @changelog), 'w') {|f| f << '' } if @changelog
end

#write_fileObject



98
99
100
# File 'lib/tumbler/generate.rb', line 98

def write_file
  copy_template('generic.rb.erb', :to => rb_path)
end

#write_gemfileObject



107
108
109
110
# File 'lib/tumbler/generate.rb', line 107

def write_gemfile
  inform "Writing Gemfile"
  copy_template('Gemfile.erb', :to => gemfile_file)
end

#write_gemspecObject



112
113
114
115
# File 'lib/tumbler/generate.rb', line 112

def write_gemspec
  inform "Writing #{name}.gemspec"
  copy_template('generic.gemspec.erb', :to => gemspec_file)
end

#write_rakefileObject



93
94
95
96
# File 'lib/tumbler/generate.rb', line 93

def write_rakefile
  inform "Writing Rakefile"
  FileUtils.cp(template_path('Rakefile'), @base)
end

#write_tumbler_configObject



117
118
119
120
# File 'lib/tumbler/generate.rb', line 117

def write_tumbler_config
  inform "Writing Tumbler config"
  copy_template('Tumbler.erb', :to => config_file)
end

#write_version(version) ⇒ Object



102
103
104
105
# File 'lib/tumbler/generate.rb', line 102

def write_version(version)
  inform "Writing version file"
  copy_template('version.rb.erb', :to => version_path, :binding => binding)
end