cog 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/API.rdoc +4 -31
  2. data/Default.cogfile +8 -5
  3. data/bin/cog +61 -51
  4. data/lib/cog.rb +18 -2
  5. data/lib/cog/config.rb +60 -42
  6. data/lib/cog/config/cogfile.rb +76 -0
  7. data/lib/cog/errors.rb +16 -0
  8. data/lib/cog/generator.rb +138 -0
  9. data/lib/cog/spec_helpers.rb +35 -0
  10. data/lib/cog/spec_helpers/matchers.rb +17 -17
  11. data/lib/cog/spec_helpers/matchers/match_maker.rb +51 -0
  12. data/lib/cog/spec_helpers/runner.rb +9 -5
  13. data/lib/cog/tool.rb +51 -0
  14. data/lib/cog/version.rb +3 -0
  15. data/templates/{snippets → cog/snippets}/c++/generated_warning.h.erb +0 -0
  16. data/templates/{snippets → cog/snippets}/generated_warning.txt +0 -0
  17. data/templates/cog/tool/API.rdoc.erb +7 -0
  18. data/templates/cog/tool/Gemfile.erb +4 -0
  19. data/templates/cog/tool/LICENSE.erb +18 -0
  20. data/templates/cog/tool/README.markdown.erb +18 -0
  21. data/templates/cog/tool/Rakefile.erb +15 -0
  22. data/templates/cog/tool/generator.rb.erb +5 -0
  23. data/templates/cog/tool/tool.gemspec.erb +18 -0
  24. data/templates/cog/tool/tool.rb.erb +4 -0
  25. data/templates/cog/tool/version.rb.erb +5 -0
  26. metadata +21 -19
  27. data/lib/cog/cogfile.rb +0 -65
  28. data/lib/cog/meta.rb +0 -9
  29. data/lib/cog/meta/gen_gen.rb +0 -85
  30. data/lib/cog/meta/mirror_gen.rb +0 -39
  31. data/lib/cog/mixins.rb +0 -13
  32. data/lib/cog/mixins/mirror.rb +0 -61
  33. data/lib/cog/mixins/uses_templates.rb +0 -152
  34. data/lib/cog_version.rb +0 -3
  35. data/templates/c++/mirror-abstract.cpp.erb +0 -0
  36. data/templates/c++/mirror-abstract.h.erb +0 -6
  37. data/templates/c++/mirror-impl.cpp.erb +0 -1
  38. data/templates/c++/mirror-impl.h.erb +0 -0
  39. data/templates/mirror.rb.erb +0 -10
@@ -0,0 +1,5 @@
1
+ require '<%= @name %>'
2
+
3
+ <%= @module_name %>.define do |definition|
4
+
5
+ end
@@ -0,0 +1,18 @@
1
+ # Ensure we require the local version and not one we might have installed already
2
+ require File.join([File.dirname(__FILE__),'lib','<%= @name %>/version.rb'])
3
+ spec = Gem::Specification.new do |s|
4
+ s.name = '<%= @name %>'
5
+ s.version = <%= @module_name %>::VERSION
6
+ s.author = '<%= @author %>'
7
+ s.email = '<%= @email %>'
8
+ s.homepage = ''
9
+ s.platform = Gem::Platform::RUBY
10
+ s.summary = '<%= @description %>'
11
+ s.files = %w(LICENSE) + Dir.glob('cog/**/*') + Dir.glob('lib/**/*.rb')
12
+ s.require_paths << 'lib'
13
+ s.has_rdoc = true
14
+ s.extra_rdoc_files = ['API.rdoc']
15
+ s.rdoc_options << '--title' << '<%= @name %>' << '-ri'
16
+ s.add_development_dependency('rake')
17
+ s.add_development_dependency('rdoc')
18
+ end
@@ -0,0 +1,4 @@
1
+ require '<%= @name %>/version'
2
+
3
+ module <%= @module_name %>
4
+ end
@@ -0,0 +1,5 @@
1
+ module <%= @module_name %>
2
+ unless const_defined? :VERSION
3
+ VERSION = '0.0.1' # :nodoc:
4
+ end
5
+ 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: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 10
10
- version: 0.0.10
9
+ - 11
10
+ version: 0.0.11
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-10-27 00:00:00 Z
18
+ date: 2012-11-03 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake
@@ -57,26 +57,28 @@ files:
57
57
  - bin/cog
