-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented template and catalog syntax
- Loading branch information
Anton Prokhorov
committed
Dec 20, 2016
1 parent
ce9404f
commit 822d24b
Showing
17 changed files
with
314 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,5 @@ require "generamba" | |
# require "pry" | ||
# Pry.start | ||
|
||
require "irb" | ||
require 'irb' | ||
IRB.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
require 'generamba' | ||
|
||
Rake.load_rakefile('generamba/tasks/default.rake') | ||
Rake.application.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Generamba | ||
module DSL | ||
module Catalogs | ||
def catalog(link, branch: nil) | ||
@catalog_link = link | ||
@catalog_branch = branch | ||
|
||
generate_install_template_ramba(link, branch) | ||
load_cashed_templates_information | ||
end | ||
|
||
private | ||
|
||
def generamba_git | ||
Generamba::Service::RemotePlugin.new( | ||
@catalog_link, | ||
type: :catalogs, | ||
branch: @catalog_branch | ||
) | ||
end | ||
|
||
def generate_install_template_ramba(link, branch) | ||
old_task = Rake.application.instance_variable_get('@tasks').delete('template:install') | ||
|
||
namespace :template do | ||
ramba :install do | ||
old_task.invoke if old_task | ||
Generamba::Service::RemotePlugin.new(link, type: :catalogs, branch: branch).sync | ||
end | ||
end | ||
end | ||
|
||
def load_cashed_templates_information | ||
return unless generamba_git.loaded_plugin? | ||
load_generamba_temlates_data(generamba_git.cached_plugin_dir) | ||
end | ||
end | ||
end | ||
end | ||
|
||
extend Generamba::DSL::Catalogs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
module Generamba | ||
module DSL | ||
module Templates | ||
def template(template_name, options = {}) | ||
locale_path = options[:path] | ||
load_generamba_temlates_data(locale_path) if locale_path | ||
|
||
templates = take_downloaded_generamba_templates(template_name, options) | ||
|
||
prepare_template_to_use(template_name, templates) | ||
end | ||
|
||
def load_generamba_temlates_data(path) | ||
specs = Dir.glob("#{path}/**/*.rambaspec") | ||
raise Generamba::Error::IncorrectRepository if specs.empty? | ||
|
||
specs.each do |spec_file| | ||
spec_source = YAML.load_file(spec_file) | ||
spec_directory = File.dirname(spec_file) | ||
parsed_rambaspec = parse_rambaspec_file(spec_source, spec_directory) | ||
|
||
Rake.application.raw_templates_list.merge!(parsed_rambaspec) | ||
end | ||
end | ||
|
||
private | ||
|
||
def take_downloaded_generamba_templates(template_name, options) | ||
versions_template = Rake.application.raw_templates_list[template_name.to_s] | ||
raise Generamba::Error::UndefinedTemplateName if versions_template.nil? | ||
|
||
version = options[:version] || versions_template.keys.sort.last | ||
templates = versions_template[version.to_s] | ||
|
||
raise Generamba::Error::UndefinedTemplateVersion if templates.nil? | ||
templates | ||
end | ||
|
||
def prepare_template_to_use(template_name, templates) | ||
Rake.application.selected_templates.merge!(template_name => templates) | ||
end | ||
|
||
def parse_rambaspec_file(spec_source, spec_directory) | ||
name = spec_source.fetch('name') | ||
version = spec_source.fetch('version') | ||
|
||
raw_files_list = [ | ||
*spec_source['code_files'], | ||
*spec_source['test_files'], | ||
*spec_source['files'] | ||
] | ||
|
||
files_list = raw_files_list.map do |file_hash| | ||
{ | ||
target: file_hash.fetch('name'), | ||
source_full_path: "#{spec_directory}/#{file_hash.fetch('path')}" | ||
} | ||
end | ||
|
||
{ name => { version => files_list } } | ||
end | ||
end | ||
end | ||
end | ||
|
||
extend Generamba::DSL::Templates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Generamba | ||
module Error | ||
class UndefinedTemplateName < StandardError | ||
def message | ||
'I don`t know this template. Try run `generamba template:install`' | ||
end | ||
end | ||
|
||
class UndefinedTemplateVersion < StandardError | ||
def message | ||
'I don`t find this version. Try run `generamba template:install`' | ||
end | ||
end | ||
|
||
class IncorrectRepository < StandardError | ||
def message | ||
'repository don`t contains `rambaspec` file' | ||
end | ||
end | ||
|
||
class Validator < StandardError | ||
def message | ||
'validator raise error' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
module Generamba | ||
module Service | ||
# == Generamba \Service \RemotePlugin | ||
# | ||
# Provides an object for synchronization and installation remote plugins | ||
# | ||
# It allows you to do: | ||
# | ||
# generamba_git = Generamba::Service::RemotePlugin.new( | ||
# '[email protected]:rambler-digital-solutions/Generamba.git', | ||
# type: :plugin_type, | ||
# branch: 'master' | ||
# ) | ||
# | ||
# generamba_git.sync # => download or update plugin | ||
# generamba_git.loaded_plugin? # => return true if loaded plugin, else return false | ||
# generamba_git.cached_plugin_dir # => return a directory on local disk | ||
|
||
class RemotePlugin | ||
GENERAMBA_PLUGINS_PATH = "#{Dir.pwd}/.generamba".freeze | ||
|
||
def initialize(repo_link, type: '', branch: :master) | ||
@link = repo_link | ||
@type = type | ||
@branch = branch | ||
end | ||
|
||
def sync | ||
loaded_plugin? ? update_plugin : download_plugin | ||
end | ||
|
||
def cached_plugin_dir | ||
dir_name = URI.parse(link).path[1..-1] unless ssh_url? | ||
dir_name ||= link.split(':').last.gsub(/\.git$/, '') # regexp: last `.git` | ||
|
||
[GENERAMBA_PLUGINS_PATH, type, dir_name].join('/') | ||
end | ||
|
||
def loaded_plugin? | ||
Dir.exist?(cached_plugin_dir) | ||
end | ||
|
||
private | ||
|
||
def download_plugin | ||
git_repo = Git.clone(link, cached_plugin_dir) | ||
|
||
git_repo.branch(branch).checkout unless branch.empty? | ||
end | ||
|
||
def update_plugin | ||
git_repo = Git.open(cached_plugin_dir) | ||
git_repo.branch(branch).checkout unless branch.empty? | ||
git_repo.pull | ||
end | ||
|
||
def ssh_url? | ||
URI.parse link | ||
false | ||
rescue URI::InvalidURIError | ||
true | ||
end | ||
|
||
attr_reader :link, :type, :branch | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
task :default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
describe Generamba::DSL::Catalogs do | ||
before(:each) do | ||
allow_any_instance_of(Generamba::Service::RemotePlugin) | ||
.to receive(:cached_plugin_dir).and_return('spec/dummy/local_template') | ||
allow_any_instance_of(Generamba::Service::RemotePlugin) | ||
.to receive(:sync).and_return(nil) | ||
end | ||
|
||
describe '#catalog' do | ||
let!(:invoke_catalog_method) { | ||
catalog 'https://github.com/some_user/generamba-catalogs', branch: :master | ||
} | ||
|
||
it 'should generate `template:install` task' do | ||
expect(Rake.application.instance_variable_get('@tasks')['template:install']) | ||
.to be_kind_of Rake::Task | ||
end | ||
|
||
it 'should read catalog data' do | ||
expect(Rake.application.raw_templates_list['local_template']).not_to be_empty | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
describe Generamba::DSL::Templates do | ||
describe '#template' do | ||
context 'when template was synced' do | ||
before(:each) do | ||
allow_any_instance_of(Generamba::Service::RemotePlugin) | ||
.to receive(:cached_plugin_dir).and_return('spec/dummy/local_template') | ||
allow_any_instance_of(Generamba::Service::RemotePlugin) | ||
.to receive(:sync).and_return(nil) | ||
|
||
catalog 'https://github.com/some_user/generamba-catalogs', branch: :master | ||
end | ||
|
||
it 'should load data about this template if selected right template' do | ||
template :local_template | ||
|
||
expect(Rake.application.selected_templates).to include(:local_template) | ||
end | ||
|
||
it 'should raise error if selected unknown template' do | ||
expect { template :unknown_template }.to raise_error(Generamba::Error::UndefinedTemplateName) | ||
end | ||
|
||
it 'should raise error if selected unknown version' do | ||
expect { template :local_template, version: '2.1' } | ||
.to raise_error(Generamba::Error::UndefinedTemplateVersion) | ||
end | ||
end | ||
|
||
context 'when template is local template' do | ||
it 'should load data about this template' do | ||
template :local_template, path: 'spec/dummy/local_template' | ||
|
||
expect(Rake.application.selected_templates).to include(:local_template) | ||
end | ||
end | ||
end | ||
|
||
describe '#load_generamba_temlates_data' do | ||
it 'should read catalog data if path is correct' do | ||
load_generamba_temlates_data 'spec/dummy/local_template' | ||
|
||
expect(Rake.application.raw_templates_list['local_template']).not_to be_empty | ||
end | ||
|
||
it 'should raise error if path does not contain `rambaspec` file' do | ||
expect { load_generamba_temlates_data 'spec/dummy/local_template/Code' } | ||
.to raise_error(Generamba::Error::IncorrectRepository) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
It`s working template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name: "local_template" | ||
summary: "test catalog" | ||
author: "rspec" | ||
version: "0.0.1" | ||
license: "MIT" | ||
|
||
files: | ||
- {name: Code/test_template.txt, path: Code/test_template.liquid} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters