From fdc6d51a298626de0aa3ddfc1e3ca27a55049130 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 29 Aug 2012 15:29:11 +0100 Subject: Move the installation of themes to a Rake task so that we can use the ALAVETELI_VERSION constant to check for tags in themes that indicate compatibility with this version of the Alaveteli codebase. --- lib/tasks/themes.rake | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 lib/tasks/themes.rake (limited to 'lib/tasks/themes.rake') diff --git a/lib/tasks/themes.rake b/lib/tasks/themes.rake new file mode 100644 index 000000000..cfa3c0a61 --- /dev/null +++ b/lib/tasks/themes.rake @@ -0,0 +1,85 @@ + +namespace :themes do + + def plugin_dir + File.join(Rails.root,"vendor","plugins") + end + + def theme_dir(theme_name) + File.join(plugin_dir, theme_name) + end + + def install_theme_using_git(name, uri, verbose=false, options={}) + mkdir_p(install_path = theme_dir(name)) + Dir.chdir install_path do + init_cmd = "git init" + init_cmd += " -q" if options[:quiet] and not verbose + puts init_cmd if verbose + system(init_cmd) + base_cmd = "git pull --depth 1 #{uri}" + # Is there a tag for this version of Alaveteli? + usage_tag = "use-with-alaveteli-#{ALAVETELI_VERSION}" + # Query the remote repository passing flags for tags and + # a non-zero return code on failure to find the tag + if system("git ls-remote --exit-code --tags #{uri} #{usage_tag}") + # If we got a tag, pull that instead of HEAD + puts "Using tag #{usage_tag}" if verbose + base_cmd += " refs/tags/#{usage_tag}" + else + puts "No specific tag for this version: using HEAD" if verbose + end + base_cmd += " -q" if options[:quiet] and not verbose + puts base_cmd if verbose + if system(base_cmd) + puts "removing: .git .gitignore" if verbose + rm_rf %w(.git .gitignore) + else + rm_rf install_path + end + end + end + + def uninstall(theme_name, verbose=false) + dir = theme_dir(theme_name) + if File.directory?(dir) + run_hook(theme_name, 'uninstall', verbose) + puts "Removing '#{dir}'" if verbose + rm_r dir + else + puts "Plugin doesn't exist: #{dir}" + end + end + + def run_hook(theme_name, hook_name, verbose=false) + hook_file = File.join(theme_dir(theme_name), "#{hook_name}.rb") + if File.exist? hook_file + puts "Running #{hook_name} hook for #{theme_name}" if verbose + load hook_file + end + end + + def installed?(theme_name) + File.directory?(theme_dir(theme_name)) + end + + def install_theme(theme_url, deprecated=false) + deprecation_string = deprecated ? " using deprecated THEME_URL" : "" + theme_name = File.basename(theme_url, '.git') + 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) + run_hook(theme_name, 'install', verbose) + run_hook(theme_name, 'post_install', verbose) + end + + desc "Install themes specified in the config file's THEME_URLS" + task :install => :environment do + verbose = true + theme_urls = MySociety::Config.get("THEME_URLS", []) + theme_urls.each{ |theme_url| install_theme(theme_url) } + if theme_url = MySociety::Config.get("THEME_URL", nil) + # Old version of the above, for backwards compatibility + install_theme(theme_url, deprecated=true) + end + end +end \ No newline at end of file -- cgit v1.2.3 From acaae2f43908fdb60779f232123e3b88d8bb39ee Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 29 Aug 2012 15:51:43 +0100 Subject: Pass through verbose param, switch to false. --- lib/tasks/themes.rake | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/tasks/themes.rake') diff --git a/lib/tasks/themes.rake b/lib/tasks/themes.rake index cfa3c0a61..9a03865ec 100644 --- a/lib/tasks/themes.rake +++ b/lib/tasks/themes.rake @@ -62,7 +62,7 @@ namespace :themes do File.directory?(theme_dir(theme_name)) end - def install_theme(theme_url, deprecated=false) + def install_theme(theme_url, verbose, deprecated=false) deprecation_string = deprecated ? " using deprecated THEME_URL" : "" theme_name = File.basename(theme_url, '.git') puts "Installing theme #{theme_name}#{deprecation_string} from #{theme_url}" @@ -74,12 +74,12 @@ namespace :themes do desc "Install themes specified in the config file's THEME_URLS" task :install => :environment do - verbose = true + verbose = false theme_urls = MySociety::Config.get("THEME_URLS", []) - theme_urls.each{ |theme_url| install_theme(theme_url) } + theme_urls.each{ |theme_url| install_theme(theme_url, verbose) } if theme_url = MySociety::Config.get("THEME_URL", nil) # Old version of the above, for backwards compatibility - install_theme(theme_url, deprecated=true) + install_theme(theme_url, verbose, deprecated=true) end end end \ No newline at end of file -- cgit v1.2.3 From 0d2d562253a94d17153686821ae818e9b12fbe26 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 29 Aug 2012 16:06:57 +0100 Subject: Handle absence of deprecated THEME_URL option. --- lib/tasks/themes.rake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/tasks/themes.rake') diff --git a/lib/tasks/themes.rake b/lib/tasks/themes.rake index 9a03865ec..f86490500 100644 --- a/lib/tasks/themes.rake +++ b/lib/tasks/themes.rake @@ -77,7 +77,8 @@ namespace :themes do verbose = false theme_urls = MySociety::Config.get("THEME_URLS", []) theme_urls.each{ |theme_url| install_theme(theme_url, verbose) } - if theme_url = MySociety::Config.get("THEME_URL", nil) + theme_url = MySociety::Config.get("THEME_URL", "") + if ! theme_url.blank? # Old version of the above, for backwards compatibility install_theme(theme_url, verbose, deprecated=true) end -- cgit v1.2.3 From 6f390226d06fc713c96df7c40c6a9cc441ee932c Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 29 Aug 2012 16:13:37 +0100 Subject: Raise an error on a non-zero return code from the git pull command. --- lib/tasks/themes.rake | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/tasks/themes.rake') diff --git a/lib/tasks/themes.rake b/lib/tasks/themes.rake index f86490500..3fb9eef19 100644 --- a/lib/tasks/themes.rake +++ b/lib/tasks/themes.rake @@ -35,6 +35,7 @@ namespace :themes do rm_rf %w(.git .gitignore) else rm_rf install_path + raise "#{base_cmd} failed! Stopping." end end end -- cgit v1.2.3