Class: StubGem::RubyProject

Inherits:
Object
  • Object
show all
Defined in:
lib/stub_gem.rb

Instance Method Summary collapse

Constructor Details

#initialize(project_name, base_path) ⇒ RubyProject

Returns a new instance of RubyProject.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stub_gem.rb', line 11

def initialize(project_name, base_path)
  @year = Date.today.year
  @author = "David Crosby"
  @project_name = project_name
  @project_class = @project_name.capitalize

  puts "* Using #{base_path}"
  @template_dir = "#{File.dirname(Pathname.new(__FILE__).realpath)}/../templates/"
  puts "* Template dir of #{@template_dir}"
  fail_usage "project folder location exists at #{base_path}" if Dir.exist? base_path
  @base_path_str = base_path
  stub_project
end

Instance Method Details

#project_classObject



25
26
27
# File 'lib/stub_gem.rb', line 25

def project_class
  @project_class ||= @project_name.capitalize
end

#stub_build_infraObject



86
87
88
89
90
91
92
93
94
# File 'lib/stub_gem.rb', line 86

def stub_build_infra
  [
    ["Rakefile"],
    ["Guardfile"],
    ["bundle_config", ".bundle", "config"]
  ].each do |name, dir, desired_name|
    stub_single_file name, dir, desired_name
  end
end

#stub_code_boilerplateObject



104
105
106
107
108
109
110
111
# File 'lib/stub_gem.rb', line 104

def stub_code_boilerplate
  [
    ["base_lib.rb.erb", "lib", "#{@project_name}.rb"],
    ["version.rb.erb", "lib/#{@project_name}", "version.rb"]
  ].each do |name, dir, desired_name|
    stub_single_file name, dir, desired_name
  end
end

#stub_directory_structureObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stub_gem.rb', line 45

def stub_directory_structure
  [
    "lib/#{@project_name}",
    ".github/workflows",
    ".bundle",
    "bin",
    "test"
  ].each do |dir|
    FileUtils.mkdir_p("#{@base_path}/#{dir}")
  end
end

#stub_library_mgmtObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/stub_gem.rb', line 72

def stub_library_mgmt
  %w[
    Gemfile
  ].each do |f|
    stub_single_file(f)
  end
  [
    ["Gemfile"],
    ["gemspec", nil, "#{@project_name}.gemspec"]
  ].each do |name, dir, desired_name|
    stub_single_file name, dir, desired_name
  end
end

#stub_lintersObject



96
97
98
99
100
101
102
# File 'lib/stub_gem.rb', line 96

def stub_linters
  %w[
    .rubocop.yml
  ].each do |f|
    stub_single_file(f)
  end
end

#stub_projectObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/stub_gem.rb', line 29

def stub_project
  Dir.mkdir @base_path_str
  dir = Dir.new @base_path_str
  @base_path = "#{dir.path}/"
  puts "* Base path of: #{@base_path}"

  stub_directory_structure
  stub_project_docs
  stub_library_mgmt
  stub_testing
  stub_build_infra
  stub_linters
  stub_code_boilerplate
  stub_version_control
end

#stub_project_docsObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/stub_gem.rb', line 57

def stub_project_docs
  # TODO: stub_contributors
  %w[
    README.md
    TODO.md
    LICENSE
  ].each do |f|
    stub_single_file(f)
  end
end

#stub_single_file(name, dir = nil, desired_name = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/stub_gem.rb', line 124

def stub_single_file(name, dir=nil, desired_name=nil)
  template = ERB.new(File.read(File.join(@template_dir, name)))
  desired_name ||= name
  filepath = "#{@base_path}/"
  filepath << "#{dir}/" if dir
  filepath << desired_name
  f = File.new(filepath, "w")
  f.write(template.result(binding))
  f.close
end

#stub_testingObject



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

def stub_testing
  # TODO: stub_minitest
end

#stub_version_controlObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/stub_gem.rb', line 113

def stub_version_control
  [
    [".gitignore"],
    ["ruby_gh_workflow.yml", ".github/workflows", "main.yml"]
  ].each do |name, dir, desired_name|
    stub_single_file name, dir, desired_name
  end
  # TODO: stub_gitcommit
  # TODO stub_github_project_templates
end