developwithpassion_expander 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/.gitignore +3 -0
  2. data/.rvmrc +1 -0
  3. data/Gemfile +4 -0
  4. data/Guardfile +5 -0
  5. data/Rakefile +1 -0
  6. data/bin/dwp_expand +4 -0
  7. data/develpwithpassion_expander.gemspec +29 -0
  8. data/lib/developwithpassion_expander.rb +32 -0
  9. data/lib/developwithpassion_expander/array.rb +8 -0
  10. data/lib/developwithpassion_expander/array_dsl.rb +34 -0
  11. data/lib/developwithpassion_expander/array_dsl_module_factory.rb +14 -0
  12. data/lib/developwithpassion_expander/array_mutator.rb +12 -0
  13. data/lib/developwithpassion_expander/array_mutator_step.rb +17 -0
  14. data/lib/developwithpassion_expander/array_readable_step.rb +15 -0
  15. data/lib/developwithpassion_expander/array_visitor.rb +12 -0
  16. data/lib/developwithpassion_expander/array_visitor_step.rb +16 -0
  17. data/lib/developwithpassion_expander/array_writeable_step.rb +15 -0
  18. data/lib/developwithpassion_expander/cli_interface.rb +41 -0
  19. data/lib/developwithpassion_expander/copy.rb +29 -0
  20. data/lib/developwithpassion_expander/copy_to_target.rb +13 -0
  21. data/lib/developwithpassion_expander/enumerable_extensions.rb +11 -0
  22. data/lib/developwithpassion_expander/erb_template_file.rb +33 -0
  23. data/lib/developwithpassion_expander/expansion.rb +84 -0
  24. data/lib/developwithpassion_expander/file.rb +32 -0
  25. data/lib/developwithpassion_expander/file_merge.rb +54 -0
  26. data/lib/developwithpassion_expander/kernel.rb +35 -0
  27. data/lib/developwithpassion_expander/log.rb +17 -0
  28. data/lib/developwithpassion_expander/mustache_template_file.rb +11 -0
  29. data/lib/developwithpassion_expander/object_extensions.rb +28 -0
  30. data/lib/developwithpassion_expander/shell.rb +9 -0
  31. data/lib/developwithpassion_expander/shell_action_against_file.rb +12 -0
  32. data/lib/developwithpassion_expander/string.rb +5 -0
  33. data/lib/developwithpassion_expander/template_processors.rb +24 -0
  34. data/lib/developwithpassion_expander/template_visitor.rb +20 -0
  35. data/lib/developwithpassion_expander/version.rb +5 -0
  36. data/spec/spec_helper.rb +77 -0
  37. data/spec/specs/array_dsl_spec.rb +38 -0
  38. data/spec/specs/array_mutator_step_spec.rb +47 -0
  39. data/spec/specs/array_readable_step_spec.rb +26 -0
  40. data/spec/specs/array_spec.rb +22 -0
  41. data/spec/specs/array_visitor_step_spec.rb +78 -0
  42. data/spec/specs/array_writeable_step_spec.rb +30 -0
  43. data/spec/specs/copy_spec.rb +89 -0
  44. data/spec/specs/copy_to_target_spec.rb +24 -0
  45. data/spec/specs/enumerable_extensions_spec.rb +27 -0
  46. data/spec/specs/erb_template_file_spec.rb +65 -0
  47. data/spec/specs/expansion_spec.rb +146 -0
  48. data/spec/specs/file_merge_spec.rb +133 -0
  49. data/spec/specs/file_spec.rb +81 -0
  50. data/spec/specs/kernel_spec.rb +56 -0
  51. data/spec/specs/mustache_template_file_spec.rb +50 -0
  52. data/spec/specs/object_spec.rb +202 -0
  53. data/spec/specs/sample.rb +5 -0
  54. data/spec/specs/shell_action_against_file_spec.rb +24 -0
  55. data/spec/specs/string_spec.rb +13 -0
  56. data/spec/specs/template_processors_spec.rb +64 -0
  57. data/spec/specs/template_visitor_spec.rb +86 -0
  58. metadata +202 -0
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg
2
+ .bundle
3
+ Gemfile.lock
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.3@expander --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/specs/.+_spec\.rb$})
3
+ watch(%r{^lib/developwithpassion_expander/(.+)\.rb$}) { |m| "spec/specs/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/dwp_expand ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'developwithpassion_expander'
4
+ DevelopWithPassion::Expander::CLIInterface.new.run(ARGV)
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib",__FILE__)
3
+ require "developwithpassion_expander/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "developwithpassion_expander"
7
+ s.version = DevelopWithPassion::Expander::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Develop With Passion®"]
10
+ s.email = ["open_source@developwithpassion.com"]
11
+ s.homepage = "http://www.developwithpassion.com"
12
+ s.summary = %q{Simple Expansion Automation Utility}
13
+ s.description = %q{Automation utitlity that I use to support cross platform file maintenance}
14
+ s.rubyforge_project = "developwithpassion_expander"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "guard"
24
+ s.add_development_dependency "guard-rspec"
25
+ s.add_development_dependency "developwithpassion_fakes"
26
+ s.add_development_dependency "rb-notifu"
27
+ s.add_runtime_dependency "configatron"
28
+ s.add_runtime_dependency "mustache"
29
+ end
@@ -0,0 +1,32 @@
1
+ require 'configatron'
2
+ require 'fileutils'
3
+ require 'erb'
4
+ require 'singleton'
5
+ require 'mustache'
6
+
7
+ require 'developwithpassion_expander/array'
8
+ require 'developwithpassion_expander/array_dsl'
9
+ require 'developwithpassion_expander/array_dsl_module_factory'
10
+ require 'developwithpassion_expander/array_mutator'
11
+ require 'developwithpassion_expander/array_mutator_step'
12
+ require 'developwithpassion_expander/array_readable_step'
13
+ require 'developwithpassion_expander/array_visitor'
14
+ require 'developwithpassion_expander/array_visitor_step'
15
+ require 'developwithpassion_expander/array_writeable_step'
16
+ require 'developwithpassion_expander/cli_interface'
17
+ require 'developwithpassion_expander/copy'
18
+ require 'developwithpassion_expander/copy_to_target'
19
+ require 'developwithpassion_expander/enumerable_extensions'
20
+ require 'developwithpassion_expander/erb_template_file'
21
+ require 'developwithpassion_expander/expansion'
22
+ require 'developwithpassion_expander/file'
23
+ require 'developwithpassion_expander/file_merge'
24
+ require 'developwithpassion_expander/kernel'
25
+ require 'developwithpassion_expander/log'
26
+ require 'developwithpassion_expander/mustache_template_file'
27
+ require 'developwithpassion_expander/object_extensions'
28
+ require 'developwithpassion_expander/shell'
29
+ require 'developwithpassion_expander/shell_action_against_file'
30
+ require 'developwithpassion_expander/string'
31
+ require 'developwithpassion_expander/template_processors'
32
+ require 'developwithpassion_expander/template_visitor'
@@ -0,0 +1,8 @@
1
+ class Array
2
+ def as_home_files
3
+ self.map{|item| item.as_home_file}
4
+ end
5
+ def as_glob_pattern
6
+ File.join(self)
7
+ end
8
+ end
@@ -0,0 +1,34 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class ArrayDSL
4
+ attr_accessor :mutators,:visitors,:name,:writable,:readable
5
+
6
+ def initialize(name)
7
+ @name = name
8
+ initialize_arrays :mutators,:visitors
9
+ initialize_false :writable,:readable
10
+ end
11
+
12
+ def mutator(value,&block)
13
+ @mutators.push(ArrayMutator.new(value,block))
14
+ end
15
+
16
+ def process_using(name,visitor)
17
+ @visitors.push(ArrayVisitor.new(name,visitor))
18
+ end
19
+
20
+ def read_and_write
21
+ writable
22
+ readable
23
+ end
24
+
25
+ def writable
26
+ @writable = true
27
+ end
28
+
29
+ def readable
30
+ @readable = true
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,14 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class ArrayDSLModuleFactory
4
+ def create_modules_from(dsl)
5
+ return [
6
+ ArrayMutatorStep.new.run_using(dsl),
7
+ ArrayVisitorStep.new.run_using(dsl),
8
+ ArrayWriteableStep.new.run_using(dsl),
9
+ ArrayReadableStep.new.run_using(dsl)
10
+ ]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class ArrayMutator
4
+ attr_accessor :name,:block
5
+
6
+ def initialize(name,block)
7
+ @name = name
8
+ @block = block
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class ArrayMutatorStep
4
+ def run_using(builder)
5
+ Module.new do
6
+ builder.mutators.each do|mutator|
7
+ define_method(mutator.name) do|value|
8
+ array_var = instance_variable_get("@#{builder.name}")
9
+ array_var.push(value) unless mutator.block
10
+ mutator.block.call(value) if mutator.block
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class ArrayReadableStep
4
+ def run_using(builder)
5
+ Module.new do
6
+ if (builder.readable)
7
+ define_method(builder.name) do
8
+ return instance_variable_get("@#{builder.name}")
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class ArrayVisitor
4
+ attr_accessor :name,:visitor
5
+
6
+ def initialize(name,visitor)
7
+ @name = name
8
+ @visitor = visitor
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class ArrayVisitorStep
4
+ def run_using(builder)
5
+ Module.new do
6
+ builder.visitors.each do|visitor|
7
+ define_method(visitor.name) do
8
+ array_var = instance_variable_get("@#{builder.name}")
9
+ array_var.each{|item| visitor.visitor.respond_to?(:run_using) ? visitor.visitor.run_using(item) : item.send(visitor.visitor)}
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class ArrayWriteableStep
4
+ def run_using(builder)
5
+ Module.new do
6
+ if (builder.writable)
7
+ define_method("#{builder.name}=") do|value|
8
+ instance_variable_set("@#{builder.name}",value)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class CLIInterface
4
+ def startup
5
+ [
6
+ TemplateProcessors,
7
+ TemplateVisitor,
8
+ Expansion,
9
+ Shell
10
+ ].each{|item| item.send(:include,Singleton)}
11
+
12
+ TemplateProcessors.instance.register_processor(:erb,ERBTemplateFile.new)
13
+ TemplateProcessors.instance.register_processor(:mustache,MustacheTemplateFile.new)
14
+ end
15
+
16
+ def get_expansion_file_name(args)
17
+ return args.count > 0 ? args[0] : "Expansionfile"
18
+ end
19
+
20
+ def ensure_expansion_file_provided(file)
21
+ unless File.exist?(file)
22
+ print <<-prompt
23
+ dwp_expand aborted!
24
+ No Expansionfile found (looking for: Expansionfile)
25
+ prompt
26
+ exit
27
+ end
28
+ end
29
+
30
+ def run(args = [])
31
+ file_to_run = get_expansion_file_name(args)
32
+ ensure_expansion_file_provided file_to_run
33
+ startup
34
+ enable_logging
35
+ log "Running Expansions defined in file:#{file_to_run}"
36
+ load file_to_run
37
+ Expansion.instance.run
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,29 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class Copy
4
+ def initialize(copy_target)
5
+ array :sources do|a|
6
+ a.read_and_write
7
+ a.mutator :folder do|item|
8
+ register(item,true)
9
+ end
10
+ a.mutator :contents do|item|
11
+ register(item,false)
12
+ end
13
+ a.mutator :all_contents_in do|set_of_folders|
14
+ set_of_folders.each{|item| contents(item)}
15
+ end
16
+ a.mutator :all_folders_in do|set_of_folders|
17
+ set_of_folders.each{|item| folder(item)}
18
+ end
19
+ a.process_using :run, copy_target
20
+ end
21
+ end
22
+
23
+ :private
24
+ def register(folder,copy_containing_folder = false)
25
+ @sources.push(copy_containing_folder ? folder : File.join(folder,'.'))
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class CopyToTarget
4
+ def initialize(target)
5
+ @target = target
6
+ end
7
+
8
+ def run_using(source_item)
9
+ Shell.instance.run("cp -rf #{source_item} #{@target}")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Enumerable
2
+ def process_all_items_using(visitor)
3
+ self.each{|item| visitor.run_using(item)}
4
+ end
5
+ end
6
+
7
+ class Hash
8
+ def process_all_values_using(&block)
9
+ self.values.each{|item| yield item}
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class ERBTemplateFile
4
+ def prepare_template(template)
5
+ token_regex = /(@\w[\w\.\_]+\w@)/
6
+
7
+ hits = template.scan(token_regex)
8
+ tags = hits.map do |item|
9
+ item[0].gsub(/@/,'').squeeze
10
+ end
11
+ tags = tags.map{|tag| tag.to_sym}.uniq
12
+
13
+ tags.inject(template) do |text, tag|
14
+ text.gsub /@#{tag.to_s}@/, "<%= #{tag.to_s} %>"
15
+ end
16
+ end
17
+
18
+ def process_template(template,binding)
19
+ erb = ERB.new(template, 0, "%")
20
+ erb.result(binding)
21
+ end
22
+
23
+ def process(args)
24
+ template = prepare_template(File.read(args[:input]))
25
+ result = process_template(template,args[:binding])
26
+
27
+ File.open_for_write(args[:output]) do|file|
28
+ file.write(result)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,84 @@
1
+ module DevelopWithPassion
2
+ module Expander
3
+ class Expansion
4
+ attr_accessor :copies,:files_to_merge
5
+
6
+ def initialize
7
+ initialize_hashes :files_to_merge,:copies
8
+
9
+ array :cleanup_items do|a|
10
+ a.readable
11
+ a.process_using :run_cleanup,:call
12
+ end
13
+
14
+ array :setup_items do|a|
15
+ a.readable
16
+ a.process_using :run_setup_items,:call
17
+ end
18
+
19
+ array :executable_files do|a|
20
+ a.mutator :register_executable
21
+ a.process_using :mark_files_executable,ShellActionAgainstFile.new("chmod +x")
22
+ end
23
+
24
+ array :templates do|a|
25
+ a.mutator :look_for_templates_in do|path|
26
+ @templates.concat(glob(path))
27
+ end
28
+ a.process_using :expand_templates,TemplateVisitor.instance
29
+ end
30
+
31
+ array :files_with_line_endings_to_fix do|a|
32
+ a.mutator :fix_line_endings_for
33
+ a.process_using :fix_line_endings,ShellActionAgainstFile.new("d2u")
34
+ end
35
+
36
+ array :folders_to_purge do|a|
37
+ a.mutator :register_folder_to_purge
38
+ a.process_using :purge_targets,ShellActionAgainstFile.new("rm -rf")
39
+ end
40
+ end
41
+
42
+ def copy_to target,&block
43
+ hash_process(target,@copies,lambda{|key| return Copy.new(CopyToTarget.new(key))},&block)
44
+ end
45
+
46
+ def merge_to target,&block
47
+ hash_process(target,@files_to_merge,lambda{|key| return FileMerge.new(key)},&block)
48
+ end
49
+
50
+
51
+ def cleanup(&block)
52
+ @cleanup_items << block
53
+ end
54
+
55
+ def before(&block)
56
+ @setup_items << block
57
+ end
58
+
59
+ def run
60
+ run_setup_items
61
+ purge_targets
62
+ expand_templates
63
+ expand_items
64
+ merge_items
65
+ fix_line_endings
66
+ mark_files_executable
67
+ run_cleanup
68
+ end
69
+
70
+ :private
71
+ def hash_process(target,hash,factory,&block)
72
+ symbol = target.to_sym
73
+ hash[symbol] = factory.call(target) unless hash.has_key?(symbol)
74
+ hash[symbol].instance_eval(&block)
75
+ end
76
+ def expand_items
77
+ @copies.process_all_values_using{|copy| copy.run}
78
+ end
79
+ def merge_items
80
+ @files_to_merge.process_all_values_using{|merge| merge.run}
81
+ end
82
+ end
83
+ end
84
+ end