Skip to content

Commit

Permalink
FEATURE: Use discourse-theme-skeleton repo (#44)
Browse files Browse the repository at this point in the history
…when creating a new theme

Co-authored-by: David Taylor <[email protected]>
  • Loading branch information
CvX and davidtaylorhq authored Feb 28, 2024
1 parent 8db6f93 commit 7db761f
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 166 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Setup Git
run: |
git config --global user.email "[email protected]"
git config --global user.name "Discourse CI"
- name: Setup ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion discourse_theme.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "listen", "~> 3.1"
spec.add_runtime_dependency "multipart-post", "~> 2.0"
spec.add_runtime_dependency "tty-prompt", "~> 0.18"
spec.add_runtime_dependency "rubyzip", "~> 1.2"
spec.add_runtime_dependency "rubyzip", "~> 2.3"
spec.add_runtime_dependency "selenium-webdriver", "> 4.11"

spec.add_development_dependency "bundler"
Expand Down
2 changes: 1 addition & 1 deletion lib/discourse_theme/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def run(args)
raise DiscourseTheme::ThemeError.new "git is not installed" if !command?("git")
raise DiscourseTheme::ThemeError.new "yarn is not installed" if !command?("yarn")

DiscourseTheme::Scaffold.generate(dir)
DiscourseTheme::Scaffold.generate(dir, name: args[1])
watch_theme?(args)
elsif command == "watch"
raise DiscourseTheme::ThemeError.new "'#{dir} does not exist" unless Dir.exist?(dir)
Expand Down
214 changes: 83 additions & 131 deletions lib/discourse_theme/scaffold.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,159 +2,111 @@

require "date"
require "json"
require "yaml"
require "resolv"

module DiscourseTheme
class Scaffold
ABOUT_JSON = {
about_url: "TODO: Put your theme's public repo or Meta topic URL here",
license_url: "TODO: Put your theme's LICENSE URL here",
assets: {
},
}

HELP = <<~STR
Are you a bit lost? Be sure to read https://meta.discourse.org/t/how-to-develop-custom-themes/60848
STR

LICENSE = <<~STR
Copyright #YEAR #AUTHOR
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
STR

GIT_IGNORE = <<~STR
.discourse-site
node_modules
HELP
STR

API_INITIALIZER = <<~STR
import { apiInitializer } from "discourse/lib/api";
export default apiInitializer("1.8.0", (api) => {
console.log("hello world from api initializer!");
});
STR

PACKAGE_JSON = <<~STR
{
"author": "#AUTHOR",
"license": "MIT",
"devDependencies": {
"eslint-config-discourse": "latest"
}
}
STR

ESLINT_RC = <<~STR
{
"extends": "eslint-config-discourse",
"globals": {
"settings": "readonly",
"themePrefix": "readonly"
}
}
STR

TEMPLATE_LINT_RC = <<~STR
module.exports = {
plugins: ["ember-template-lint-plugin-discourse"],
extends: "discourse:recommended",
};
STR

EN_YML = <<~YAML
en:
theme_metadata:
description: "#DESCRIPTION"
YAML

SETTINGS_YML = <<~YAML
todo_rename_and_use_setting:
default: ""
YAML

def self.generate(dir)
def self.generate(dir, name:)
UI.progress "Generating a scaffold theme at #{dir}"

name =
loop do
input = UI.ask("What would you like to call your theme?").to_s.strip
if input.empty?
UI.error("Theme name cannot be empty")
else
break input
end
end

name = UI.ask("What would you like to call your theme?", default: name).to_s.strip
is_component = UI.yes?("Is this a component?")

FileUtils.mkdir_p dir
Dir.chdir dir do
author =
loop do
input = UI.ask("Who is authoring the theme?", default: ENV["USER"]).to_s.strip
if input.empty?
UI.error("Author cannot be empty")
else
break input
end
end
get_theme_skeleton(dir)

Dir.chdir dir do
author = UI.ask("Who is authoring the theme?", default: "Discourse").to_s.strip
description = UI.ask("How would you describe this theme?").to_s.strip

about_template = ABOUT_JSON.dup
about_template[:name] = name
if is_component
about_template[:component] = true
else
about_template[:color_schemes] = {}
about = JSON.parse(File.read("about.json"))
about["name"] = name
about["authors"] = author
if !is_component
about.delete("component")
about["color_schemes"] = {}
end
File.write("about.json", JSON.pretty_generate(about))

if author != "Discourse"
license = File.read("LICENSE")
license.sub!(/^(Copyright\s\(c\))\s(.+)/, "\\1 #{author}")
File.write("LICENSE", license)
end

readme = File.read("README.md")
readme.sub!("**Theme Name**", name)
File.write("README.md", readme)

encoded_name = name.downcase.gsub(/[^a-zA-Z0-9_-]+/, "_")
encoded_name = name.downcase.gsub(/[^a-zA-Z0-9_-]+/, "-")
FileUtils.mv(
"javascripts/discourse/api-initializers/todo.js",
"javascripts/discourse/api-initializers/#{encoded_name}.js",
)

write("about.json", JSON.pretty_generate(about_template))
write("HELP", HELP)
write("LICENSE", LICENSE.sub("#YEAR", "#{Date.today.year}").sub("#AUTHOR", author))
write(".eslintrc", ESLINT_RC)
write(".gitignore", GIT_IGNORE)
write(".template-lintrc.js", TEMPLATE_LINT_RC)
write("package.json", PACKAGE_JSON.sub("#AUTHOR", author))
write("settings.yml", SETTINGS_YML)
write("common/common.scss", "")
write("javascripts/discourse/api-initializers/#{encoded_name}.js", API_INITIALIZER)
write("locales/en.yml", EN_YML.sub("#DESCRIPTION", description))
i18n = YAML.safe_load(File.read("locales/en.yml"))
i18n["en"]["theme_metadata"]["description"] = description
File.write("locales/en.yml", YAML.safe_dump(i18n).sub(/\A---\n/, ""))

UI.info "Initializing git repo"
puts `git init && git symbolic-ref HEAD refs/heads/main`
FileUtils.rm_rf(".git")
FileUtils.rm_rf("**/.gitkeep")
system "git", "init", exception: true
system "git", "symbolic-ref", "HEAD", "refs/heads/main", exception: true
root_files = Dir.glob("*").select { |f| File.file?(f) }
system "git", "add", *root_files, exception: true
system "git", "add", ".*", exception: true
system "git", "add", "locales", exception: true
system "git",
"commit",
"-m",
"Initial commit by `discourse_theme` CLI",
"--quiet",
exception: true

UI.info "Installing dependencies"
puts `yarn`
system "yarn", exception: true
end

puts "✅ Done!"
puts "See https://meta.discourse.org/t/how-to-develop-custom-themes/60848 for more information!"
end

private

def self.write(filename, contents)
UI.info "Creating #{filename}"
FileUtils.mkdir_p(File.dirname(filename))
File.write(filename, contents)
def self.get_theme_skeleton(dir)
if online?
puts "Downloading discourse-theme-skeleton"
tmp = Dir.mktmpdir
system "git",
"clone",
"https://github.com/discourse/discourse-theme-skeleton",
tmp,
"--depth",
"1",
"--quiet",
exception: true
FileUtils.rm_rf(skeleton_dir)
# Store the local copy for offline use
FileUtils.cp_r(tmp, skeleton_dir)

FileUtils.cp_r(skeleton_dir, dir)
elsif Dir.exist?(skeleton_dir)
puts "⚠️ No internet connection detected, using the local copy of discourse-theme-skeleton"
FileUtils.cp_r(skeleton_dir, dir)
else
raise "🛑 Couldn't download discourse-theme-skeleton"
end
end

def self.online?
!!Resolv::DNS.new.getaddress("github.com")
rescue Resolv::ResolvError => e
false
end

def self.skeleton_dir
File.expand_path("~/.discourse_theme_skeleton")
end
end
end
Empty file.
21 changes: 21 additions & 0 deletions test/fixtures/skeleton-lite/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Civilized Discourse Construction Kit, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions test/fixtures/skeleton-lite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# **Theme Name**

**Theme Summary**

For more information, please see: **url to meta topic**
13 changes: 13 additions & 0 deletions test/fixtures/skeleton-lite/about.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "TODO",
"component": true,
"authors": "Discourse",
"about_url": "TODO: Put your theme's public repo or Meta topic URL here",
"license_url": "TODO: Put your theme's LICENSE URL here",
"learn_more": "TODO",
"theme_version": "0.0.1",
"minimum_discourse_version": null,
"maximum_discourse_version": null,
"assets": {},
"modifiers": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// import { apiInitializer } from "discourse/lib/api";

// export default apiInitializer("1.8.0", (api) => {
// console.log("hello world from api initializer!");
// });
3 changes: 3 additions & 0 deletions test/fixtures/skeleton-lite/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
en:
theme_metadata:
description: "TODO"
Loading

0 comments on commit 7db761f

Please sign in to comment.