Class: ThemesInstallTask

Inherits:
Object
  • Object
show all
Defined in:
app/services/themes_install_task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_or_options = nil) ⇒ ThemesInstallTask

Returns a new instance of ThemesInstallTask.



39
40
41
42
43
44
45
46
47
48
# File 'app/services/themes_install_task.rb', line 39

def initialize(url_or_options = nil)
  if url_or_options.is_a?(Hash)
    url_or_options.deep_symbolize_keys!
    @url = url_or_options.fetch(:url, nil)
    @options = url_or_options
  else
    @url = url_or_options
    @options = {}
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



37
38
39
# File 'app/services/themes_install_task.rb', line 37

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



37
38
39
# File 'app/services/themes_install_task.rb', line 37

def url
  @url
end

Class Method Details

.install(themes) ⇒ Object



4
5
6
7
8
9
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
# File 'app/services/themes_install_task.rb', line 4

def self.install(themes)
  counts = { installed: 0, updated: 0, errors: 0, skipped: 0 }
  log = []
  themes.each do |name, val|
    installer = new(val)
    next if installer.url.nil?

    if installer.theme_exists?
      if installer.options.fetch(:skip_update, nil)
        log << "#{name}: is already installed. Skipping update."
        counts[:skipped] += 1
      elsif installer.update
        log << "#{name}: is already installed. Updating from remote."
        counts[:updated] += 1
      else
        log << "#{name}: is already installed, but there was an error updating from remote."
        counts[:errors] += 1
      end
    else
      begin
        installer.install
        log << "#{name}: installed from #{installer.url}"
        counts[:installed] += 1
      rescue RemoteTheme::ImportError, Discourse::InvalidParameters => err
        log << "#{name}: #{err.message}"
        counts[:errors] += 1
      end
    end
  end

  [log, counts]
end

Instance Method Details

#add_component_to_all_themesObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/services/themes_install_task.rb', line 83

def add_component_to_all_themes
  return if (!@options.fetch(:add_to_all_themes, false) || !@theme.component)

  Theme
    .where(component: false)
    .each do |parent_theme|
      if ChildTheme.where(parent_theme_id: parent_theme.id, child_theme_id: @theme.id).exists?
        next
      end
      parent_theme.add_relative_theme!(:child, @theme)
    end
end

#installObject



64
65
66
67
68
69
70
71
72
73
74
# File 'app/services/themes_install_task.rb', line 64

def install
  @theme =
    RemoteTheme.import_theme(
      @url,
      Discourse.system_user,
      private_key: @options[:private_key],
      branch: @options[:branch],
    )
  @theme.set_default! if @options.fetch(:default, false)
  add_component_to_all_themes
end

#repo_nameObject



50
51
52
# File 'app/services/themes_install_task.rb', line 50

def repo_name
  @url.gsub(Regexp.union("[email protected]:", "https://github.com/", ".git"), "")
end

#theme_exists?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
# File 'app/services/themes_install_task.rb', line 54

def theme_exists?
  @remote_theme =
    RemoteTheme
      .where("remote_url like ?", "%#{repo_name}%")
      .where(branch: @options.fetch(:branch, nil))
      .first
  @theme = @remote_theme&.theme
  @theme.present?
end

#updateObject



76
77
78
79
80
81
# File 'app/services/themes_install_task.rb', line 76

def update
  @remote_theme.update_from_remote
  @theme.save
  add_component_to_all_themes
  @remote_theme.last_error_text.nil?
end