Skip to content

Commit

Permalink
imported lib and resources from core gem; created bin script
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerflint committed Jun 11, 2015
1 parent cf43bbb commit 2a87c63
Show file tree
Hide file tree
Showing 33 changed files with 1,748 additions and 2 deletions.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
*.bundle
*.so
*.o
*.a
mkmf.log
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in hooky.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2014 Tyler Flint

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# hooky
a ruby framework to provide hooky scripts with re-usable components and resources via an elegant dsl
# Hooky

TODO: Write a gem description

## Installation

Add this line to your application's Gemfile:

gem 'hooky'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hooky

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it ( https://github.com/[my-github-username]/hooky/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "bundler/gem_tasks"

44 changes: 44 additions & 0 deletions bin/hooky
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env ruby

HOOKS_DIR = "#{Dir.pwd}/tmp"

hook = ARGV.shift
mod = nil
script = nil

if not hook
$stderr.puts "hook is required"
exit 1
end

hook.match '(.+)\[(.+)\]' do |m|
mod = m[1]
script = m[2]
end

if not (mod and script)
$stderr.puts "invalid hook format, expecting: php[default-configure]"
exit 1
end

if not File.exists? "#{HOOKS_DIR}/#{mod}/hooks/#{script}"
$stderr.puts "hook: #{hook} does not exist"
end

# TODO: find a way to conditionally trigger this if dev only
lib = File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'hooky'

include Hooky::Hook # payload helpers
include Hooky::DSL # awesome resource library

set :hook_root, "#{HOOKS_DIR}/#{mod}"

# require hook libs
Dir.glob("#{HOOKS_DIR}/#{mod}/lib/*.rb").sort.each do |file|
require file
end

load "#{HOOKS_DIR}/#{mod}/hooks/#{script}"
28 changes: 28 additions & 0 deletions hooky.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'hooky/version'

Gem::Specification.new do |spec|
spec.name = "hooky"
spec.version = Hooky::VERSION
spec.authors = ["Tyler Flint"]
spec.email = ["[email protected]"]
spec.summary = %q{Hooky is the framework to provide hooky scripts with re-usable components and resources via an elegant dsl.}
spec.description = %q{The core framework to provide hooky scripts with re-usable components.}
spec.homepage = ""
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency 'tilt'
spec.add_dependency 'erubis'
spec.add_dependency 'oj'
spec.add_dependency 'multi_json', '>= 1.3'

spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "rake"
end
20 changes: 20 additions & 0 deletions lib/hooky.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'hooky/converginator'
require 'hooky/db'
require 'hooky/dsl'
require 'hooky/error'
require 'hooky/exit'
require 'hooky/helpers'
require 'hooky/hook'
require 'hooky/registry'
require 'hooky/resource'
require "hooky/version"

module Hooky
extend self

def resources
@resources ||= Hooky::Registry.new
end
end

require 'hooky/resources'
118 changes: 118 additions & 0 deletions lib/hooky/converginator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
module Hooky
class Converginator

def initialize(map, list)
@map = map
@list = list
end

def converge!
output = {}
@map.each do |key, template|
if @list.key? key
output[key] = converge_value template, @list[key]
else
output[key] = template[:default]
end
end
output
end

def converge_value(template, value)
if valid? template, value
value
else
template[:default]
end
end

def valid?(template, value)
valid_type?(template, value) and valid_value?(template, value)
end

def valid_type?(template, value)
case template[:type]
when :array
valid_array? template, value
when :byte
valid_byte? value
when :file
valid_file? value
when :folder
valid_folder? value
when :hash
valid_hash? value
when :integer
valid_integer? value
when :on_off
valid_on_off? value
when :string
valid_string? value
end
end

def valid_value?(template, value)

return true if not template.key? :from

if template[:type] == :array
!( value.map {|element| template[:from].include? element} ).include? false
else
template[:from].include? value
end
end

def valid_string?(element)
element.is_a? String
end

def valid_array?(template, value)

return false if not value.is_a? Array

return true if not template.key? :of

case template[:of]
when :byte
!( value.map {|element| valid_byte? element} ).include? false
when :file
!( value.map {|element| valid_file? element} ).include? false
when :folder
!( value.map {|element| valid_folder? element} ).include? false
when :integer
!( value.map {|element| valid_integer? element} ).include? false
when :on_off
!( value.map {|element| valid_on_off? element} ).include? false
when :string
!( value.map {|element| valid_string? element} ).include? false
else
true
end
end

def valid_hash?(value)
value.is_a? Hash
end

def valid_integer?(value)
value.is_a? Integer || (value.to_i.to_s == value.to_s)
end

def valid_file?(value)
value =~ /^\/?[^\/]+(\/[^\/]+)*$/
end

def valid_folder?(value)
value =~ /^\/?[^\/]+(\/[^\/]+)*\/?$/
end

def valid_on_off?(value)
['true', 'false', 'On', 'on', 'Off', 'off', '1', '0'].include? value.to_s
end

def valid_byte?(value)
value.to_s =~ /^\d+[BbKkMmGgTt]?$/
end

end
end
37 changes: 37 additions & 0 deletions lib/hooky/db.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'oj'
require 'multi_json'
require 'fileutils'

module Hooky
class DB

DEFAULT_PATH = '/var/db/hooky/db.json'

def initialize(path=nil)
@path = path || DEFAULT_PATH
end

def fetch(key)
data[key]
end

def put(key, value)
data[key] = value
save
end

def load
::MultiJson.load(::File.read(@path)) rescue {}
end

def save
::FileUtils.mkdir_p(File.dirname(@path))
::File.write(@path, ::MultiJson.dump(data))
end

def data
@data ||= load
end

end
end
40 changes: 40 additions & 0 deletions lib/hooky/dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Hooky
module DSL

def dict
@dict ||= {}
end

def set(key, value)
dict[key] = value
end

def get(key)
dict[key]
end

def method_missing(method_symbol, *args, &block)
resource_klass = Hooky.resources.get(method_symbol)
if resource_klass
resource = resource_klass.new(*args)
resource.dict = dict
resource.instance_eval(&block) if block_given?
if resource.can_run?
actions = resource.action
if actions.length > 1
res = {}
actions.each do |action|
res[action] = resource.run action
end
res
else
resource.run actions.first
end
end
else
super
end
end

end
end
6 changes: 6 additions & 0 deletions lib/hooky/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Hooky
module Error
class UnexpectedExit < StandardError; end
class UnknownAction < StandardError; end
end
end
Loading

0 comments on commit 2a87c63

Please sign in to comment.