Skip to content

Commit

Permalink
Added the ability to search templates in multiple catalogs (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Tolstoy committed Jan 24, 2016
1 parent e5956f1 commit a0ae568
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
21 changes: 1 addition & 20 deletions lib/generamba/cli/template/template_list_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,11 @@ class Template < Thor

desc 'list', 'Prints out the list of all templates available in the shared GitHub catalog'
def list
does_rambafile_exist = Dir[RAMBAFILE_NAME].count > 0

if does_rambafile_exist
rambafile = YAML.load_file(RAMBAFILE_NAME)
catalogs = rambafile[CATALOGS_KEY]
end

terminator = CatalogTerminator.new
terminator.remove_all_catalogs

downloader = CatalogDownloader.new
catalog_paths = [downloader.download_catalog(GENERAMBA_CATALOG_NAME, RAMBLER_CATALOG_REPO)]

if catalogs != nil && catalogs.count > 0
catalogs.each do |catalog_url|
catalog_name = catalog_url.split('://').last
catalog_name = catalog_name.gsub('/', '-');
catalog_paths.push(downloader.download_catalog(catalog_name, catalog_url))
end
end

catalog_template_list_helper = CatalogTemplateListHelper.new

templates = []
catalog_paths = downloader.update_all_catalogs_and_return_filepaths
catalog_paths.each do |path|
templates += catalog_template_list_helper.obtain_all_templates_from_a_catalog(path)
templates = templates.uniq
Expand Down
12 changes: 9 additions & 3 deletions lib/generamba/cli/template/template_search_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ class Template < Thor
desc 'search [SEARCH_STRING]', 'Searches a template with a given name in the shared GitHub catalog'
def search(term)
downloader = CatalogDownloader.new
generamba_catalog_path = downloader.download_catalog(GENERAMBA_CATALOG_NAME, RAMBLER_CATALOG_REPO)

catalog_template_search_helper = CatalogTemplateSearchHelper.new
templates = catalog_template_search_helper.search_templates_in_a_catalog(generamba_catalog_path, term)

catalog_paths = downloader.update_all_catalogs_and_return_filepaths

templates = []
catalog_paths.each do |path|
templates += catalog_template_search_helper.search_templates_in_a_catalog(path, term)
templates = templates.uniq
end

templates.map { |template_name|
keywords = term.squeeze.strip.split(' ').compact.uniq
matcher = Regexp.new('(' + keywords.join('|') + ')')
Expand Down
27 changes: 27 additions & 0 deletions lib/generamba/template/helpers/catalog_downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@ module Generamba
# Provides the functionality to download template catalogs from the remote repository
class CatalogDownloader

# Updates all of the template catalogs and returns their filepaths.
# If there is a Rambafile in the current directory, it also updates all of the catalogs specified there.
#
# @return [Array] An array of filepaths to downloaded catalogs
def update_all_catalogs_and_return_filepaths
does_rambafile_exist = Dir[RAMBAFILE_NAME].count > 0

if does_rambafile_exist
rambafile = YAML.load_file(RAMBAFILE_NAME)
catalogs = rambafile[CATALOGS_KEY]
end

terminator = CatalogTerminator.new
terminator.remove_all_catalogs

catalog_paths = [download_catalog(GENERAMBA_CATALOG_NAME, RAMBLER_CATALOG_REPO)]

if catalogs != nil && catalogs.count > 0
catalogs.each do |catalog_url|
catalog_name = catalog_url.split('://').last
catalog_name = catalog_name.gsub('/', '-');
catalog_paths.push(download_catalog(catalog_name, catalog_url))
end
end
return catalog_paths
end

# Clones a template catalog from a remote repository
#
# @param name [String] The name of the template catalog
Expand Down

0 comments on commit a0ae568

Please sign in to comment.