Created
April 23, 2010 11:14
-
-
Save Sixeight/376446 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env ruby | |
| # -*- coding: utf-8 -*- | |
| require 'uri' | |
| class DrupalCtl | |
| class << self; private :new end | |
| def self.instance(type = :theme, options = {}) | |
| settings = { | |
| :drupal_dir => '/var/www/drupal', | |
| :tmp_dir => '/home/tomohiro/tmp/drupal', | |
| }.merge(options) | |
| unless @type == type | |
| @type = type | |
| @_instance = case type | |
| when :theme then DrupalTheme.new(settings) | |
| when :module then DrupalModule.new(settings) | |
| else | |
| raise 'no such type' | |
| end | |
| end | |
| @_instance | |
| end | |
| def self.list | |
| puts 'Theme list:' | |
| self.instance(:theme).list | |
| puts | |
| puts 'Module list:' | |
| self.instance(:module).list | |
| end | |
| def self.show_help | |
| puts <<-EOS | |
| drupalctl [type] [command] [args] - Drupal Theme/Module manager | |
| add [uri] - install stuff | |
| del [name]|[number] - uninstall stuff | |
| list - list all stuff | |
| help - show this message. | |
| EOS | |
| end | |
| class DrupalStuff | |
| def initialize(settings, type) | |
| @working_dir = "#{settings[:drupal_dir]}/sites/all/#{type}" | |
| @tmp_dir = "#{settings[:tmp_dir]}/#{type}" | |
| update_list | |
| end | |
| def update_list | |
| @list = Dir.entries(@working_dir).delete_if {|d| /\A\./ =~ d } | |
| end | |
| def list | |
| @list.each_with_index {|theme, idx| puts "#{idx}: #{theme}" } | |
| end | |
| def add(uri) | |
| target = URI.parse(uri) | |
| filename = File.basename(target.path) | |
| unless filename =~ /\.tar.gz$/ | |
| warn 'Theme must be compressed by gzip and archive by tar' | |
| exit -1 | |
| end | |
| Dir.chdir @tmp_dir | |
| unless File.exist?(filename) | |
| puts "...downloading: #{filename}" | |
| system "sudo wget #{target}" | |
| end | |
| theme_name = filename[/[^\-]+/] | |
| if @list.include?(theme_name) | |
| warn "#{theme_name} has already installed." | |
| exit -1 | |
| end | |
| puts "...installing: #{filename}" | |
| system "sudo tar xzf #{filename} > /dev/null" | |
| system "sudo mv #{theme_name} #{@working_dir}/" | |
| puts "Install successful!: #{theme_name}" | |
| end | |
| def del(name) | |
| filename = name | |
| if filename =~ /\A\d+\z/ | |
| filename = @list[filename.to_i] | |
| end | |
| unless filename | |
| warn 'invalid number or filename' | |
| exit -1 | |
| end | |
| Dir.chdir @working_dir | |
| puts "...removing: #{filename}" | |
| system "sudo rm -rf #{filename}" | |
| end | |
| end | |
| class DrupalTheme < DrupalStuff | |
| def initialize(settings) | |
| super settings, :themes | |
| end | |
| end | |
| class DrupalModule < DrupalStuff | |
| def initialize(settings) | |
| super settings, :modules | |
| end | |
| end | |
| 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('-n') | |
| $verbose = ARGV.delete('-v') | |
| case ARGV.shift | |
| when 'theme' then controller = DrupalCtl.instance(:theme) | |
| when 'module' then controller = DrupalCtl.instance(:module) | |
| when 'list' then DrupalCtl.list | |
| when 'help' | |
| DrupalCtl.show_help | |
| exit | |
| else | |
| DrupalCtl.show_help | |
| exit | |
| end | |
| case ARGV.shift | |
| when 'add' then controller.add(ARGV[0]) | |
| when 'del' then controller.del(ARGV[0]) | |
| when 'list' then controller.list | |
| end | |
| end | |
| main if $0 == __FILE__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment