What is the simplest way to add a custom command to my site? #655
-
Hello! 👋🏻 I'm enjoying Bridgetown so far but I find the documentation around plugin customization a bit confusing. I would prefer to make it a Thor command, rather than a Rake task in the Rakefile, because Thor provides more powerful tools, for example around command line option parsing. I would rather avoid creating a whole plugin for such a simple command, if at all possible. I tried using an How do I get my file loaded when necessary, in a way that would allow me to declare a new command the way it's described at https://www.bridgetownrb.com/docs/plugins/commands? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Here's the minimum amount of code I found would work to add a diff --git c/Gemfile i/Gemfile
index baffc2c..2a83ea2 100644
--- c/Gemfile
+++ i/Gemfile
@@ -31,3 +31,7 @@ gem "bridgetown", "~> 1.1.0"
# Puma is a Rack-compatible server used by Bridgetown
# (you can optionally limit this to the "development" group)
gem "puma", "~> 5.6"
+
+group :bridgetown_plugins do
+ gem "create_post", path: "./create_post"
+end
diff --git c/Gemfile.lock i/Gemfile.lock
index 967c373..836c114 100644
--- c/Gemfile.lock
+++ i/Gemfile.lock
@@ -1,3 +1,8 @@
+PATH
+ remote: create_post
+ specs:
+ create_post (0.1.0)
+
GEM
remote: https://rubygems.org/
specs:
@@ -120,6 +125,7 @@ PLATFORMS
DEPENDENCIES
bridgetown (~> 1.1.0)
+ create_post!
puma (~> 5.6)
BUNDLED WITH
diff --git c/create_post/create_post.gemspec i/create_post/create_post.gemspec
new file mode 100644
index 0000000..119baf4
--- /dev/null
+++ i/create_post/create_post.gemspec
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+Gem::Specification.new do |spec|
+ spec.name = "create_post"
+ spec.version = "0.1.0"
+ spec.author = "David"
+ spec.summary = "Provide a command line interface to create a new post"
+end
diff --git c/create_post/lib/create_post.rb i/create_post/lib/create_post.rb
new file mode 100644
index 0000000..6b8ac36
--- /dev/null
+++ i/create_post/lib/create_post.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+require "bridgetown"
+
+require_all "bridgetown-core/commands/concerns"
+
+module CreatePost
+ module Commands
+ class Post < Thor::Group
+ include Thor::Actions
+ extend Bridgetown::Commands::Summarizable
+
+ Bridgetown::Commands::Registrations.register do
+ register(Post, "post", "post TITLE", Post.summary)
+ end
+
+ def self.banner
+ "bridgetown post TITLE"
+ end
+ summary "Creates a new post"
+
+ def create_post
+ # Write the command's action code here
+ end
+ end
+ end
+end |
Beta Was this translation helpful? Give feedback.
Here's the minimum amount of code I found would work to add a
post
command to my Bridgetown project: