Class: DiscourseTheme::Scaffold

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse_theme/scaffold.rb

Constant Summary collapse

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 =
"Are you a bit lost? Be sure to read https://meta.discourse.org/t/how-to-develop-custom-themes/60848\n"
LICENSE =
"Copyright #YEAR #AUTHOR\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
GIT_IGNORE =
".discourse-site\nnode_modules\nHELP\n"
API_INITIALIZER =
"import { apiInitializer } from \"discourse/lib/api\";\n\nexport default apiInitializer(\"1.8.0\", (api) => {\n  console.log(\"hello world from api initializer!\");\n});\n"
PACKAGE_JSON =
"{\n  \"author\": \"#AUTHOR\",\n  \"license\": \"MIT\",\n  \"devDependencies\": {\n    \"eslint-config-discourse\": \"latest\"\n  }\n}\n"
ESLINT_RC =
"{\n  \"extends\": \"eslint-config-discourse\",\n  \"globals\": {\n    \"settings\": \"readonly\",\n    \"themePrefix\": \"readonly\"\n  }\n}\n"
TEMPLATE_LINT_RC =
"module.exports = {\n  plugins: [\"ember-template-lint-plugin-discourse\"],\n  extends: \"discourse:recommended\",\n};\n"
EN_YML =
"en:\n  theme_metadata:\n    description: \"#DESCRIPTION\"\n"
SETTINGS_YML =
"todo_rename_and_use_setting:\n  default: \"\"\n"

Class Method Summary collapse

Class Method Details

.generate(dir) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/discourse_theme/scaffold.rb', line 93

def self.generate(dir)
  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

  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

    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] = {}
    end

    encoded_name = name.downcase.gsub(/[^a-zA-Z0-9_-]+/, "_")

    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))

    UI.info "Initializing git repo"
    puts `git init && git symbolic-ref HEAD refs/heads/main`

    UI.info "Installing dependencies"
    puts `yarn`
  end
end