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

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

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Attributes inherited from WriteGeneratedFilesToDisk

#paths_to_source_code

Attributes included from CommandPatternImplementation::Concerns::Subcommands

#is_subcommand

Attributes included from CommandPatternImplementation::Concerns::Runtime

#exception, #outcome, #raw_result

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

#relevant_entity_classes

Methods included from NestedTransactionable

#auto_detect_current_transactions, #commit_transaction, #commit_transaction_if_open, #open_transaction, #opened_transactions, #relevant_entity_classes, relevant_entity_classes_for_type, #relevant_entity_classes_for_type, #rollback_transaction, #transactions, #use_transaction, with_needed_transactions_for_type

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

Instance Attribute Details

#push_to_github_failedObject

Returns the value of attribute push_to_github_failed.



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

def push_to_github_failed
  @push_to_github_failed
end

#use_git_failedObject

Returns the value of attribute use_git_failed.



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

def use_git_failed
  @use_git_failed
end

Class Method Details

.generator_keyObject



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

def generator_key
  "ruby-project"
end

Instance Method Details

#bundle_installObject



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

def bundle_install
  puts "bundling..."
  cmd = "bundle install"

  if Bundler.respond_to?(:with_unbundled_env)
    Bundler.with_unbundled_env do
      run_cmd_and_return_output(cmd)
    end
  else
    # :nocov:
    run_cmd_and_return_output(cmd)
    # :nocov:
  end
end

#create_output_directory_if_neededObject



40
41
42
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 40

def create_output_directory_if_needed
  FileUtils.mkdir_p output_directory
end

#default_output_directoryObject



62
63
64
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 62

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.22/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



48
49
50
51
52
53
54
55
56
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 48

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)


44
45
46
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 44

def extract_from_another_repo?
  !!extract_from_repo
end

#generate_file_contentsObject



78
79
80
81
82
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 78

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



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

def git_add_all
  return if use_git_failed

  cmd = "git add ."
  run_cmd_and_return_output(cmd)
rescue CouldNotExecuteError => e
  # :nocov:
  self.use_git_failed = true
  warn e.message
  # :nocov:
end

#git_add_remote_originObject



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

def git_add_remote_origin
  return if push_to_github_failed

  cmd = "git remote add origin git@github.com:#{project_config.org_slash_project_kebab}.git"
  exit_status = run_cmd_and_write_output(cmd, raise_if_fails: false)

  unless exit_status&.success?
    # :nocov:
    self.push_to_github_failed = true
    # :nocov:
  end
end

#git_branch_mainObject



221
222
223
224
225
226
227
228
229
230
231
232
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 221

def git_branch_main
  return if use_git_failed

  cmd = "git branch -M main"
  exit_status = run_cmd_and_write_output(cmd, raise_if_fails: false)

  unless exit_status&.success?
    # :nocov:
    self.use_git_failed = true
    # :nocov:
  end
end

#git_commitObject



183
184
185
186
187
188
189
190
191
192
193
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 183

def git_commit
  return if use_git_failed

  cmd = "git commit -m 'Create ruby project files'"
  run_cmd_and_return_output(cmd)
rescue CouldNotExecuteError => e
  # :nocov:
  self.use_git_failed = true
  warn e.message
  # :nocov:
end

#git_initObject



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

def git_init
  return if use_git_failed

  cmd = "git init"
  run_cmd_and_return_output(cmd)
rescue CouldNotExecuteError => e
  # :nocov:
  self.use_git_failed = true
  warn e.message
  # :nocov:
end

#github_create_repoObject



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

def github_create_repo
  return if push_to_github_failed

  puts "pushing to github..."

  cmd = "gh repo create --private #{project_config.org_slash_project_kebab}"
  exit_status = run_cmd_and_write_output(cmd, raise_if_fails: false)

  unless exit_status&.success?
    self.push_to_github_failed = true
  end
end

#make_bin_files_executableObject



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

def make_bin_files_executable
  Dir["bin/*"].each do |file|
    if File.file?(file)
      cmd = "chmod u+x #{file}"
      begin
        run_cmd_and_return_output(cmd)
      rescue CouldNotExecuteError => e
        # :nocov:
        warn e.message
        # :nocov:
      end
    end
  end
end

#outputObject



249
250
251
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 249

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

#output_directoryObject



58
59
60
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 58

def output_directory
  inputs[:output_directory] || default_output_directory
end

#push_to_githubObject



234
235
236
237
238
239
240
241
242
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 234

def push_to_github
  return if push_to_github_failed

  cmd = "git push -u origin main"
  run_cmd_and_return_output(cmd)
rescue CouldNotExecuteError => e
  self.push_to_github_failed = true
  warn e.message
end

#push_to_github?Boolean

Returns:

  • (Boolean)


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

def push_to_github?
  project_config.push_to_github
end

#rbenv_bundler_onObject



244
245
246
247
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 244

def rbenv_bundler_on
  cmd = "rbenv bundler on"
  run_cmd_and_write_output(cmd, raise_if_fails: false)
end

#rubocop_autocorrectObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 124

def rubocop_autocorrect
  puts "linting..."

  cmd = "bundle exec rubocop --no-server -A"

  if Bundler.respond_to?(:with_unbundled_env)
    Bundler.with_unbundled_env do
      run_cmd_and_return_output(cmd)
    end
  else
    # :nocov:
    run_cmd_and_return_output(cmd)
    # :nocov:
  end
rescue CouldNotExecuteError => e
  # :nocov:
  warn e.message
  # :nocov:
end

#run_post_generation_tasksObject



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

def run_post_generation_tasks
  Dir.chdir output_directory do
    bundle_install
    make_bin_files_executable
    rubocop_autocorrect

    if use_git?
      git_init unless extract_from_another_repo?
      git_add_all
      git_commit
      git_branch_main

      if push_to_github?
        github_create_repo
        git_add_remote_origin
        push_to_github
      end

      if turn_on_rbenv_bundler?
        rbenv_bundler_on
      end
    end
  end
end

#turn_on_rbenv_bundler?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 70

def turn_on_rbenv_bundler?
  project_config.turn_on_rbenv_bundler
end

#use_git?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'foobara-empty-ruby-project-generator-0.0.22/src/write_empty_ruby_project_to_disk.rb', line 74

def use_git?
  project_config.use_git
end