58
58
  - Default.cogfile
59
59
  - LICENSE
60
- - templates/c++/mirror-abstract.cpp.erb
61
- - templates/c++/mirror-abstract.h.erb
62
- - templates/c++/mirror-impl.cpp.erb
63
- - templates/c++/mirror-impl.h.erb
64
- - templates/mirror.rb.erb
65
- - templates/snippets/c++/generated_warning.h.erb
66
- - templates/snippets/generated_warning.txt
67
- - lib/cog/cogfile.rb
60
+ - templates/cog/snippets/c++/generated_warning.h.erb
61
+ - templates/cog/snippets/generated_warning.txt
62
+ - templates/cog/tool/API.rdoc.erb
63
+ - templates/cog/tool/Gemfile.erb
64
+ - templates/cog/tool/generator.rb.erb
65
+ - templates/cog/tool/LICENSE.erb
66
+ - templates/cog/tool/Rakefile.erb
67
+ - templates/cog/tool/README.markdown.erb
68
+ - templates/cog/tool/tool.gemspec.erb
69
+ - templates/cog/tool/tool.rb.erb
70
+ - templates/cog/tool/version.rb.erb
71
+ - lib/cog/config/cogfile.rb
68
72
  - lib/cog/config.rb
69
- - lib/cog/meta/gen_gen.rb
70
- - lib/cog/meta/mirror_gen.rb
71
- - lib/cog/meta.rb
72
- - lib/cog/mixins/mirror.rb
73
- - lib/cog/mixins/uses_templates.rb
74
- - lib/cog/mixins.rb
73
+ - lib/cog/errors.rb
74
+ - lib/cog/generator.rb
75
+ - lib/cog/spec_helpers/matchers/match_maker.rb
75
76
  - lib/cog/spec_helpers/matchers.rb
76
77
  - lib/cog/spec_helpers/runner.rb
77
78
  - lib/cog/spec_helpers.rb
79
+ - lib/cog/tool.rb
80
+ - lib/cog/version.rb
78
81
  - lib/cog.rb
79
- - lib/cog_version.rb
80
82
  - API.rdoc
81
83
  homepage: https://github.com/ktonon/cog
82
84
  licenses: []
