| 1 |
|
| 2 |
require 'miquire' |
| 3 |
require 'plugin' |
| 4 |
require 'miquire_to_spec' |
| 5 |
|
| 6 |
のロードに関すること |
| 7 |
module Miquire::Plugin |
| 8 |
class << self |
| 9 |
using Miquire::ToSpec |
| 10 |
include Enumerable |
| 11 |
|
| 12 |
スの配列を返す。 |
| 13 |
したい場合は、以下のようにすればいい |
| 14 |
|
| 15 |
|
| 16 |
def loadpath |
| 17 |
@loadpath ||= [] end |
| 18 |
|
| 19 |
ァイル名(フルパス)で繰り返す。 |
| 20 |
def each(&block) |
| 21 |
iterated = Set.new |
| 22 |
detected = [] |
| 23 |
loadpath.reverse.each { |path| |
| 24 |
Dir[File.join(File.expand_path(path), '*')].each { |file| |
| 25 |
if FileTest.directory?(file) and FileTest.exist?(File.join(file, File.basename(file))+'.rb') |
| 26 |
file = File.join(file, File.basename(file))+'.rb' |
| 27 |
elsif not file.end_with?('.rb'.freeze) |
| 28 |
next end |
| 29 |
plugin_name = File.basename(file, '.rb') |
| 30 |
if not iterated.include? plugin_name |
| 31 |
iterated << plugin_name |
| 32 |
detected << file end } } |
| 33 |
detected.sort_by{ |a| |
| 34 |
[:bundle == get_kind(a) ? 0 : 1, a] |
| 35 |
}.each(&block) end |
| 36 |
|
| 37 |
def each_spec |
| 38 |
each{ |path| |
| 39 |
spec = get_spec path |
| 40 |
yield spec if spec } end |
| 41 |
|
| 42 |
def to_hash |
| 43 |
result = {} |
| 44 |
each_spec{ |spec| |
| 45 |
result[spec[:slug].to_sym] = spec } |
| 46 |
result end |
| 47 |
|
| 48 |
にあるプラグインのスラッグを返す |
| 49 |
|
| 50 |
ing) |
| 51 |
|
| 52 |
スラッグ(Symbol) |
| 53 |
def get_slug(path) |
| 54 |
type_strict path => String |
| 55 |
spec = get_spec(path) |
| 56 |
if spec |
| 57 |
spec[:slug] |
| 58 |
else |
| 59 |
File.basename(path, ".rb").to_sym end end |
| 60 |
|
| 61 |
があればそれを返す |
| 62 |
|
| 63 |
ing) |
| 64 |
|
| 65 |
容か、存在しなければnil |
| 66 |
def get_spec(path) |
| 67 |
type_strict path => String |
| 68 |
plugin_dir = FileTest.directory?(path) ? path : File.dirname(path) |
| 69 |
spec_filename = File.join(plugin_dir, ".mikutter.yml") |
| 70 |
deprecated_spec = false |
| 71 |
unless FileTest.exist? spec_filename |
| 72 |
spec_filename = File.join(plugin_dir, "spec") |
| 73 |
deprecated_spec = true end |
| 74 |
if FileTest.exist? spec_filename |
| 75 |
YAML.load_file(spec_filename).symbolize |
| 76 |
.merge(kind: get_kind(path), |
| 77 |
path: plugin_dir, |
| 78 |
deprecated_spec: deprecated_spec) |
| 79 |
elsif FileTest.exist? path |
| 80 |
{ slug: File.basename(path, ".rb").to_sym, |
| 81 |
kind: get_kind(path), |
| 82 |
path: plugin_dir, |
| 83 |
deprecated_spec: false } end end |
| 84 |
|
| 85 |
def get_spec_by_slug(slug) |
| 86 |
type_strict slug => Symbol |
| 87 |
to_hash[slug] end |
| 88 |
|
| 89 |
yかbundleかを返す |
| 90 |
def get_kind(path) |
| 91 |
type_strict path => String |
| 92 |
if Environment::PLUGIN_PATH.any?(&path.method(:start_with?)) |
| 93 |
:bundle |
| 94 |
else |
| 95 |
:thirdparty end end |
| 96 |
|
| 97 |
def load_all |
| 98 |
each_spec do |spec| |
| 99 |
load spec |
| 100 |
rescue Miquire::LoadError => e |
| 101 |
::Plugin.call(:modify_activity, |
| 102 |
kind: "system", |
| 103 |
title: "#{spec[:slug]} load failed", |
| 104 |
date: Time.new, |
| 105 |
exception: e, |
| 106 |
description: e.to_s) |
| 107 |
end |
| 108 |
end |
| 109 |
|
| 110 |
def satisfy_mikutter_version?(spec) |
| 111 |
if defined?(spec[:depends][:mikutter]) and spec[:depends][:mikutter] |
| 112 |
version = Environment::Version.new(*(spec[:depends][:mikutter].split(".").map(&:to_i) + ([0]*4))[0...4]) |
| 113 |
if Environment::VERSION < version |
| 114 |
raise Miquire::LoadError, "plugin #{spec[:slug]}: #{Environment::NAME} version too old (#{spec[:depends][:mikutter]} required, but #{Environment::NAME} version is #{Environment::VERSION})" |
| 115 |
return false end end |
| 116 |
true |
| 117 |
end |
| 118 |
|
| 119 |
def depended_plugins(_spec, recursive: false) |
| 120 |
spec = _spec.to_spec |
| 121 |
unless spec |
| 122 |
error "spec #{_spec.inspect}" |
| 123 |
return false |
| 124 |
end |
| 125 |
if spec.dig(:depends, :plugin) |
| 126 |
if recursive |
| 127 |
Array(spec.dig(:depends, :plugin)).map { |s| Array(s).first.to_sym }.flat_map { |s| |
| 128 |
depended_plugins(s, recursive: recursive).map { |d| d[:slug].to_sym } |
| 129 |
}.uniq.map { |d| d.to_spec } |
| 130 |
else |
| 131 |
Array(spec.dig(:depends, :plugin)).map do |s| |
| 132 |
Array(s).first.to_sym&.to_spec |
| 133 |
end |
| 134 |
end |
| 135 |
else |
| 136 |
[] |
| 137 |
end |
| 138 |
end |
| 139 |
|
| 140 |
def load(_spec) |
| 141 |
return false unless _spec |
| 142 |
spec = _spec.to_spec |
| 143 |
return false unless spec |
| 144 |
return true if ::Plugin.instance_exist?(spec[:slug]) |
| 145 |
return false unless satisfy_mikutter_version?(spec) |
| 146 |
|
| 147 |
atomic do |
| 148 |
depended_plugins(spec).each do |depend| |
| 149 |
raise Miquire::LoadError, "plugin #{spec[:slug].inspect} was not loaded because dependent plugin #{depend.inspect} was not loaded." unless load(depend) |
| 150 |
rescue Miquire::LoadError => err |
| 151 |
raise Miquire::LoadError, "plugin #{spec[:slug].inspect} was not loaded because dependent plugin was not loaded. previous error is:\n#{err.to_s}" |
| 152 |
end |
| 153 |
|
| 154 |
notice "plugin loaded: " + File.join(spec[:path], "#{spec[:slug]}.rb") |
| 155 |
::Plugin.create(spec[:slug].to_sym) do |
| 156 |
self.spec = spec end |
| 157 |
Kernel.load File.join(spec[:path], "#{spec[:slug]}.rb") |
| 158 |
if spec[:deprecated_spec] |
| 159 |
title = "#{spec[:slug]}: specファイルは非推奨になりました。" |
| 160 |
Plugin.call(:modify_activity, |
| 161 |
{ plugin: spec[:slug], |
| 162 |
kind: "error", |
| 163 |
title: title, |
| 164 |
date: Time.now, |
| 165 |
spec: spec, |
| 166 |
description: "#{title}\n代わりに.mikutter.ymlを使ってください。"}) end |
| 167 |
true end |
| 168 |
end |
| 169 |
end |
| 170 |
end |