Class: Pupu::CLI
- Inherits:
-
Object
- Object
- Pupu::CLI
- Defined in:
- lib/pupu/cli.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #check ⇒ Object
- #check_setup ⇒ Object
- #config_path ⇒ Object
- #detect ⇒ Object
-
#initialize(args, options = Hash.new) ⇒ CLI
constructor
A new instance of CLI.
- #install ⇒ Object
- #list ⇒ Object
- #load_config ⇒ Object
- #parse_argv ⇒ Object
-
#search(pattern) ⇒ Object
search pattern or list all the available pupus if pattern is nil.
- #uninstall ⇒ Object (also: #remove)
- #update ⇒ Object
Constructor Details
#initialize(args, options = Hash.new) ⇒ CLI
Returns a new instance of CLI.
44 45 46 47 48 49 50 51 52 |
# File 'lib/pupu/cli.rb', line 44 def initialize(args, = Hash.new) @args, @options = args, self.detect self.load_config self.parse_argv self.check_setup note "Using media directory: #{::Pupu.media_root}" note "Using strategy: #{::Pupu.strategy}" end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
43 44 45 |
# File 'lib/pupu/cli.rb', line 43 def args @args end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
43 44 45 |
# File 'lib/pupu/cli.rb', line 43 def @options end |
Instance Method Details
#check ⇒ Object
143 144 145 146 |
# File 'lib/pupu/cli.rb', line 143 def check abort "Config file doesn't exist" unless self.config_path abort "Config file #{self.config_path} can't be loaded" unless self.load_config end |
#check_setup ⇒ Object
83 84 85 |
# File 'lib/pupu/cli.rb', line 83 def check_setup abort "You have to provide media directory (use --media-root=path)" unless ::Pupu.media_root end |
#config_path ⇒ Object
133 134 135 136 137 |
# File 'lib/pupu/cli.rb', line 133 def config_path ["config/pupu.rb", "settings/pupu.rb", "pupu.rb"].find do |file| File.file?(file) end end |
#detect ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/pupu/cli.rb', line 74 def detect pupu_dir = Dir["media/pupu", "public/pupu", "content/assets/pupu"].first path = pupu_dir ? File.(File.dirname(pupu_dir)) : nil path ||= ["media", "public"].find { |directory| File.directory?(directory) } return if path.nil? ::Pupu.media_root = File.(path) ::Pupu.strategy ||= :copy end |
#install ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/pupu/cli.rb', line 87 def install self.args.each do |pupu| begin GitHub.install(pupu, ) rescue PluginIsAlreadyInstalled => e puts e.backtrace.join("\n- ") info "Plugin #{pupu} is already installed, skipping" end end end |
#list ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/pupu/cli.rb', line 122 def list entries = Dir["#{Pupu.root}/*"].select { |entry| File.directory?(entry) } if File.exist?(Pupu.root_path) and not entries.empty? puts entries.map { |item| "- #{File.basename(item)}" } else error "Any pupu isn't installed yet." end rescue Errno::ENOENT error "Any pupu isn't installed yet." end |
#load_config ⇒ Object
139 140 141 |
# File 'lib/pupu/cli.rb', line 139 def load_config self.config_path && load(self.config_path) end |
#parse_argv ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pupu/cli.rb', line 54 def parse_argv self.args.each do |argument| if argument.match(/--media-root=(.+)/) self.args.delete(argument) Pupu.root = Dir.pwd # TODO: ? ::Pupu.media_root = File.($1) unless File.directory?(::Pupu.media_root) abort "#{Pupu.media_root} doesn't exist" end elsif argument.match(/--strategy=(.+)/) self.args.delete(argument) if %[copy submodules].include?($1) ::Pupu.strategy = $1.to_sym else abort "Available strategies: copy, submodules" end end end end |
#search(pattern) ⇒ Object
search pattern or list all the available pupus if pattern is nil
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/pupu/cli.rb', line 148 def search(pattern) # search pattern or list all the available pupus if pattern is nil # search on github require "yaml" require "open-uri" open("https://github.com/api/v1/yaml/search/pupu") do |stream| repositories = YAML::load(stream.read)["repositories"] repositories.each do |repository| repository = OpenStruct.new(repository) if repository.name.match(/^pupu-/) if pattern.nil? || repository.name.match(pattern) # this is the convention, everything must start with pupu- # name, size, followers, username, language, fork, id, type, pushed, forks, description, score, created puts "[#{repository.username}/#{repository.name}] #{repository.description}#{" (fork)" if repository.fork}" end end end end end |
#uninstall ⇒ Object Also known as: remove
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/pupu/cli.rb', line 98 def uninstall self.args.each do |pupu| begin Pupu[pupu].uninstall info "Uninstalling #{pupu}" rescue warning "#{pupu} isn't installed" end end end |
#update ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/pupu/cli.rb', line 109 def update args = self.args args = Pupu.all if args.empty? # update all if no pupu specified args.each do |pupu| begin GitHub.update(pupu) rescue PluginNotFoundError error "Plugin not found: #{pupu}" next end end end |