@@ -1,65 +0,0 @@
1
- module Cog
2
-
3
- # In your project's +Cogfile+, +self+ has been set to an instance of this class.
4
- #
5
- # ==== Example +Cogfile+
6
- # cog_dir 'cog'
7
- # app_dir 'src'
8
- # language 'c++'
9
- #
10
- # Typing `cog init` will create a +Cogfile+ in the present working directory.
11
- #
12
- # +Cogfile+ files are used to configure an instance of Config.
13
- class Cogfile
14
-
15
- def initialize(config) # :nodoc:
16
- @config = config
17
- end
18
-
19
- # Interpret the Cogfile and initialize @config
20
- def interpret # :nodoc:
21
- eval File.read(@config.cogfile_path), binding
22
- rescue Exception => e
23
- raise CogfileError.new(e.to_s)
24
- end
25
-
26
- # Define the directory in which to place Ruby generators and +ERB+ templates.
27
- # ==== Arguments
28
- # * +path+ - A file system path
29
- # * +absolute+ - If false, the path is relative to the directory containing the +Cogfile+
30
- def cog_dir(path, absolute=false)
31
- @config.instance_eval do
32
- @cog_dir = absolute ? path : File.join(project_root, path)
33
- @generator_dir = File.join @cog_dir, 'generators'
34
- @template_dir = File.join @cog_dir, 'templates'
35
- end
36
- end
37
-
38
- # Define the directory in which to place generated application source code.
39
- # ==== Arguments
40
- # * +path+ - A file system path
41
- # * +absolute+ - If false, the path is relative to the directory containing the +Cogfile+
42
- def app_dir(path, absolute=false)
43
- @config.instance_eval do
44
- @app_dir = absolute ? path : File.join(project_root, path)
45
- end
46
- end
47
-
48
- # Define the default language in which to generated application source code.
49
- # ==== Arguments
50
- # * +lang+ - A code for the language. Acceptable values are <tt>c++</tt>.
51
- def language(lang)
52
- @config.instance_eval do
53
- @language = lang
54
- end
55
- end
56
- end
57
-
58
- # For wrapping errors which occur during the processing of a +Cogfile+.
59
- class CogfileError < StandardError
60
- def message
61
- "in Cogfile, " + super
62
- end
63
- end
64
-
65
- end
@@ -1,9 +0,0 @@
1
- require 'cog/meta/mirror_gen'
2
-
3
- module Cog
4
-
5
- # Used by cog internals.
6
- module Meta
7
- end
8
-
9
- end
@@ -1,85 +0,0 @@
1
- require 'cog/mixins/uses_templates'
2
- require 'cog/config'
3
-
4
- module Cog
5
- module Meta
6
-
7
- # An abstract generator generator.
8
- #
9
- # Subclasses define generators which make other generators.
10
- class GenGen
11
-
12
- include Mixins::UsesTemplates
13
-
14
- # Create a ruby generator from a template.
15
- #
16
- # === Parameters
17
- # * +ruby_template+ - name of the ruby template. This is a template
18
- # which is distributed in the cog gem. It is relative to
19
- # <tt>templates/</tt>
20
- # * +generator_name+ - name of the generator
21
- #
22
- # === Options
23
- # * <tt>:language</tt> - only <tt>'c++'</tt> is accepted at this time
24
- # * <tt>:package</tt> - slash (/) separated path which will prefix
25
- # +generator_name+
26
- # * <tt>:binding</tt> - specify an alternate binding to use when resolving
27
- # the +ERB+ template. If none is provided +self.binding+ will be used.
28
- def stamp_generator(ruby_template, generator_name, opt={})
29
- template = File.join Config.gem_dir, 'templates', "#{ruby_template}.rb"
30
- target = "#{generator_name}.rb"
31
- target = File.join opt[:package], target if opt[:package]
32
- target = File.join 'generators', target
33
- stamp template,
34
- :target => target,
35
- :target_type => :cog,
36
- :use_absolute_path => true,
37
- :binding => opt[:binding]
38
- end
39
-
40
- # Create an app module template from a template.
41
- #
42
- # === Parameters
43
- # * +source_template+ - name of the source template. This is a template
44
- # which is distributed in the cog gem. It is relative to
45
- # <tt>templates/{language}/</tt>, where language is specified by the
46
- # <tt>:language</tt> option. Do not include any file extension such as
47
- # <tt>.h</tt> (the <tt>:language</tt> will also determine this)
48
- # * +module_name+ - name of the module. For languages like <tt>:c++</tt>,
49
- # two templates will be generated (i.e. one <tt>.h</tt> and one
50
- # <tt>.cpp</tt>)
51
- #
52
- # === Options
53
- # * <tt>:language</tt> - only <tt>'c++'</tt> is accepted at this time
54
- # * <tt>:package</tt> - slash (/) separated path which will prefix
55
- # +module_name+
56
- # * <tt>:binding</tt> - specify an alternate binding to use when resolving
57
- # the +ERB+ template. If none is provided +self.binding+ will be used.
58
- def stamp_module(source_template, module_name, opt={})
59
- lang = opt[:language] || 'c++'
60
- extensions_for_language(lang).each do |ext|
61
- template = File.join Config.gem_dir, 'templates', lang, "#{source_template}.#{ext}"
62
- target = "#{module_name}.#{ext}"
63
- target = File.join opt[:package], target if opt[:package]
64
- target = File.join 'templates', target
65
- stamp template,
66
- :target => target,
67
- :target_type => :cog,
68
- :use_absolute_path => true,
69
- :binding => opt[:binding]
70
- end
71
- end
72
-
73
- private
74
- def extensions_for_language(lang)
75
- case lang.to_s
76
- when /(cpp|c\+\+)/i
77
- [:h, :cpp]
78
- else
79
- []
80
- end
81
- end
82
- end
83
-
84
- end
85
- end
@@ -1,39 +0,0 @@
1
- require 'cog/meta/gen_gen'
2
-
3
- module Cog
4
- module Meta
5
-
6
- # Generates a Cog::Mixins::Mirror and associated templates.
7
- class MirrorGen
8
-
9
- # Create a mirror generator
10
- #
11
- # === Parameters
12
- # * +name+ - the name of the mirror. Will be forced to singular form.
13
- def initialize(name, opt={})
14
- @name = name.singularize
15
- @gg = GenGen.new
16
- end
17
-
18
- def filename
19
- @name.underscore
20
- end
21
-
22
- def classname
23
- @name.camelize
24
- end
25
-
26
- def abstract_classname
27
- "Cog#{@name.camelize}"
28
- end
29
-
30
- def stamp(opt={})
31
- opt[:binding] = binding
32
- @gg.stamp_generator 'mirror', filename, opt
33
- @gg.stamp_module 'mirror-abstract', "abstract_#{filename}", opt
34
- @gg.stamp_module 'mirror-impl', filename, opt
35
- end
36
- end
37
-
38
- end
39
- end
@@ -1,13 +0,0 @@
1
- require 'cog/mixins/uses_templates'
2
-
3
- module Cog
4
-
5
- # Modules that extend your code generating classes with helper methods.
6
- #
7
- # * Mixins::UsesTemplates - methods for using +ERB+ template files.
8
- # * Mixins::Mirror - code the interface in Ruby and implement in the target
9
- # langauge.
10
- module Mixins
11
- end
12
-
13
- end
@@ -1,61 +0,0 @@
1
- require 'cog/mixins/uses_templates'
2
- module Cog
3
- module Mixins
4
-
5
- # Code the interface in Ruby and implement in the target langauge.
6
- #
7
- # <b>Not implemented</b>
8
- #
9
- # === Example
10
- #
11
- # In Ruby
12
- # class Dog
13
- # include Cog::Mixins::Mirror
14
- #
15
- # cog_reader :name, Cog::Type.string
16
- # cog_accessor :age, Cog::Type.int
17
- #
18
- # meth :speak
19
- # end
20
- #
21
- # In C++
22
- # class CogDog
23
- # {
24
- # protected:
25
- # char* _name;
26
- # int _age;
27
- # public:
28
- # AbstractDog();
29
- # virtual ~AbstractDog();
30
- #
31
- # virtual const char* name() const { return _name; }
32
- #
33
- # virtual int age() const { return _age; }
34
- # virtual void setAge(int age) { _age = age; }
35
- #
36
- # virtual void speak() = 0;
37
- # };
38
- #
39
- # class Dog : public CogDog
40
- # {
41
- # public:
42
- # Dog();
43
- # virtual ~Dog();
44
- #
45
- # virtual void speak();
46
- # };
47
- #
48
- # The abstract class +CogDog+ will be regenerated whenever the source Ruby
49
- # specification is changed. The derived C++ class +Dog+ will only be
50
- # generated once. The C++ compiler will let you know if any abstract methods
51
- # are missing from +Dog+ when you try to instantiate it.
52
- module Mirror
53
- def self.included(base)
54
- base.include UsesTemplates
55
- end
56
-
57
-
58
- end
59
-
60
- end
61
- end
@@ -1,152 +0,0 @@
1
- require 'cog/config'
2
- require 'erb'
3
-
4
- module Cog
5
- module Mixins
6
-
7
- # Mixin for classes that can use templates to generate code
8
- module UsesTemplates
9
-
10
- # File extension for a snippet of the given source code language.
11
- # ==== Example
12
- # snippet_extension 'c++' # => 'h'
13
- def snippet_extension(lang = 'text')
14
- case lang
15
- when /(c\+\+|c|objc)/i
16
- 'h'
17
- else
18
- 'txt'
19
- end
20
- end
21
-
22
- # Get the template with the given name.
23
- #
24
- # === Parameters
25
- # * +path+ - a path to a template file which is relative to
26
- # Config#template_dir and does not include the +.erb+ extension.
27
- #
28
- # === Options
29
- # * <tt>:absolute</tt> - set to +true+ to indicate that path is absolute
30
- # and should not be prefixed. Default value is +false+.
31
- #
32
- # === Returns
33
- # An instance of ERB.
34
- def get_template(path, opt={})
35
- path += '.erb'
36
- prefix = if opt[:cog_template]
37
- File.join Config.gem_dir, 'templates'
38
- elsif !opt[:absolute]
39
- Config.for_project.template_dir
40
- end
41
- path = File.join prefix, path unless prefix.nil?
42
- raise Errors::MissingTemplate.new path unless File.exists? path
43
- ERB.new File.read(path), 0, '>'
44
- end
45
-
46
- # Stamp this object using the template at the given path.
47
- #
48
- # === Parameters
49
- # * +path+ - to the template which is relative to Config#template_dir and
50
- # does not include the +.erb+ extension.
51
- #
52
- # === Options
53
- # * <tt>:target</tt> - the destination path to the generated file which is
54
- # relative to either Config#app_dir or Config#cog_dir, depending on the
55
- # value of the <tt>:target_type</tt> option.
56
- # * <tt>:target_type</tt> - either <tt>:cog</tt> or <tt>:app</tt>.
57
- # Determines what path to prefix to the <tt>:target</tt> option. The
58
- # default value is <tt>:app</tt>
59
- # * <tt>:use_absolute_path</tt> - set to +true+ to indicate that path is
60
- # absolute and should not be prefixed. Default value is +false+.
61
- # * <tt>:binding</tt> - specify an alternate binding to use when resolving
62
- # the +ERB+ template. If none is provided +self.binding+ will be used.
63
- #
64
- # === Returns
65
- # +nil+ if generated to a file, otherwise returns the generated code as a
66
- # string.
67
- def stamp(path, opt={})
68
- t = get_template path, :absolute => opt[:use_absolute_path]
69
- b = opt[:binding] || binding
70
- target = opt[:target]
71
- if target.nil?
72
- t.result(b)
73
- else
74
- target_type = opt[:target_type] || :app
75
- target = case target_type.to_s
76
- when /cog/i
77
- File.join Config.for_project.cog_dir, target
78
- when /app/i
79
- File.join Config.for_project.app_dir, target
80
- end
81
- FileUtils.mkpath File.dirname(target) unless File.exists? target
82
- scratch = case target_type.to_s
83
- when /cog/i
84
- File.join Config.for_project.cog_dir, "#{opt[:target]}.scratch"
85
- when /app/i
86
- File.join Config.for_project.app_dir, "#{opt[:target]}.scratch"
87
- end
88
- File.open(scratch, 'w') {|file| file.write t.result(b)}
89
- unless same? target, scratch
90
- puts "Generated #{target}"
91
- FileUtils.mv scratch, target
92
- else
93
- FileUtils.rm scratch
94
- end
95
- nil
96
- end
97
- end
98
-
99
- # A warning that indicates a file is maintained by a generator
100
- def generated_warning
101
- lang = Config.for_project.language
102
- t = get_template "snippets/#{lang}/generated_warning.#{snippet_extension lang}", :cog_template => true
103
- t.result(binding)
104
- end
105
-
106
- def include_guard_begin(name)
107
- full = "COG_INCLUDE_GUARD_#{name.upcase}"
108
- "#ifndef #{full}\n#define #{full}"
109
- end
110
-
111
- def include_guard_end
112
- "#endif // COG_INCLUDE_GUARD_[...]"
113
- end
114
-
115
- def namespace_begin(name)
116
- return if name.nil?
117
- case Config.for_project.language
118
- when /c\+\+/
119
- "namespace #{name} {"
120
- end
121
- end
122
-
123
- def namespace_end(name)
124
- return if name.nil?
125
- case Config.for_project.language
126
- when /c\+\+/
127
- "} // namespace #{name}"
128
- end
129
- end
130
-
131
- private
132
- def same?(original, scratch) # :nodoc:
133
- if File.exists? original
134
- File.read(original) == File.read(scratch)
135
- else
136
- false
137
- end
138
- end
139
- end
140
-
141
- end
142
-
143
- module Errors
144
- # Indiciates an attempt to use a non-existant template.
145
- class MissingTemplate < Exception
146
- def message
147
- 'could not find the template ' + super
148
- end
149
- end
150
- end
151
-
152
- end