cog 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ = cog API docs
2
+
3
+ +cog+ is a command line utility that makes it a bit easier to organize a project
4
+ which uses code generation. Many of your interactions with +cog+ will be using
5
+ the command line interface. However, to get any real mileage out of it you'll
6
+ need to use the API (documented here). For more information on the command line
7
+ interface see https://github.com/ktonon/cog#cog
8
+
9
+ +cog+ is configured with a +Cogfile+, which should be in the root of your
10
+ project's file tree. The +Cogfile+ is just a ruby file and the commands
11
+ available in it are defined in the Cog::Cogfile class. It is generated for you
12
+ when you issue this command from a shell in the root directory of your project:
13
+
14
+ cog project
15
+
16
+ and it will look something like this:
17
+
18
+ # All paths are relative to the directory containing this file.
19
+
20
+ # The directory in which to find ERB template files.
21
+ template_dir 'templates'
22
+
23
+ # The directory in which application code can be found. This is where
24
+ # generated code will go. Probably along side non-generated code.
25
+ code_dir 'src'
data/bin/cog CHANGED
@@ -29,7 +29,18 @@ desc 'Add cog to a project by generating a Cogfile in the current directory'
29
29
  command :project do |c|
30
30
  c.action do |global_options, options, args|
31
31
  puts 'Created Cogfile'
