chuckya/app/lib/themes.rb

81 lines
1.9 KiB
Ruby
Raw Permalink Normal View History

2017-09-26 11:24:32 +09:00
# frozen_string_literal: true
require 'singleton'
require 'yaml'
class Themes
include Singleton
THEME_COLORS = {
dark: '#191b22',
light: '#f3f5f7',
}.freeze
2017-09-26 11:24:32 +09:00
def initialize
@flavours = {}
Rails.root.glob('app/javascript/flavours/*/theme.yml') do |pathname|
data = YAML.load_file(pathname)
next unless data['pack_directory']
2022-01-17 05:46:48 +09:00
dir = pathname.dirname
name = dir.basename.to_s
2017-12-11 13:15:09 +09:00
locales = []
screenshots = []
2022-01-17 05:46:48 +09:00
2017-12-08 12:07:47 +09:00
if data['locales']
2017-12-11 13:15:09 +09:00
Dir.glob(File.join(dir, data['locales'], '*.{js,json}')) do |locale|
locale_name = File.basename(locale, File.extname(locale))
locales.push(locale_name) unless /defaultMessages|whitelist|index/.match?(locale_name)
2017-12-08 12:07:47 +09:00
end
2017-12-11 13:15:09 +09:00
end
2022-01-17 05:46:48 +09:00
2017-12-11 13:15:09 +09:00
if data['screenshot']
if data['screenshot'].is_a? Array
screenshots = data['screenshot']
else
screenshots.push(data['screenshot'])
end
2017-12-08 12:07:47 +09:00
end
2022-01-17 05:46:48 +09:00
data['name'] = name
data['locales'] = locales
data['screenshot'] = screenshots
data['skins'] = []
@flavours[name] = data
2017-09-26 11:24:32 +09:00
end
2017-11-21 15:13:37 +09:00
Rails.root.glob('app/javascript/skins/*/*') do |pathname|
ext = pathname.extname.to_s
skin = pathname.basename.to_s
name = pathname.dirname.basename.to_s
next unless @flavours[name]
2022-01-17 05:46:48 +09:00
if pathname.directory?
@flavours[name]['skins'] << skin if pathname.glob('{common,index,application}.{css,scss}').any?
elsif /^\.s?css$/i.match?(ext)
@flavours[name]['skins'] << pathname.basename(ext).to_s
2022-01-17 05:46:48 +09:00
end
2017-12-01 12:29:47 +09:00
end
2017-11-21 15:13:37 +09:00
end
2017-12-04 16:26:40 +09:00
def flavour(name)
@flavours[name]
2017-11-17 14:35:25 +09:00
end
2017-12-04 16:26:40 +09:00
def flavours
@flavours.keys
2017-09-26 11:24:32 +09:00
end
2017-12-01 12:29:47 +09:00
def skins_for(name)
skins = @flavours[name]['skins']
skins.include?('default') && skins.include?('mastodon-light') ? ['system'] + skins : skins
2017-12-01 12:29:47 +09:00
end
def flavours_and_skins
flavours.map do |flavour|
[flavour, skins_for(flavour).map { |skin| [flavour, skin] }]
end
end
2017-09-26 11:24:32 +09:00
end