Class: Pupu::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/pupu/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = Hash.new)
  @args, @options = args, options
  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

#argsObject (readonly)

Returns the value of attribute args.



43
44
45
# File 'lib/pupu/cli.rb', line 43

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



43
44
45
# File 'lib/pupu/cli.rb', line 43

def options
  @options
end

Instance Method Details

#checkObject



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_setupObject



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_pathObject



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

#detectObject



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.expand_path(File.dirname(pupu_dir)) : nil
  path ||= ["media", "public"].find { |directory| File.directory?(directory) }
  return if path.nil?
  ::Pupu.media_root = File.expand_path(path)
  ::Pupu.strategy ||= :copy
end

#installObject



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, options)
    rescue PluginIsAlreadyInstalled => e
      puts e.backtrace.join("\n- ")
      info "Plugin #{pupu} is already installed, skipping"
    end
  end
end

#listObject



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_configObject



139
140
141
# File 'lib/pupu/cli.rb', line 139

def load_config
  self.config_path && load(self.config_path)
end

#parse_argvObject



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.expand_path($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

#uninstallObject 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

#updateObject



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