greg 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ce205a1383267a783a85edffb4caddd38698ab3
4
+ data.tar.gz: 53b65e3e6061c23c28aa4c6b4e2262ab554b7499
5
+ SHA512:
6
+ metadata.gz: 98557f0cd97fa4b2947f45487a0d31c943c48601cd0e23997915fc93b19781d3c924c7531e1a431e6ed8c04fc0c93deaeb7084c3af5860af1fcbba6b4330dc2e
7
+ data.tar.gz: 998427235a0c5e6d1f5cf4c820b47214330c4290d64867ce13d80559f20ef116c07ebc67fe39600210cdc60822e6992c27dbac7b53b4cf76541796b167078ea8
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+ gemspec
3
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Federico Iachetti
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Greg
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'greg'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install greg
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/greg/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/greg ADDED
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.expand_path("../../lib", __FILE__))
3
+
4
+ require "colin"
5
+
6
+ options = Colin::Parser.new(ARGV).named_options([:name]).options
7
+
8
+ if options[:h] || options[:help]
9
+ usage = <<-EOF
10
+ Usage:
11
+ greg <project-name> [options]
12
+
13
+ Options:
14
+ --template Template to use
15
+ --force Force creation of project directory (if it already exists, it will be overwriten).
16
+ --output-directory Select where the new project will be located (current directory by default).
17
+ --templates-dir Select the source location for templates (~/.greg_templates by default)
18
+
19
+ Installing a template
20
+ greg --install=<template-name> [--path=<template-path>]
21
+ greg -i <template-name> [--path=<template-path>]
22
+ EOF
23
+
24
+ puts usage
25
+ exit(0)
26
+ end
27
+
28
+ if options[:i] || options[:install]
29
+ require "greg/template_installer"
30
+ installer = Greg::TemplateInstaller.new(options).install
31
+ exit(0)
32
+ end
33
+
34
+ begin
35
+ require "greg"
36
+
37
+ templates_dir = options.delete(:templates_dir)
38
+ Greg.templates_dir = templates_dir if templates_dir
39
+
40
+ options[:template_name] = options.delete(:template)
41
+
42
+ gen = Greg.generator(**options).run
43
+
44
+ def sh(command)
45
+ puts nil, "=== #{command} ==="
46
+ puts `#{command}`
47
+ puts "=" * (8+command.size)
48
+ end
49
+
50
+ # gen.inside_dir do
51
+ # sh "ls -la"
52
+ # sh "ls -la app/"
53
+ # sh "ls -la public/"
54
+ # sh "cat app/TheFruit.rb"
55
+ # sh "cat Gemfile"
56
+ # end
57
+
58
+ rescue Greg::UnexistingTemplateError => e
59
+ error = <<-EOF
60
+ Error:
61
+ "#{e.template}" template doesn't exist.
62
+ EOF
63
+ puts error
64
+
65
+ exit(1)
66
+ rescue Greg::ExistingProjectError => e
67
+ error = <<-EOF
68
+ Error:
69
+ "#{e.dir}" already exists.
70
+ If you want to overwrite it, use the '--force' option.
71
+ EOF
72
+ puts error
73
+
74
+ exit(1)
75
+ end
data/greg.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'greg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "greg"
8
+ spec.version = Greg::VERSION
9
+ spec.authors = ["Federico Iachetti"]
10
+ spec.email = ["iachetti.federico@gmail.com"]
11
+ spec.summary = %q{Simple generator.}
12
+ spec.description = %q{Simple generator.}
13
+ spec.homepage = "https://github.com/iachettifederico/greg"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "matest", "~> 1.5.6"
24
+
25
+ spec.add_runtime_dependency "fattr"
26
+ spec.add_runtime_dependency "colin", "~> 0.1.6"
27
+ end
data/lib/greg.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "greg/version"
2
+
3
+ require "pathname"
4
+ require "fattr"
5
+
6
+ require "greg/generator"
7
+ require "greg/exceptions"
8
+
9
+ require "greg/templates"
10
+
11
+ module Greg
12
+ def self.generator(**options)
13
+ @generator ||= get_generator(**options)
14
+ end
15
+
16
+ def self.get_generator(**options) #name:, template_name: , output_directory: ".", force: false)
17
+ template_name = options[:template_name]
18
+ generator_name = "#{template_name}_generator"
19
+ template = Greg.templates_dir + "/#{template_name}/#{generator_name}"
20
+ if Pathname(template + ".rb").exist?
21
+ require template.to_s
22
+ else
23
+ raise UnexistingTemplateError.new(template_name)
24
+ end
25
+ generator_class_name = generator_name.split("_").map(&:capitalize).join
26
+ generator_class = Kernel.const_get(generator_class_name)
27
+ generator_class.new( **options )
28
+ end
29
+
30
+ def self.templates_dir
31
+ @templates_dir ||= Pathname("~/.greg_templates").expand_path.to_s
32
+ end
33
+
34
+ def self.templates_dir=(new_dir)
35
+ @templates_dir = Pathname(new_dir).expand_path.to_s
36
+ end
37
+
38
+ end
@@ -0,0 +1,15 @@
1
+ module Greg
2
+ class UnexistingTemplateError < Exception
3
+ attr_reader :template
4
+ def initialize(template)
5
+ @template = template
6
+ end
7
+ end
8
+
9
+ class ExistingProjectError < Exception
10
+ attr_reader :dir
11
+ def initialize(dir)
12
+ @dir = dir
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,60 @@
1
+ module Greg
2
+ class Generator
3
+ attr_reader :name
4
+ attr_reader :template_name
5
+ attr_reader :template_path
6
+ attr_reader :output_directory
7
+ attr_reader :current_dir
8
+ attr_reader :force
9
+ attr_reader :generator
10
+
11
+ def initialize(name:, template_name: , output_directory: ".", force: false)
12
+ @name = name
13
+ @template_name = template_name
14
+ @output_directory = Pathname(output_directory + "/" + name).expand_path
15
+ @current_dir = Pathname(".").expand_path
16
+
17
+ @force = force
18
+ end
19
+
20
+ def run
21
+ create_directory
22
+ create_files
23
+ self
24
+ end
25
+
26
+ def files
27
+ []
28
+ end
29
+
30
+ def inside_dir(&block)
31
+ Dir.chdir(output_directory, &block)
32
+ end
33
+
34
+ def template_dir
35
+ Pathname(Greg.templates_dir + "/" + template_name).expand_path.to_s
36
+ end
37
+
38
+ private
39
+
40
+ def create_files
41
+ puts "Creating Project:"
42
+ files.each do |file|
43
+ puts file.msg if file.msg
44
+ file.create!
45
+ end
46
+ end
47
+
48
+ def create_directory
49
+ if output_directory.exist?
50
+ if force
51
+ puts "#{output_directory + template_name} exists, removing it.\n\n"
52
+ output_directory.rmtree
53
+ else
54
+ raise ExistingProjectError.new(output_directory + template_name)
55
+ end
56
+ end
57
+ output_directory.mkpath
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ module Greg
2
+ class TemplateInstaller
3
+
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require "greg/templates/default_template"
2
+ require "greg/templates/file_template"
3
+ require "greg/templates/dir_template"
4
+ require "greg/templates/from_template"
5
+
6
+ require "greg/templates/gemfile_template"
7
+
8
+ require "greg/templates/file_tree_template"
9
+
@@ -0,0 +1,29 @@
1
+ module Greg
2
+ class DefaultTemplate
3
+ def app_name
4
+ generator.name
5
+ end
6
+
7
+ def app_class_name
8
+ @template_class_name ||= generator.name.split("_").map(&:capitalize).join
9
+ end
10
+
11
+ private
12
+
13
+ def initialize(**attrs)
14
+ attrs.each do |attr, value|
15
+ send attr, value
16
+ end
17
+ end
18
+
19
+ def generator
20
+ Greg.generator
21
+ end
22
+
23
+ def self.attribute(attr)
24
+ fattr attr
25
+ end
26
+ private_class_method :attribute
27
+
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ module Greg
2
+ class DirTemplate < DefaultTemplate
3
+ attr_reader :destination
4
+ def initialize(destination)
5
+ @destination = destination
6
+ end
7
+
8
+ def create!
9
+ generator.inside_dir do
10
+ new_dir = Pathname(destination).expand_path
11
+ new_dir.mkdir unless new_dir.exist?
12
+ end
13
+ end
14
+
15
+ def msg
16
+ " [create] #{destination}/"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ require "erb"
2
+
3
+ module Greg
4
+ class FileTemplate < DefaultTemplate
5
+ attribute :destination
6
+ attribute :contents
7
+ def create!
8
+ Greg.generator.inside_dir do
9
+ current_file = Pathname(destination).expand_path
10
+ current_file.dirname.mkdir unless current_file.dirname.exist?
11
+ current_file.write(contents)
12
+ end
13
+ end
14
+
15
+ def msg
16
+ " [create] #{destination}"
17
+ end
18
+
19
+ private
20
+
21
+ def template(file)
22
+ Pathname(generator.template_dir + "/" + file).expand_path.read
23
+ end
24
+
25
+ def erb(str)
26
+ ERB.new(str).result(instance_eval { binding })
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,48 @@
1
+ require "pp"
2
+ module Greg
3
+ class FileTreeTemplate < DefaultTemplate
4
+ attr_reader :templates
5
+
6
+ def initialize(templates)
7
+ @templates ||= templates
8
+ end
9
+
10
+ #def each(&block)
11
+ def create!
12
+ iterate_tree(templates) do |entry|
13
+ puts entry.msg if entry.msg
14
+ entry.create!
15
+ end
16
+ end
17
+
18
+ def msg
19
+ nil
20
+ end
21
+
22
+ private
23
+ def iterate_tree(dir=".", path, &block)
24
+ current_relative_path = Pathname([path, dir].join("/")).cleanpath
25
+ output_path = Pathname(current_relative_path.to_s.gsub(/\A#{templates}/, ".")).cleanpath
26
+
27
+ current_path = Pathname([
28
+ Greg.templates_dir,
29
+ Greg.generator.template_name,
30
+ current_relative_path
31
+ ].join("/"))
32
+
33
+ (Dir.new(current_path).entries - [".", ".."]).each do |entry|
34
+ new_name = entry.
35
+ gsub("@APP_NAME@", Greg.generator.name).
36
+ gsub("@TEMPLATE_NAME@", Greg.generator.template_name)
37
+ destination = (output_path + new_name).cleanpath.to_s
38
+ if (current_path + entry).directory?
39
+ block.call(DirTemplate.new(destination))
40
+ iterate_tree(entry, current_relative_path, &block)
41
+ else
42
+ template_name = (current_relative_path + entry).cleanpath.to_s
43
+ block.call(FromTemplate.new(destination, template_name))
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,15 @@
1
+ module Greg
2
+ class FromTemplate < FileTemplate
3
+ attr_reader :destination
4
+ attr_reader :contents
5
+
6
+ def initialize(destination, template)
7
+ @destination = destination
8
+ @contents = erb( template(template) )
9
+ end
10
+
11
+ def msg
12
+ " [create] #{destination}"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ module Greg
2
+ class GemfileTemplate < FileTemplate
3
+ attr_reader :gems
4
+
5
+ def initialize(*gems)
6
+ @gems = gems
7
+ end
8
+
9
+ def destination
10
+ "Gemfile"
11
+ end
12
+
13
+ def contents
14
+ @contents = []
15
+ @contents << 'source "https://rubygems.org"'
16
+ @contents << nil
17
+
18
+ @gems.each do |gem|
19
+ if gem.is_a?(Hash)
20
+ gem_name, gem_options = gem.first
21
+ r = []
22
+ r << "gem \"#{gem_name}\""
23
+ gem_options.each do |key, value|
24
+ r << "#{key.to_sym}: \"#{value.to_s}\""
25
+ end
26
+ @contents << r.join(", ")
27
+ else
28
+ @contents << "gem \"#{gem}\""
29
+ end
30
+ end
31
+
32
+ @contents.join("\n")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Greg
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: greg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Federico Iachetti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: matest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.6
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.5.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: fattr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: colin
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.1.6
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.1.6
83
+ description: Simple generator.
84
+ email:
85
+ - iachetti.federico@gmail.com
86
+ executables:
87
+ - greg
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/greg
97
+ - greg.gemspec
98
+ - lib/greg.rb
99
+ - lib/greg/exceptions.rb
100
+ - lib/greg/generator.rb
101
+ - lib/greg/template_installer.rb
102
+ - lib/greg/templates.rb
103
+ - lib/greg/templates/default_template.rb
104
+ - lib/greg/templates/dir_template.rb
105
+ - lib/greg/templates/file_template.rb
106
+ - lib/greg/templates/file_tree_template.rb
107
+ - lib/greg/templates/from_template.rb
108
+ - lib/greg/templates/gemfile_template.rb
109
+ - lib/greg/version.rb
110
+ homepage: https://github.com/iachettifederico/greg
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.4.6
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Simple generator.
134
+ test_files: []