kissgen 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +2 -2
- data/Rakefile +2 -1
- data/bin/kissgen +3 -3
- data/lib/kissgen/generator.rb +17 -7
- data/lib/kissgen/template.rb +16 -10
- data/lib/kissgen/version.rb +1 -1
- data/spec/public/generator_spec.rb +23 -7
- data/spec/public/generators/merb_very_flat/%app_file_name%.rb +15 -0
- data/spec/public/generators/merb_very_flat/README.txt +9 -0
- data/spec/public/template_spec.rb +1 -0
- metadata +4 -3
- data/spec/public/generators/empty/README +0 -3
- data/spec/public/generators/empty/Rakefile +0 -0
data/README
CHANGED
@@ -13,9 +13,9 @@ Keep it stupid simple (or simple stupid) code generator.
|
|
13
13
|
KISSGen::Generator.generate(:pretend => true)
|
14
14
|
KISSGen::Generator.generate
|
15
15
|
|
16
|
-
== Console Usage
|
16
|
+
== Console Usage
|
17
17
|
|
18
|
-
|
18
|
+
kissgen /path/to/generator /path/where/files/are/generated
|
19
19
|
|
20
20
|
== Repository
|
21
21
|
|
data/Rakefile
CHANGED
@@ -73,7 +73,7 @@ Rake::RDocTask.new do |rdoc|
|
|
73
73
|
rdoc.rdoc_files.include("lib/**/*.rb")
|
74
74
|
end
|
75
75
|
|
76
|
-
desc
|
76
|
+
desc "send rdoc to rubyforge"
|
77
77
|
task :rf_doc do
|
78
78
|
sh %{sudo chmod -R 755 doc}
|
79
79
|
sh %{/usr/bin/scp -r -p doc/* lancelot@rubyforge.org:/var/www/gforge-projects/kissgen}
|
@@ -83,5 +83,6 @@ end
|
|
83
83
|
# misc
|
84
84
|
##############################################################################
|
85
85
|
task :release => :package do
|
86
|
+
puts "Remember to login if you haven't"
|
86
87
|
sh %{rubyforge add_release #{NAME} #{NAME} "#{KISSGen::VERSION}" pkg/#{NAME}-#{KISSGen::VERSION}.gem}
|
87
88
|
end
|
data/bin/kissgen
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
#! /usr/bin/env ruby
|
2
2
|
#
|
3
3
|
|
4
|
-
unless ARGV.size == 2
|
5
|
-
puts "Usage: kissgen /path/to/generator /path/to/generate/files"
|
4
|
+
unless ARGV.size == 2 || ARGV.size == 3
|
5
|
+
puts "Usage: kissgen /path/to/generator /path/to/generate/files [-p]"
|
6
6
|
exit
|
7
7
|
end
|
8
8
|
|
9
9
|
require "kissgen"
|
10
10
|
@generator = KISSGen::Generator.new ARGV.shift, ARGV.shift
|
11
|
-
@generator.generate
|
11
|
+
ARGV.shift == "-p" ? @generator.generate(:pretend => true) : @generator.generate
|
data/lib/kissgen/generator.rb
CHANGED
@@ -2,13 +2,14 @@ module KISSGen
|
|
2
2
|
# The generator class is where you configure path and import files
|
3
3
|
#
|
4
4
|
# require 'rcgen'
|
5
|
-
# KISSGen::Generator.new "/path/to/generator", "/path/where/files/are/generated"
|
6
|
-
#
|
7
|
-
#
|
5
|
+
# @g = KISSGen::Generator.new "/path/to/generator", "/path/where/files/are/generated"
|
6
|
+
# @g.directory = "/"
|
7
|
+
# @g.assign :my_name, "lancelot"
|
8
|
+
# @g.generate(:pretend => true)
|
9
|
+
# @g.generate
|
8
10
|
class Generator
|
9
|
-
class SetupFailure < StandardError; end
|
10
11
|
|
11
|
-
attr_reader :path, :copy_path, :files
|
12
|
+
attr_reader :path, :copy_path, :files, :assigns
|
12
13
|
attr_accessor :explain_pretend, :explain_created, :explain_footer
|
13
14
|
attr_accessor :setup_file_path
|
14
15
|
|
@@ -16,6 +17,7 @@ module KISSGen
|
|
16
17
|
@path = path
|
17
18
|
@copy_path = copy_path
|
18
19
|
@files = []
|
20
|
+
@assigns = {}
|
19
21
|
@explain_pretend = "== PRETEND MODE: Would have created the following files: =="
|
20
22
|
@explain_generate = "== Generated the following files: =="
|
21
23
|
@explain_footer = "====== Generator Finished ======"
|
@@ -29,8 +31,7 @@ module KISSGen
|
|
29
31
|
|
30
32
|
# Import setup file which is located in /path/to/generator/setup.rb
|
31
33
|
def import_setup
|
32
|
-
|
33
|
-
instance_eval(File.new(setup_file_path).read)
|
34
|
+
instance_eval(File.new(setup_file_path).read) if File.exists?(setup_file_path)
|
34
35
|
end
|
35
36
|
|
36
37
|
def generate(options = {})
|
@@ -71,5 +72,14 @@ module KISSGen
|
|
71
72
|
end
|
72
73
|
end
|
73
74
|
end
|
75
|
+
|
76
|
+
# assigns :app_file_name, "lancelot"
|
77
|
+
def assign(name, value)
|
78
|
+
@assigns[name] = value
|
79
|
+
end
|
80
|
+
|
81
|
+
def delete
|
82
|
+
@files.each { |file| file.delete }
|
83
|
+
end
|
74
84
|
end
|
75
85
|
end
|
data/lib/kissgen/template.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
module KISSGen
|
2
2
|
class Template
|
3
3
|
class SourceFileNotFoundError < StandardError; end
|
4
|
-
|
4
|
+
|
5
|
+
attr_reader :generator, :path
|
5
6
|
|
6
7
|
def initialize(generator, path, copy_path)
|
7
8
|
@generator = generator
|
@@ -11,25 +12,30 @@ module KISSGen
|
|
11
12
|
source_path_check
|
12
13
|
end
|
13
14
|
|
15
|
+
def copy_path
|
16
|
+
@copy_path.gsub(/%([^\}]*)%/) {|a| @generator.assigns[$1.to_sym]} # Yehuda OWNZ
|
17
|
+
end
|
18
|
+
|
14
19
|
def source_path_check
|
15
20
|
raise SourceFileNotFoundError, "Template source file could not be found at #{full_source_path}" unless File.exists?(full_source_path)
|
16
21
|
end
|
17
22
|
|
18
23
|
def full_source_path
|
19
|
-
File.join @generator.path,
|
24
|
+
File.join @generator.path, path
|
20
25
|
end
|
21
26
|
|
22
27
|
def full_copy_path
|
23
|
-
File.join @generator.copy_path,
|
28
|
+
File.join @generator.copy_path, copy_path
|
24
29
|
end
|
25
30
|
|
26
31
|
# Parsed ERB output
|
27
32
|
def output
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
33
|
+
b = binding
|
34
|
+
|
35
|
+
# define local assignment variables
|
36
|
+
@generator.assigns.each { |name, value| eval "#{name} = \"#{value}\"", b }
|
32
37
|
|
38
|
+
ERB.new(File.read(full_source_path), 0, "%<>").result(b)
|
33
39
|
end
|
34
40
|
|
35
41
|
def write_or_prompt
|
@@ -38,9 +44,9 @@ module KISSGen
|
|
38
44
|
@replace = gets.chomp
|
39
45
|
if ["Y","y",""].include? @replace
|
40
46
|
write
|
41
|
-
puts "#{copy_path}
|
47
|
+
puts "#{copy_path} replaced."
|
42
48
|
else
|
43
|
-
puts "#{copy_path}
|
49
|
+
puts "#{copy_path} preserved."
|
44
50
|
end
|
45
51
|
else
|
46
52
|
write
|
@@ -49,7 +55,7 @@ module KISSGen
|
|
49
55
|
|
50
56
|
# This will create the file with parsed data in the destination directory
|
51
57
|
def create
|
52
|
-
puts
|
58
|
+
puts copy_path
|
53
59
|
FileUtils.mkdir_p(File.dirname(full_copy_path))
|
54
60
|
|
55
61
|
write_or_prompt
|
data/lib/kissgen/version.rb
CHANGED
@@ -4,18 +4,12 @@ describe KISSGen::Generator do
|
|
4
4
|
describe "new" do
|
5
5
|
before(:each) do
|
6
6
|
@path = File.dirname(__FILE__) + "/generators/merb_app"
|
7
|
-
@copy_path = File.
|
7
|
+
@copy_path = File.dirname(__FILE__) + "/generators/empty"
|
8
8
|
@bad_path = File.dirname(__FILE__) + "/doesnotexist"
|
9
9
|
@empty = File.dirname(__FILE__) + "/generators/empty"
|
10
10
|
@generator = KISSGen::Generator.new(@path, @copy_path)
|
11
11
|
end
|
12
12
|
|
13
|
-
it "should fail if the setup file cannot be found" do
|
14
|
-
lambda {
|
15
|
-
KISSGen::Generator.new(@empty, @copy_path)
|
16
|
-
}.should raise_error("Setup file does not exist in #{File.expand_path(@empty) + "/setup.rb"}")
|
17
|
-
end
|
18
|
-
|
19
13
|
it "should import template files for a new generator" do
|
20
14
|
@generator.files.length.should == 12
|
21
15
|
@generator.files[0].copy_path.should == "README"
|
@@ -34,6 +28,28 @@ describe KISSGen::Generator do
|
|
34
28
|
it "should generate new template files when no options are not set" do
|
35
29
|
@generator.generate
|
36
30
|
File.exists?(@copy_path + "/README").should be_true
|
31
|
+
File.delete(@copy_path + "/README")
|
32
|
+
File.delete(@copy_path + "/Rakefile")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "assigns" do
|
37
|
+
before(:each) do
|
38
|
+
@path = File.dirname(__FILE__) + "/generators/merb_very_flat"
|
39
|
+
@copy_path = File.dirname(__FILE__) + "/generators/empty"
|
40
|
+
@generator = KISSGen::Generator.new(@path, @copy_path)
|
41
|
+
@generator.directory "/"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should allow you to pass through assigned methods" do
|
45
|
+
class String; def camel_case; split('_').map{|e| e.capitalize}.join; end; end
|
46
|
+
@generator.assign :app_file_name, "lancelot"
|
47
|
+
|
48
|
+
@generator.assigns.should == {:app_file_name => "lancelot"}
|
49
|
+
@generator.files.first.copy_path.should == "/lancelot.rb"
|
50
|
+
|
51
|
+
@generator.generate
|
52
|
+
@generator.delete
|
37
53
|
end
|
38
54
|
end
|
39
55
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Merb::Router.prepare do |r|
|
2
|
+
r.match('/').to(:controller => '<%= app_file_name %>', :action =>'index')
|
3
|
+
end
|
4
|
+
|
5
|
+
class <%= app_file_name.camel_case %> < Merb::Controller
|
6
|
+
def index
|
7
|
+
"hi"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
Merb::Config.use { |c|
|
12
|
+
c[:framework] = {},
|
13
|
+
c[:session_store] = 'none',
|
14
|
+
c[:exception_details] = true
|
15
|
+
}
|
@@ -9,6 +9,7 @@ describe KISSGen::Generator do
|
|
9
9
|
@generator = mock(KISSGen::Generator)
|
10
10
|
@generator.stub!(:path).and_return(@copy_from)
|
11
11
|
@generator.stub!(:copy_path).and_return(@copy_to)
|
12
|
+
@generator.stub!(:assigns).and_return({})
|
12
13
|
@template = KISSGen::Template.new(@generator, @path, @path)
|
13
14
|
end
|
14
15
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kissgen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lance Carlson
|
@@ -31,8 +31,6 @@ files:
|
|
31
31
|
- spec/public/generator_spec.rb
|
32
32
|
- spec/public/generators
|
33
33
|
- spec/public/generators/empty
|
34
|
-
- spec/public/generators/empty/Rakefile
|
35
|
-
- spec/public/generators/empty/README
|
36
34
|
- spec/public/generators/merb_app
|
37
35
|
- spec/public/generators/merb_app/app
|
38
36
|
- spec/public/generators/merb_app/app/controllers
|
@@ -55,6 +53,9 @@ files:
|
|
55
53
|
- spec/public/generators/merb_app/Rakefile
|
56
54
|
- spec/public/generators/merb_app/README
|
57
55
|
- spec/public/generators/merb_app/setup.rb
|
56
|
+
- spec/public/generators/merb_very_flat
|
57
|
+
- spec/public/generators/merb_very_flat/%app_file_name%.rb
|
58
|
+
- spec/public/generators/merb_very_flat/README.txt
|
58
59
|
- spec/public/template_spec.rb
|
59
60
|
- spec/spec.opts
|
60
61
|
- spec/spec_helper.rb
|
File without changes
|