Class: DiscourseTheme::Cli

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

Constant Summary collapse

@@cli_settings_filename =
File.expand_path("~/.discourse_theme")

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.settings_fileObject



8
9
10
# File 'lib/discourse_theme/cli.rb', line 8

def self.settings_file
  @@cli_settings_filename
end

.settings_file=(filename) ⇒ Object



12
13
14
# File 'lib/discourse_theme/cli.rb', line 12

def self.settings_file=(filename)
  @@cli_settings_filename = filename
end

Instance Method Details

#run(args) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/discourse_theme/cli.rb', line 39

def run(args)
  if args.delete("--version")
    puts VERSION
    return
  end

  usage unless args[1]

  reset = !!args.delete("--reset")

  command = args[0].to_s.downcase
  dir = File.expand_path(args[1])

  config = DiscourseTheme::Config.new(self.class.settings_file)
  settings = config[dir]

  theme_id = settings.theme_id
  components = settings.components

  if command == "new"
    if Dir.exist?(dir) && !Dir.empty?(dir)
      raise DiscourseTheme::ThemeError.new "'#{dir}' is not empty"
    end
    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, name: args[1])
    watch_theme?(args)
  elsif command == "watch"
    raise DiscourseTheme::ThemeError.new "'#{dir} does not exist" unless Dir.exist?(dir)
    client = DiscourseTheme::Client.new(dir, settings, reset: reset)

    theme_list = client.get_themes_list

    options = {}

    if theme_id && theme = theme_list.find { |t| t["id"] == theme_id }
      options["Sync with existing theme: '#{theme["name"]}' (id:#{theme_id})"] = :default
    end

    options["Create and sync with a new theme"] = :create
    options["Select a different theme"] = :select

    choice = UI.select("How would you like to sync this theme?", options.keys)

    if options[choice] == :create
      theme_id = nil
    elsif options[choice] == :select
      themes = render_theme_list(theme_list)
      choice = UI.select("Which theme would you like to sync with?", themes)
      theme_id = extract_theme_id(choice)
      theme = theme_list.find { |t| t["id"] == theme_id }
    end

    about_json =
      begin
        JSON.parse(File.read(File.join(dir, "about.json")))
      rescue StandardError
        nil
      end

    already_uploaded = !!theme
    is_component = theme&.[]("component")
    component_count = about_json&.[]("components")&.length || 0

    if !already_uploaded && !is_component && component_count > 0
      options = {}
      options["Yes"] = :sync
      options["No"] = :none
      options = options.sort_by { |_, b| b == components.to_sym ? 0 : 1 }.to_h if components
      choice = UI.select("Would you like to update child theme components?", options.keys)
      settings.components = components = options[choice].to_s
    end

    uploader =
      DiscourseTheme::Uploader.new(
        dir: dir,
        client: client,
        theme_id: theme_id,
        components: components,
      )

    UI.progress "Uploading theme from #{dir}"

    settings.theme_id =
      theme_id = uploader.upload_full_theme(skip_migrations: skip_migrations(theme, dir))

    UI.success "Theme uploaded (id:#{theme_id})"
    UI.info "Preview: #{client.url}/?preview_theme_id=#{theme_id}"

    if client.is_theme_creator
      UI.info "Manage: #{client.url}/my/themes"
    else
      UI.info "Manage: #{client.url}/admin/customize/themes/#{theme_id}"
    end

    UI.info "Tests: #{client.url}/theme-qunit?id=#{theme_id}"

    watcher = DiscourseTheme::Watcher.new(dir: dir, uploader: uploader)
    UI.progress "Watching for changes in #{dir}..."
    watcher.watch
  elsif command == "download"
    client = DiscourseTheme::Client.new(dir, settings, reset: reset)
    downloader = DiscourseTheme::Downloader.new(dir: dir, client: client)

    FileUtils.mkdir_p dir unless Dir.exist?(dir)
    raise DiscourseTheme::ThemeError.new "'#{dir} is not empty" unless Dir.empty?(dir)

    UI.progress "Loading theme list..."
    themes = render_theme_list(client.get_themes_list)

    choice = UI.select("Which theme would you like to download?", themes)
    theme_id = extract_theme_id(choice)

    UI.progress "Downloading theme into #{dir}"

    downloader.download_theme(theme_id)
    settings.theme_id = theme_id

    UI.success "Theme downloaded"

    watch_theme?(args)
  elsif command == "upload"
    raise DiscourseTheme::ThemeError.new "'#{dir} does not exist" unless Dir.exist?(dir)
    if theme_id == 0
      raise DiscourseTheme::ThemeError.new "No theme_id is set, please sync via the 'watch' command initially"
    end
    client = DiscourseTheme::Client.new(dir, settings, reset: reset)

    theme_list = client.get_themes_list

    theme = theme_list.find { |t| t["id"] == theme_id }
    unless theme
      raise DiscourseTheme::ThemeError.new "theme_id is set, but the theme does not exist in Discourse"
    end

    uploader =
      DiscourseTheme::Uploader.new(
        dir: dir,
        client: client,
        theme_id: theme_id,
        components: components,
      )

    UI.progress "Uploading theme (id:#{theme_id}) from #{dir} "
    settings.theme_id = theme_id = uploader.upload_full_theme

    UI.success "Theme uploaded (id:#{theme_id})"
    UI.info "Preview: #{client.root}/?preview_theme_id=#{theme_id}"
    if client.is_theme_creator
      UI.info "Manage: #{client.root}/my/themes"
    else
      UI.info "Manage: #{client.root}/admin/customize/themes/#{theme_id}"
    end
  elsif command == "rspec"
    DiscourseTheme::CliCommands::Rspec.run(
      settings: config[dir.split("/spec")[0]],
      dir: dir,
      args: args,
      reset: reset,
    )
  else
    usage
  end

  UI.progress "Exiting..."
rescue DiscourseTheme::ThemeError => e
  UI.error "#{e.message}"
rescue Interrupt, TTY::Reader::InputInterrupt => e
  UI.error "Interrupted"
end

#usageObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/discourse_theme/cli.rb', line 16

def usage
  puts <<~USAGE
  Usage: discourse_theme COMMAND [DIR] [OPTIONS]

  Commands:
    new DIR               - Creates a new theme in the specified directory.
    download DIR          - Downloads a theme from the server and stores it in the specified directory.
    upload DIR            - Uploads the theme from the specified directory to Discourse.
    watch DIR             - Watches the theme in the specified directory and synchronizes any changes with Discourse.
    rspec DIR [OPTIONS]   - Runs the RSpec tests in the specified directory. The tests can be run using a local Discourse repository or a Docker container.
      --headful           - Runs the RSpec system type tests in headful mode. Applies to both modes.

      If specified directory has been configured to run in a Docker container, the additional options are supported.
      --rebuild           - Forces a rebuilds of Docker container.
      --verbose           - Runs the command to prepare the Docker container in verbose mode.

  Global Options:
    --reset               - Resets the configuration for the specified directory.
  USAGE

  exit 1
end