| 1 |
|
| 2 |
|
| 3 |
作成するためのDSLと、実行環境のセットアップ |
| 4 |
module Prelude |
| 5 |
class NameError < RuntimeError; end |
| 6 |
|
| 7 |
module DSL |
| 8 |
def namespace(name, &) |
| 9 |
@namespaces ||= {} |
| 10 |
if toplevel? && Prelude.plugin_context[:slug].to_s != name.to_s |
| 11 |
raise NameError, '最上位のnamespaceはプラグインのslugと同じ名前にする必要があります' |
| 12 |
end |
| 13 |
(@namespaces[name] ||= Namespace.new(self, name)).instance_eval(&) |
| 14 |
end |
| 15 |
|
| 16 |
def command(name, description: nil, &block) |
| 17 |
@commands_dict ||= {} |
| 18 |
if !name || name.empty? |
| 19 |
raise NameError, 'command name cannot be empty' |
| 20 |
end |
| 21 |
if toplevel? && Prelude.plugin_context[:slug].to_s != name.to_s |
| 22 |
raise NameError, 'namespaceに含まれないcommandはプラグインのslugと同じ名前にする必要があります' |
| 23 |
end |
| 24 |
if @commands_dict[name] |
| 25 |
warn "already defined command `#{[full_name, name].compact.join(':')}`" |
| 26 |
end |
| 27 |
@commands_dict[name] = Command.new(self, name, action: block, description:) |
| 28 |
end |
| 29 |
|
| 30 |
def commands |
| 31 |
@commands ||= commands_nocached |
| 32 |
end |
| 33 |
|
| 34 |
def commands_nocached |
| 35 |
[@namespaces&.values&.map(&:commands), @commands_dict&.values].compact.flatten.freeze |
| 36 |
end |
| 37 |
end |
| 38 |
|
| 39 |
class Namespace |
| 40 |
include DSL |
| 41 |
|
| 42 |
attr_reader :parent, :name |
| 43 |
|
| 44 |
def initialize(parent, name) |
| 45 |
@parent = parent |
| 46 |
@name = name |
| 47 |
end |
| 48 |
|
| 49 |
def full_name |
| 50 |
[parent.full_name, name].compact.join(':') |
| 51 |
end |
| 52 |
|
| 53 |
private |
| 54 |
|
| 55 |
def toplevel? |
| 56 |
false |
| 57 |
end |
| 58 |
end |
| 59 |
|
| 60 |
class Command |
| 61 |
attr_reader :parent, :name, :description |
| 62 |
|
| 63 |
def initialize(parent, name, action:, description: nil) |
| 64 |
@parent = parent |
| 65 |
@name = name |
| 66 |
@action = action |
| 67 |
@description = description |
| 68 |
@plugin = Prelude.plugin_context |
| 69 |
end |
| 70 |
|
| 71 |
def full_name |
| 72 |
[parent.full_name, name].compact.join(':') |
| 73 |
end |
| 74 |
|
| 75 |
def execute |
| 76 |
ExecutionScope.new(full_name, @plugin).instance_eval(&@action) |
| 77 |
end |
| 78 |
end |
| 79 |
|
| 80 |
class ExecutionScope |
| 81 |
attr_reader :name, :spec |
| 82 |
|
| 83 |
def initialize(name, spec) |
| 84 |
@name = name |
| 85 |
@spec = spec |
| 86 |
end |
| 87 |
|
| 88 |
def load_plugin(*slugs) |
| 89 |
if slugs.empty? |
| 90 |
Miquire::Plugin.load(spec) |
| 91 |
else |
| 92 |
slugs.each(&Miquire::Plugin.method(:load)) |
| 93 |
end |
| 94 |
end |
| 95 |
end |
| 96 |
|
| 97 |
class << self |
| 98 |
include DSL |
| 99 |
|
| 100 |
attr_accessor :plugin_context |
| 101 |
|
| 102 |
def full_name |
| 103 |
nil |
| 104 |
end |
| 105 |
|
| 106 |
def load_by_slug(slug) |
| 107 |
Prelude.plugin_context = spec = Miquire::Plugin.get_spec_by_slug(slug) |
| 108 |
return unless spec |
| 109 |
Array(spec[:prelude]).each do |file| |
| 110 |
require File.join(spec[:path], file) |
| 111 |
end |
| 112 |
ensure |
| 113 |
Prelude.plugin_context = nil |
| 114 |
end |
| 115 |
|
| 116 |
def load_all |
| 117 |
Miquire::Plugin.each_spec do |spec| |
| 118 |
Prelude.plugin_context = spec |
| 119 |
Array(spec[:prelude]).each do |file| |
| 120 |
require File.join(spec[:path], file) |
| 121 |
end |
| 122 |
ensure |
| 123 |
Prelude.plugin_context = nil |
| 124 |
end |
| 125 |
end |
| 126 |
|
| 127 |
def execute!(command_name) |
| 128 |
require_relative '../utils' |
| 129 |
require 'boot/check_config_permission' |
| 130 |
|
| 131 |
load_by_slug(command_name.split(':', 2).first.to_sym) |
| 132 |
cmd = Prelude.commands.find { |c| c.full_name == command_name } |
| 133 |
if cmd |
| 134 |
require 'fileutils' |
| 135 |
require 'lib/diva_hacks' |
| 136 |
require 'lib/lazy' |
| 137 |
require 'lib/reserver' |
| 138 |
require 'lib/timelimitedqueue' |
| 139 |
require 'lib/uithreadonly' |
| 140 |
require 'lib/weakstorage' |
| 141 |
require 'userconfig' |
| 142 |
cmd.execute |
| 143 |
else |
| 144 |
file = File.join(__dir__, "shell/#{command_name}.rb") |
| 145 |
if FileTest.exist?(file) |
| 146 |
require file |
| 147 |
else |
| 148 |
puts "no such command: #{command_name}" |
| 149 |
end |
| 150 |
end |
| 151 |
|
| 152 |
exit |
| 153 |
end |
| 154 |
|
| 155 |
private |
| 156 |
|
| 157 |
def toplevel? |
| 158 |
true |
| 159 |
end |
| 160 |
end |
| 161 |
end |