aboutsummaryrefslogtreecommitdiffstats
path: root/script/switch-theme.rb
blob: 03d59a2f946e167da1849a504b9799198aa26f0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/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:
  1. restart any development server you have running.
  2. run: bundle exec rake assets:clean
  3. run: bundle exec rake assets:precompile
"""