Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented template and catalog syntax #181

Merged
merged 1 commit into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ require "generamba"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start
1 change: 1 addition & 0 deletions bin/generamba
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

require 'generamba'

Rake.load_rakefile('generamba/tasks/default.rake')
Rake.application.run
1 change: 1 addition & 0 deletions generamba.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.2'

spec.add_runtime_dependency 'rake', '~> 10.5'
spec.add_runtime_dependency 'git', '~> 1.3'

spec.add_development_dependency 'bundler', '~> 1.10'
spec.add_development_dependency 'rspec', '~> 3.4'
Expand Down
8 changes: 8 additions & 0 deletions lib/generamba.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
require 'rake'
require 'git'
require 'yaml'

require 'generamba/version'
require 'generamba/errors'

require 'generamba/service/remote_plugin'

require 'generamba/dsl/validators'
require 'generamba/dsl/hooks'
require 'generamba/dsl/attributes'
require 'generamba/dsl/methods'
require 'generamba/dsl/catalogs'
require 'generamba/dsl/templates'

require 'generamba/rake/application'
require 'generamba/rake/dsl'
Expand Down
41 changes: 41 additions & 0 deletions lib/generamba/dsl/catalogs.rb
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
66 changes: 66 additions & 0 deletions lib/generamba/dsl/templates.rb
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
4 changes: 2 additions & 2 deletions lib/generamba/dsl/validators.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Generamba
module DSL
module Validators
def validate(validate_name, _ = {})
def validate(_validate_name, _options = {})
result = Proc.new.call if block_given?
# load plugin
result = true unless block_given?

raise "validator #{validate_name} raise error" unless result
raise Generamba::Error::Validator unless result
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions lib/generamba/errors.rb
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
12 changes: 8 additions & 4 deletions lib/generamba/rake/application.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
class RambaApplication < Rake::Application
attr_accessor :current_task, :before_hooks, :after_hooks, :error_hooks
attr_accessor :current_task
attr_accessor :before_hooks, :after_hooks, :error_hooks
attr_accessor :raw_templates_list, :selected_templates

def initialize
super
@rakefiles = DEFAULT_RAKEFILES.dup << 'Rambafile'
@before_hooks = {}
@after_hooks = {}
@error_hooks = {}
@before_hooks = {}
@after_hooks = {}
@error_hooks = {}
@raw_templates_list = {}
@selected_templates = {}
end

def standard_rake_options
Expand Down
67 changes: 67 additions & 0 deletions lib/generamba/service/remote_plugin.rb
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really need comments for classes and methods, otherwise I can't understand the purpose of some classes :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now that it's a generic class for any type of plugin installed from a remote repository. And catalog is a plugin too.
However it'd be great if you'll add comments for new classes. Or maybe a brief description for a pull request with some implementation details.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, it`s good idea for new service classes. Done

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
1 change: 1 addition & 0 deletions lib/generamba/tasks/default.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
task :default
23 changes: 23 additions & 0 deletions spec/dsl/catalogs_spec.rb
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
50 changes: 50 additions & 0 deletions spec/dsl/templates_spec.rb
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
2 changes: 1 addition & 1 deletion spec/dsl/validators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
validate :test_validator do
false
end
}.to raise_error(RuntimeError)
}.to raise_error(Generamba::Error::Validator)
end

it 'should remain silent if it return true' do
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/local_template/Code/test_template.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
It`s working template
8 changes: 8 additions & 0 deletions spec/dummy/local_template/local_template.rambaspec
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}
12 changes: 8 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
RSpec.configure do |config|
config.before(:each) do
extend Rake::DSL
extend Generamba::DSL::Attributes
extend Generamba::DSL::Hooks
extend Generamba::DSL::Methods
extend Generamba::DSL::Validators

dsl_parent_module = Generamba::DSL

dsl_parent_module.constants.each do |module_name|
child_module = dsl_parent_module.const_get(module_name)

extend child_module if child_module.is_a? Module
end
end
end