cog 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/BuiltIn.cogfile +96 -0
  2. data/bin/cog +30 -39
  3. data/built_in/generators/sort.rb +3 -0
  4. data/built_in/plugins/basic/Cogfile +6 -0
  5. data/built_in/plugins/basic/templates/basic/generator.rb.erb +1 -0
  6. data/built_in/templates/cog/Cogfile.erb +40 -0
  7. data/built_in/templates/cog/plugin/generator.rb.erb.erb +3 -0
  8. data/{templates/cog/custom_tool/tool.rb.erb → built_in/templates/cog/plugin/plugin.rb.erb} +4 -11
  9. data/{templates → built_in/templates}/warning.erb +0 -0
  10. data/lib/cog.rb +25 -11
  11. data/lib/cog/config.rb +112 -49
  12. data/lib/cog/config/{language_methods.rb → language_config.rb} +23 -27
  13. data/lib/cog/config/plugin_config.rb +31 -0
  14. data/lib/cog/config/project_config.rb +48 -0
  15. data/lib/cog/controllers.rb +6 -8
  16. data/lib/cog/controllers/generator_controller.rb +30 -21
  17. data/lib/cog/controllers/plugin_controller.rb +43 -0
  18. data/lib/cog/controllers/template_controller.rb +12 -33
  19. data/lib/cog/dsl.rb +9 -0
  20. data/lib/cog/dsl/cogfile.rb +145 -0
  21. data/lib/cog/dsl/language_dsl.rb +142 -0
  22. data/lib/cog/embed_context.rb +0 -2
  23. data/lib/cog/embeds.rb +2 -7
  24. data/lib/cog/errors.rb +11 -24
  25. data/lib/cog/generator.rb +10 -15
  26. data/lib/cog/generator/file_methods.rb +2 -2
  27. data/lib/cog/generator/filters.rb +10 -14
  28. data/lib/cog/generator/language_methods.rb +2 -3
  29. data/lib/cog/generator_sandbox.rb +32 -0
  30. data/lib/cog/helpers.rb +10 -2
  31. data/lib/cog/helpers/cascading_set.rb +104 -0
  32. data/lib/cog/{embeds → helpers}/file_scanner.rb +1 -1
  33. data/lib/cog/language.rb +140 -0
  34. data/lib/cog/native_extensions.rb +2 -0
  35. data/lib/cog/native_extensions/array.rb +19 -0
  36. data/lib/cog/native_extensions/string.rb +54 -0
  37. data/lib/cog/plugin.rb +35 -0
  38. data/lib/cog/spec_helpers.rb +36 -14
  39. data/lib/cog/spec_helpers/matchers.rb +14 -4
  40. data/lib/cog/spec_helpers/matchers/match_maker.rb +1 -1
  41. data/lib/cog/spec_helpers/runner.rb +3 -9
  42. data/lib/cog/version.rb +1 -1
  43. metadata +27 -44
  44. data/Default.cogfile +0 -25
  45. data/lib/cog/built_in_tools.rb +0 -9
  46. data/lib/cog/built_in_tools/basic.rb +0 -10
  47. data/lib/cog/built_in_tools/basic/cog_tool.rb +0 -11
  48. data/lib/cog/config/cogfile.rb +0 -117
  49. data/lib/cog/config/lang_info.rb +0 -40
  50. data/lib/cog/config/project_methods.rb +0 -35
  51. data/lib/cog/config/tool_methods.rb +0 -94
  52. data/lib/cog/controllers/tool_controller.rb +0 -50
  53. data/lib/cog/helpers/cascading_template_set.rb +0 -75
  54. data/lib/cog/helpers/string.rb +0 -26
  55. data/lib/cog/languages.rb +0 -52
  56. data/lib/cog/languages/c_language.rb +0 -29
  57. data/lib/cog/languages/c_plus_plus_language.rb +0 -28
  58. data/lib/cog/languages/c_sharp_language.rb +0 -24
  59. data/lib/cog/languages/java_language.rb +0 -24
  60. data/lib/cog/languages/java_script_language.rb +0 -24
  61. data/lib/cog/languages/language.rb +0 -73
  62. data/lib/cog/languages/mixins.rb +0 -10
  63. data/lib/cog/languages/mixins/c_style_comments.rb +0 -23
  64. data/lib/cog/languages/mixins/hash_comments.rb +0 -19
  65. data/lib/cog/languages/python_language.rb +0 -20
  66. data/lib/cog/languages/qt_project_language.rb +0 -16
  67. data/lib/cog/languages/ruby_language.rb +0 -28
  68. data/lib/cog/tool.rb +0 -61
  69. data/lib/cog/tool/dsl.rb +0 -26
  70. data/templates/basic/generator.rb.erb +0 -4
  71. data/templates/cog/custom_tool/Gemfile.erb +0 -8
  72. data/templates/cog/custom_tool/LICENSE.erb +0 -18
  73. data/templates/cog/custom_tool/README.markdown.erb +0 -18
  74. data/templates/cog/custom_tool/Rakefile.erb +0 -15
  75. data/templates/cog/custom_tool/cog_tool.rb.erb +0 -17
  76. data/templates/cog/custom_tool/generator.rb.erb.erb +0 -9
  77. data/templates/cog/custom_tool/template.txt.erb.erb +0 -1
  78. data/templates/cog/custom_tool/tool.gemspec.erb +0 -17
  79. data/templates/cog/custom_tool/version.rb.erb +0 -5
@@ -1,50 +0,0 @@
1
- require 'cog/config'
2
- require 'cog/errors'
3
- require 'cog/generator'
4
- require 'rainbow'
5
-
6
- module Cog
7
- module Controllers
8
-
9
- # Manage +cog+ tools
10
- #
11
- # @see https://github.com/ktonon/cog#tools Introduction to Tools
12
- module ToolController
13
-
14
- # Generate a new tool with the given name
15
- # @param name [String] name of the tool to create. Should not conflict with other tool names
16
- # @return [nil]
17
- def self.create(name)
18
- raise Errors::DestinationAlreadyExists.new(name) if File.exists?(name)
19
- raise Errors::DuplicateTool.new(name) if Cog.tool_registered?(name)
20
- @tool_name = name.to_s.downcase
21
- @tool_module = name.to_s.capitalize
22
- @tool_author = '<Your name goes here>'
23
- @tool_email = 'youremail@...'
24
- @tool_description = 'A one-liner'
25
- @cog_version = Cog::VERSION
26
- opt = { :absolute_destination => true, :binding => binding }
27
- Generator.stamp 'cog/custom_tool/tool.rb', "#{@tool_name}/lib/#{@tool_name}.rb", opt
28
- Generator.stamp 'cog/custom_tool/cog_tool.rb', "#{@tool_name}/lib/#{@tool_name}/cog_tool.rb", opt
29
- Generator.stamp 'cog/custom_tool/version.rb', "#{@tool_name}/lib/#{@tool_name}/version.rb", opt
30
- Generator.stamp 'cog/custom_tool/generator.rb.erb', "#{@tool_name}/cog/templates/#{@tool_name}/generator.rb.erb", opt
31
- Generator.stamp 'cog/custom_tool/template.txt.erb', "#{@tool_name}/cog/templates/#{@tool_name}/#{@tool_name}.txt.erb", opt
32
- Generator.stamp 'cog/custom_tool/Gemfile', "#{@tool_name}/Gemfile", opt
33
- Generator.stamp 'cog/custom_tool/Rakefile', "#{@tool_name}/Rakefile", opt
34
- Generator.stamp 'cog/custom_tool/tool.gemspec', "#{@tool_name}/#{@tool_name}.gemspec", opt
35
- Generator.stamp 'cog/custom_tool/LICENSE', "#{@tool_name}/LICENSE", opt
36
- Generator.stamp 'cog/custom_tool/README.markdown', "#{@tool_name}/README.markdown", opt
37
- nil
38
- end
39
-
40
- # @param opt [Boolean] :verbose (false) list full paths to tools
41
- # @return [Array<String>] a list of available tools
42
- def self.list(opt={})
43
- Cog.tools.collect do |tool|
44
- opt[:verbose] ? tool.path : tool.name
45
- end
46
- end
47
-
48
- end
49
- end
50
- end
@@ -1,75 +0,0 @@
1
- require 'cog/helpers/string'
2
- require 'rainbow'
3
-
4
- module Cog
5
- module Helpers
6
-
7
- # @api developer
8
- class TemplateInfo
9
- attr_reader :name
10
- attr_accessor :path
11
-
12
- def initialize(name)
13
- @sources = []
14
- @source_types = []
15
- @name = name
16
- end
17
-
18
- def add_source(source, source_type)
19
- @sources << source
20
- @source_types << source_type
21
- end
22
-
23
- def style(text, type)
24
- case type
25
- when :built_in
26
- text.color :cyan
27
- when :tool
28
- text.color :yellow
29
- when :project
30
- text.color(:white).bright
31
- end
32
- end
33
-
34
- def override_s(width=nil)
35
- colorless = "[#{@sources.join ' < '}]"
36
- if width
37
- x = @sources.zip(@source_types).collect {|s, t| style s, t}
38
- "[#{x.join ' < '}]" + " " * (width - colorless.length)
39
- else
40
- colorless
41
- end
42
- end
43
-
44
- def <=>(other)
45
- (@path || @name) <=> (other.path || other.name)
46
- end
47
-
48
- def to_s(override_column_width)
49
- "#{override_s override_column_width} #{style @path || @name, @source_types.last}"
50
- end
51
- end
52
-
53
- # @api developer
54
- class CascadingTemplateSet
55
- def initialize
56
- @templates = {}
57
- end
58
-
59
- def add_templates(source, source_type, root, opt={})
60
- Dir.glob("#{root}/**/*.erb") do |path|
61
- name = path.relative_to(root).slice(0..-5)
62
- @templates[name] ||= TemplateInfo.new name
63
- @templates[name].path = path if opt[:verbose]
64
- @templates[name].add_source source, source_type
65
- end
66
- end
67
-
68
- def to_a
69
- w = @templates.values.collect {|t| t.override_s.length}.max
70
- @templates.values.sort.collect {|t| t.to_s(w)}
71
- end
72
- end
73
-
74
- end
75
- end
@@ -1,26 +0,0 @@
1
- require 'cog/config'
2
-
3
- class String
4
-
5
- # @return [String] strips {Cog::Config::ProjectMethods#project_root} from the beginning of this string
6
- def relative_to_project_root
7
- return dup unless Cog.project?
8
- relative_to Cog.project_root
9
- end
10
-
11
- # @param prefix [String] path prefix to strip from the beginning of this string
12
- # @return [String] this string as a file system path relative to the +prefix+
13
- def relative_to(prefix)
14
- return dup if prefix.nil?
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
25
- end
26
- end
@@ -1,52 +0,0 @@
1
- require 'cog/errors'
2
- require 'cog/languages/language'
3
- require 'cog/languages/mixins'
4
-
5
- module Cog
6
-
7
- # Submodules define helpers for the target languages supported by +cog+
8
- module Languages
9
-
10
- # @return [Hash] a mapping from file extensions to languages identifiers
11
- DEFAULT_LANGUAGE_EXTENSION_MAP = {
12
- :c => 'c',
13
- :cpp => 'c++',
14
- :cs => 'c#',
15
- :h => 'c',
16
- :hpp => 'c++',
17
- :java => 'java',
18
- :js => 'javascript',
19
- :pri => 'qt',
20
- :pro => 'qt',
21
- :py => 'python',
22
- :rb => 'ruby',
23
- }
24
-
25
- # @return [Hash] a mapping from language aliases to language identifiers
26
- ALIAS = {
27
- 'c++' => 'c_plus_plus',
28
- 'c#' => 'c_sharp',
29
- 'javascript' => 'java_script',
30
- 'qt' => 'qt_project',
31
- }
32
-
33
- # @return [Hash] a mapping of language identifiers to lists of aliases
34
- REV_ALIAS = {}
35
- ALIAS.each_pair do |a, lang|
36
- REV_ALIAS[lang] ||= []
37
- REV_ALIAS[lang] << a
38
- end
39
-
40
- # @param lang_id [String] a langauge identifier
41
- # @return [Language] a language helper corresponding to the given identifier
42
- def self.get_language(lang_id)
43
- lang_id = Languages::ALIAS[lang_id] || lang_id
44
- require "cog/languages/#{lang_id.to_s.downcase}_language"
45
- Languages.instance_eval {const_get "#{lang_id.camelize}Language"}.new
46
- rescue LoadError
47
- raise Errors::NoSuchLanguage.new lang_id
48
- end
49
-
50
- end
51
-
52
- end
@@ -1,29 +0,0 @@
1
- require 'cog/languages/language'
2
- require 'cog/languages/mixins/c_style_comments'
3
-
4
- module Cog
5
- module Languages
6
-
7
- class CLanguage < Language
8
- include Mixins::CStyleComments
9
-
10
- # @return [Array<String>] a set of extensions needed to define a module in this language
11
- def module_extensions
12
- [:c, :h]
13
- end
14
-
15
- # @param name [String] name of the module to protect
16
- # @return [String] an include guard statement
17
- def include_guard_begin(name)
18
- "#ifndef #{name}\n#define #{name}"
19
- end
20
-
21
- # @param name [String] name of the module to protect
22
- # @return [String] an include guard end statement
23
- def include_guard_end(name)
24
- "#endif // #{name}"
25
- end
26
-
27
- end
28
- end
29
- end
@@ -1,28 +0,0 @@
1
- require 'cog/languages/c_language'
2
- require 'cog/languages/mixins/c_style_comments'
3
-
4
- module Cog
5
- module Languages
6
-
7
- class CPlusPlusLanguage < Language
8
- include Mixins::CStyleComments
9
-
10
- def module_extensions
11
- [:cpp, :hpp]
12
- end
13
-
14
- def use_named_scope(name)
15
- "using namespace #{name};"
16
- end
17
-
18
- def named_scope_begin(name)
19
- "namespace #{name} {"
20
- end
21
-
22
- def named_scope_end(name)
23
- "} // namespace #{name}"
24
- end
25
-
26
- end
27
- end
28
- end
@@ -1,24 +0,0 @@
1
- require 'cog/languages/c_language'
2
- require 'cog/languages/mixins/c_style_comments'
3
-
4
- module Cog
5
- module Languages
6
-
7
- class CSharpLanguage < Language
8
- include Mixins::CStyleComments
9
-
10
- def module_extensions
11
- [:cs]
12
- end
13
-
14
- def named_scope_begin(name)
15
- "namespace #{name} {"
16
- end
17
-
18
- def named_scope_end(name)
19
- "} // namespace #{name}"
20
- end
21
-
22
- end
23
- end
24
- end
@@ -1,24 +0,0 @@
1
- require 'cog/languages/c_language'
2
- require 'cog/languages/mixins/c_style_comments'
3
-
4
- module Cog
5
- module Languages
6
-
7
- class JavaLanguage < Language
8
- include Mixins::CStyleComments
9
-
10
- def module_extensions
11
- [:java]
12
- end
13
-
14
- def named_scope_begin(name)
15
- "package #{name};"
16
- end
17
-
18
- def named_scope_end(name)
19
- "// end of package: #{name}"
20
- end
21
-
22
- end
23
- end
24
- end
@@ -1,24 +0,0 @@
1
- require 'cog/languages/c_language'
2
- require 'cog/languages/mixins/c_style_comments'
3
-
4
- module Cog
5
- module Languages
6
-
7
- class JavaScriptLanguage < Language
8
- include Mixins::CStyleComments
9
-
10
- def module_extensions
11
- [:js]
12
- end
13
-
14
- def named_scope_begin(name)
15
- "var #{name} = { // named scope"
16
- end
17
-
18
- def named_scope_end(name)
19
- "}; // named scope: #{name}"
20
- end
21
-
22
- end
23
- end
24
- end
@@ -1,73 +0,0 @@
1
- module Cog
2
- module Languages
3
-
4
- # Interface that must be supported by all +cog+ language helpers.
5
- # This base class corresponds to a plain text language.
6
- class Language
7
-
8
- # @param nested_pattern [String] regular expression pattern (as a string) to embed in the regular expression which matches one line comments in this language
9
- # @return [Regexp] pattern for matching one line comments in this language
10
- def comment_pattern(nested_pattern)
11
- /^\s*#{nested_pattern}\s$/
12
- end
13
-
14
- # @param text [String] some text which should be rendered as a comment
15
- # @return [String] a comment appropriate for this language
16
- def comment(text)
17
- if text =~ /\n/
18
- multi_line_comment text
19
- else
20
- one_line_comment text
21
- end
22
- end
23
-
24
- # @api developer
25
- def one_line_comment(text)
26
- text
27
- end
28
-
29
- # @api developer
30
- def multi_line_comment(text)
31
- text.split("\n").collect do |line|
32
- one_line_comment line
33
- end.join "\n"
34
- end
35
-
36
- # @return [Array<String>] a set of extensions needed to define a module in this language
37
- def module_extensions
38
- [:txt]
39
- end
40
-
41
- # @param name [String] name of the scope to use
42
- # @return [String] a using statement for the named scope
43
- def use_named_scope(name)
44
- ""
45
- end
46
-
47
- # @param name [String] name of the scope
48
- # @return [String] begin a named scope
49
- def named_scope_begin(name)
50
- ""
51
- end
52
-
53
- # @param name [String] name of the scope
54
- # @return [String] end the given named scope
55
- def named_scope_end(name)
56
- ""
57
- end
58
-
59
- # @param name [String] name of the module to protect
60
- # @return [String] an include guard statement
61
- def include_guard_begin(name)
62
- ""
63
- end
64
-
65
- # @param name [String] name of the module to protect
66
- # @return [String] an include guard end statement
67
- def include_guard_end(name)
68
- ""
69
- end
70
-
71
- end
72
- end
73
- end
@@ -1,10 +0,0 @@
1
- require 'cog/languages/mixins/c_style_comments'
2
-
3
- module Cog
4
- module Languages
5
-
6
- # Place for mixins which define common patterns in language syntax
7
- module Mixins
8
- end
9
- end
10
- end
@@ -1,23 +0,0 @@
1
- module Cog
2
- module Languages
3
- module Mixins
4
-
5
- # Methods for defining single and multiline c-style comments
6
- module CStyleComments
7
-
8
- def comment_pattern(nested_pattern)
9
- /^\s*(?:\/\/|\/\*)\s*#{nested_pattern}\s*(?:\*\/)?\s*$/i
10
- end
11
-
12
- def one_line_comment(text)
13
- "// #{text}"
14
- end
15
-
16
- def multi_line_comment(text)
17
- "/*\n#{text}\n */"
18
- end
19
-
20
- end
21
- end
22
- end
23
- end