Class: WPPlugin

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

Constant Summary collapse

VERSION =
'0.2'

Instance Method Summary collapse

Constructor Details

#initializeWPPlugin

Returns a new instance of WPPlugin.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wpplugin.rb', line 27

def initialize
	command = ARGV.shift
	command = command.to_sym unless command.nil?
	plugin = ARGV.shift
	plugin = plugin.dup.to_str unless plugin.nil?
	if !plugin.nil?
		plugin.downcase
		plugin.gsub! ".", ""
		plugin.gsub! "/", ""
		plugin.gsub! " ", ""
	end
	plugin = nil if !plugin.nil? and plugin.empty?
	case command
		when :"--v", :"--version"
			info
		when nil, :"--help", :"-h"
			exit_message "Usage: wpplugin update\n       wpplugin [update|add|remove] {plugin-slug}"
		when :add, :remove, :install, :delete, :uninstall
			exit_error_message "You must provide a plugin slug" if plugin.nil?
			send command, plugin
		when :update, :upgrade
			if plugin.nil?
				update_all
			else
				update plugin
			end
		else
			exit_error_message "Invalid command"
	end
end

Instance Method Details

#add(plugin) ⇒ Object Also known as: install



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wpplugin.rb', line 70

def add plugin
	# If the directory exists, do an update instead
	if File.directory? plugin
		update plugin
		return
	end
	uri = "http://downloads.wordpress.org/plugin/#{plugin}.latest-stable.zip"
	`wget -O #{plugin}.zip #{uri} > /dev/null 2>&1`
	`unzip #{plugin}.zip > /dev/null 2>&1`
	`rm #{plugin}.zip > /dev/null 2>&1`
	`svn add --force #{plugin} > /dev/null 2>&1`
	`git add --all #{plugin} > /dev/null 2>&1`
end

#debug(message) ⇒ Object



103
104
105
# File 'lib/wpplugin.rb', line 103

def debug message
	puts message if @debug
end

#exit_error_message(message) ⇒ Object



112
113
114
# File 'lib/wpplugin.rb', line 112

def exit_error_message message
	exit_message '[ERROR] ' + message
end

#exit_message(message) ⇒ Object



107
108
109
110
# File 'lib/wpplugin.rb', line 107

def exit_message message
	puts message
	exit
end

#infoObject



99
100
101
# File 'lib/wpplugin.rb', line 99

def info
	puts "WPPlugin #{self.class::VERSION}"
end

#remove(plugin) ⇒ Object Also known as: delete, uninstall



90
91
92
93
94
# File 'lib/wpplugin.rb', line 90

def remove plugin
	remove_files plugin
	`svn rm #{plugin} > /dev/null 2>&1`
	`git rm -r #{plugin} > /dev/null 2>&1`
end

#remove_files(plugin) ⇒ Object



86
87
88
# File 'lib/wpplugin.rb', line 86

def remove_files plugin
	FileUtils.rm_rf plugin if File.directory? plugin
end

#update(plugin) ⇒ Object



58
59
60
61
62
# File 'lib/wpplugin.rb', line 58

def update plugin
	remove_files plugin
	add plugin
	`svn status #{plugin} 2>/dev/null | grep '\!' | awk '{print $2;}' | xargs svn rm`
end

#update_allObject



64
65
66
67
68
# File 'lib/wpplugin.rb', line 64

def update_all
	plugins = Pathname.glob("*/readme.txt").map { |i| i.dirname.to_s }.each do |plugin|
		update plugin
	end
end