aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Longair <mhl@pobox.com>2013-10-30 10:12:02 +0000
committerMark Longair <mhl@pobox.com>2013-10-30 10:12:02 +0000
commit95a92efe385ebe21b8f31424f2cef86ea9c9455d (patch)
treec7d2530cf615cedc70cb463a9bc0f1c0c4de6729
parent5964dc79a0e6608529ccf610460d1928811e8ef0 (diff)
parent5aed12997b6d8950e20b1620fd643256ca56f818 (diff)
Merge branch 'switch-theme-script' into rails-3-develop
Conflicts: config/initializers/alaveteli.rb
-rw-r--r--config/initializers/alaveteli.rb1
-rw-r--r--config/initializers/theme_loader.rb3
-rw-r--r--lib/tasks/themes.rake2
-rw-r--r--lib/theme.rb3
-rwxr-xr-xscript/switch-theme.rb120
-rw-r--r--spec/lib/theme_spec.rb25
6 files changed, 151 insertions, 3 deletions
diff --git a/config/initializers/alaveteli.rb b/config/initializers/alaveteli.rb
index 4041ef7a8..5171c052f 100644
--- a/config/initializers/alaveteli.rb
+++ b/config/initializers/alaveteli.rb
@@ -51,6 +51,7 @@ require 'alaveteli_file_types'
require 'alaveteli_localization'
require 'message_prominence'
require 'actionmailer_patches'
+require 'theme'
AlaveteliLocalization.set_locales(AlaveteliConfiguration::available_locales,
AlaveteliConfiguration::default_locale)
diff --git a/config/initializers/theme_loader.rb b/config/initializers/theme_loader.rb
index 1ad2d01f1..b3ae11e1e 100644
--- a/config/initializers/theme_loader.rb
+++ b/config/initializers/theme_loader.rb
@@ -18,7 +18,6 @@ if Rails.env == "test"
end
else
for url in AlaveteliConfiguration::theme_urls.reverse
- theme_name = url.sub(/.*\/(.*).git/, "\\1")
- require_theme(theme_name)
+ require_theme theme_url_to_theme_name(url)
end
end
diff --git a/lib/tasks/themes.rake b/lib/tasks/themes.rake
index a8d16f108..1eed92f1e 100644
--- a/lib/tasks/themes.rake
+++ b/lib/tasks/themes.rake
@@ -85,7 +85,7 @@ namespace :themes do
def install_theme(theme_url, verbose, deprecated=false)
deprecation_string = deprecated ? " using deprecated THEME_URL" : ""
- theme_name = File.basename(theme_url, '.git')
+ theme_name = theme_url_to_theme_name theme_url
puts "Installing theme #{theme_name}#{deprecation_string} from #{theme_url}"
uninstall(theme_name, verbose) if installed?(theme_name)
install_theme_using_git(theme_name, theme_url, verbose)
diff --git a/lib/theme.rb b/lib/theme.rb
new file mode 100644
index 000000000..4f03b5d99
--- /dev/null
+++ b/lib/theme.rb
@@ -0,0 +1,3 @@
+def theme_url_to_theme_name(theme_url)
+ File.basename theme_url, '.git'
+end
diff --git a/script/switch-theme.rb b/script/switch-theme.rb
new file mode 100755
index 000000000..47f81c7a8
--- /dev/null
+++ b/script/switch-theme.rb
@@ -0,0 +1,120 @@
+#!/usr/bin/env ruby
+# -*- coding: utf-8 -*-
+
+# A simple script to swap around your Alaveteli themes when you're
+# hacking on Alaveteli. By default this assumes that you have an
+# 'alaveteli-themes' directory at the same level as your alaveteli git
+# repository, e.g.:
+#
+# alaveteli
+# ├── app
+# ├── cache
+# ...
+# └── vendor
+# alaveteli-themes/
+# ├── alavetelitheme
+# ├── asktheeu-theme
+# ├── chiediamo-theme
+# ├── ipvtheme
+# ├── queremossabertheme
+# ├── tuderechoasaber-theme
+# ├── whatdotheyknow-theme
+# └── yourrighttoknow
+#
+# However, you can override the location of your themes directory with
+# the environment variable ALAVETELI_THEMES_DIR.
+#
+# You need to have a corresponding general.yml file called, for example:
+#
+# config/general-whatdotheyknow-theme.yml
+# config/general-yourrighttoknow.yml
+
+require 'tempfile'
+
+theme_directory = ENV['ALAVETELI_THEMES_DIR']
+alaveteli_directory = File.expand_path(File.join(File.dirname(__FILE__),
+ ".."))
+unless theme_directory
+ theme_directory = File.expand_path File.join(alaveteli_directory,
+ '..',
+ 'alaveteli-themes')
+end
+
+unless File.exists? theme_directory
+ STDERR.puts "The theme directory '#{theme_directory}' didn't exist."
+ exit 1
+end
+
+# Assume that any directory directly under theme_directory is a theme:
+$available_themes = Dir.entries(theme_directory).find_all do |local_theme_name|
+ next if [".", ".."].index local_theme_name
+ next unless local_theme_name
+ full_path = File.join theme_directory, local_theme_name
+ next unless File.directory? full_path
+ next unless File.directory? File.join(full_path, '.git')
+ local_theme_name
+end
+
+if $available_themes.empty?
+ STDERR.puts "There were no theme directories found in '#{theme_directory}'"
+ exit
+end
+
+def usage_and_exit
+ STDERR.puts "Usage: #{$0} <THEME-NAME>"
+ $available_themes.sort.each do |theme_name|
+ STDERR.puts " #{theme_name}"
+ end
+ exit 1
+end
+
+usage_and_exit unless ARGV.length == 1
+requested_theme = ARGV[0]
+usage_and_exit unless $available_themes.include? requested_theme
+
+full_theme_path = File.join theme_directory, requested_theme
+
+config_directory = File.join alaveteli_directory, 'config'
+general_filename = File.join config_directory, "general.yml"
+theme_filename = File.join config_directory, "general-#{requested_theme}.yml"
+
+if File.exists?(general_filename) && ! (File.symlink? general_filename)
+ STDERR.puts "'#{general_filename}' exists, but isn't a symlink"
+ exit 1
+end
+
+unless File.exists? theme_filename
+ STDERR.puts "'#{theme_filename}' didn't exist"
+ exit 1
+end
+
+def symlink target, link_directory, link_name
+ tmp = Tempfile.new link_name, link_directory
+ if system("ln", "-sfn", target, tmp.path)
+ full_link_name = File.join(link_directory, link_name)
+ begin
+ File.rename tmp.path, full_link_name
+ rescue Errno::EISDIR
+ STDERR.puts "Couldn't overwrite #{full_link_name} since it's a directory"
+ exit 1
+ end
+ else
+ STDERR.puts "Failed to create a symlink from #{tmp.path} to #{target}"
+ exit 1
+ end
+end
+
+symlink(File.basename(theme_filename),
+ config_directory,
+ "general.yml")
+
+symlink(File.join(full_theme_path, 'public'),
+ File.join(alaveteli_directory, 'public'),
+ 'alavetelitheme')
+
+symlink(full_theme_path,
+ File.join(alaveteli_directory, 'vendor', 'plugins'),
+ requested_theme)
+
+STDERR.puts """Switched to #{requested_theme}!
+You will need to restart any development server you have running."""
diff --git a/spec/lib/theme_spec.rb b/spec/lib/theme_spec.rb
new file mode 100644
index 000000000..829c1a269
--- /dev/null
+++ b/spec/lib/theme_spec.rb
@@ -0,0 +1,25 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe "theme_url_to_theme_name" do
+
+ it "should deal with a typical bare repo URL" do
+ url = 'git://wherever/blah-theme.git'
+ theme_url_to_theme_name(url).should == 'blah-theme'
+ end
+
+ it "should deal with a typical bare repo URL with trailing slashes" do
+ url = 'ssh://wherever/blah-theme.git//'
+ theme_url_to_theme_name(url).should == 'blah-theme'
+ end
+
+ it "should deal with a typical non-bare repo URL" do
+ url = '/home/whoever/themes/blah-theme'
+ theme_url_to_theme_name(url).should == 'blah-theme'
+ end
+
+ it "should deal with a typical non-bare repo URL with a trailing slash" do
+ url = '/home/whoever/themes/blah-theme/'
+ theme_url_to_theme_name(url).should == 'blah-theme'
+ end
+
+end