Permalink
Browse files

Implemented 'add', 'delete', 'remove' options in binary. Resolved a b…

…ug in catalog, where themes wernt recognized correctly due to the git_repo?(theme) function being called in absolute paths, rather then plain theme_names.
  • Loading branch information...
1 parent 40a594e commit fd61e5624b93366d84f5cf329d04fa6c0ef291ab Jeroen van Schagen committed Nov 20, 2009
Showing with 88 additions and 29 deletions.
  1. +73 −12 bin/interfacelift
  2. +13 −15 lib/catalog.rb
  3. +2 −2 spec/catalog_spec.rb
View
@@ -12,26 +12,25 @@ program :description, 'Installs layouts in rails apps'
# Install
command :install do |c|
c.syntax = 'install theme path'
- c.description = 'installs theme in rails app'
+ c.description = 'Installs theme in rails app'
c.option '--repo git://repo.git', String, 'Fetch the theme from a git repo'
c.action do |args, options|
if args.size < 1
- puts "You need to give the theme name."
+ puts "You need to specify a theme name."
else
begin
+ raise "You need to provide the git repo of #{theme}, using the --repo flag" if options.repo.nil?
+
theme = args[0]
path = args[1].nil? ? "." : args[1]
-
- # Retrieve theme and store in catalog
catalog = InterfaceLift::Catalog.new
- if ! catalog.theme_exists?(theme)
- raise "You need to provide the git repo of #{theme}, using the --repo flag" if options.repo.nil?
- puts "Fetching theme #{theme}..."
- catalog.add_theme(theme,options.repo)
- end
+
+ # Retrieve theme from repository
+ puts "Fetching theme #{theme}..."
+ catalog.add_theme(theme, options.repo)
# Transfer theme from catalog in rails app
- installer = InterfaceLift::Installer.new(path,theme)
+ installer = InterfaceLift::Installer.new(path, theme)
installer.install!
rescue Exception => e
puts e
@@ -44,15 +43,77 @@ end
# List
command :list do |c|
c.syntax = 'list'
- c.description = 'shows a list of all available themes'
+ c.description = 'Shows a list of all available themes'
c.action do |args, options|
catalog = InterfaceLift::Catalog.new
themes = catalog.themes
- if themes.size == 0
+ if themes.empty?
puts "No themes installed, use 'install' or 'add'"
else
puts "Available themes:"
catalog.themes.each { |theme| puts theme }
end
end
+end
+
+# Remove
+command :remove do |c|
+ c.syntax = 'remove theme'
+ c.description = 'remove theme from system'
+ c.action do |args, options|
+ if args.size < 1
+ puts "You need to specify a theme name."
+ else
+ theme = args[0]
+ catalog = InterfaceLift::Catalog.new
+ if catalog.theme_exists?(theme)
+ catalog.remove_theme(theme)
+ puts "Removed theme #{theme}."
+ else
+ puts "Cannot remove theme #{theme}, because it isn't installed."
+ end
+ end
+ end
+end
+
+# Add
+command :add do |c|
+ c.syntax = 'add theme'
+ c.description = 'fetches theme and store on system'
+ c.option '--repo git://repo.git', String, 'Fetch the theme from a git repo'
+ c.action do |args, options|
+ if args.size < 1
+ puts "You need to specify a theme name."
+ else
+ # Temporarly requires users to specify a git repository, as the default theme website is still offline
+ raise "You need to provide the git repo of #{theme}, using the --repo flag" if options.repo.nil?
+
+ theme = args[0]
+ catalog = InterfaceLift::Catalog.new
+
+ puts "Fetching theme #{theme}..."
+ catalog.add_theme(theme, options.repo)
+ end
+ end
+end
+
+# Update
+command :update do |c|
+ c.syntax = 'update theme'
+ c.description = 'retrieves latest version of theme'
+ c.action do |args, options|
+ if args.size < 1
+ puts "You need to specify a theme name."
+ else
+ theme = args[0]
+ catalog = InterfaceLift::Catalog.new
+
+ if catalog.theme_exists?(theme)
+ puts "Updating theme #{theme}..."
+ catalog.update_theme(theme)
+ else
+ puts "Cannot update theme #{theme}, because it isn't installed."
+ end
+ end
+ end
end
View
@@ -2,9 +2,12 @@
require 'git'
module InterfaceLift
+
+ # Catalogs manage themes
class Catalog
attr_reader :catalog_path
+ # Construct catalog
def initialize(catalog_path = nil)
@catalog_path = catalog_path.nil? ? ::InterfaceLift::DEFAULT_CATALOG_PATH : catalog_path
create_catalog_path!
@@ -14,32 +17,33 @@ def initialize(catalog_path = nil)
def themes
themes = []
Dir.glob("#{@catalog_path}/*").each do |item|
- themes << item if git_repo?(item)
+ theme = item.split("/").last
+ themes << theme if git_repo?(theme)
end
- themes.map { |path| path.split("/").last }
+ return themes
end
# Does the given theme exist in the working path
def theme_exists?(theme)
- themes.include?(theme)
+ return themes.include?(theme)
end
# Is the argumented theme a git repository?
- def git_repo?(theme)
+ def git_repo?(theme)
begin
g = Git.open("#{catalog_path}/#{theme}")
rescue ArgumentError => e
# If the path does not exist it will return an ArgumentError
return false
end
- g.index.readable?
+ return g.index.readable?
end
# Retrieve the latest version of argumented theme from git repository
def update_theme(theme)
return false unless git_repo?(theme)
g = Git.open("#{catalog_path}/#{theme}")
- !g.pull.match(/^Updating/).nil?
+ return ! g.pull.match(/^Updating/).nil?
end
# Remove theme from catalog
@@ -48,21 +52,15 @@ def remove_theme(theme)
end
# Download a theme from git repository
- # returns true when sucessfull
- # raises exception when it fails
def add_theme(theme, repo)
remove_theme(theme)
- !Git.clone(repo, "#{@catalog_path}/#{theme}").repo.path.nil?
+ return ! Git.clone(repo, "#{@catalog_path}/#{theme}").repo.path.nil?
end
- #create catalog path when it does not exist
+ # Create catalog path when it does not exist
def create_catalog_path!
FileUtils.mkdir_p(@catalog_path) unless File.exists?(@catalog_path)
end
end
-end
-
-
-# test = InterfaceLift::Catalog.new
-# test.add_theme "bright_admin", "git://github.com/michel/bright_admin.git"
+end
@@ -29,7 +29,7 @@
themes = ["test_theme", "test_theme2"]
paths = themes.map { |path| "#{@cat.catalog_path}/#{path}" }
Dir.stubs(:glob).with("#{@cat.catalog_path}/*").returns(paths)
- paths.each { |path| @cat.expects(:git_repo?).with(path).returns(true) }
+ themes.each { |theme| @cat.expects(:git_repo?).with(theme).returns(true) }
@cat.themes.sort.should == themes.sort
end
@@ -106,4 +106,4 @@
FileUtils.expects(:mkdir_p).with(non_existing_path)
@new_cat = Catalog.new(non_existing_path)
end
-end
+end

0 comments on commit fd61e56

Please sign in to comment.