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

.get_theme_skeleton(dir) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/discourse_theme/scaffold.rb', line 77

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

.online?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
# File 'lib/discourse_theme/scaffold.rb', line 102

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

.skeleton_dirObject



108
109
110
# File 'lib/discourse_theme/scaffold.rb', line 108

def self.skeleton_dir
  File.expand_path("~/.discourse_theme_skeleton")
end