Class: DiscourseTheme::Cli

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

Constant Summary collapse

SETTINGS_FILE =
File.expand_path("~/.discourse_theme")
@@prompt =
::TTY::Prompt.new(help_color: :cyan)
@@pastel =
Pastel.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ask(message, default: nil) ⇒ Object



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

def self.ask(message, default: nil)
  @@prompt.ask(@@pastel.cyan("? ") + message, default: default)
end

.error(message) ⇒ Object



27
28
29
# File 'lib/discourse_theme/cli.rb', line 27

def self.error(message)
  puts @@pastel.red("#{message}")
end

.info(message) ⇒ Object



19
20
21
# File 'lib/discourse_theme/cli.rb', line 19

def self.info(message)
  puts @@pastel.blue("i ") + message
end

.progress(message) ⇒ Object



23
24
25
# File 'lib/discourse_theme/cli.rb', line 23

def self.progress(message)
  puts @@pastel.yellow("» ") + message
end

.select(message, options) ⇒ Object



15
16
17
# File 'lib/discourse_theme/cli.rb', line 15

def self.select(message, options)
  @@prompt.select(@@pastel.cyan("? ") + message, options)
end

.success(message) ⇒ Object



31
32
33
# File 'lib/discourse_theme/cli.rb', line 31

def self.success(message)
  puts @@pastel.green("#{message}")
end

.yes?(message) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.yes?(message)
  @@prompt.yes?(@@pastel.cyan("? ") + message)
end

Instance Method Details

#extract_theme_id(rendered_name) ⇒ Object



166
167
168
# File 'lib/discourse_theme/cli.rb', line 166

def extract_theme_id(rendered_name)
  /\(id:([0-9]+)\)$/.match(rendered_name)[1].to_i
end

#render_theme_list(themes) ⇒ Object



161
162
163
164
# File 'lib/discourse_theme/cli.rb', line 161

def render_theme_list(themes)
  themes.sort_by { |t| t["updated_at"] }
    .reverse.map { |theme| "#{theme["name"]} (id:#{theme["id"]})" }
end

#run(args) ⇒ Object



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
# File 'lib/discourse_theme/cli.rb', line 48

def run(args)
  usage unless args[1]

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

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

  config = DiscourseTheme::Config.new(SETTINGS_FILE)
  settings = config[dir]

  theme_id = settings.theme_id
  components = settings.components

  if command == "new"
    raise DiscourseTheme::ThemeError.new "'#{dir} is not empty" if Dir.exists?(dir) && !Dir.empty?(dir)
    DiscourseTheme::Scaffold.generate(dir)
    if Cli.yes?("Would you like to start 'watching' this theme?")
      args[0] = "watch"
      Cli.progress "Running discourse_theme #{args.join(' ')}"
      run(args)
    end
  elsif command == "watch"
    raise DiscourseTheme::ThemeError.new "'#{dir} does not exist" unless Dir.exists?(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 = Cli.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 = Cli.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 = JSON.parse(File.read(File.join(dir, 'about.json'))) rescue nil
    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 = Cli.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)

    Cli.progress "Uploading theme from #{dir}"
    settings.theme_id = theme_id = uploader.upload_full_theme

    Cli.success "Theme uploaded (id:#{theme_id})"
    Cli.info "Preview: #{client.root}/?preview_theme_id=#{theme_id}"
    if client.is_theme_creator
      Cli.info "Manage: #{client.root}/my/themes"
    else
      Cli.info "Manage: #{client.root}/admin/customize/themes/#{theme_id}"
    end
    watcher = DiscourseTheme::Watcher.new(dir: dir, uploader: uploader)

    Cli.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.exists?(dir)
    raise DiscourseTheme::ThemeError.new "'#{dir} is not empty" unless Dir.empty?(dir)

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

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

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

    downloader.download_theme(theme_id)
    settings.theme_id = theme_id

    Cli.success "Theme downloaded"

    if Cli.yes?("Would you like to start 'watching' this theme?")
      args[0] = "watch"
      Cli.progress "Running discourse_theme #{args.join(' ')}"
      run(args)
    end
  else
    usage
  end

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

#usageObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/discourse_theme/cli.rb', line 37

def usage
  puts "Usage: discourse_theme COMMAND [--reset]"
  puts
  puts "discourse_theme new DIR : Creates a new theme in the designated directory"
  puts "discourse_theme download DIR : Download a theme from the server, and store in the designated directory"
  puts "discourse_theme watch DIR : Watches the theme directory and synchronizes with Discourse"
  puts
  puts "Use --reset to change the configuration for a directory"
  exit 1
end