Module: Bridgetown::Commands::Actions

Included in:
Apply, Configure
Defined in:
bridgetown-core/lib/bridgetown-core/commands/concerns/actions.rb

Constant Summary collapse

GITHUB_REGEX =
%r!https://github\.com!
GITHUB_TREE_REGEX =
%r!#{GITHUB_REGEX}/.*/.*/tree/.*/?!
GITHUB_BLOB_REGEX =
%r!#{GITHUB_REGEX}/.*/.*/blob/!
GITHUB_REPO_REGEX =
%r!github\.com/(.*?/[^/]*)!

Instance Method Summary collapse

Instance Method Details

#add_gem(gemname, group: nil, version: nil) ⇒ Object Also known as: add_bridgetown_plugin



57
58
59
60
61
62
63
64
65
66
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/actions.rb', line 57

def add_gem(gemname, group: nil, version: nil)
  options = +""
  options += " -v \"#{version}\"" if version
  options += " -g #{group}" if group
  # in_bundle? returns the path to the gemfile
  run "bundle add #{gemname}#{options}",
      env: { "BUNDLE_GEMFILE" => Bundler::SharedHelpers.in_bundle? }
rescue SystemExit
  say_status :run, "Gem not added due to bundler error", :red
end

#add_initializer(name, data = "") ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/actions.rb', line 69

def add_initializer(name, data = "")
  say_status :initializer, name
  data = yield if block_given?
  data = data.indent(2).lstrip
  data = " #{data}" unless data.start_with?(",")
  data += "\n" unless data[-1] == "\n"

  init_file = File.join("config", "initializers.rb")
  unless File.exist?(init_file)
    create_file("config/initializers.rb", verbose: true) do
      File.read(File.expand_path("../../../site_template/config/initializers.rb", __dir__))
    end
  end

  inject_into_file init_file, %(  init :"#{name}"#{data}),
                   before: %r!^end$!, verbose: false, force: false
end

#add_npm_for_gem(gemname) ⇒ Object Also known as: add_yarn_for_gem



104
105
106
107
108
109
110
111
112
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/actions.rb', line 104

def add_npm_for_gem(gemname)
  say_status :add_npm, gemname

  Bundler.reset!
  Bridgetown::PluginManager.load_determined_bundler_environment
  Bridgetown::PluginManager.install_npm_dependencies(name: gemname)
rescue SystemExit
  say_status :add_npm, "Package not added due to NPM error", :red
end

#add_npm_package(package_details) ⇒ Object



115
116
117
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/actions.rb', line 115

def add_npm_package(package_details)
  run "#{Bridgetown::PluginManager.package_manager} #{Bridgetown::PluginManager.package_manager_install_command} #{package_details}" # rubocop:disable Layout
end

#apply_from_url(url) ⇒ Object



119
120
121
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/actions.rb', line 119

def apply_from_url(url)
  apply transform_automation_url(url.dup)
end

#create_builder(filename, data = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/actions.rb', line 13

def create_builder(filename, data = nil)
  say_status :create_builder, filename
  data ||= yield if block_given?

  site_builder = File.join("plugins", "site_builder.rb")
  unless File.exist?(site_builder)
    create_file("plugins/site_builder.rb", verbose: true) do
      <<~RUBY
        class SiteBuilder < Bridgetown::Builder
        end
      RUBY
    end
  end

  create_file("plugins/builders/#{filename}", data, verbose: false)
end

#javascript_import(data = nil, filename: "index.js") ⇒ Object

rubocop:todo Metrics/PerceivedComplexity



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/actions.rb', line 30

def javascript_import(data = nil, filename: "index.js") # rubocop:todo Metrics/PerceivedComplexity
  data ||= yield if block_given?
  data += "\n" unless data[-1] == "\n"

  say_status :javascript_import, filename

  js_index = File.join("frontend", "javascript", filename)
  if File.exist?(js_index)
    index_file = File.read(js_index)

    last_import = ""
    index_file.each_line do |line|
      line.start_with?("import ") ? last_import = line : break
    end

    if last_import == ""
      # add to top of file
      prepend_file js_index, data, verbose: false
    else
      # inject after the last import line
      inject_into_file js_index, data, after: last_import, verbose: false, force: false
    end
  else
    create_file(js_index, data, verbose: false)
  end
end

#ruby_configure(name, data = "") ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/actions.rb', line 87

def ruby_configure(name, data = "")
  say_status :configure, name
  data = yield if block_given?
  data = data.indent(2)
  data += "\n" unless data[-1] == "\n"

  init_file = File.join("config", "initializers.rb")
  unless File.exist?(init_file)
    create_file("config/initializers.rb", verbose: true) do
      File.read(File.expand_path("../../../site_template/config/initializers.rb", __dir__))
    end
  end

  inject_into_file init_file, data,
                   before: %r!^end$!, verbose: false, force: false
end