cog 0.0.18 → 0.0.19

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.
@@ -32,6 +32,12 @@ module Cog
32
32
  def project?
33
33
  !@project_root.nil?
34
34
  end
35
+
36
+ # A value which is set by the active tool
37
+ attr_writer :tool_templates_path
38
+
39
+ # A value which is set by the active tool
40
+ attr_accessor :tool_generator_template
35
41
 
36
42
  # A list of directories in which to find ERB template files.
37
43
  # Priority should be given first to last.
@@ -33,25 +33,41 @@ module Cog
33
33
  # ==== Returns
34
34
  # Whether or not the generator was created successfully
35
35
  def self.create(name, opt={})
36
- tool = (opt[:tool] || :basic).to_s
37
36
  return false unless Config.instance.project?
37
+
38
38
  gen_name = File.join Config.instance.project_generators_path, "#{name}.rb"
39
- template_name = File.join Config.instance.project_templates_path, "#{name}.txt.erb"
40
39
  if File.exists? gen_name
41
40
  STDERR.write "Generator '#{gen_name}' already exists\n".color(:red)
42
- false
43
- elsif File.exists? template_name
41
+ return false
42
+ end
43
+
44
+ tool = (opt[:tool] || :basic).to_s
45
+ template_name = File.join Config.instance.project_templates_path, "#{name}.txt.erb"
46
+ if tool == 'basic' && File.exists?(template_name)
44
47
  STDERR.write "Template '#{template_name}' already exists\n".color(:red)
45
- false
46
- else
47
- Object.new.instance_eval do
48
- extend Generator
49
- @name = name
50
- @class_name = name.to_s.camelize
48
+ return false
49
+ end
50
+
51
+ Object.new.instance_eval do
52
+ extend Generator
53
+ @name = name
54
+ @class_name = name.to_s.camelize
55
+ if tool == 'basic'
51
56
  stamp 'cog/generator/basic.rb', gen_name, :absolute_destination => true
52
57
  stamp 'cog/generator/basic-template.txt.erb', template_name, :absolute_destination => true
58
+ else
59
+ tool_path = Tool.find(tool)
60
+ if tool_path.nil?
61
+ STDERR.write "No such tool '#{tool}'"
62
+ false
63
+ else
64
+ require tool_path
65
+ @absolute_require = tool_path != tool
66
+ @tool_parent_path = File.dirname(tool_path)
67
+ stamp Config.instance.tool_generator_template, gen_name, :absolute_destination => true
68
+ true
69
+ end
53
70
  end
54
- true
55
71
  end
56
72
  end
57
73
 
@@ -97,7 +113,7 @@ module Cog
97
113
  found.empty? && File.exists?(x) ? x : found
98
114
  end
99
115
  end
100
- raise Errors::MissingTemplate.new fullpath unless File.exists? fullpath
116
+ raise Errors::MissingTemplate.new path unless File.exists? fullpath
101
117
  ERB.new File.read(fullpath), 0, '>'
102
118
  end
103
119
 
@@ -53,7 +53,7 @@ module Cog
53
53
 
54
54
  # Path to the test tool with the given name
55
55
  def tool(name)
56
- File.expand_path File.join(spec_root, 'tools', name.to_s)
56
+ File.expand_path File.join(spec_root, 'tools', name.to_s, 'lib', "#{name}.rb")
57
57
  end
58
58
 
59
59
  # The next cog spec will execute in a fresh copy of the given fixture directory.
@@ -10,11 +10,11 @@ module Cog
10
10
  # A list of available tools
11
11
  def self.list(verbose=false)
12
12
  x = (ENV['COG_TOOLS'] || '').split ':'
13
- if x.all? {|path| File.exists?(path) && File.directory?(path)}
13
+ if x.all? {|path| path.slice(-3..-1) != '.rb' || File.exists?(path)}
14
14
  if verbose
15
- x.collect {|path| File.expand_path path}
15
+ x.collect {|path| path.slice(-3..-1) == '.rb' ? File.expand_path(path) : path}
16
16
  else
17
- x.collect {|path| File.basename path}
17
+ x.collect {|path| path.slice(-3..-1) == '.rb' ? File.basename(path).slice(0..-4) : path}
18
18
  end
19
19
  else
20
20
  x.each do |path|
@@ -41,13 +41,15 @@ module Cog
41
41
  extend Generator
42
42
  @name = name.to_s.downcase
43
43
  @module_name = name.to_s.capitalize
44
+ @letter = name.to_s.slice(0,1).downcase
44
45
  @author = '<Your name goes here>'
45
46
  @email = 'youremail@...'
46
47
  @description = 'A one-liner'
47
48
  @cog_version = Cog::VERSION
48
49
  stamp 'cog/tool/tool.rb', "#{@name}/lib/#{@name}.rb", :absolute_destination => true
