Class: Hoboken::Generate

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/hoboken/generate.rb

Overview

Main project generator.

rubocop:disable Metrics/ClassLength

Constant Summary collapse

NULL =
RbConfig::CONFIG['host_os'].match?(/mingw|mswin/) ? 'NUL' : '/dev/null'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/hoboken/generate.rb', line 193

def self.exit_on_failure?
  true
end

.source_rootObject



46
47
48
# File 'lib/hoboken/generate.rb', line 46

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#app_folderObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hoboken/generate.rb', line 50

def app_folder
  empty_directory(snake_name)
  apply_template('classic.rb.tt', 'app.rb')
  apply_template('Gemfile.erb.tt', 'Gemfile')
  apply_template('config.ru.tt', 'config.ru')
  apply_template('README.md.tt', 'README.md')
  apply_template('Rakefile.tt', 'Rakefile')

  create_file("#{snake_name}/Procfile") do
    'web: bundle exec puma -C config/puma.rb'
  end
end

#asset_pipelineObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/hoboken/generate.rb', line 86

def asset_pipeline
  return if options[:api_only]

  inside snake_name do
    empty_directory('assets')
    empty_directory('public')
    %w[stylesheets images javascripts].each { |f| empty_directory("assets/#{f}") }
  end

  apply_template('styles.css.tt', 'assets/stylesheets/styles.scss')
  create_file("#{snake_name}/assets/javascripts/app.js", '')

  %w[favicon hoboken sinatra].each do |f|
    copy_file("templates/#{f}.png", "#{snake_name}/assets/images/#{f}.png")
  end
end

#bin_folderObject



63
64
65
66
67
68
69
70
# File 'lib/hoboken/generate.rb', line 63

def bin_folder
  empty_directory("#{snake_name}/bin")
  %w[console server setup].each do |f|
    target = "#{snake_name}/bin/#{f}"
    copy_file("templates/#{f}", target)
    chmod(target, 0o755)
  end
end

#config_folderObject



72
73
74
75
76
# File 'lib/hoboken/generate.rb', line 72

def config_folder
  empty_directory("#{snake_name}/config")
  apply_template('puma.rb.tt', 'config/puma.rb')
  apply_template('classic_environment.rb.tt', 'config/environment.rb')
end

#create_git_repositoryObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/hoboken/generate.rb', line 173

def create_git_repository
  return unless options[:git]

  if git?
    copy_file('templates/gitignore', "#{snake_name}/.gitignore")
    inside snake_name do
      run('git init .')
      run('git add .')
      run('git commit -m "Initial commit."')
    end
  else
    say "\nYou asked that a Git repository be created for the " \
        'project, but no Git executable could be found.'
  end
end

#directionsObject



189
190
191
# File 'lib/hoboken/generate.rb', line 189

def directions
  say "\nSuccessfully created #{name}."
end

#env_fileObject



130
131
132
133
134
135
136
# File 'lib/hoboken/generate.rb', line 130

def env_file
  inside snake_name do
    create_file('.env') do
      "RACK_ENV=development\nPORT=9292\nSESSION_SECRET=secret"
    end
  end
end

#inline_viewsObject

rubocop:enable Metrics/AbcSize



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/hoboken/generate.rb', line 161

def inline_views
  return unless options[:tiny]
  return if options[:api_only]

  combined_views = %w[layout index].map { |f|
    "@@#{f}\n" + File.read("#{snake_name}/views/#{f}.erb")
  }.join("\n")

  append_to_file("#{snake_name}/app.rb", "\n__END__\n\n#{combined_views}")
  remove_dir("#{snake_name}/views")
end

#make_modularObject

rubocop:disable Metrics/AbcSize



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/hoboken/generate.rb', line 139

def make_modular
  return unless 'modular' == options[:type]

  empty_directory("#{snake_name}/helpers")
  remove_file("#{snake_name}/app.rb")
  remove_file("#{snake_name}/config/environment.rb")
  apply_template('modular.rb.tt', 'app.rb')
  apply_template('modular_environment.rb.tt', 'config/environment.rb')

  files = [].tap do |f|
    f << 'config.ru'
    f << 'test/support/rack_helpers.rb' if 'test-unit' == options[:test_framework]
    f << 'spec/support/rack_helpers.rb' if 'rspec' == options[:test_framework]
  end

  files.each do |f|
    path = File.join(snake_name, f)
    gsub_file(path, /Sinatra::Application/, "#{camel_name}::App")
  end
end

#rspec_setupObject



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/hoboken/generate.rb', line 117

def rspec_setup
  return unless 'rspec' == options[:test_framework]

  empty_directory("#{snake_name}/spec")
  empty_directory("#{snake_name}/spec/support")
  create_file("#{snake_name}/.rspec") { '--require spec_helper' }
  apply_template('rspec.rake.tt', 'tasks/rspec.rake')
  apply_template('spec/app_spec.rb.tt', 'spec/app_spec.rb')
  apply_template('spec/spec_helper.rb.tt', 'spec/spec_helper.rb')
  apply_template('support/rack_helpers.rb.tt', 'spec/support/rack_helpers.rb')
  apply_template('spec/rack_matchers.rb.tt', 'spec/support/rack_matchers.rb')
end

#test_folderObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hoboken/generate.rb', line 103

def test_folder
  return unless 'test-unit' == options[:test_framework]

  empty_directory("#{snake_name}/test/unit")
  empty_directory("#{snake_name}/test/integration")
  empty_directory("#{snake_name}/test/support")
  apply_template('test_unit.rake.tt', 'tasks/test_unit.rake')
  apply_template('test/test_helper.rb.tt', 'test/test_helper.rb')
  apply_template('test/unit/app_test.rb.tt', 'test/unit/app_test.rb')
  apply_template('support/rack_helpers.rb.tt', 'test/support/rack_helpers.rb')
  apply_template('support/rack_test_assertions.rb.tt',
                 'test/support/rack_test_assertions.rb')
end

#view_folderObject



78
79
80
81
82
83
84
# File 'lib/hoboken/generate.rb', line 78

def view_folder
  return if options[:api_only]

  empty_directory("#{snake_name}/views")
  apply_template('views/layout.erb.tt', 'views/layout.erb')
  apply_template('views/index.erb.tt', 'views/index.erb')
end