Class: Foobara::Generators::EmptyRubyProjectGenerator::WriteEmptyRubyProjectToDisk

Inherits:
WriteGeneratedFilesToDisk show all
Defined in:
foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary

Attributes inherited from WriteGeneratedFilesToDisk

#paths_to_source_code

Attributes included from CommandPatternImplementation::Concerns::Subcommands

#is_subcommand

Attributes included from CommandPatternImplementation::Concerns::Runtime

#exception, #outcome

Attributes included from CommandPatternImplementation::Concerns::Errors

#error_collection

Attributes included from CommandPatternImplementation::Concerns::Inputs

#inputs, #raw_inputs

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WriteGeneratedFilesToDisk

#delete_old_files_if_needed, #generate_generated_files_json, #generated_files_json_filename, #run_cmd_and_return_output, #run_cmd_and_write_output, #stats, #write_all_files_to_disk, #write_file_to_disk

Methods inherited from Command

install!, reset_all

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods included from CommandPatternImplementation::Concerns::Reflection

#initialize

Methods included from CommandPatternImplementation::Concerns::DomainMappers

#domain_map, #domain_map!, #run_mapped_subcommand!

Methods included from CommandPatternImplementation::Concerns::Subcommands

#run_subcommand!, #subcommand?

Methods included from CommandPatternImplementation::Concerns::Entities

#load_entities, #load_records

Methods included from CommandPatternImplementation::Concerns::Transactions

#auto_detect_current_transactions, #commit_transaction, #open_transaction, #opened_transactions, #relevant_entity_classes, #rollback_transaction, #transactions

Methods included from CommandPatternImplementation::Concerns::StateMachine

#state_machine

Methods included from CommandPatternImplementation::Concerns::Runtime

#halt!, #run, #run!, #run_execute, #succeed, #success?, #validate, #validate_records

Methods included from CommandPatternImplementation::Concerns::Errors

#initialize

Methods included from CommandPatternImplementation::Concerns::Inputs

#cast_and_validate_inputs, #initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?

Methods included from TruncatedInspect

#inspect, truncating

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::CommandPatternImplementation::Concerns::Inputs

Class Method Details

.generator_keyObject



10
11
12
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 10

def generator_key
  "ruby-project"
end

Instance Method Details

#bundle_installObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 86

def bundle_install
  puts "bundling..."
  do_it = proc do
    Open3.popen3("bundle install") do |_stdin, _stdout, stderr, wait_thr|
      exit_status = wait_thr.value
      unless exit_status.success?
        # :nocov:
        raise "could not bundle install. #{stderr.read}"
        # :nocov:
      end
    end
  end

  if Bundler.respond_to?(:with_unbundled_env)
    Bundler.with_unbundled_env(&do_it)
  else
    # :nocov:
    do_it.call
    # :nocov:
  end
end

#create_output_directory_if_neededObject



38
39
40
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 38

def create_output_directory_if_needed
  FileUtils.mkdir_p output_directory
end

#default_output_directoryObject



60
61
62
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 60

def default_output_directory
  project_config.org_slash_project_kebab
end

#executeObject



26
27
28
29
30
31
32
33
34
35
36
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 26

def execute
  create_output_directory_if_needed
  if extract_from_another_repo?
    extract_from_another_repo
  end
  generate_file_contents
  write_all_files_to_disk
  run_post_generation_tasks

  output
end

#extract_from_another_repoObject



46
47
48
49
50
51
52
53
54
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 46

def extract_from_another_repo
  run_subcommand!(
    ExtractRepo,
    repo_url_or_path: extract_from_repo,
    paths: paths_to_extract,
    delete_extracted:,
    output_path: output_directory
  )
end

#extract_from_another_repo?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 42

def extract_from_another_repo?
  !!extract_from_repo
end

#generate_file_contentsObject



64
65
66
67
68
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 64

def generate_file_contents
  puts "generating..."
  # TODO: just pass this in as the inputs instead of the command??
  self.paths_to_source_code = run_subcommand!(GenerateEmptyRubyProject, project_config.attributes)
end

#git_add_allObject



145
146
147
148
149
150
151
152
153
154
155
156
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 145

