Module: Bridgetown::Commands::Actions

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

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



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

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



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

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_yarn_for_gem(gemname) ⇒ Object



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

def add_yarn_for_gem(gemname)
  say_status :add_yarn, gemname

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

#apply_from_url(url) ⇒ Object



112
113
114
# File 'lib/bridgetown-core/commands/concerns/actions.rb', line 112

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

#create_builder(filename, data = nil) ⇒ Object



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

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



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

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



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

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