Module: Nextgen::Actions

Includes:
Bundler, Git, Javascript
Defined in:
lib/nextgen/actions.rb

Defined Under Namespace

Modules: Bundler, Git, Javascript

Instance Method Summary collapse

Methods included from Javascript

#add_js_packages, #add_package_json_scripts, #js_package_manager, #remove_js_packages, #remove_package_json_script, #run_js_command, #yarn?

Methods included from Git

#apply_as_git_commit, #git_commit_all, #git_status, #git_user_configured?, #git_working?, #say_git

Methods included from Bundler

#binstubs, #bundle_command!, #bundler_ruby_file_supported?, #bundler_version_satisifed?, #install_gems, #noisy_bundler_version?, #remove_gem

Instance Method Details

#add_lint_task(task, fix: "#{task}:autocorrect") ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/nextgen/actions.rb', line 95

def add_lint_task(task, fix: "#{task}:autocorrect")
  unless File.read("README.md").include?(" and lint checks")
    inject_into_file "README.md", " and lint checks", after: "automated tests"
    inject_into_file "README.md", <<~MARKDOWN, after: "```\nbin/rake\n```\n"

      > [!TIP]
      > Rake allows you to run all checks in parallel with the `-m` option. This is much faster, but since the output is interleaved, it may be harder to read.

      ```
      bin/rake -m
      ```

      ### Fixing lint issues

      Some lint issues can be auto-corrected. To fix them, run:

      ```
      bin/rake fix
      ```
    MARKDOWN
  end

  unless File.read("Rakefile").include?("task fix:")
    append_to_file "Rakefile", <<~RUBY

      desc "Apply auto-corrections"
      task fix: %w[] do
        puts ">>>>>> [OK] All fixes applied!"
      end
    RUBY
  end

  inject_into_file "Rakefile", " #{task}", after: /task default: %w\[[^\]]*/
  inject_into_file "Rakefile", " #{fix}", after: /task fix: %w\[[^\]]*/
end

#copy_test_support_file(file) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nextgen/actions.rb', line 58

def copy_test_support_file(file)
  copy_action = file.end_with?(".tt") ? method(:template) : method(:copy_file)

  if minitest?
    create_test_support_directory
    copy_action.call "test/support/#{file}"
  elsif rspec?
    empty_directory "spec/support"
    spec_file_path = "spec/support/#{file}"
    if Nextgen.template_path.join(spec_file_path).exist?
      copy_action.call spec_file_path
    else
      copy_action.call "test/support/#{file}", spec_file_path.sub(/\.tt$/, "")
    end
  end
end

#create_test_support_directoryObject



75
76
77
78
79
80
81
82
83
# File 'lib/nextgen/actions.rb', line 75

def create_test_support_directory
  empty_directory "test/support"
  return if File.exist?("test/test_helper.rb") && File.read("test/test_helper.rb").include?("support/**/*.rb")

  append_to_file "test/test_helper.rb", <<~RUBY

    Dir[File.expand_path("support/**/*.rb", __dir__)].sort.each { |rb| require(rb) }
  RUBY
end

#die(message = nil) ⇒ Object

Raises:

  • (Thor::Error)


153
154
155
156
157
# File 'lib/nextgen/actions.rb', line 153

def die(message = nil)
  message = message.sub(/^/, "ERROR: ") if message && !message.start_with?(/error/i)

  raise Thor::Error, message
end

#document_deploy_var(var_name, desc = nil, required: false, default: nil) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/nextgen/actions.rb', line 85

def document_deploy_var(var_name, desc = nil, required: false, default: nil)
  insertion = "`#{var_name}`"
  insertion += " **REQUIRED**" if required
  insertion += " - #{desc}" if desc.present?
  insertion += " (default: #{default})" unless default.nil?

  copy_file "DEPLOYMENT.md" unless File.exist?("DEPLOYMENT.md")
  inject_into_file "DEPLOYMENT.md", "#{insertion}\n- ", after: /^## Environment variables.*?^- /m
end

#gitignore(*lines) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/nextgen/actions.rb', line 131

def gitignore(*lines)
  return unless File.exist?(".gitignore")

  lines -= File.read(".gitignore").lines(chomp: true)
  return if lines.empty?

  text = lines.map(&:strip).join("\n")
  append_to_file ".gitignore", "#{text}\n"
end

#minitest?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/nextgen/actions.rb', line 50

def minitest?
  File.exist?("test/test_helper.rb")
end

#move(from, to) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nextgen/actions.rb', line 36

def move(from, to)
  if File.exist?(to)
    say_status :skip, "#{to} exists", :yellow
    return
  end
  unless File.exist?(from)
    say_status :skip, "#{from} does not exit", :yellow
    return
  end

  say_status :move, [from, to].join(""), :green
  FileUtils.mv(from, to)
end

#prevent_autoload_lib(*paths) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/nextgen/actions.rb', line 141

def prevent_autoload_lib(*paths)
  return unless File.read("config/application.rb").match?(/^\s*config.autoload_lib/)

  paths.reverse_each do |path|
    gsub_file("config/application.rb", /autoload_lib\(ignore: %w.*$/) do |match|
      next match if match.match?(/%w.*[\(\[ ]#{Regexp.quote(path)}[ \)\]]/)

      match.sub(/%w[\(\[]/, '\0' + path + " ")
    end
  end
end

#read_system_time_zone_nameObject



159
160
161
162
163
# File 'lib/nextgen/actions.rb', line 159

def read_system_time_zone_name
  return unless File.symlink?("/etc/localtime")

  File.readlink("/etc/localtime")[%r{zoneinfo/(.+)$}, 1]
end

#rspec?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/nextgen/actions.rb', line 54

def rspec?
  File.exist?("spec/spec_helper.rb")
end

#run!(cmd, env: {}, verbose: true, capture: false) ⇒ Object

Like Thor’s built-in ‘run`, but raises a descriptive exception if the command fails.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nextgen/actions.rb', line 18

def run!(cmd, env: {}, verbose: true, capture: false)
  if capture
    say_status :run, cmd, :green if verbose
    return if options[:pretend]

    require "open3"
    result, status = Open3.capture2e(env, cmd)
    success = status.success?
  else
    result = success = run(cmd, env:, verbose:)
  end

  return result if success

  say result if result.present?
  die "Failed to run command. Cannot continue. (#{cmd})"
end

#with_nextgen_source_pathObject



9
10
11
12
13
14
15
# File 'lib/nextgen/actions.rb', line 9

def with_nextgen_source_path
  path = Nextgen.template_path.to_s
  source_paths.unshift(path)
  yield
ensure
  source_paths.shift if source_paths[0] == path
end