cableguy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "sqlite3"
4
+ gem "sequel"
5
+
6
+ # Specify your gem's dependencies in cableguy.gemspec
7
+ gemspec
data/Manifest ADDED
@@ -0,0 +1,19 @@
1
+ README
2
+ Rakefile
3
+ bin/cable
4
+ lib/palmade/cableguy.rb
5
+ lib/palmade/cableguy/builders.rb
6
+ lib/palmade/cableguy/builders/cable_chmod.rb
7
+ lib/palmade/cableguy/builders/cable_copy.rb
8
+ lib/palmade/cableguy/builders/cable_custom.rb
9
+ lib/palmade/cableguy/builders/cable_mkdir.rb
10
+ lib/palmade/cableguy/builders/cable_move.rb
11
+ lib/palmade/cableguy/builders/cable_symlink.rb
12
+ lib/palmade/cableguy/builders/cable_template.rb
13
+ lib/palmade/cableguy/cable.rb
14
+ lib/palmade/cableguy/cable_configurator.rb
15
+ lib/palmade/cableguy/cabler.rb
16
+ lib/palmade/cableguy/configurator.rb
17
+ lib/palmade/cableguy/templatebinding.rb
18
+ lib/palmade/hash.rb
19
+ Manifest
data/README ADDED
@@ -0,0 +1 @@
1
+ Build config files for your rails apps
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/cable ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ CABLE_LIB_PATH = File.expand_path(File.join(File.dirname(__FILE__), '../lib/palmade'))
4
+ require File.join(CABLE_LIB_PATH, 'cableguy')
5
+ require 'optparse'
6
+
7
+ options = {}
8
+ options[:verbose] = false
9
+
10
+ optparse = OptionParser.new do |opts|
11
+ opts.banner = "Set cable options.\n"
12
+
13
+ opts.on('-p', '--path PATH', 'Specify cabling path') do |path|
14
+ options[:path] = path
15
+ end
16
+
17
+ opts.on('-l', '--location LOCATION', 'Specify cabling location') do |location|
18
+ options[:location] = location
19
+ end
20
+
21
+ opts.on('-t', '--target TARGET', 'Specify cabling target') do |target|
22
+ options[:target] = target
23
+ end
24
+
25
+ opts.on('-v', '--verbose', 'Verbose logging') do
26
+ options[:verbose] = true
27
+ end
28
+
29
+ opts.on('-h', '--help', 'Help screen') do
30
+ puts opts
31
+ exit
32
+ end
33
+ end
34
+
35
+
36
+ optparse.parse!
37
+
38
+ Palmade::Cableguy::Runner.run(Dir.pwd, ARGV.shift, options)
39
+
40
+
data/cableguy.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'palmade/cableguy/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cableguy"
7
+ s.version = Palmade::Cableguy::VERSION
8
+ s.authors = ["Jan Mendoza"]
9
+ s.email = ["poymode@gmail.com"]
10
+ s.homepage = "https://github.com/poymode/cableguy"
11
+ s.summary = %q{Generate rails configurations from a sqlite key-value storage}
12
+ s.description = %q{cableguy}
13
+
14
+ s.rubyforge_project = "cableguy"
15
+ s.add_dependency "sqlite3"
16
+ s.add_dependency "sequel"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ # specify any dependencies here; for example:
24
+ # s.add_development_dependency "rspec"
25
+ # s.add_runtime_dependency "rest-client"
26
+ end
@@ -0,0 +1,12 @@
1
+ module Palmade::Cableguy
2
+ class Builders::CableChmod < Cable
3
+ add_as :chmod
4
+
5
+ def configure(cabler, cabling, target)
6
+ cabler.say_with_time "changing permissions #{args.join(' -> ')}" do
7
+ FileUtils.chmod(@args.shift.to_i(8), File.join(cabler.app_root, @args.shift))
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,12 @@
1
+ module Palmade::Cableguy
2
+ class Builders::CableCopy < Cable
3
+ add_as :copy
4
+
5
+ def configure(cabler, cabling, target)
6
+ cabler.say_with_time "copying #{@args.join(' -> ')}" do
7
+ FileUtils.cp(@args.shift, @args.shift)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,12 @@
1
+ module Palmade::Cableguy
2
+ class Builders::CableCustom < Cable
3
+ add_as :custom
4
+
5
+ def configure(cabler, cabling, target)
6
+ unless @block.nil?
7
+ @block.call(self)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,14 @@
1
+ module Palmade::Cableguy
2
+ class Builders::CableMkdir < Cable
3
+ add_as :mkdir
4
+
5
+ def configure(cabler, cabling, target)
6
+ cabler.say_with_time "mkdir #{@args.join(' ')}" do
7
+ @args.each do |path|
8
+ FileUtils.mkdir_p(File.join(cabler.app_root, path))
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,12 @@
1
+ module Palmade::Cableguy
2
+ class Builders::CableMove < Cable
3
+ add_as :move
4
+
5
+ def configure(cabler, cabling, target)
6
+ cabler.say_with_time "moving #{@args.join(' -> ')}" do
7
+ FileUtils.mv(@args.shift, @args.shift)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,15 @@
1
+ module Palmade::Cableguy
2
+ class Builders::CableSymlink < Cable
3
+ add_as :symlink
4
+
5
+ def configure(cabler, cabling, target)
6
+
7
+ source = @args.shift
8
+ destination = @args.shift
9
+ cabler.say_with_time "creating symlink #{source} -> #{destination}" do
10
+ FileUtils.ln_s(source, destination, :force => true)
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,18 @@
1
+ module Palmade::Cableguy
2
+ class Builders::CableTemplate < Cable
3
+ add_as :template
4
+
5
+ def configure(cabler, cabling, target)
6
+ cabler.say_with_time "configuring #{@args[0]}" do
7
+ unless @args[1].nil?
8
+ target_path = File.join(cabler.app_root, @args[1], @args[0])
9
+ else
10
+ target_path = nil
11
+ end
12
+
13
+ install_template(@args[0], cabler, cabling, target, target_path)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,22 @@
1
+ module Palmade::Cableguy
2
+ module Builders
3
+ def self.load_all_builders
4
+ CableTemplate
5
+ CableMkdir
6
+ CableChmod
7
+ CableSymlink
8
+ CableCustom
9
+ CableMove
10
+ CableCopy
11
+ end
12
+
13
+ autoload :CableChmod, File.join(File.dirname(__FILE__), 'builders/cable_chmod')
14
+ autoload :CableCustom, File.join(File.dirname(__FILE__), 'builders/cable_custom')
15
+ autoload :CableMkdir, File.join(File.dirname(__FILE__), 'builders/cable_mkdir')
16
+ autoload :CableSymlink, File.join(File.dirname(__FILE__), 'builders/cable_symlink')
17
+ autoload :CableTemplate, File.join(File.dirname(__FILE__), 'builders/cable_template')
18
+ autoload :CableMove, File.join(File.dirname(__FILE__), 'builders/cable_move')
19
+ autoload :CableCopy, File.join(File.dirname(__FILE__), 'builders/cable_copy')
20
+ end
21
+ end
22
+
@@ -0,0 +1,62 @@
1
+ module Palmade::Cableguy
2
+ class Cable
3
+ attr_reader :args
4
+
5
+ @@builders = { }
6
+ def self.builders
7
+ @@builders
8
+ end
9
+
10
+ def self.add_as(key, klass = nil)
11
+ builders[key] = klass || self
12
+ end
13
+
14
+ def self.build_key(klass = nil)
15
+ builders.index(klass || self)
16
+ end
17
+
18
+ def self.build(what, *args, &block)
19
+ if builders.include?(what)
20
+ builders[what].new(*args, &block)
21
+ else
22
+ raise ArgumentError, "Unknown builder: #{what} -- supports: #{supported_builders.join(', ')}"
23
+ end
24
+ end
25
+
26
+ def self.supported_builders
27
+ builders.keys
28
+ end
29
+
30
+ def initialize(*args, &block)
31
+ @args = args
32
+ @block = block
33
+ end
34
+
35
+ def configure(cabler, cabling, target, &block)
36
+ puts "Not implemented: #{self.class.build_key}"
37
+ end
38
+
39
+ protected
40
+
41
+ def install_template(template_file, cabler, cabling, target, target_path = nil)
42
+ app_root = cabler.app_root
43
+
44
+ template_path = File.join(app_root, DEFAULT_TEMPLATES_PATH, template_file)
45
+ if target_path.nil?
46
+ target_path = File.join(app_root, DEFAULT_TARGET_PATH, template_file)
47
+ end
48
+
49
+ if File.exists?(template_path)
50
+ cabler.say "installing template file: #{template_file}", true
51
+
52
+ tb = TemplateBinding.new(self, cabler, cabling, target)
53
+ tb.install(template_path, target_path)
54
+ else
55
+ raise ArgumentError, "template file #{template_file} not found in #{DEFAULT_TEMPLATES_PATH}"
56
+ end
57
+ end
58
+ end
59
+
60
+ Builders.load_all_builders
61
+ end
62
+
@@ -0,0 +1,32 @@
1
+ module Palmade::Cableguy
2
+ class CableConfigurator < Configurator
3
+ attr_reader :setups
4
+ attr_reader :requires
5
+
6
+ def initialize(*args)
7
+ super
8
+ @setups = [ ]
9
+ end
10
+
11
+ def setup_cabling(*args, &block)
12
+ if block_given?
13
+ update_section(:setup_cabling, *args, &block)
14
+ else
15
+ call_section(:setup_cabling, *args)
16
+ end
17
+ end
18
+
19
+ def requirements(*args, &block)
20
+ if block_given?
21
+ update_section(:requirements, *args, &block)
22
+ else
23
+ call_section(:requirements, *args)
24
+ end
25
+ end
26
+
27
+ def setup(what, *args, &block)
28
+ @setups.push([ what, args, block ])
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,115 @@
1
+ module Palmade::Cableguy
2
+ class Cabler
3
+ include Constants
4
+
5
+ attr_reader :app_root
6
+ attr_reader :builds
7
+ attr_reader :cabling_path
8
+ attr_reader :target
9
+ attr_reader :location
10
+ attr_reader :database
11
+ attr_reader :cabler
12
+ attr_reader :logger
13
+ attr_reader :options
14
+ attr_reader :db_path
15
+ attr_reader :db
16
+ attr_accessor :targets
17
+ attr_accessor :group
18
+
19
+ def initialize(app_root, options)
20
+ @options = options
21
+ @app_root = app_root
22
+ @cabling_path = @options[:path]
23
+ @target = @options[:target]
24
+ @location = @options[:location]
25
+ @builds = nil
26
+ @logger = Logger.new($stdout)
27
+ @targets = [ :development ]
28
+ @db_path = File.join(@cabling_path, DB_DIRECTORY, "#{@target}.#{DB_EXTENSION}")
29
+
30
+ if @options[:verbose]
31
+ @logger.level = Logger::DEBUG
32
+ else
33
+ @logger.level = Logger::WARN
34
+ end
35
+
36
+ @db = Palmade::Cableguy::DB.new(self)
37
+ end
38
+
39
+ def boot
40
+ @database = @db.boot
41
+
42
+ self
43
+ end
44
+
45
+ def migrate
46
+ init_file = File.join(@cabling_path, 'init.rb')
47
+ require init_file if File.exist?(init_file)
48
+
49
+ say_with_time "Migrating..." do
50
+ Migration.new(self).boot
51
+ end
52
+
53
+ say "Done!"
54
+ end
55
+
56
+ def configure
57
+ @configurator = CableConfigurator.new
58
+ @configurator_path = File.join(@app_root, DEFAULT_CABLEGUY_PATH)
59
+
60
+ # checks for config/cableguy.rb
61
+ if File.exists?(@configurator_path)
62
+ @configurator.configure(@configurator_path)
63
+ else
64
+ raise MissingFile, "Required cableguy file (#{@configurator_path}) not found!"
65
+ end
66
+
67
+ check_requirements
68
+
69
+ build_setups.each do |s|
70
+ s.configure(self, @cabling, @target)
71
+ end
72
+ end
73
+
74
+ def check_requirements
75
+ if @configurator.include?(:requirements)
76
+ @configurator.requirements(self, @cabling, @target)
77
+ end
78
+ end
79
+
80
+ def say(message, subitem = false)
81
+ puts "#{subitem ? " ->" : "--"} #{message}"
82
+ end
83
+
84
+ def say_with_time(message)
85
+ say(message)
86
+ result = nil
87
+ time = Benchmark.measure { result = yield }
88
+ say "%.4fs" % time.real, :subitem
89
+ say("#{result} rows", :subitem) if result.is_a?(Integer)
90
+ result
91
+ end
92
+
93
+ def require_cables(path)
94
+ if path =~ /\//
95
+ require(path)
96
+ else
97
+ require(File.join(@app_root, DEFAULT_CABLES_PATH, path))
98
+ end
99
+ end
100
+
101
+ protected
102
+
103
+ def build_setups
104
+ if @builds.nil?
105
+ @configurator.setup_cabling(self, @cabling, @target)
106
+ @builds = @configurator.setups.collect do |s|
107
+ Cable.build(s[0], *s[1], &s[2])
108
+ end
109
+ else
110
+ @builds
111
+ end
112
+ end
113
+ end
114
+ end
115
+
@@ -0,0 +1,113 @@
1
+ module Palmade::Cableguy
2
+ class Configurator
3
+ def self.configure(file_path, *args)
4
+ c = Class.new(self).new(*args)
5
+ c.configure(file_path)
6
+ end
7
+
8
+ def initialize(*args)
9
+ @args = args
10
+ @sections = { }
11
+ end
12
+
13
+ def append_args(*args)
14
+ unless args.empty?
15
+ @args += @args
16
+ end
17
+ end
18
+
19
+ def method_missing(method, *args, &block)
20
+ method = method.to_s
21
+ if method[-1,1] == '='
22
+ # this is a setter
23
+ # create a new accessor
24
+
25
+ var_name = method[0..-2]
26
+ eval = <<EVAL
27
+ def #{var_name}=(val)
28
+ @#{var_name} = val
29
+ end
30
+
31
+ def #{var_name}
32
+ @#{var_name}
33
+ end
34
+ EVAL
35
+ self.class.send(:class_eval, eval, __FILE__, __LINE__)
36
+ self.send(method, *args)
37
+ elsif block_given?
38
+ update_section(method, *args, &block)
39
+
40
+ # create a new section
41
+ eval = <<EVAL
42
+ def #{method}(*args, &block)
43
+ if block_given?
44
+ update_section(#{method}, *args, &block)
45
+ else
46
+ call_section(#{method}, *args)
47
+ end
48
+ end
49
+ EVAL
50
+ self.class.send(:class_eval, eval, __FILE__, __LINE__)
51
+ else
52
+ # generate an error!
53
+ raise ArgumentError, "Section #{method} not defined."
54
+ end
55
+ end
56
+
57
+ def include?(section)
58
+ @sections.include?(prep(section))
59
+ end
60
+ alias :has? :include?
61
+
62
+ def configure(file_path)
63
+ if File.exists?(file_path)
64
+ fcontents = File.read(file_path)
65
+ self.instance_eval(fcontents, file_path)
66
+ else
67
+ raise ArgumentError, "File #{file_path} not found"
68
+ end
69
+ self
70
+ end
71
+
72
+ def call_section(section, *args)
73
+ section = prep(section)
74
+ if @sections.include?(section)
75
+ @sections[section].each do |block|
76
+ block.call(*(args + @args))
77
+ end
78
+ else
79
+ raise ArgumentError, "Section #{section} not defined."
80
+ end
81
+ end
82
+ alias :call :call_section
83
+ alias :include :call_section
84
+
85
+ protected
86
+
87
+ def prep(section)
88
+ section.to_sym
89
+ end
90
+
91
+ def update_section(section, *args, &block)
92
+ return unless block_given?
93
+
94
+ options = args.last.is_a?(Hash) ? args.pop : { }
95
+ command = args.first || :push
96
+
97
+ section = prep(section)
98
+ unless @sections.include?(section)
99
+ @sections[section] = [ ]
100
+ end
101
+
102
+ case command
103
+ when :unshift
104
+ @sections[section].unshift(block)
105
+ when :push
106
+ @sections[section].push(block)
107
+ else
108
+ raise ArgumentError, "Unknown update command: #{command}"
109
+ end
110
+ end
111
+ end
112
+ end
113
+
@@ -0,0 +1,6 @@
1
+ module Palmade::Cableguy
2
+ module Constants
3
+ DB_DIRECTORY = "db"
4
+ DB_EXTENSION = "sqlite3"
5
+ end
6
+ end
@@ -0,0 +1,150 @@
1
+ module Palmade::Cableguy
2
+ class DB
3
+ attr_reader :database
4
+
5
+ def initialize(cabler)
6
+ @cabler = cabler
7
+ @database = nil
8
+ @sql_options = { :logger => @cabler.logger, :sql_log_level => :info }
9
+
10
+ if @cabler.options[:verbose]
11
+ @sql_options[:sql_log_level] = :debug
12
+ end
13
+ end
14
+
15
+ def boot
16
+ @database = Sequel.sqlite(@cabler.db_path, @sql_options)
17
+ @group = ""
18
+ @prefix_stack = []
19
+
20
+ @dataset = @database[:cablingdatas]
21
+ end
22
+
23
+ def final_key(key)
24
+ unless @prefix_stack.empty?
25
+ @prefix_stack.push(key)
26
+ key = nil
27
+ end
28
+
29
+ key ||= @prefix_stack.join('.')
30
+ end
31
+
32
+ def set(key, value, group = nil)
33
+ group ||= @group
34
+ key = final_key(key)
35
+
36
+ @dataset.insert(:key => key, :value => value, :group => group)
37
+
38
+ stack_pop
39
+ end
40
+
41
+ def delete(key, value)
42
+ key = final_key(key)
43
+ @dataset.filter(:key => key).delete
44
+ stack_pop
45
+ end
46
+
47
+ def update(key, value, group = nil)
48
+ group ||= @group
49
+ key = final_key(key)
50
+
51
+ @dataset.filter(:key => key, :group => group).update(:value => value)
52
+
53
+ stack_pop
54
+ end
55
+
56
+ def group(group = nil, &block)
57
+ @group = group
58
+
59
+ @database.transaction do
60
+ yield
61
+ end
62
+ end
63
+
64
+ def globals(&block)
65
+ @group = "globals"
66
+
67
+ @database.transaction do
68
+ yield
69
+ end
70
+ end
71
+
72
+ def prefix(prefix, &block)
73
+ @prefix_stack.push(prefix)
74
+ yield
75
+
76
+ stack_pop
77
+ end
78
+
79
+ def stack_pop
80
+ @prefix_stack.pop
81
+ end
82
+
83
+ def has_key?(key, group)
84
+ group ||= @cabler.group.to_s
85
+
86
+ val = @dataset.where(:key => key, :group => group).count
87
+
88
+ if val == 0
89
+ val = @dataset.where(:key => key, :group => "globals").count
90
+
91
+ val == 0 ? false : true
92
+ else
93
+ true
94
+ end
95
+ end
96
+
97
+ def get(key, group = nil)
98
+ group ||= @cabler.group.to_s
99
+
100
+ val = @dataset.where(:key => key, :group => group)
101
+
102
+ if val.empty?
103
+ val = @dataset.where(:key => key, :group => "globals")
104
+ end
105
+
106
+ if val.count > 0
107
+ val.first[:value]
108
+ else
109
+ raise "key \'#{key}\' cannot be found!"
110
+ end
111
+ end
112
+
113
+ def get_children(key, group = nil)
114
+ group ||= @cabler.group.to_s
115
+ values = []
116
+
117
+ res = @dataset.where(:key.like("#{key}%"), :group => group)
118
+
119
+ if res.empty?
120
+ res = @dataset.where(:key.like("#{key}%"), :group => "globals")
121
+ end
122
+
123
+ key = key.split('.')
124
+
125
+ res.each do |r|
126
+ res_key = r[:key].split('.')
127
+ res_key = (res_key - key).shift
128
+ values.push(res_key)
129
+ end
130
+
131
+ if values.count > 0
132
+ values & values
133
+ else
134
+ raise "no values for \'#{key}\'!"
135
+ end
136
+ end
137
+
138
+ def create_table_if_needed
139
+ if @database.tables.include? :cablingdatas
140
+ @database.drop_table :cablingdatas
141
+ end
142
+
143
+ @database.create_table :cablingdatas do
144
+ String :key
145
+ String :value
146
+ String :group
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,92 @@
1
+ module Palmade::Cableguy
2
+ class Migration
3
+ attr_reader :cabler
4
+ attr_reader :db
5
+
6
+ def initialize(cabler)
7
+ @cabler = cabler
8
+
9
+ @db = @cabler.db
10
+ @cabling_path = @cabler.cabling_path
11
+ @utils = Palmade::Cableguy::Utils
12
+ end
13
+
14
+ def boot
15
+ @db.create_table_if_needed
16
+
17
+ file_stack = []
18
+
19
+ sort_directories.each do |p|
20
+ path = File.join(@cabling_path, p)
21
+
22
+ if p == 'targets'
23
+ if !@cabler.location.nil? && @cabler.location != @cabler.target
24
+ f = File.join(path, "#{@cabler.target}_#{@cabler.location}.rb")
25
+ else
26
+ f = File.join(path, "#{@cabler.target}.rb")
27
+ end
28
+
29
+ if File.exists?(f)
30
+ require f
31
+ file_stack.push(f)
32
+ else
33
+ raise "File #{f} doesn't exist!"
34
+ end
35
+ elsif p == '.'
36
+ if File.exist?(File.join(path, 'custom.rb'))
37
+ f = Dir["#{path}/custom.rb"].shift
38
+ require f
39
+ file_stack.push(f)
40
+ end
41
+ else
42
+ Dir["#{path}/*.rb"].each do |d|
43
+ require d
44
+ file_stack.push(d)
45
+ end
46
+ end
47
+
48
+ file_stack.each do |f|
49
+ class_name = File.basename(f).chomp(".rb")
50
+ camelized_class_name = (@utils.camelize(class_name))
51
+ klass = Palmade::Cableguy::Migrations.const_get(camelized_class_name)
52
+ k = klass.new(@cabler)
53
+ k.migrate!
54
+ end
55
+ file_stack.clear
56
+ end
57
+ end
58
+
59
+ def sort_directories
60
+ dir_stack = ["base", "targets", '.']
61
+ end
62
+
63
+ def migrate!
64
+ raise "class #{self.class.name} doesn't have a migrate method. Override!"
65
+ end
66
+
67
+ def set(key, value, set = nil)
68
+ @db.set(key, value, set)
69
+ end
70
+
71
+ def group(group, &block)
72
+ @group = group
73
+ @db.group(@group, &block)
74
+ end
75
+
76
+ def globals(&block)
77
+ @db.globals(&block)
78
+ end
79
+
80
+ def prefix(prefix, &block)
81
+ @db.prefix(prefix, &block)
82
+ end
83
+
84
+ def delete(key, value = nil)
85
+ @db.delete(key, value)
86
+ end
87
+
88
+ def update(key, value)
89
+ @db.update(key, value)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,44 @@
1
+ module Palmade::Cableguy
2
+ class Runner
3
+ def self.run(app_root, cmd, options)
4
+ if options[:path].nil?
5
+ if ENV.include?("CABLING_PATH")
6
+ options[:path] = ENV["CABLING_PATH"]
7
+ elsif File.exist?(File.expand_path('~/cabling'))
8
+ options[:path] = File.expand_path('~/cabling')
9
+ elsif File.exist?("/var/cabling")
10
+ options[:path] = "/var/cabling"
11
+ elsif File.exist?("/etc/cabling")
12
+ options[:path] = "/etc/cabling"
13
+ else
14
+ raise "You don't seem to have any paths for cabling.\n"
15
+ end
16
+ end
17
+
18
+ if options[:target].nil?
19
+ if ENV.include?("CABLING_TARGET")
20
+ options[:target] = ENV["CABLING_TARGET"]
21
+ else
22
+ options[:target] = 'development'
23
+ end
24
+ end
25
+
26
+ if options[:location].nil?
27
+ if ENV.include?("CABLING_LOCATION")
28
+ options[:location] = ENV["CABLING_LOCATION"]
29
+ else
30
+ options[:location] = options[:target]
31
+ end
32
+ end
33
+
34
+ ca = Cabler.new(app_root, options)
35
+
36
+ case cmd
37
+ when 'migrate'
38
+ ca.boot.migrate
39
+ else
40
+ ca.boot.configure
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,129 @@
1
+ module Palmade::Cableguy
2
+ class TemplateBinding
3
+ attr_reader :cable
4
+ attr_reader :cabler
5
+ attr_reader :cabling
6
+ attr_reader :arg_hash
7
+ attr_reader :target
8
+ attr_reader :location
9
+ attr_reader :db
10
+ attr_reader :reserved_keys
11
+ attr_accessor :output_buffer
12
+
13
+ def initialize(cable, cabler, cabling, target)
14
+ @cable = cable
15
+ @cabler = cabler
16
+ @cabling = cabling
17
+ @target = target
18
+ @location = @cabler.location
19
+ @arg_hash = @cable.args[2]
20
+ @db = @cabler.db
21
+ @key_prefix = []
22
+ @reserved_keys = ['target', 'location']
23
+ end
24
+
25
+ def join_keys(key)
26
+ "#{@key_prefix.join('.')}.#{key}"
27
+ end
28
+
29
+ def has_key?(key, group = nil)
30
+ if !@key_prefix.empty?
31
+ key = join_keys(key)
32
+ end
33
+
34
+ @db.has_key?(key, group)
35
+ end
36
+
37
+ def get(key, group = nil)
38
+ if !@key_prefix.empty?
39
+ key = join_keys(key)
40
+ end
41
+
42
+ @db.get(key, group)
43
+ end
44
+
45
+ def get_children(key, group = nil, &block)
46
+ if block_given?
47
+ fill_key_prefix(key)
48
+
49
+ yield @db.get_children(key, group)
50
+ @key_prefix.clear
51
+ else
52
+ @db.get_children(key, group)
53
+ end
54
+ end
55
+
56
+ def get_each_child(key, group = nil, &block)
57
+ if block_given?
58
+ fill_key_prefix(key)
59
+
60
+ children = @db.get_children(key, group)
61
+
62
+ children.each do |c|
63
+ yield c
64
+ end
65
+
66
+ @key_prefix.clear
67
+ else
68
+ @db.get_children(key, group)
69
+ end
70
+ end
71
+
72
+ def fill_key_prefix(key)
73
+ key.split('.').each do |k|
74
+ @key_prefix << k
75
+ end
76
+ end
77
+
78
+ def parse(file_path)
79
+ Palmade::Cableguy.require_erb
80
+ fcontents = File.read(file_path)
81
+
82
+ parsed = ERB.new(fcontents, nil, "-%>", "@output_buffer").result(binding)
83
+ parsed = special_parse(parsed, [ '{', '}' ], false)
84
+ end
85
+
86
+ def special_parse(parsed, delim = [ '{', '}' ], cabling_only = false)
87
+ delim0 = "\\#{delim[0]}"
88
+ delim1 = "\\#{delim[1]}"
89
+
90
+ parsed = parsed.gsub(/#{delim0}(.+)#{delim1}/) do |match|
91
+ found = $1
92
+
93
+ if @reserved_keys.include?(found)
94
+ eval_ret = self.send(found)
95
+ else
96
+ eval_ret = get(found)
97
+ end
98
+ end
99
+ end
100
+
101
+ def install(source_file, target_file)
102
+ parsed = parse(source_file)
103
+
104
+ File.open(target_file, 'w') do |f|
105
+ f.write(parsed)
106
+ end
107
+ target_file
108
+ end
109
+
110
+ protected
111
+
112
+ def concat(buffer)
113
+ output_buffer.concat(buffer)
114
+ end
115
+
116
+ def capture(*args, &block)
117
+ with_output_buffer { block.call(*args) }
118
+ end
119
+
120
+ def with_output_buffer(buf = '') #:nodoc:
121
+ self.output_buffer, old_buffer = buf, output_buffer
122
+ yield
123
+ output_buffer
124
+ ensure
125
+ self.output_buffer = old_buffer
126
+ end
127
+ end
128
+ end
129
+
@@ -0,0 +1,13 @@
1
+ module Palmade::Cableguy
2
+ class Utils
3
+ # some rails methods
4
+
5
+ def self.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
6
+ if first_letter_in_uppercase
7
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
8
+ else
9
+ lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Palmade
2
+ module Cableguy
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'benchmark'
3
+ require 'fileutils'
4
+ require 'sequel'
5
+ require 'logger'
6
+ require File.join(File.dirname(__FILE__), 'cableguy/version')
7
+
8
+ module Palmade
9
+ module Cableguy
10
+ autoload :Builders, File.join(File.dirname(__FILE__), 'cableguy/builders')
11
+ autoload :Cable, File.join(File.dirname(__FILE__), 'cableguy/cable')
12
+ autoload :Cabler, File.join(File.dirname(__FILE__), 'cableguy/cabler')
13
+ autoload :CableConfigurator, File.join(File.dirname(__FILE__), 'cableguy/cable_configurator')
14
+ autoload :Configurator, File.join(File.dirname(__FILE__), 'cableguy/configurator')
15
+ autoload :Constants, File.join(File.dirname(__FILE__), 'cableguy/constants')
16
+ autoload :DB, File.join(File.dirname(__FILE__), 'cableguy/db')
17
+ autoload :Migration, File.join(File.dirname(__FILE__), 'cableguy/migration')
18
+ autoload :Runner, File.join(File.dirname(__FILE__), 'cableguy/runner')
19
+ autoload :TemplateBinding, File.join(File.dirname(__FILE__), 'cableguy/templatebinding')
20
+ autoload :Utils, File.join(File.dirname(__FILE__), 'cableguy/utils')
21
+
22
+ DEFAULT_CABLEGUY_PATH = "config/cableguy.rb"
23
+ DEFAULT_TEMPLATES_PATH = "config/templates"
24
+ DEFAULT_TARGET_PATH = "config"
25
+
26
+ class CableguyError < StandardError; end
27
+ class NotImplemented < CableguyError; end
28
+ class MissingFile < CableguyError; end
29
+
30
+ def self.check_requirements(app_root)
31
+ boot(app_root).check_requirements
32
+ end
33
+
34
+ def self.configure(app_root)
35
+ boot(app_root).configure
36
+ end
37
+
38
+ def self.require_erb
39
+ require 'erb'
40
+ end
41
+ end
42
+ end
43
+
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cableguy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan Mendoza
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-31 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sqlite3
16
+ requirement: &21819640 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *21819640
25
+ - !ruby/object:Gem::Dependency
26
+ name: sequel
27
+ requirement: &21819220 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *21819220
36
+ description: cableguy
37
+ email:
38
+ - poymode@gmail.com
39
+ executables:
40
+ - cable
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - Manifest
47
+ - README
48
+ - Rakefile
49
+ - bin/cable
50
+ - cableguy.gemspec
51
+ - lib/palmade/cableguy.rb
52
+ - lib/palmade/cableguy/builders.rb
53
+ - lib/palmade/cableguy/builders/cable_chmod.rb
54
+ - lib/palmade/cableguy/builders/cable_copy.rb
55
+ - lib/palmade/cableguy/builders/cable_custom.rb
56
+ - lib/palmade/cableguy/builders/cable_mkdir.rb
57
+ - lib/palmade/cableguy/builders/cable_move.rb
58
+ - lib/palmade/cableguy/builders/cable_symlink.rb
59
+ - lib/palmade/cableguy/builders/cable_template.rb
60
+ - lib/palmade/cableguy/cable.rb
61
+ - lib/palmade/cableguy/cable_configurator.rb
62
+ - lib/palmade/cableguy/cabler.rb
63
+ - lib/palmade/cableguy/configurator.rb
64
+ - lib/palmade/cableguy/constants.rb
65
+ - lib/palmade/cableguy/db.rb
66
+ - lib/palmade/cableguy/migration.rb
67
+ - lib/palmade/cableguy/runner.rb
68
+ - lib/palmade/cableguy/templatebinding.rb
69
+ - lib/palmade/cableguy/utils.rb
70
+ - lib/palmade/cableguy/version.rb
71
+ homepage: https://github.com/poymode/cableguy
72
+ licenses: []
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project: cableguy
91
+ rubygems_version: 1.8.10
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Generate rails configurations from a sqlite key-value storage
95
+ test_files: []