Skip to content

Commit

Permalink
Implement basic behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
hidakatsuya committed Aug 1, 2024
1 parent 99833ef commit 3676d9e
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 44 deletions.
21 changes: 11 additions & 10 deletions lib/redminep.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require_relative "redminep/version"
require_relative "redminep/definition"
require_relative "redminep/plugin"
require_relative "redminep/theme"
require_relative "redminep/commands/install"
require_relative "redminep/source/base"
require_relative "redminep/source/git"
require_relative "redminep/cli"

module Redminep
class Error < StandardError; end
Config = Data.define(:definition_file)

def self.config
@config ||= Config.new(definition_file: ".redminep.rb")
end
end

require "zeitwerk"

loader = Zeitwerk::Loader.for_gem
loader.setup
loader.eager_load
8 changes: 6 additions & 2 deletions lib/redminep/cli.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
require "thor"
require_relative "commands/install"

module Redminep
class Cli < Thor
desc "install", "install plugin"
desc "install", "Install plugin and theme"
def install
Commands::Install.new.call
end

desc "uninstall", "Uninstall plugin and theme"
def uninstall
Commands::Uninstall.new.call
end
end
end

Expand Down
36 changes: 11 additions & 25 deletions lib/redminep/commands/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,24 @@ module Redminep
module Commands
class Install
def call
definition = load_definition
definition = Definition.load_from_file

definition.plugins.each do |plugin_or_theme|
installer =
case plugin_or_theme
when Definition::Plugin then Plugin::Installer.new(plugin_or_theme)
when Definition::Theme then Theme::Installer.new(plugin_or_theme)
end
installer.install
end

uninstall_plugins_and_themes_out_of_defintion
insall_themes(definition.themes)
install_plugins(definition.plugins)
end

private

attr_reader :file, :definition

def load_definition
Definition.build(".redminep.rb")
def install_plugins(plugins)
plugins.each do
Plugin::Installer.new(_1).install
end
end

def uninstall_plugins_and_themes_out_of_defintion
# Dir.glob("plugins/*").each do |plugin|
# next if definition.plugins.any? { |p| p.name == plugin }
# FileUtils.rm_rf(plugin)
# end

# Dir.glob("themes/*").each do |theme|
# next if definition.themes.any? { |t| t.name == theme }
# FileUtils.rm_rf(theme)
# end
def insall_themes(themes)
themes.each do
Theme::Installer.new(_1).install
end
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions lib/redminep/commands/uninstall.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Redminep
module Commands
class Uninstall
def call
definition = Definition.load_from_file

uninsall_themes(definition.themes)
uninstall_plugins(definition.plugins)
end

private

def uninstall_plugins(plugins)
plugins.each do
Plugin::Uninstaller.new(_1).uninstall
end
end

def uninsall_themes(themes)
themes.each do
Theme::Uninstaller.new(_1).uninstall
end
end
end
end
end
7 changes: 3 additions & 4 deletions lib/redminep/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ def theme(name, **opts, &hooks)

include Dsl

def self.build(definition_file)
new.tap do |definition|
definition.instance_eval(File.read(definition_file))
end
def self.load_from_file
dsl = File.read(Redminep.config.definition_file)
new.tap { _1.instance_eval(dsl) }
end

attr_reader :plugins, :themes
Expand Down
6 changes: 5 additions & 1 deletion lib/redminep/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module Redminep
module Plugin
def self.dir
Pathname.pwd.join("plugins")
end

class Base
def initialize(definition)
@name = definition.name
Expand All @@ -12,7 +16,7 @@ def initialize(definition)
attr_reader :name, :source, :hooks

def plugin_dir
@plugin_dir ||= Pathname.pwd.join("plugins", name.to_s)
@plugin_dir ||= Plugin.dir.join(name.to_s)
end

def plugin_exists?
Expand Down
53 changes: 51 additions & 2 deletions lib/redminep/theme.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,58 @@
module Redminep
module Theme
class Installer
def self.dir
Pathname.pwd.join("themes")
end

class Uninstaller
class Base
def initialize(definition)
@name = definition.name
@source = definition.source
@hooks = definition.hooks || {}
end

private

attr_reader :name, :source, :hooks

def theme_dir
@theme_dir ||= Theme.dir.join(name.to_s)
end

def theme_exists?
theme_dir.exist? && !theme_dir.empty?
end
end

class Installer < Base
def install
return if theme_exists?

load_from_source
hooks[:installed]&.call
end

private

def load_from_source
src = Source.const_get(source.type.capitalize).new(**source.options)
src.load(theme_dir.to_s)
end
end

class Uninstaller < Base
def uninstall
return unless theme_exists?

remove_theme
hooks[:uninstalled]&.call
end

private

def remove_theme
theme_dir.rmtree
end
end
end
end
1 change: 1 addition & 0 deletions redminep.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Gem::Specification.new do |spec|

spec.add_dependency "thor", ">= 1.3.1"
spec.add_dependency "git", ">= 2.1.1"
spec.add_dependency "zeitwerk", "~> 2.6"
end

0 comments on commit 3676d9e

Please sign in to comment.