Skip to content

Instantly share code, notes, and snippets.

@Sixeight
Created April 21, 2010 12:18
Show Gist options
  • Select an option

  • Save Sixeight/373757 to your computer and use it in GitHub Desktop.

Select an option

Save Sixeight/373757 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'uri'
$settings = {
:wordpress_dir => '/var/www/wordpress'
}
def show_list
`ls #{$settings[:wordpress_dir]}/wp-content/themes/ | sort`.split(/\s/).
delete_if {|i| i == 'index.php' }.
each_with_index do |theme, idx|
puts "#{idx}: #{theme}"
end
end
def install_theme(uri)
target = URI.parse(uri)
filename = File.basename(target.path)
unless filename =~ /\.zip$/
warn 'Theme must be compressed by zip'
exit -1
end
Dir.chdir "#{$settings[:wordpress_dir]}/wp-content/uploads/"
unless File.exist?(filename)
puts "...downloading: #{filename}"
system "sudo wget #{target}"
end
theme_name = filename[/[^\.]+/]
if File.exist?("../themes/#{theme_name}")
warn "#{theme_name} has already installed."
exit -1
end
puts "...installing: #{filename}"
system "sudo unzip #{filename} > /dev/null"
system "sudo mv #{theme_name} ../themes/"
puts "Install successful!: #{theme_name}"
end
def uninstall_theme(name)
Dir.chdir "#{$settings[:wordpress_dir]}/wp-content/themes/"
filename = name
if filename =~ /\A\d+\z/
list = `ls #{$settings[:wordpress_dir]}/wp-content/themes/ | sort`.
split(/\s/).delete_if {|i| i == 'index.php' }
filename = list[filename.to_i]
end
unless filename
warn 'invalid number or filename'
exit -1
end
puts "...removing: #{filename}"
system "sudo rm -rf #{filename}"
end
def show_help
puts <<-EOS
wp_theme [command] [args] - WordPress Themes manager.
add [uri] - install new theme.
del [name]|[number] - install new theme.
list - install new theme.
help - show this message.
EOS
end
# hack system method
def system_with_display(command)
puts command if $verbose
system_without_display command
end
alias system_without_display system
alias system system_with_display
def main
alias system puts if ARGV.delete('--debug')
$verbose = ARGV.delete('-v')
if ARGV.length == 0
show_list
exit
end
case ARGV.shift
when 'add' then install_theme(ARGV[0])
when 'del' then uninstall_theme(ARGV[0])
when 'list' then show_list
when 'help' then show_help
else
show_help
end
end
main if $0 == __FILE__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment