cog 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/cog CHANGED
@@ -24,6 +24,9 @@ arg_name 'name'
24
24
  default_value 'basic'
25
25
  flag :tool
26
26
 
27
+ desc 'Suppress the use of color in output'
28
+ switch :colorless
29
+
27
30
  desc 'Add cog to the project in the present working directory'
28
31
  command :init do |c|
29
32
 
@@ -93,7 +96,7 @@ command :tool do |c|
93
96
  end
94
97
 
95
98
  desc 'Manage templates'
96
- command :template do |c|
99
+ command [:template, :tm] do |c|
97
100
 
98
101
  c.default_command :list
99
102
 
@@ -104,9 +107,27 @@ command :template do |c|
104
107
  puts x.join "\n" unless x.empty?
105
108
  end
106
109
  end
110
+
111
+ c.desc 'Create a new project template'
112
+ c.command :new do |sub|
113
+ sub.desc 'Force override of existing built-in or tool templates'
114
+ sub.switch ['force-override', :f]
115
+
116
+ sub.action do |gopt, opt, args|
117
+ args.each do |name|
118
+ begin
119
+ Cog::Controllers::TemplateController.create name, :force_override => opt[:f]
120
+ rescue Cog::Errors::DuplicateTemplate => e
121
+ STDERR.write "Use --force-override to copy the original template into your project templates\n"
122
+ raise
123
+ end
124
+ end
125
+ end
126
+ end
107
127
  end
108
128
 
109
129
  pre do |gopt, command, opt, args|
130
+ Sickill::Rainbow.enabled = false if gopt[:colorless]
110
131
  if gopt[:cogfile] && !File.exists?(gopt[:cogfile])
111
132
  STDERR.write "No such Cogfile at #{gopt[:cogfile]}\n"
112
133
  false
@@ -0,0 +1,9 @@
1
+ module Cog
2
+
3
+ # A place to store built-in +cog+ tools.
4
+ #
5
+ # Built-in tools are distributed with the +cog+ gem.
6
+ module BuiltInTools
7
+ end
8
+
9
+ end
@@ -1,9 +1,9 @@
1
1
  module Cog
2
- module Tools
2
+ module BuiltInTools
3
3
 
4
4
  # The default +cog+ tool.
5
5
  # Creates generator/template pairs.
6
- module BasicTool
6
+ module Basic
7
7
  end
8
8
 
9
9
  end
@@ -1,5 +1,6 @@
1
1
  require 'cog/config'
2
2
  require 'cog/errors'
3
+ require 'cog/generator'
3
4
  require 'cog/helpers'
4
5
  require 'rainbow'
5
6
 
@@ -26,6 +27,32 @@ module Cog
26
27
  cts.to_a
27
28
  end
28
29
 
30
+ # Create a new project template
31
+ # @param name [String] name of the template, relative to the project's templates directory
32
+ # @option opt [Boolean] :force_override (false) if a built-in or tool template with the same name already exists, should a project override be created?
33
+ # @return [nil]
34
+ def self.create(name, opt={})
35
+ raise Errors::ActionRequiresProject.new('create template') unless Config.instance.project?
36
+ name = name.without_extension :erb
37
+ dest = File.join Config.instance.project_templates_path, "#{name}.erb"
38
+ Object.instance_eval do
39
+ extend Generator
40
+ begin
41
+ original = get_template name, :as_path => true
42
+ return if original == dest # Nothing to create
43
+ if opt[:force_override]
44
+ copy_if_missing original, dest
45
+ else
46
+ raise Errors::DuplicateTemplate.new original
47
+ end
48
+ rescue Errors::NoSuchTemplate
49
+ # No original, ok to create an empty template
50
+ touch_file dest
51
+ end
52
+ nil
53
+ end
54
+ end
55
+
29
56
  end
30
57
  end
31
58
  end
@@ -38,7 +38,9 @@ module Cog
38
38
  "a file or directory at the given path already exists, cannot create anything there"
39
39
  end
40
40
 
41
- define_error :DuplicateGenerator, 'generator name'
41
+ define_error :DuplicateGenerator, 'generator'
42
+
43
+ define_error :DuplicateTemplate, 'template'
42
44
 
43
45
  define_error :DuplicateTool, 'tool'
44
46
 
@@ -16,7 +16,8 @@ module Cog
16
16
  # Get the template with the given name
17
17
  # @param path [String] path to template file relative one of the {Config#template_paths}
18
18
  # @option opt [Boolean] :absolute (false) is the +path+ argument absolute?
19
- # @return [ERB] an instance of {http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html ERB}
19
+ # @option opt [Boolean] :as_path (false) return the template as an ERB instance (+false+) or an absolute path to the file (+true+)
20
+ # @return [ERB, String] an instance of {http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html ERB} or an absolute path to the template
20
21
  def get_template(path, opt={})
21
22
  path += '.erb'
22
23
  fullpath = if opt[:absolute]
@@ -28,7 +29,11 @@ module Cog
28
29
  end
29
30
  end
30
31
  raise Errors::NoSuchTemplate.new path unless File.exists? fullpath
31
- ERB.new File.read(fullpath), 0, '>'
32
+ if opt[:as_path]
33
+ File.expand_path fullpath
34
+ else
35
+ ERB.new File.read(fullpath), 0, '>'
36
+ end
32
37
  end
33
38
 
34
39
  # Stamp a template into a file or return it as a string
@@ -64,6 +69,7 @@ module Cog
64
69
  # @return [nil]
65
70
  def copy_if_missing(src, dest, opt={})
66
71
  unless File.exists? dest
72
+ touch_path File.dirname(dest), opt
67
73
  FileUtils.cp src, dest
68
74
  STDOUT.write "Created #{dest.relative_to_project_root}\n".color(:green) unless opt[:quiet]
69
75
  end
@@ -81,6 +87,18 @@ module Cog
81
87
  nil
82
88
  end
83
89
 
90
+ # Create the file at the given path,
91
+ # Creates directories along the path as required.
92
+ # @param path [String] a file system path representing a file
93
+ # @option opt [Boolean] :quiet (false) suppress writing to STDOUT?
94
+ # @return [nil]
95
+ def touch_file(path, opt={})
96
+ touch_path File.dirname(path), opt
97
+ FileUtils.touch path
98
+ STDOUT.write "Created #{path.relative_to_project_root}\n".color(:green) unless opt[:quiet]
99
+ nil
100
+ end
101
+
84
102
  # File extension for a snippet of the given source code language.
85
103
  # @api unstable
86
104
  # @example
@@ -12,6 +12,15 @@ class String
12
12
  # @return [String] this string as a file system path relative to the +prefix+
13
13
  def relative_to(prefix)
14
14
  return dup if prefix.nil?
15
- start_with?(prefix) ? slice(prefix.length+1..-1) : dup
15
+ start_with?(prefix.to_s) ? slice(prefix.to_s.length+1..-1) : dup
16
+ end
17
+
18
+ # @param ext [String] file extension to remove from the end of this string
19
+ # @return [String] a copy of this string with the given extension removed. Does nothing if this string does not edit with the extension
20
+ def without_extension(ext)
21
+ return dup if ext.nil?
22
+ ext = ext.to_s
23
+ ext = '.' + ext unless ext.start_with? '.'
24
+ end_with?(ext) ? slice(0..(-ext.length - 1)) : dup
16
25
  end
17
26
  end
@@ -19,7 +19,7 @@ module Cog
19
19
  # @param args [Array<String>] arguments to pass to +cog+
20
20
  # @return [Invocation] an object which can be used with custom {Matchers}
21
21
  def run(*args)
22
- args = [@cog] + args
22
+ args = [@cog, '--colorless'] + args
23
23
  Invocation.new(args.collect {|x| x.to_s}, :tools => @tools)
24
24
  end
25
25
  end
@@ -1,5 +1,5 @@
1
1
  module Cog
2
2
  unless const_defined? :VERSION
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kevin Tonon
@@ -100,6 +100,7 @@ files:
100
100
  - templates/warning.h.erb
101
101
  - lib/cog/built_in_tools/basic/cog_tool.rb
102
102
  - lib/cog/built_in_tools/basic.rb
103
+ - lib/cog/built_in_tools.rb
103
104
  - lib/cog/config/cogfile.rb
104
105
  - lib/cog/config/tool.rb
105
106
  - lib/cog/config.rb