Class: Depot::AppCustomizer

Inherits:
Object
  • Object
show all
Defined in:
lib/depot/app_customizer.rb

Constant Summary collapse

ICON_EXTENSIONS =
[".png", ".svg", ".xpm"].freeze

Instance Method Summary collapse

Constructor Details

#initialize(store: ManifestStore.new) ⇒ AppCustomizer

Returns a new instance of AppCustomizer.



15
16
17
# File 'lib/depot/app_customizer.rb', line 15

def initialize(store: ManifestStore.new)
  @store = store
end

Instance Method Details

#change_icon(app_id, source_path) ⇒ Object



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
# File 'lib/depot/app_customizer.rb', line 32

def change_icon(app_id, source_path)
  manifest = @store.find(app_id)
  return Result.err("No Depot manifest found for #{app_id}.") unless manifest

  source_path = File.expand_path(source_path.to_s)
  return Result.err("Icon file does not exist.") unless File.file?(source_path)

  ext = File.extname(source_path).downcase
  return Result.err("Choose a PNG, SVG, or XPM icon.") unless ICON_EXTENSIONS.include?(ext)

  ensure_defaults(manifest)
  remove_custom_icon(manifest)

  icon_name = "#{manifest.fetch("app_id")}-custom"
  target_dir = File.join(Paths.icon_root, icon_theme_size(source_path, ext), "apps")
  FileUtils.mkdir_p(target_dir)
  target = File.join(target_dir, "#{icon_name}#{ext}")
  FileUtils.cp(source_path, target)

  manifest["custom_icon"] = {
    "source" => source_path,
    "path" => target,
    "icon_name" => icon_name,
    "set_at" => Time.now.utc.iso8601
  }
  manifest["created_files"] = (manifest["created_files"].to_a + [target]).uniq
  persist(manifest)
rescue SystemCallError => e
  Result.err("Could not change icon: #{e.message}")
end

#rename(app_id, title) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/depot/app_customizer.rb', line 19

def rename(app_id, title)
  manifest = @store.find(app_id)
  return Result.err("No Depot manifest found for #{app_id}.") unless manifest

  title = title.to_s.strip
  return Result.err("Title cannot be empty.") if title.empty?

  ensure_defaults(manifest)
  manifest["display_name"] = title
  manifest["customizations"]["display_name"] = title
  persist(manifest)
end

#reset(app_id) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/depot/app_customizer.rb', line 63

def reset(app_id)
  manifest = @store.find(app_id)
  return Result.err("No Depot manifest found for #{app_id}.") unless manifest

  ensure_defaults(manifest)
  remove_custom_icon(manifest)
  manifest["display_name"] = manifest.fetch("default_display_name")
  manifest.delete("custom_icon")
  manifest["customizations"] = {}
  persist(manifest)
end