Module: Bridgetown::Commands::Automations

Includes:
Freyia::Setup
Included in:
Apply, Configure, Esbuild, New, Plugins::New
Defined in:
bridgetown-core/lib/bridgetown-core/commands/concerns/automations.rb

Overview

Automation tasks to aid in setting up new Bridgetown site configs or plugin setup. Also includes all of the tasks provided by Freyia

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/(.*?/[^/]*)!
GITLAB_REGEX =
%r!https://gitlab\.com!
GITLAB_TREE_REGEX =
%r!#{GITLAB_REGEX}/.*/.*/-/tree/.*/?!
GITLAB_BLOB_REGEX =
%r!#{GITLAB_REGEX}/.*/.*/-/blob/!
GITLAB_REPO_REGEX =
%r!gitlab\.com/(.*?/[^/]*)!
CODEBERG_REGEX =
%r!https://codeberg\.org!
CODEBERG_TREE_REGEX =
%r!#{CODEBERG_REGEX}/.*/.*/src/branch/.*/?!
CODEBERG_REPO_REGEX =
%r!codeberg\.org/(.*?/[^/]*)!

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



10
11
12
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/automations.rb', line 10

def self.included(klass)
  klass.extend Freyia::Setup::ClassMethods
end

Instance Method Details

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

Uses bundle add to add a new gem to the project Gemfile

a particular group

Parameters:

  • gemname (String)
  • group (String) (defaults to: nil)

    normally the gem isn’t added to any group, but you can specify

  • version (String) (defaults to: nil)

    useful if you need to force a specific version or range



86
87
88
89
90
91
92
93
94
95
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/automations.rb', line 86

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

Add an init statement to the project’s config/initializers.rb file

Parameters:

  • name (Symbol)

    initializer / plugin name

  • data (String) (defaults to: "")

    additional Ruby code, if block not provided



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/automations.rb', line 102

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

Given the name of a gem, it will analyze that gem’s metadata looking for a suitable NPM companion package. (Requires npm_add to be defined.)

Parameters:

  • gemname (Symbol)


146
147
148
149
150
151
152
153
154
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/automations.rb', line 146

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

Adds the provided NPM package to the project’s package.json

Parameters:

  • package_details (String)

    the package name, and any optional flags



160
161
162
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/automations.rb', line 160

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

Calls Freyia’s apply method after transforming the URL according to Automations rules

Parameters:

  • url (String)

    URL to a file or a repo



174
175
176
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/automations.rb', line 174

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

#create_builder(filename, data = nil) ⇒ Object

Creates a new Builder class with the provided filename and Ruby code

Parameters:

  • filename (String)
  • data (String) (defaults to: nil)

    Ruby code, if block not provided



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/automations.rb', line 32

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

Adds a new JavaScript import statement to the end of existing import statements (if any)

Parameters:

  • data (String) (defaults to: nil)

    Ruby code, if block not provided

  • filename (String) (defaults to: "index.js")

    supply a filename if the default index.js isn’t desired



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/automations.rb', line 53

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

#remove_npm_package(package_details) ⇒ Object

Removes an NPM package

Parameters:

  • package_details (String)

    the package name, and any optional flags



167
168
169
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/automations.rb', line 167

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

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

Similar to the add_initializer method, but supports adding arbitrary Ruby code of any kind to the config/initializers.rb file

Parameters:

  • name (Symbol, String)

    name of configuration (purely for user display feedback)

  • data (String) (defaults to: "")

    Ruby code, if block not provided



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'bridgetown-core/lib/bridgetown-core/commands/concerns/automations.rb', line 125

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