Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
got plugin installation, enabling working
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Adams committed Feb 15, 2009
1 parent 583cb1b commit 6404beb
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public/stylesheets/scaffold.css
public/photo_album_photo_images/*
public/uploads/*
public/themes/*
public/ansuz_plugins.xml
public/ansuz_themes.xml
themes/*
tmp/*
vendor/gems/*
1 change: 1 addition & 0 deletions ansuz_installed_plugins/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory is used as a holding directory for ansuz plugins. When you install a plugin, it is placed here. When you enable a plugin, it is copied from here to vendor/plugins
1 change: 1 addition & 0 deletions ansuz_installed_plugins/ansuz_blog
Submodule ansuz_blog added at abbc72
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
map.from_plugin :ansuz_theme_repository
map.from_plugin :ansuz_theme_installer
map.from_plugin :ansuz_plugin_repository
map.from_plugin :ansuz_plugin_installer
map.from_plugin :ansuz_menu_system

Ansuz::PluginManagerInstance.plugins_for_routes.each do |plugin|
map.from_plugin plugin
end

map.resources :tags
map.resources :users
map.resources :comments
Expand Down
11 changes: 11 additions & 0 deletions lib/ansuz/plugin_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ def register_page_type name, modules=[]
@page_types << [name, modules]
end

# Determine all of the installed plugins
def plugins_for_routes
# plugins installed into the holding directory
installed_plugins = Dir.glob(File.join(RAILS_ROOT, 'ansuz_installed_plugins', '*')).map{|d| d.split("/").split("\\").last.last}
# plugins in vendor/plugins
enabled_plugins = Dir.glob(File.join(RAILS_ROOT, 'vendor', 'plugins', '*')).map{|d| d.split("/").split("\\").last.last}

# Find the intersection of the two directories, and return them as syms
(installed_plugins & enabled_plugins).map(&:to_sym)
end

protected
def create_settings(name)
begin
Expand Down
13 changes: 13 additions & 0 deletions lib/plugins_management_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module PluginsManagementHelper
def list_plugins
plugins_dir.entries.sort.delete_if{|e| e =~ /^\.$|^\.\.$/ }
end

def plugins_dir
Dir.new(plugins_root)
end

def plugins_root
"#{RAILS_ROOT}/ansuz_installed_plugins"
end
end
1 change: 1 addition & 0 deletions vendor/plugins/ansuz_blog
Submodule ansuz_blog added at abbc72
4 changes: 4 additions & 0 deletions vendor/plugins/ansuz_plugin_installer/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
AnsuzPluginInstaller
===================

This plugin provides the ability to install themes from remote repositories.
22 changes: 22 additions & 0 deletions vendor/plugins/ansuz_plugin_installer/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the ansuz_content_section plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the ansuz_content_section plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'AnsuzContentSection'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Admin::AnsuzPluginInstallersController < Admin::BaseController
unloadable # This is required if you subclass a controller provided by the base rails app
include PluginsManagementHelper
helper :plugins_management

layout 'admin'

before_filter :load_plugins_listing_var
before_filter :load_plugins, :only => [:index]

protected
def load_plugins_listing_var
@plugins_util = Ansuz::JAdams::AnsuzPluginsListing
end

def load_plugins
@plugins = @plugins_util.listing
end

public
def index
end

# TODO: These should be REST actions probably
def install
@plugins_util.install(params[:repository_url], params[:name])
flash[:notice] = "Plugin installed successfully."
redirect_to admin_ansuz_plugin_installers_path
end

# expects a params[:name]
def enable
@plugins_util.enable(params[:name])
flash[:notice] = "Plugin has been enabled."
redirect_to admin_ansuz_plugin_installers_path
end

# expects a params[:name]
def disable
@plugins_util.disable(params[:name])
flash[:notice] = "Plugin has been disabled."
redirect_to admin_ansuz_plugin_installers_path
end

def update
@plugins_util.update(params[:id])
flash[:notice] = "Plugin updated successfully."
redirect_to admin_ansuz_plugin_installers_path
end

def destroy
@plugins_util.delete(params[:id])
flash[:notice] = "Plugin deleted successfully."
redirect_to admin_ansuz_plugin_installers_path
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<%= title "Install Plugins" -%>
<% content_for :sidebar do -%>
<div class='note'>
To install a plugin, just click on its "Install" link.<br /><br />

At the moment, the only repository available to you is the master plugin repository at www.ansuzcms.com. This is hard coded in, and there's currently no interface for adding new plugin repositories (but one's coming!). If you're adventurous and desperate, ansuz's main repository is the same code you've got, so you could easily change the hardcoded value to your own instance of ansuz and be off and running.<br /><br />
</div>
<% end -%>
<div class='fullwidth'>
<table class='subdued'>
<thead>
<tr>
<th>Name</th>
<th></th>
</tr>
</thead>
<tbody>
<% @plugins.each do |plugin| -%>
<tr class="<%= cycle('odd', 'even') %>">
<td><%= plugin[0] %></td>
<td>
<% if @plugins_util.installed?(plugin[0]) %>
<% if @plugins_util.enabled?(plugin[0]) %>
<%= link_to("Disable", disable_admin_ansuz_plugin_installers_path(:name => plugin[0])) -%> |
<% else %>
<%= link_to("Enable", enable_admin_ansuz_plugin_installers_path(:name => plugin[0])) -%> |
<% end %>
<%= link_to("Update", admin_ansuz_plugin_installer_path(plugin[0]), :method => :put) -%> |
<%= link_to("Uninstall", admin_ansuz_plugin_installer_path(plugin[0]), :method => :delete, :confirm => "Are you sure?") -%>
<% else %>
<%= link_to("Install", install_admin_ansuz_plugin_installers_path(:repository_url => plugin[1], :name => plugin[0])) -%>
<% end %>
</td>
</tr>
<% end -%>
</tbody>
</table>
</div>
1 change: 1 addition & 0 deletions vendor/plugins/ansuz_plugin_installer/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ansuz::PluginManagerInstance.register_admin_menu_entry('Manage', 'Plugins', '/admin/ansuz_plugin_installers', { :span_options => { :note => "Install plugins from remote repositories."} })
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'open-uri'
module Ansuz
module JAdams
class AnsuzPluginsListing
#REPOSITORY = "http://ansuzcms.com/ansuz_plugins.xml"
REPOSITORY = "http://localhost:3000/ansuz_plugins.xml"

PLUGIN_HOLDING_DIR = 'ansuz_installed_plugins'

# Returns an array of plugin arrays. A plugin array
# looks like [name, repository_url]
def self.listing
ary = []
xml.elements.each("plugins/plugin") do |plugin|
name = plugin.elements["name"].text
repository_url = plugin.elements["repository_url"].text
ary << [name, repository_url, install_dir_for_repo(repository_url)]
end
ary
end

def self.xml_document
uri = URI.parse REPOSITORY
uri.read
end

def self.install_dir_for_repo repo
repo.split("/").last.split(".").first
end

def self.xml
REXML::Document.new xml_document
end

# This method will install a plugin, given a repository url
def self.install repository_url, name
`cd #{RAILS_ROOT}/#{PLUGIN_HOLDING_DIR}; git clone #{repository_url} #{name}`
end

# this method will copy a given plugin from PLUGIN_HOLDING_DIR to plugins
def self.enable name
FileUtils.cp_r(File.join(RAILS_ROOT, PLUGIN_HOLDING_DIR, name), File.join(RAILS_ROOT, 'vendor', 'plugins', name))
restart_ansuz
end

# This method will disable a given plugin (i.e. rm from plugins dir)
def self.disable name
FileUtils.rm_r(File.join(RAILS_ROOT, 'vendor', 'plugins', name))
restart_ansuz
end

def self.update name
`cd #{plugin_folder_for(name)}; git pull`
end

def self.delete name
FileUtil.rm_r plugin_folder_for(name)
FileUtil.rm_r installed_plugin_folder_for(name)
end

def self.installed?(name)
File.exist?(plugin_folder_for(name))
end

def self.enabled?(name)
File.exist?(installed_plugin_folder_for(name))
end

def self.plugin_folder_for name
File.join(RAILS_ROOT, PLUGIN_HOLDING_DIR, name)
end

def self.installed_plugin_folder_for name
File.join(RAILS_ROOT, 'vendor', 'plugins', name)
end

def self.restart_ansuz
FileUtils.touch(File.join(RAILS_ROOT, 'tmp', 'restart.txt'))
end
end
end
end
3 changes: 3 additions & 0 deletions vendor/plugins/ansuz_plugin_installer/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace :admin do |admin|
admin.resources :ansuz_plugin_installers, :collection => [:install, :update, :delete, :enable, :disable]
end

0 comments on commit 6404beb

Please sign in to comment.