automatthew-genitor 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
File without changes
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008 Matthew King
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,16 @@
1
+ CHANGELOG
2
+ genitor.gemspec
3
+ lib/genitor/polite_file.rb
4
+ lib/genitor.rb
5
+ LICENSE
6
+ Manifest
7
+ README
8
+ test/helper.rb
9
+ test/test_genitor.rb
10
+ test/test_polite_file.rb
11
+ test/testdata/app/alpha/beta/five.erb
12
+ test/testdata/app/four.erb
13
+ test/testdata/app/one
14
+ test/testdata/app/six.textile
15
+ test/testdata/app/three.rb
16
+ test/testdata/app/two.txt
data/README ADDED
@@ -0,0 +1,21 @@
1
+ = Genitor
2
+
3
+ Genitor is a Rake extension for generating and updating projects from templates. Give Genitor a source and a target, and it creates a rake task that will create the necessary directories and copy the template files to the target. Genitor processes .erb files by default, but you can register a template processing lambda for any file extension. Because Genitor is Rake-based, you can add your own dependencies and actions to any target file.
4
+
5
+ == Usage
6
+
7
+ generator = Genitor.new("generate:app") do |gen|
8
+ gen.source = "templates/application"
9
+ gen.target = "/Users/matthew/dev/waves/thingy"
10
+ gen.excludes << "**/*.yaml"
11
+ gen.template_assigns = {:application_name => "thingy"}
12
+ file gen.target("bin/console") do
13
+ system "chmod +x #{gen.target("bin/console")}"
14
+ end
15
+ end
16
+
17
+ This will define a task named, naturally enough, "generate:app". You can invoke it in the usual way, with <tt>rake generate_app</tt>, or you can call it from Ruby:
18
+
19
+ Rake::Task["generate:app"].invoke
20
+
21
+ At the moment, Genitor happily clobbers any copyable files with mod dates older than the associated template file. Files produced by template processing always get clobbered. Soon to come is a more courteous file task that asks if you want to clobber or skip.
data/genitor.gemspec ADDED
@@ -0,0 +1,55 @@
1
+
2
+ # Gem::Specification for Genitor-0.6.0
3
+ # Originally generated by Echoe
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{genitor}
7
+ s.version = "0.6.0"
8
+
9
+ s.specification_version = 2 if s.respond_to? :specification_version=
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Matthew King"]
13
+ s.date = %q{2008-05-12}
14
+ s.description = %q{Generation and updation of projects from templates. Rake-powered, for sustainable blah blah.}
15
+ s.email = %q{automatthew@gmail.com}
16
+ s.files = ["CHANGELOG", "genitor.gemspec", "lib/genitor/polite_file.rb", "lib/genitor.rb", "LICENSE", "Manifest", "README", "test/helper.rb", "test/test_genitor.rb", "test/test_polite_file.rb", "test/testdata/app/alpha/beta/five.erb", "test/testdata/app/four.erb", "test/testdata/app/one", "test/testdata/app/six.textile", "test/testdata/app/three.rb", "test/testdata/app/two.txt"]
17
+ s.has_rdoc = true
18
+ s.homepage = %q{}
19
+ s.require_paths = ["lib"]
20
+ s.rubyforge_project = %q{genitor}
21
+ s.rubygems_version = %q{1.1.1}
22
+ s.summary = %q{Generation and updation of projects from templates. Rake-powered, for sustainable blah blah.}
23
+ s.test_files = ["test/test_genitor.rb", "test/test_polite_file.rb"]
24
+
25
+ s.add_dependency(%q<rake>, [">= 0"])
26
+ s.add_dependency(%q<erubis>, [">= 0"])
27
+ end
28
+
29
+
30
+ # # Original Rakefile source (requires the Echoe gem):
31
+ #
32
+ # %w{rubygems}.each do |dep|
33
+ # require dep
34
+ # end
35
+ #
36
+ # Version = '0.6.0'
37
+ #
38
+ # task :default => [:test]
39
+ #
40
+ # begin
41
+ # gem 'echoe', '>=2.7'
42
+ # require 'echoe'
43
+ # Echoe.new('genitor', Version) do |p|
44
+ # p.project = 'genitor'
45
+ # p.summary = "Generation and updation of projects from templates. Rake-powered, for sustainable blah blah."
46
+ # p.author = "Matthew King"
47
+ # p.email = "automatthew@gmail.com"
48
+ # p.ignore_pattern = /^(\.git).+/
49
+ # p.test_pattern = "test/test_*.rb"
50
+ # p.dependencies << "rake"
51
+ # p.dependencies << "erubis"
52
+ # end
53
+ # rescue
54
+ # "(ignored echoe gemification, as you don't have the Right Stuff)"
55
+ # end
@@ -0,0 +1,13 @@
1
+ class PoliteFileTask < Rake::FileTask
2
+
3
+ # Is this file task needed? Yes if doesn't exist (or different content, TBI)
4
+ def needed?
5
+ return true unless File.exist?(name)
6
+ return confirm?
7
+ end
8
+
9
+ def confirm?
10
+ # will use highline stuff
11
+ end
12
+
13
+ end
data/lib/genitor.rb ADDED
@@ -0,0 +1,113 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/tasklib'
4
+ require 'genitor/polite_file'
5
+ require 'erubis'
6
+
7
+ class Genitor < Rake::TaskLib
8
+
9
+ # Name of the primary Rake task
10
+ attr_accessor :name
11
+
12
+ # Directory to use as source
13
+ attr_writer :source
14
+
15
+ # Directory to use as target. Not required to exist.
16
+ attr_writer :target
17
+
18
+ # List of directories that need to be created
19
+ attr_reader :directories
20
+
21
+ # List of files to be copied
22
+ attr_accessor :copy_files
23
+
24
+ # List of files to be processed (using, e.g. Erubis)
25
+ attr_accessor :template_files
26
+
27
+ # Hash of extension => lambda
28
+ attr_accessor :template_processors
29
+
30
+ # variables for use in template processing
31
+ attr_accessor :template_assigns
32
+
33
+ attr_accessor :excludes
34
+
35
+ # Create a Genitor task named <em>task_name</em>. Default task name is +app+.
36
+ def initialize(name=:app)
37
+ @name = name
38
+ @excludes = []
39
+ @executables = []
40
+ @template_processors = {}
41
+ @template_processors["erb"] = lambda do |source_file, target_file|
42
+ require 'erubis'
43
+ File.open(target_file, 'w') do |trg|
44
+ File.open(source_file, 'r') do |src|
45
+ trg.print Erubis::Eruby.new(src.read).evaluate(template_assigns)
46
+ end
47
+ end
48
+ end
49
+
50
+ yield self # if block_given?
51
+
52
+ Dir.chdir(@source) do
53
+ @files = Rake::FileList.new("**/*").exclude(*@excludes).to_a
54
+ @directories = Rake::FileList.new("**/").map { |f| f.chomp("/") }.to_a
55
+ end
56
+
57
+
58
+ @template_files = @template_processors.inject([]) do |tfiles, processor|
59
+ ext = processor[0]
60
+ tfiles + @files.select { |f| f =~ /\.#{ext}$/ }
61
+ end
62
+ @copy_files = @files - @directories - @template_files
63
+
64
+ define
65
+ end
66
+
67
+
68
+ def polite_file(args, &block)
69
+ PoliteFileTask.define_task(args, &block)
70
+ end
71
+
72
+ # If given a path, joins it to @source. Otherwise returns @source
73
+ def source(path=nil)
74
+ path ? File.join(@source, path) : @source
75
+ end
76
+
77
+ # If given a path, joins it to @targe. Otherwise returns @target
78
+ def target(path=nil)
79
+ path ? File.join(@target, path) : @target
80
+ end
81
+
82
+ # Define the necessary Genitor tasks
83
+ def define
84
+ desc "Create or update project using Genitor"
85
+ task name => @files.map { |f| target(f) }
86
+
87
+ # default is namespace(:app)
88
+ namespace name do
89
+
90
+ @copy_files.each do |file|
91
+ source_file, target_file = source(file), target(file)
92
+ file target_file => source_file do
93
+ cp source_file, target_file
94
+ end
95
+ end
96
+
97
+ # Define a rule for each template extension. Rake rules are
98
+ # only used when no other task for a file is defined.
99
+ @template_processors.each do |ext, block|
100
+ rule ext do |t|
101
+ block.call(source(t.name.sub(@target, "")), t.name.chomp(".#{ext}"))
102
+ end
103
+ end
104
+
105
+ @directories.each do |file|
106
+ source_file, target_file = source(file), target(file)
107
+ directory(target_file)
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ %w( rubygems test/spec mocha English ).each { |f| require f }
2
+ require 'redgreen' if ENV['TM_FILENAME'].nil?
3
+
4
+ $:.unshift File.dirname(__FILE__) + '/../lib'
5
+ require 'genitor'
6
+
7
+ TEST_DIR = File.join(File.dirname(__FILE__), "testdata")
8
+ TEST_APP = File.join(TEST_DIR, "app")
9
+ COPY_FILE = File.join(TEST_DIR, "copy")
10
+ CREATE_FILE = File.join(TEST_DIR, "create_only")
11
+ TEMPLATE_FILE = File.join(TEST_DIR, "template.erb")
@@ -0,0 +1,77 @@
1
+ require File.join(File.dirname(__FILE__), "helper")
2
+
3
+ context "Simple genitor" do
4
+
5
+ before(:each) do
6
+ @target = File.join(TEST_DIR, "app_target")
7
+ @generator = Genitor.new("waves:app") do |gen|
8
+ gen.source = TEST_APP
9
+ gen.target = @target
10
+ gen.excludes << "**/two.*"
11
+ gen.template_assigns = {:verb => "jumped"}
12
+ end
13
+
14
+ @tasks = Rake.application.tasks.map { |t| t.name }
15
+
16
+ @directories = %w{
17
+ alpha
18
+ alpha/beta
19
+ alpha/gamma
20
+ }
21
+
22
+ @copy_files = %w{
23
+ one
24
+ six.textile
25
+ three.rb
26
+ }
27
+
28
+ @template_files = %w{
29
+ alpha/beta/five.erb
30
+ four.erb
31
+ }
32
+
33
+ end
34
+
35
+ after(:each) do
36
+ rm_r @target if File.exist?(@target)
37
+ end
38
+
39
+ specify "should have all directories in the directories list" do
40
+ @generator.directories.to_a.should == @directories
41
+ end
42
+
43
+ specify "should have all non-erb files in the copy list" do
44
+ @generator.copy_files.to_a.should == @copy_files
45
+ end
46
+
47
+ specify "should have all .erb files in the template list" do
48
+ @generator.template_files.should == @template_files
49
+ end
50
+
51
+ specify "should have an empty excludes list" do
52
+ @generator.excludes.should == ["**/two.*"]
53
+ end
54
+
55
+ specify "should have a working erb template_processor" do
56
+ @generator.template_processors["erb"].should.respond_to :call
57
+ @generator.template_processors["erb"].call("#{TEST_APP}/four.erb", "#{TEST_DIR}/catch.txt")
58
+ File.open("#{TEST_DIR}/catch.txt", "r").read.should == "Yossarian jumped."
59
+ rm "#{TEST_DIR}/catch.txt"
60
+ end
61
+
62
+ specify "should copy or process all files and directories" do
63
+ Rake::Task["waves:app"].invoke
64
+ @copy_files.each do |f|
65
+ assert File.exist?(File.join(@target, f))
66
+ end
67
+ @directories.each do |dir|
68
+ assert File.directory?(File.join(@target, dir))
69
+ end
70
+ @template_files.each do |tf|
71
+ assert File.exist?(File.join(@target, tf.chomp(".erb")))
72
+ end
73
+ File.open(File.join(@target, "four"), "r").read.should == "Yossarian jumped."
74
+ end
75
+
76
+
77
+ end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), "helper")
2
+
3
+ context "A file_copy task" do
4
+
5
+ before(:all) do
6
+ File.open(COPY_FILE, "w") { |f| f.print "one" }
7
+ end
8
+
9
+ before(:each) do
10
+ @target = File.join(TEST_DIR, "copy_target")
11
+
12
+ @generator = Genitor.new do |gen|
13
+ gen.source = "/tmp"
14
+ gen.target = @target
15
+
16
+ gen.polite_file @target do
17
+ cp COPY_FILE, @target
18
+ end
19
+
20
+ end
21
+ end
22
+
23
+ after(:each) do
24
+ rm @target if File.exist?(@target)
25
+ end
26
+
27
+ after(:all) do
28
+ rm COPY_FILE if File.exist?(COPY_FILE)
29
+ end
30
+
31
+
32
+ specify "should be needed when the file does not exist" do
33
+ Rake::Task[@target].needed?.should == true
34
+ end
35
+
36
+ specify "should ask if file exists (no idea how to test Highline stuff)" do
37
+ File.open(@target, "w") { |f| f.print "two" }
38
+ # Rake::Task[@target].needed?.should == true
39
+ end
40
+
41
+
42
+
43
+ end
@@ -0,0 +1 @@
1
+ <%= Time.now %>
@@ -0,0 +1 @@
1
+ Yossarian <%= @verb %>.
File without changes
File without changes
File without changes
File without changes
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: automatthew-genitor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew King
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-12 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: erubis
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0"
32
+ version:
33
+ description: Generation and updation of projects from templates. Rake-powered, for sustainable blah blah.
34
+ email: automatthew@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - CHANGELOG
43
+ - genitor.gemspec
44
+ - lib/genitor/polite_file.rb
45
+ - lib/genitor.rb
46
+ - LICENSE
47
+ - Manifest
48
+ - README
49
+ - test/helper.rb
50
+ - test/test_genitor.rb
51
+ - test/test_polite_file.rb
52
+ - test/testdata/app/alpha/beta/five.erb
53
+ - test/testdata/app/four.erb
54
+ - test/testdata/app/one
55
+ - test/testdata/app/six.textile
56
+ - test/testdata/app/three.rb
57
+ - test/testdata/app/two.txt
58
+ has_rdoc: true
59
+ homepage: ""
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ requirements: []
78
+
79
+ rubyforge_project: genitor
80
+ rubygems_version: 1.0.1
81
+ signing_key:
82
+ specification_version: 2
83
+ summary: Generation and updation of projects from templates. Rake-powered, for sustainable blah blah.
84
+ test_files:
85
+ - test/test_genitor.rb
86
+ - test/test_polite_file.rb