49
50
  stamp 'cog/tool/version.rb', "#{@name}/lib/#{@name}/version.rb", :absolute_destination => true
50
- stamp 'cog/tool/generator.rb', "#{@name}/cog/templates/#{@name}/generator.rb.erb", :absolute_destination => true
51
+ stamp 'cog/tool/generator.rb.erb', "#{@name}/cog/templates/#{@name}/generator.rb.erb", :absolute_destination => true
52
+ stamp 'cog/tool/template.txt.erb', "#{@name}/cog/templates/#{@name}/#{@name}.txt.erb", :absolute_destination => true
51
53
  stamp 'cog/tool/Gemfile', "#{@name}/Gemfile", :absolute_destination => true
52
54
  stamp 'cog/tool/Rakefile', "#{@name}/Rakefile", :absolute_destination => true
53
55
  stamp 'cog/tool/tool.gemspec', "#{@name}/#{@name}.gemspec", :absolute_destination => true
@@ -58,6 +60,22 @@ module Cog
58
60
  true
59
61
  end
60
62
  end
61
- end
62
63
 
64
+ # Find an available tool with the given name
65
+ #
66
+ # ==== Returns
67
+ # A fully qualified tool path, which can be required
68
+ def self.find(name)
69
+ x = (ENV['COG_TOOLS'] || '').split ':'
70
+ x.each do |path|
71
+ if path.slice(-3..-1) == '.rb'
72
+ short = File.basename(path).slice(0..-4)
73
+ return path if short == name
74
+ else
75
+ return path if path == name
76
+ end
77
+ end
78
+ end
79
+
80
+ end
63
81
  end
@@ -1,3 +1,3 @@
1
1
  module Cog
2
- VERSION = '0.0.18' unless const_defined? :VERSION
2
+ VERSION = '0.0.19' unless const_defined? :VERSION
3
3
  end
@@ -0,0 +1,6 @@
1
+ <%% if @absolute_require %>$LOAD_PATH << "<%%= @tool_parent_path %>"
2
+ <%% end %>require '<%= @name %>'
3
+
4
+ <%= @module_name %>.widget '<%%= @name %>' do |w|
5
+ w.context = 'this is the context'
6
+ end
@@ -0,0 +1 @@
1
+ This is a custom generator template with context: <%%= @context %>!
@@ -1,4 +1,42 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__))
1
2
  require '<%= @name %>/version'
3
+ require 'cog'
2
4
 
5
+ # Inject this tools templates onto the cog template lookup path
6
+ # Do not remove this line
7
+ Cog::Config.instance.tool_templates_path = File.expand_path(File.join(__FILE__, '..', '..', 'cog', 'templates'))
8
+
9
+ # Set the template which is used to create generators for this tool
10
+ # Do not remove this line
11
+ Cog::Config.instance.tool_generator_template = '<%= @name %>/generator.rb'
12
+
13
+ # Custom cog tool <%= @name %>
3
14
  module <%= @module_name %>
15
+
16
+ # Root of the DSL
17
+ # Feel free to rename this to something more appropriate
18
+ def self.widget(generator_name, &block)
19
+ w = Widget.new generator_name
20
+ block.call w
21
+ w.generate
22
+ nil
23
+ end
24
+
25
+ # Root type of the DSL
26
+ # You'll want to rename this to something more meaningful
27
+ # and probably place it in a separate file.
28
+ class Widget
29
+
30
+ include Cog::Generator
31
+
32
+ attr_accessor :context
33
+
34
+ def initialize(generator_name)
35
+ @generator_name = generator_name
36
+ end
37
+
38
+ def generate
39
+ stamp '<%= @name %>/<%= @name %>.txt', "generated_#{@generator_name}.txt"
40
+ end
41
+ end
4
42
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cog
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
4
+ hash: 57
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 18
10
- version: 0.0.18
9
+ - 19
10
+ version: 0.0.19
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kevin Tonon
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-11-04 00:00:00 Z
18
+ date: 2012-11-05 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: gli
@@ -91,10 +91,11 @@ files:
91
91
  - templates/cog/snippets/generated_warning.txt
92
92
  - templates/cog/tool/API.rdoc.erb
93
93
  - templates/cog/tool/Gemfile.erb
94
- - templates/cog/tool/generator.rb.erb
94
+ - templates/cog/tool/generator.rb.erb.erb
95
95
  - templates/cog/tool/LICENSE.erb
96
96
  - templates/cog/tool/Rakefile.erb
97
97
  - templates/cog/tool/README.markdown.erb
98
+ - templates/cog/tool/template.txt.erb.erb
98
99
  - templates/cog/tool/tool.gemspec.erb
99
100
  - templates/cog/tool/tool.rb.erb
100
101
  - templates/cog/tool/version.rb.erb
@@ -1,5 +0,0 @@
1
- require '<%= @name %>'
2
-
3
- <%= @module_name %>.define do |definition|
4
-
5
- end