p 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/Gemfile.lock +20 -0
- data/README.md +26 -0
- data/Rakefile +9 -0
- data/bin/p +2 -1
- data/lib/p.rb +11 -1
- data/lib/p/actions.rb +29 -0
- data/lib/p/actions/template_file.rb +34 -0
- data/lib/p/builder.rb +60 -0
- data/lib/p/command.rb +16 -0
- data/lib/p/template.rb +28 -0
- data/lib/templates/gem.rb +9 -0
- metadata +36 -10
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
p (0.1.3)
|
5
|
+
thor (~> 0.18.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
minitest (5.0.8)
|
11
|
+
rake (10.1.0)
|
12
|
+
thor (0.18.1)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
minitest (~> 5.0)
|
19
|
+
p!
|
20
|
+
rake (~> 10.0)
|
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# p [![Gem Version](https://badge.fury.io/rb/p.png)](http://badge.fury.io/rb/p) [![Build Status](https://travis-ci.org/Lytol/p.png)](https://travis-ci.org/Lytol/p)
|
2
|
+
|
3
|
+
`p` is a project generator
|
4
|
+
|
5
|
+
|
6
|
+
Usage
|
7
|
+
-----
|
8
|
+
|
9
|
+
`p <project-type> [project-name]`
|
10
|
+
|
11
|
+
If `project-name` is omitted, it will use the current directory; otherwise, it will create the project in a new directory also called `project-name`.
|
12
|
+
|
13
|
+
|
14
|
+
Design
|
15
|
+
------
|
16
|
+
|
17
|
+
- Templates are defined in `templates/*.rb`
|
18
|
+
- Definitions should include `options` and `actions`
|
19
|
+
- `options` should be able to have defaults and can be required or optional
|
20
|
+
- `actions` can inherit from other actions types
|
21
|
+
|
22
|
+
References
|
23
|
+
----------
|
24
|
+
|
25
|
+
* `bundle gem`
|
26
|
+
* Rails generator
|
data/Rakefile
ADDED
data/bin/p
CHANGED
data/lib/p.rb
CHANGED
@@ -1,5 +1,15 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
1
3
|
module P
|
2
4
|
|
3
|
-
VERSION = "0.1.
|
5
|
+
VERSION = "0.1.3"
|
4
6
|
|
7
|
+
def self.template_paths
|
8
|
+
[ File.join(File.dirname(__FILE__), "templates") ]
|
9
|
+
end
|
5
10
|
end
|
11
|
+
|
12
|
+
require_relative "p/template"
|
13
|
+
require_relative "p/actions"
|
14
|
+
require_relative "p/actions/template_file"
|
15
|
+
require_relative "p/builder"
|
data/lib/p/actions.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module P
|
2
|
+
class FileNotFound < StandardError; end
|
3
|
+
|
4
|
+
module Actions
|
5
|
+
|
6
|
+
def touch(path)
|
7
|
+
say "Touching file `#{path}`" do
|
8
|
+
FileUtils.touch(destination_file(path))
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def file(path, &block)
|
13
|
+
say "Creating file `#{path}`" do
|
14
|
+
source = source_file(path)
|
15
|
+
destination = destination_file(path)
|
16
|
+
|
17
|
+
file = P::Actions::TemplateFile.new(self, source, destination)
|
18
|
+
file.instance_eval(&block) if block_given?
|
19
|
+
file.run!
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def directory(path)
|
24
|
+
say "Creating directory `#{path}`" do
|
25
|
+
FileUtils.mkdir(destination_file(path))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
module P
|
4
|
+
module Actions
|
5
|
+
class TemplateFile
|
6
|
+
def initialize(builder, source, destination)
|
7
|
+
@builder = builder
|
8
|
+
@source = source
|
9
|
+
@destination = destination
|
10
|
+
end
|
11
|
+
|
12
|
+
def source(src = nil)
|
13
|
+
if src.nil?
|
14
|
+
@source
|
15
|
+
else
|
16
|
+
@source = @builder.source_file(src)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def run!
|
21
|
+
unless File.exists?(@source)
|
22
|
+
raise(P::FileNotFound, "missing template file `#{@source}`")
|
23
|
+
end
|
24
|
+
|
25
|
+
if File.extname(@source) == ".erb"
|
26
|
+
content = ERB.new(IO.read(@source)).result(@builder._binding)
|
27
|
+
IO.write(@destination, content)
|
28
|
+
else
|
29
|
+
FileUtils.cp(@source, @destination)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/p/builder.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module P
|
2
|
+
class Builder
|
3
|
+
include P::Actions
|
4
|
+
|
5
|
+
attr_accessor :template
|
6
|
+
|
7
|
+
|
8
|
+
def self.run!(template)
|
9
|
+
new(template).run!
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(template)
|
13
|
+
@template = template
|
14
|
+
copy_template_variables!
|
15
|
+
end
|
16
|
+
|
17
|
+
def run!
|
18
|
+
announce "Generating #{@template.template} project `#{@name}`"
|
19
|
+
|
20
|
+
say "Creating base directory `#{@name}`" do
|
21
|
+
FileUtils.mkdir(base_directory)
|
22
|
+
end
|
23
|
+
|
24
|
+
eval(IO.read(@template.path))
|
25
|
+
end
|
26
|
+
|
27
|
+
def source_file(path)
|
28
|
+
File.join(@template.directory, path)
|
29
|
+
end
|
30
|
+
|
31
|
+
def destination_file(path)
|
32
|
+
File.join(base_directory, path)
|
33
|
+
end
|
34
|
+
|
35
|
+
def _binding
|
36
|
+
binding
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def copy_template_variables!
|
42
|
+
@template.variables.each do |k,v|
|
43
|
+
instance_variable_set("@#{k}".to_sym, v)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def base_directory
|
48
|
+
File.join(Dir.pwd, @name)
|
49
|
+
end
|
50
|
+
|
51
|
+
def announce(str)
|
52
|
+
$stdout.print "= #{str}\n"
|
53
|
+
end
|
54
|
+
|
55
|
+
def say(str, &block)
|
56
|
+
$stdout.print("- #{str}\n")
|
57
|
+
yield block
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/p/command.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'thor'
|
2
|
+
|
3
|
+
module P
|
4
|
+
class Command < Thor::Group
|
5
|
+
desc "Generate a project using TEMPLATE and NAME"
|
6
|
+
namespace ''
|
7
|
+
|
8
|
+
argument :template, type: :string, desc: "The template for the project"
|
9
|
+
argument :name, type: :string, desc: "The name of the project"
|
10
|
+
|
11
|
+
def default
|
12
|
+
t = P::Template.new(template, name)
|
13
|
+
P::Builder.run!(t)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/p/template.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module P
|
2
|
+
class TemplateNotFound < StandardError; end
|
3
|
+
|
4
|
+
class Template
|
5
|
+
attr_reader :template, :path, :directory, :name
|
6
|
+
|
7
|
+
def initialize(template, name)
|
8
|
+
@template = template
|
9
|
+
@path = find_template(template)
|
10
|
+
@directory = File.join(File.dirname(@path), template)
|
11
|
+
@name = name
|
12
|
+
end
|
13
|
+
|
14
|
+
def variables
|
15
|
+
{ name: name }
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def find_template(t)
|
21
|
+
P.template_paths.each do |base|
|
22
|
+
path = File.join(base, t + ".rb")
|
23
|
+
return path if File.exists?(path)
|
24
|
+
end
|
25
|
+
raise(TemplateNotFound, "Could not find template `#{t}` in: #{P.template_paths.inspect}")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: p
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,40 +9,56 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-11-
|
12
|
+
date: 2013-11-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: thor
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.18.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.18.0
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: rake
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
17
33
|
none: false
|
18
34
|
requirements:
|
19
|
-
- -
|
35
|
+
- - ~>
|
20
36
|
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
37
|
+
version: '10.0'
|
22
38
|
type: :development
|
23
39
|
prerelease: false
|
24
40
|
version_requirements: !ruby/object:Gem::Requirement
|
25
41
|
none: false
|
26
42
|
requirements:
|
27
|
-
- -
|
43
|
+
- - ~>
|
28
44
|
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
45
|
+
version: '10.0'
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: minitest
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
33
49
|
none: false
|
34
50
|
requirements:
|
35
|
-
- -
|
51
|
+
- - ~>
|
36
52
|
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
53
|
+
version: '5.0'
|
38
54
|
type: :development
|
39
55
|
prerelease: false
|
40
56
|
version_requirements: !ruby/object:Gem::Requirement
|
41
57
|
none: false
|
42
58
|
requirements:
|
43
|
-
- -
|
59
|
+
- - ~>
|
44
60
|
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
61
|
+
version: '5.0'
|
46
62
|
description:
|
47
63
|
email: bsmith@swig505.com
|
48
64
|
executables:
|
@@ -50,7 +66,17 @@ executables:
|
|
50
66
|
extensions: []
|
51
67
|
extra_rdoc_files: []
|
52
68
|
files:
|
69
|
+
- lib/p/actions/template_file.rb
|
70
|
+
- lib/p/actions.rb
|
71
|
+
- lib/p/builder.rb
|
72
|
+
- lib/p/command.rb
|
73
|
+
- lib/p/template.rb
|
53
74
|
- lib/p.rb
|
75
|
+
- lib/templates/gem.rb
|
76
|
+
- Gemfile
|
77
|
+
- Gemfile.lock
|
78
|
+
- Rakefile
|
79
|
+
- README.md
|
54
80
|
- test/p_test.rb
|
55
81
|
- bin/p
|
56
82
|
homepage: https://github.com/Lytol/p
|