automatthew-rakegen 0.6.3

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,15 @@
1
+ CHANGELOG
2
+ lib/rakegen/polite_file.rb
3
+ lib/rakegen.rb
4
+ LICENSE
5
+ README
6
+ test/helper.rb
7
+ test/test_polite_file.rb
8
+ test/test_rakegen.rb
9
+ test/testdata/app/alpha/beta/five.erb
10
+ test/testdata/app/four.erb
11
+ test/testdata/app/one
12
+ test/testdata/app/six.textile
13
+ test/testdata/app/three.rb
14
+ test/testdata/app/two.txt
15
+ Manifest
data/README ADDED
@@ -0,0 +1,19 @@
1
+ = Rakegen
2
+
3
+ Rakegen is a Rake extension for generating and updating projects from templates. Give Rakegen a source and a target, and it creates a rake task that will make the necessary directories and copy the template files to the target. Rakegen processes .erb files by default, but you can register a template processing lambda for any file extension. Because Rakegen is Rake-based, you can add your own dependencies and actions to any target file.
4
+
5
+ == Usage
6
+
7
+ generator = Rakegen.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
+ gen.executables = %w{ bin/waves-console bin/waves-server }
13
+ end
14
+
15
+ 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:
16
+
17
+ Rake::Task["generate:app"].invoke
18
+
19
+ At the moment, Rakegen 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/lib/rakegen.rb ADDED
@@ -0,0 +1,134 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/tasklib'
4
+ require 'rakegen/polite_file'
5
+ require 'erubis'
6
+ require "highline/import"
7
+
8
+ class Rakegen < Rake::TaskLib
9
+
10
+ # Name of the primary Rake task
11
+ attr_accessor :name
12
+
13
+ attr_accessor :description
14
+
15
+ # Directory to use as source
16
+ attr_writer :source
17
+
18
+ # Directory to use as target. Not required to exist.
19
+ attr_writer :target
20
+
21
+ # List of directories that need to be created
22
+ attr_reader :directories
23
+
24
+ # List of files to be copied
25
+ attr_accessor :copy_files
26
+ attr_accessor :all_files
27
+ attr_accessor :files
28
+
29
+ # List of files to be processed (using, e.g. Erubis)
30
+ attr_accessor :template_files
31
+
32
+ # Hash of extension => lambda
33
+ attr_accessor :template_processors
34
+
35
+ # variables for use in template processing
36
+ attr_accessor :template_assigns
37
+
38
+ attr_accessor :excludes
39
+
40
+ attr_accessor :executables
41
+
42
+ # Create a Rakegen task named <em>task_name</em>. Default task name is +app+.
43
+ def initialize(name=:app)
44
+ @name = name
45
+ @excludes = []
46
+ @executables = []
47
+ @template_processors = {}
48
+ @template_processors["erb"] = lambda do |source_file, target_file|
49
+ require 'erubis'
50
+ File.open(target_file, 'w') do |trg|
51
+ File.open(source_file, 'r') do |src|
52
+ trg.print Erubis::Eruby.new(src.read).evaluate(template_assigns)
53
+ end
54
+ end
55
+ end
56
+
57
+ yield self # if block_given?
58
+
59
+ Dir.chdir(@source) do
60
+ @files = Rake::FileList.new("**/*").exclude(*@excludes).to_a
61
+ @directories = Rake::FileList.new("**/").map { |f| f.chomp("/") }.to_a
62
+ end
63
+
64
+
65
+ @template_files = @template_processors.inject([]) do |tfiles, processor|
66
+ ext = processor[0]
67
+ tfiles + @files.select { |f| f =~ /\.#{ext}$/ }
68
+ end
69
+ @copy_files = @files - @directories - @template_files
70
+
71
+ @all_files = @files - @template_files + @template_files.map { |f| f.chomp(File.extname(f)) }
72
+
73
+ @target_executables = @executables.map { |f| target(f) }
74
+
75
+ define
76
+ end
77
+
78
+
79
+ def polite_file(args, &block)
80
+ PoliteFileTask.define_task(args, &block)
81
+ end
82
+
83
+ # If given a path, joins it to @source. Otherwise returns @source
84
+ def source(path=nil)
85
+ path ? File.join(@source, path) : @source
86
+ end
87
+
88
+ # If given a path, joins it to @targe. Otherwise returns @target
89
+ def target(path=nil)
90
+ path ? File.join(@target, path) : @target
91
+ end
92
+
93
+ # Define the necessary Rakegen tasks
94
+ def define
95
+ desc "Create or update project using Rakegen"
96
+ task name => @all_files.map { |f| target(f) }
97
+
98
+ # default is namespace(:app)
99
+ namespace name do
100
+
101
+ @copy_files.each do |cf|
102
+ source_file, target_file = source(cf), target(cf)
103
+ polite_file target_file => source_file do
104
+ cp source_file, target_file
105
+ end
106
+ end
107
+
108
+ @template_files.each do |tf|
109
+ extension = File.extname(tf)
110
+ source_file, target_file = source(tf), target(tf.chomp(extension))
111
+ block = @template_processors[extension[1..-1]]
112
+ polite_file target_file do
113
+ block.call(source_file, target_file)
114
+ end
115
+ end
116
+
117
+ @directories.each do |file|
118
+ source_file, target_file = source(file), target(file)
119
+ directory(target_file)
120
+ end
121
+
122
+ unless RUBY_PLATFORM =~ /mswin32/
123
+ @target_executables.each do |executable|
124
+ file executable do |task|
125
+ system "chmod +x #{task.name}"
126
+ end
127
+ end
128
+ end
129
+
130
+ end
131
+
132
+ end
133
+
134
+ end
@@ -0,0 +1,15 @@
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
+ agree("Overwrite '#{name}' ?", false)
11
+ true
12
+ # will use highline stuff
13
+ end
14
+
15
+ end
data/rakegen.gemspec ADDED
@@ -0,0 +1,55 @@
1
+
2
+ # Gem::Specification for Rakegen-0.6.3
3
+ # Originally generated by Echoe
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{rakegen}
7
+ s.version = "0.6.3"
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-14}
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", "lib/rakegen/polite_file.rb", "lib/rakegen.rb", "LICENSE", "README", "test/helper.rb", "test/test_polite_file.rb", "test/test_rakegen.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", "Manifest", "rakegen.gemspec"]
17
+ s.has_rdoc = true
18
+ s.homepage = %q{}
19
+ s.require_paths = ["lib"]
20
+ s.rubyforge_project = %q{rakegen}
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_polite_file.rb", "test/test_rakegen.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.3'
37
+ #
38
+ # task :default => [:test]
39
+ #
40
+ # begin
41
+ # gem 'echoe', '>=2.7'
42
+ # require 'echoe'
43
+ # Echoe.new('rakegen', Version) do |p|
44
+ # p.project = 'rakegen'
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
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 'rakegen'
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,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 = Rakegen.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,84 @@
1
+ require File.join(File.dirname(__FILE__), "helper")
2
+
3
+ context "Simple rakegen" do
4
+
5
+ before(:each) do
6
+ @target = File.join(TEST_DIR, "app_target")
7
+ @generator = Rakegen.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
+ gen.executables << "one"
13
+ end
14
+
15
+ @tasks = Rake.application.tasks.map { |t| t.name }
16
+
17
+ @directories = %w{
18
+ alpha
19
+ alpha/beta
20
+ alpha/gamma
21
+ }
22
+
23
+ @copy_files = %w{
24
+ one
25
+ six.textile
26
+ three.rb
27
+ }
28
+
29
+ @template_files = %w{
30
+ alpha/beta/five.erb
31
+ four.erb
32
+ }
33
+
34
+ end
35
+
36
+ after(:each) do
37
+ rm_r @target if File.exist?(@target)
38
+ end
39
+
40
+ specify "should have all directories in the directories list" do
41
+ @generator.directories.to_a.should == @directories
42
+ # @generator.template_files.should == 1
43
+ end
44
+
45
+ specify "should have all non-erb files in the copy list" do
46
+ @generator.copy_files.to_a.should == @copy_files
47
+ end
48
+
49
+ specify "should have all .erb files in the template list" do
50
+ @generator.template_files.should == @template_files
51
+ end
52
+
53
+ specify "should have an empty excludes list" do
54
+ @generator.excludes.should == ["**/two.*"]
55
+ end
56
+
57
+ specify "should have an executables list" do
58
+ @generator.executables.should == ["one"]
59
+ end
60
+
61
+ specify "should have a working erb template_processor" do
62
+ @generator.template_processors["erb"].should.respond_to :call
63
+ @generator.template_processors["erb"].call("#{TEST_APP}/four.erb", "#{TEST_DIR}/catch.txt")
64
+ File.open("#{TEST_DIR}/catch.txt", "r").read.should == "Yossarian jumped."
65
+ rm "#{TEST_DIR}/catch.txt"
66
+ end
67
+
68
+ specify "should copy or process all files and directories" do
69
+ Rake::Task["waves:app"].invoke
70
+ @copy_files.each do |f|
71
+ assert File.exist?(File.join(@target, f))
72
+ end
73
+ @directories.each do |dir|
74
+ assert File.directory?(@generator.target(dir))
75
+ end
76
+ @template_files.each do |tf|
77
+ assert File.exist?(@generator.target(tf.chomp(".erb")))
78
+ end
79
+ File.open(File.join(@target, "four"), "r").read.should == "Yossarian jumped."
80
+ assert File.executable?(@generator.target("one"))
81
+ end
82
+
83
+
84
+ 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-rakegen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.3
5
+ platform: ruby
6
+ authors:
7
+ - Matthew King
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-14 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
+ - lib/rakegen/polite_file.rb
44
+ - lib/rakegen.rb
45
+ - LICENSE
46
+ - README
47
+ - test/helper.rb
48
+ - test/test_polite_file.rb
49
+ - test/test_rakegen.rb
50
+ - test/testdata/app/alpha/beta/five.erb
51
+ - test/testdata/app/four.erb
52
+ - test/testdata/app/one
53
+ - test/testdata/app/six.textile
54
+ - test/testdata/app/three.rb
55
+ - test/testdata/app/two.txt
56
+ - Manifest
57
+ - rakegen.gemspec
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: rakegen
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_polite_file.rb
86
+ - test/test_rakegen.rb