def git_add_all
  cmd = "git add ."

  Open3.popen3(cmd) do |_stdin, _stdout, stderr, wait_thr|
    exit_status = wait_thr.value
    unless exit_status.success?
      # :nocov:
      raise "could not #{cmd}\n#{stderr.read}"
      # :nocov:
    end
  end
end

#git_add_remote_originObject



183
184
185
186
187
188
189
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 183

def git_add_remote_origin
  unless system("git remote add origin git@github.com:#{project_config.org_slash_project_kebab}.git")
    # :nocov:
    raise "could not git remote add origin git@github.com:#{project_config.org_slash_project_kebab}.git"
    # :nocov:
  end
end

#git_branch_mainObject



191
192
193
194
195
196
197
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 191

def git_branch_main
  unless system("git branch -M main")
    # :nocov:
    raise "could not git branch -M main"
    # :nocov:
  end
end

#git_commitObject



158
159
160
161
162
163
164
165
166
167
168
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 158

def git_commit
  # TODO: set author/name with git config in CI so we don't have to skip this
  # :nocov:
  Open3.popen3("git commit -m 'Create ruby project files'") do |_stdin, stdout, stderr, wait_thr|
    exit_status = wait_thr.value
    unless exit_status.success?
      raise "could not git commit -m 'Initial commit'. OUTPUT\n#{stdout.read}\nERROR:#{stderr.read}"
    end
  end
  # :nocov:
end

#git_initObject



132
133
134
135
136
137
138
139
140
141
142
143
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 132

def git_init
  cmd = "git init"

  Open3.popen3(cmd) do |_stdin, _stdout, stderr, wait_thr|
    exit_status = wait_thr.value
    unless exit_status.success?
      # :nocov:
      raise "could not #{cmd}\n#{stderr.read}"
      # :nocov:
    end
  end
end

#github_create_repoObject



170
171
172
173
174
175
176
177
178
179
180
181
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 170

def github_create_repo
  puts "pushing to github..."

  cmd = "gh repo create --private #{project_config.org_slash_project_kebab}"

  Open3.popen3(cmd) do |_stdin, _stdout, _stderr, wait_thr|
    exit_status = wait_thr.value
    unless exit_status.success?
      warn "WARNING: could not #{cmd}"
    end
  end
end

#make_bin_files_executableObject



124
125
126
127
128
129
130
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 124

def make_bin_files_executable
  Dir["bin/*"].each do |file|
    if File.file?(file)
      system("chmod u+x #{file}")
    end
  end
end

#outputObject



219
220
221
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 219

def output
  "\nDone. #{stats}"
end

#output_directoryObject



56
57
58
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 56

def output_directory
  inputs[:output_directory] || default_output_directory
end

#push_to_githubObject



199
200
201
202
203
204
205
206
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 199

def push_to_github
  Open3.popen3("git push -u origin main") do |_stdin, _stdout, stderr, wait_thr|
    exit_status = wait_thr.value
    unless exit_status.success?
      warn "WARNING: could not git push -u origin main \n #{stderr.read}"
    end
  end
end

#rbenv_bundler_onObject



208
209
210
211
212
213
214
215
216
217
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 208

def rbenv_bundler_on
  # :nocov:
  Open3.popen3("rbenv bundler on") do |_stdin, _stdout, stderr, wait_thr|
    exit_status = wait_thr.value
    unless exit_status.success?
      warn "WARNING: could not rbenv bundler on \n #{stderr.read}"
    end
  end
  # :nocov:
end

#rubocop_autocorrectObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 108

def rubocop_autocorrect
  puts "linting..."

  do_it = -> { run_cmd_and_return_output("bundle exec rubocop --no-server -A") }

  if Bundler.respond_to?(:with_unbundled_env)
    Bundler.with_unbundled_env do
      do_it.call
    end
  else
    # :nocov:
    do_it.call
    # :nocov:
  end
end

#run_post_generation_tasksObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'foobara-empty-ruby-project-generator-0.0.18/src/write_empty_ruby_project_to_disk.rb', line 70

def run_post_generation_tasks
  Dir.chdir output_directory do
    bundle_install
    make_bin_files_executable
    rubocop_autocorrect
    git_init unless extract_from_another_repo?
    git_add_all
    git_commit
    github_create_repo
    git_add_remote_origin
    git_branch_main
    push_to_github
    rbenv_bundler_on
  end
end