32
- FileUtils.touch 'Cogfile'
32
+ File.open('Cogfile', 'w') do |file|
33
+ # TODO: use a template to do this instead
34
+ file.write("# All paths are relative to the directory containing this file.
35
+
36
+ # The directory in which to find ERB template files.
37
+ template_dir 'templates'
38
+
39
+ # The directory in which application code can be found. This is where
40
+ # generated code will go. Probably along side non-generated code.
41
+ code_dir 'src'
42
+ ")
43
+ end
33
44
  end
34
45
  end
35
46
 
@@ -37,7 +48,7 @@ desc 'Create a new template'
37
48
  arg_name 'NAME'
38
49
  command :template do |c|
39
50
  c.action do |global_options, options, args|
40
- # TODO: implement me
51
+ puts 'Not implemented'
41
52
  end
42
53
  end
43
54
 
@@ -47,12 +58,12 @@ pre do |global, command, options, args|
47
58
  # chosen command
48
59
  # Use skips_pre before a command to skip this block
49
60
  # on that command only
50
- cogfile = Cog.load_cogfile
61
+ cogfile = Cog.load_cogfile
51
62
  if !cogfile && command && command.name != :project
52
63
  puts 'No Cogfile could be found'
53
64
  false
54
65
  elsif cogfile && command && command.name == :project
55
- puts "A Cogfile already exists at #{cogfile.path.inspect}"
66
+ puts "A Cogfile already exists at #{cogfile.cogfile_path.inspect}"
56
67
  false
57
68
  else
58
69
  global[:cogfile] = cogfile
data/lib/cog.rb CHANGED
@@ -1,23 +1,17 @@
1
- require 'cog/config'
1
+ require 'cog/cogfile'
2
2
  require 'cog/has_template'
3
3
 
4
4
  module Cog
5
5
 
6
- class CogfileError < StandardError
7
- def message
8
- "in Cogfile, " + super
9
- end
10
- end
11
-
12
- # Loads the +Cogfile+.
6
+ # Loads the +Cogfile+ for the current project.
13
7
  #
14
8
  # The +Cogfile+ will be looked for in the present working directory. If none
15
9
  # is found there the parent directory will be checked, and then the
16
10
  # grandparent, and so on.
17
11
  #
18
12
  # === Returns
19
- # An instance of Config which has been configured using the +Cogfile+, if
20
- # one was found, otherwise +nil+.
13
+ # An instance of Config which has been configured with a Cogfile. Or +nil+ if
14
+ # no +Cogfile+ could be found.
21
15
  def self.load_cogfile
22
16
  parts = Dir.pwd.split File::SEPARATOR
23
17
  i = parts.length
@@ -26,14 +20,17 @@ module Cog
26
20
  end
27
21
  path = File.join(parts.slice(0, i) + ['Cogfile']) if i >= 0
28
22
  if path
29
- cogfile = Config.new path
23
+ Cogfile.instance.instance_eval do
24
+ @cogfile_path = path
25
+ @project_root = File.dirname path
26
+ end
30
27
  begin
31
- b = cogfile.instance_eval {binding}
28
+ b = Cogfile.instance.instance_eval {binding}
32
29
  eval File.read(path), b
33
30
  rescue Exception => e
34
31
  raise CogfileError.new(e.to_s)
35
32
  end
36
- cogfile
33
+ Cogfile.instance
37
34
  end
38
35
  end
39
36
 
@@ -0,0 +1,59 @@
1
+ require 'cog/config'
2
+ require 'singleton'
3
+
4
+ module Cog
5
+
6
+ # When the +Cogfile+ is processed, +self+ will be the singleton instance of
7
+ # this object.
8
+ class Cogfile
9
+ include Singleton
10
+
11
+ def get_or_set(name, val, &block) # :nodoc:
12
+ if val.nil?
13
+ instance_variable_get "@#{name}"
14
+ else
15
+ val = block.call val unless block.nil?
16
+ instance_variable_set "@#{name}", val
17
+ end
18
+ end
19
+
20
+ # The directory in which the +Cogfile+ is found.
21
+ attr_reader :project_root
22
+
23
+ # The path to the +Cogfile+.
24
+ attr_reader :cogfile_path
25
+
26
+ # The directory in which to find ERB template files. This is relative to
27
+ # project_root
28
+ #
29
+ # Can be used as a getter or a setter (within the +Cogfile+).
30
+ def template_dir(val=nil)
31
+ get_or_set :template_dir, val do |val|
32
+ File.join project_root, val
33
+ end
34
+ end
35
+
36
+ # The directory in which application code can be found. This is where
37
+ # generated code will go. Probably along side non-generated code. It is
38
+ # relative to project_root
39
+ #
40
+ # Can be used as a getter or a setter (within the +Cogfile+).
41
+ def code_dir(val=nil)
42
+ get_or_set :code_dir, val do |val|
43
+ File.join project_root, val
44
+ end
45
+ end
46
+
47
+ def to_s # :nodoc:
48
+ "#{project_root} #{code_dir}"
49
+ end
50
+ end
51
+
52
+ # For wrapping errors which occur during the processing of a +Cogfile+.
53
+ class CogfileError < StandardError
54
+ def message
55
+ "in Cogfile, " + super
56
+ end
57
+ end
58
+
59
+ end
@@ -1,12 +1,44 @@
1
+ require 'erb'
2
+
1
3
  module Cog
2
4
 
3
5
  # Mixin for classes that can use templates to generate code
4
6
  module HasTemplate
5
7
 
6
- def cog(template_name)
7
-
8
+ # Get the template with the given name.
9
+ #
10
+ # === Parameters
11
+ # * +path+ - a path to a template file which is relative to
12
+ # Cogfile#template_dir and does not include the +.erb+ extension.
13
+ #
14
+ # === Returns
15
+ # An instance of ERB.
16
+ def get_template(path)
17
+ path = File.join Cogfile.instance.template_dir, "#{path}.erb"
18
+ unless File.exists? path
19
+ raise MissingTemplate.new path
20
+ end
8
21
  end
9
22
 
23
+ # Stamp this object using the template at the given path.
24
+ #
25
+ # === Parameters
26
+ # * +path+ - the path to the template to use which is relative to
27
+ # Config#template_dir and does not include the +.erb+ extension.
28
+ # * +dest+ - the destination path to the generated file which is relative
29
+ # to Config#code_dir. If this is not provided, then the generated code
30
+ # will be returned as a string.
31
+ #
32
+ # === Returns
33
+ # +nil+ if generated to a file, otherwise returns the generated code as a
34
+ # string.
35
+ def stamp(path, dest=nil)
36
+ end
10
37
  end
11
38
 
39
+ class MissingTemplate < Exception
40
+ def message
41
+ 'could not find the template ' + super
42
+ end
43
+ end
12
44
  end
@@ -1,3 +1,3 @@
1
1
  module Cog
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  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: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kevin Tonon
@@ -52,14 +52,14 @@ executables:
52
52
  extensions: []
53
53
 
54
54
  extra_rdoc_files:
55
- - cog.rdoc
55
+ - API.rdoc
56
56
  files:
57
57
  - bin/cog
58
- - lib/cog/config.rb
58
+ - lib/cog/cogfile.rb
59
59
  - lib/cog/has_template.rb
60
60
  - lib/cog.rb
61
61
  - lib/cog_version.rb
62
- - cog.rdoc
62
+ - API.rdoc
63
63
  homepage: https://github.com/ktonon/cog
64
64
  licenses: []
65
65
 
data/cog.rdoc DELETED
@@ -1,3 +0,0 @@
1
- = cog
2
-
3
- This is the API documentation.
@@ -1,19 +0,0 @@
1
- module Cog
2
-
3
- # When the Cogfile is processed, +self+ will be set to an instance of this
4
- # object at the global context.
5
- class Config
6
- attr_reader :path
7
-
8
- # Create an empty config object
9
- #
10
- # === Arguments
11
- # * +path+ - The path to the +Cogfile+ which will be used to configure
12
- # this object.
13
- def initialize(path)
14
- @path = path
15
- end
16
-
17
- end
18
-
19
- end