Class: DiscourseTheme::Scaffold

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

Class Method Summary collapse

Class Method Details

.generate(dir, name:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/discourse_theme/scaffold.rb', line 10

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

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

  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 = 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_-]+/, "-")
    FileUtils.mv(
      "javascripts/discourse/api-initializers/todo.js",
      "javascripts/discourse/api-initializers/#{encoded_name}.js",
    )

    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"
    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"
    system "yarn", exception: true
  end

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