canpe 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.canpe_repos/default_repository/binding.yml +5 -0
- data/.canpe_repos/default_repository/templates/.canpe_repos/<%= canpe[:repository_name] %>/binding.yml +5 -0
- data/.canpe_repos/default_repository/templates/.canpe_repos/<%= canpe[:repository_name] %>/templates/sample.html.erb +2 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +82 -0
- data/Rakefile +6 -0
- data/bin/canpe +4 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/canpe.gemspec +42 -0
- data/lib/canpe.rb +16 -0
- data/lib/canpe/command.rb +51 -0
- data/lib/canpe/file_manipulation.rb +35 -0
- data/lib/canpe/repository.rb +55 -0
- data/lib/canpe/repository_creator.rb +25 -0
- data/lib/canpe/repository_operation.rb +49 -0
- data/lib/canpe/repository_operation_context.rb +47 -0
- data/lib/canpe/repository_store.rb +63 -0
- data/lib/canpe/runner.rb +24 -0
- data/lib/canpe/template_binding.rb +17 -0
- data/lib/canpe/template_binding_container.rb +19 -0
- data/lib/canpe/template_binding_reflector.rb +22 -0
- data/lib/canpe/template_renderer.rb +74 -0
- data/lib/canpe/version.rb +3 -0
- metadata +202 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 58a3d41ace2ef08b090677809699338f608c3aa1
|
4
|
+
data.tar.gz: 06fbec2a66e046f41872c7292f105f3aaf1bdc52
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dbc6951633d03049b4ba48f1df33f4b9023a7a646f2bee5c302d395d9e8fc118c2d81af7cc9a5374e6ad5f6e393aefe2d0138932c5e6e24474caa0d66a210266
|
7
|
+
data.tar.gz: 07713abd6cf82132bdcd70e41975ddeb7d223561d22aa2257e3bac0fccb98f6da0e0b9849928599311e8e3bbe1af3ce3d222bb5e2ba9a05db994889770e0ce67
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at yoshinori.ksk@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Yoshinori Kosaka
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# Canpe
|
2
|
+
|
3
|
+
Canpe is a template-based file generator.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install canpe
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
When run the `canpe create sample_repository`, canpe initialize new repo in the current directory.
|
12
|
+
|
13
|
+
```
|
14
|
+
$ canpe create sample_repository
|
15
|
+
create directory: ./.canpe_repos/sample_repository/templates
|
16
|
+
copy ./.canpe_repos/sample_repository/binding.yml
|
17
|
+
```
|
18
|
+
|
19
|
+
`binding.yml` defines the variables which you can access from templates.
|
20
|
+
|
21
|
+
```
|
22
|
+
variables:
|
23
|
+
- name: sample_string
|
24
|
+
type: string
|
25
|
+
- name: sample_array
|
26
|
+
type: array
|
27
|
+
```
|
28
|
+
|
29
|
+
Set your template files under the templates directory.
|
30
|
+
Each file is automatically evaluated as ERB template.
|
31
|
+
|
32
|
+
```
|
33
|
+
<%= canpe[:sample_string] %>
|
34
|
+
<%= canpe[:sample_array].join ', ' %>
|
35
|
+
```
|
36
|
+
|
37
|
+
Then run `canpe generate sample_repository`.
|
38
|
+
|
39
|
+
```
|
40
|
+
$ bundle exec bin/canpe generate sample_repository
|
41
|
+
working directory (/Users/Yoshinori/gem_projects/canpe) ?
|
42
|
+
you need to set variables to generate codes!
|
43
|
+
1: sample_string (string)
|
44
|
+
2: sample_array (array)
|
45
|
+
|
46
|
+
If you want to stop setting array, let it blank and press enter.
|
47
|
+
sample_string ?) Hello, world
|
48
|
+
sample_array[0] ?) 1
|
49
|
+
sample_array[1] ?) 2
|
50
|
+
sample_array[2] ?) 3
|
51
|
+
sample_array[3] ?)
|
52
|
+
finished variable settings: {"sample_string"=>"Hello, world", "sample_array"=>["1", "2", "3"]}
|
53
|
+
copy: /Users/Yoshinori/gem_projects/canpe/hello
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
If you set sample_string as 'Hello, world', and sample_array as [1, 2, 3],
|
58
|
+
This template is rendered like this.
|
59
|
+
|
60
|
+
```
|
61
|
+
Hello, world
|
62
|
+
1, 2, 3
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
## Development
|
67
|
+
|
68
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
69
|
+
|
70
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ykosaka/canpe. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
75
|
+
|
76
|
+
## License
|
77
|
+
|
78
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
79
|
+
|
80
|
+
## Code of Conduct
|
81
|
+
|
82
|
+
Everyone interacting in the Canpe project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/canpe/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/canpe
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "canpe"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "pry"
|
14
|
+
require "awesome_print"
|
15
|
+
Pry.start
|
data/bin/setup
ADDED
data/canpe.gemspec
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "canpe/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "canpe"
|
8
|
+
spec.version = Canpe::VERSION
|
9
|
+
spec.authors = ["Yoshinori Kosaka"]
|
10
|
+
spec.email = ["yoshinori.ksk@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = 'customizable template generator written by Ruby.'
|
13
|
+
spec.description = ''
|
14
|
+
spec.homepage = 'https://github.com/ykosaka/canpe'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "bin"
|
30
|
+
spec.executables = ['canpe']
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_dependency "thor", "~> 0.20"
|
34
|
+
spec.add_dependency "tilt", "2.0.8"
|
35
|
+
spec.add_dependency "activesupport", "~> 5"
|
36
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
38
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
39
|
+
spec.add_development_dependency "pry", "~> 0"
|
40
|
+
spec.add_development_dependency "pry-byebug", "~> 3.5"
|
41
|
+
spec.add_development_dependency "awesome_print", "~> 1.8"
|
42
|
+
end
|
data/lib/canpe.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'canpe/version'
|
4
|
+
require 'canpe/command'
|
5
|
+
require 'canpe/repository'
|
6
|
+
require 'canpe/repository_store'
|
7
|
+
require 'canpe/repository_operation'
|
8
|
+
require 'canpe/repository_operation_context'
|
9
|
+
require 'canpe/template_binding'
|
10
|
+
require 'canpe/template_renderer'
|
11
|
+
|
12
|
+
require 'canpe/runner'
|
13
|
+
require 'canpe/file_manipulation'
|
14
|
+
|
15
|
+
module Canpe
|
16
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'thor'
|
3
|
+
|
4
|
+
require 'canpe/runner'
|
5
|
+
require 'canpe/repository_store'
|
6
|
+
|
7
|
+
class Canpe::Command < Thor
|
8
|
+
|
9
|
+
desc "generate repo_name", "Generate files from repository"
|
10
|
+
|
11
|
+
def generate(repository_name)
|
12
|
+
repository_list = Canpe::RepositoryStore.repository_list
|
13
|
+
repository = repository_list.find { |repository| repository.match? repository_name }
|
14
|
+
|
15
|
+
if repository.blank?
|
16
|
+
$stderr.puts "Could not find repository \"#{repository_name}\"."
|
17
|
+
exit 1
|
18
|
+
end
|
19
|
+
|
20
|
+
Canpe::Runner.generate repository
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "create repo_name", "create repository"
|
24
|
+
|
25
|
+
def create(repository_name)
|
26
|
+
Canpe::Runner.create repository_name
|
27
|
+
end
|
28
|
+
|
29
|
+
desc "list", "List up registered repositorys"
|
30
|
+
|
31
|
+
def list
|
32
|
+
repository_list = Canpe::RepositoryStore.repository_list
|
33
|
+
repository_list.each do |repository_name|
|
34
|
+
puts repository_name
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
desc "open", "open registered repository"
|
39
|
+
|
40
|
+
def open(repository_name)
|
41
|
+
repository_list = Canpe::RepositoryStore.repository_list
|
42
|
+
repository = repository_list.find { |repository| repository.match? repository_name }
|
43
|
+
|
44
|
+
if repository.blank?
|
45
|
+
$stderr.puts "Could not find repository \"#{repository_name}\"."
|
46
|
+
exit 1
|
47
|
+
end
|
48
|
+
|
49
|
+
`open #{repository.repository_url}`
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module Canpe
|
4
|
+
module FileManipulation
|
5
|
+
|
6
|
+
class NotDirectoryError < StandardError; end
|
7
|
+
|
8
|
+
def create_directory(url)
|
9
|
+
puts "create directory: #{url}"
|
10
|
+
FileUtils.mkdir_p(url)
|
11
|
+
end
|
12
|
+
|
13
|
+
def copy_file(source_url, destination_url)
|
14
|
+
puts "copy: #{destination_url}"
|
15
|
+
|
16
|
+
directory = File.dirname(destination_url)
|
17
|
+
|
18
|
+
if File.exists?(directory)
|
19
|
+
if File.directory?(directory)
|
20
|
+
FileUtils.cp(source_url, destination_url)
|
21
|
+
else
|
22
|
+
raise NotDirectoryError, "Failed to copy #{destination_url}, because #{directory} is not a directory."
|
23
|
+
end
|
24
|
+
else
|
25
|
+
create_directory(directory)
|
26
|
+
FileUtils.cp(source_url, destination_url)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def remove_file(url)
|
31
|
+
puts "delete: #{url}"
|
32
|
+
FileUtils.rm(url)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'yaml'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'pry'
|
6
|
+
require 'canpe/repository_store'
|
7
|
+
|
8
|
+
module Canpe
|
9
|
+
class Repository
|
10
|
+
attr_reader :name, :store
|
11
|
+
|
12
|
+
def initialize(name, store)
|
13
|
+
@name = name
|
14
|
+
@store = store
|
15
|
+
end
|
16
|
+
|
17
|
+
# Directory which is root directory of this repository
|
18
|
+
def repository_url
|
19
|
+
File.join(store.repositories_root, name)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Directory in which templates file exists
|
23
|
+
def templates_url
|
24
|
+
File.join(store.repositories_root, name, 'templates')
|
25
|
+
end
|
26
|
+
|
27
|
+
def binding_option_url
|
28
|
+
File.join(repository_url, 'binding.yml')
|
29
|
+
end
|
30
|
+
|
31
|
+
def binding_options
|
32
|
+
@_binding_options ||= File.exists?(binding_option_url) ? YAML.load_file(binding_option_url) : {}
|
33
|
+
end
|
34
|
+
|
35
|
+
def file_paths(absolute_path: false)
|
36
|
+
files = Dir.glob(File.join(templates_url, '**', '*'), File::FNM_DOTMATCH)
|
37
|
+
files = files.reject { |e| [".", ".."].any? { |s| s== File::basename(e) } }
|
38
|
+
|
39
|
+
if absolute_path
|
40
|
+
files.map { |file| Pathname.new(file) }
|
41
|
+
else
|
42
|
+
base = Pathname.new(templates_url)
|
43
|
+
files.map { |file| Pathname.new(file).relative_path_from(base) }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_s
|
48
|
+
"#{store.name}::#{name}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def match?(query)
|
52
|
+
/#{query}/ === to_s
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'canpe/file_manipulation'
|
2
|
+
require 'canpe/repository_store'
|
3
|
+
|
4
|
+
module Canpe
|
5
|
+
class RepositoryCreator
|
6
|
+
include FileManipulation
|
7
|
+
attr_reader :repository_name
|
8
|
+
|
9
|
+
def initialize(repository_name)
|
10
|
+
@repository_name = repository_name
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute
|
14
|
+
repository_list = Canpe::RepositoryStore.repository_list
|
15
|
+
repository = repository_list.find { |repository| repository.match? 'default_repository' }
|
16
|
+
|
17
|
+
if repository.blank?
|
18
|
+
$stderr.puts "Could not find repository \"#{repository_name}\"."
|
19
|
+
exit 1
|
20
|
+
end
|
21
|
+
|
22
|
+
Canpe::Runner.generate repository, options: { variables: { repository_name: repository_name }, working_directory: Dir.pwd }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'tilt'
|
4
|
+
|
5
|
+
require 'canpe/file_manipulation'
|
6
|
+
require 'canpe/template_renderer'
|
7
|
+
require 'canpe/repository_operation_context'
|
8
|
+
|
9
|
+
module Canpe
|
10
|
+
class RepositoryOperation
|
11
|
+
include FileManipulation
|
12
|
+
|
13
|
+
attr_reader :repository, :renderer, :context
|
14
|
+
|
15
|
+
def initialize(repository)
|
16
|
+
@repository = repository
|
17
|
+
@renderer = TemplateRenderer.new(self)
|
18
|
+
@context = RepositoryOperationContext.new(self)
|
19
|
+
end
|
20
|
+
|
21
|
+
def prepare_operation(options = {})
|
22
|
+
context.prepare(options)
|
23
|
+
renderer.prepare(options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def generate_file(path)
|
27
|
+
if File.directory?(context.source_file_path(path))
|
28
|
+
create_evaluated_directory(path)
|
29
|
+
else
|
30
|
+
copy_evaluated_file(path)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def create_evaluated_directory(path)
|
35
|
+
path = renderer.render_string(path)
|
36
|
+
url = File.join(context.destination_root, path)
|
37
|
+
create_directory(url)
|
38
|
+
end
|
39
|
+
|
40
|
+
def copy_evaluated_file(path)
|
41
|
+
template_file = renderer.render_file(context.source_file_path(path))
|
42
|
+
copy_file(template_file.path, context.destination_file_path(path))
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete_file(path)
|
46
|
+
super context.destination_file_path(path)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Canpe
|
2
|
+
class RepositoryOperationContext
|
3
|
+
attr_accessor :repository_operation, :root_directory
|
4
|
+
|
5
|
+
def initialize(repository_operation, root_directory = nil)
|
6
|
+
@repository_operation = repository_operation
|
7
|
+
@root_directory = root_directory
|
8
|
+
end
|
9
|
+
|
10
|
+
def repository
|
11
|
+
repository_operation.repository
|
12
|
+
end
|
13
|
+
|
14
|
+
def prepare(options)
|
15
|
+
working_directory = options[:working_directory]
|
16
|
+
|
17
|
+
if working_directory.nil?
|
18
|
+
print "working directory (#{Dir.pwd}) ? "
|
19
|
+
working_directory = STDIN.gets.chomp
|
20
|
+
end
|
21
|
+
|
22
|
+
if working_directory.blank?
|
23
|
+
working_directory = Dir.pwd
|
24
|
+
elsif !File.directory?(working_directory)
|
25
|
+
raise FileManipulation::NotDirectoryError, "#{working_directory} is not a directory."
|
26
|
+
end
|
27
|
+
|
28
|
+
@root_directory = working_directory
|
29
|
+
end
|
30
|
+
|
31
|
+
def source_root
|
32
|
+
repository.templates_url
|
33
|
+
end
|
34
|
+
|
35
|
+
def destination_root
|
36
|
+
root_directory
|
37
|
+
end
|
38
|
+
|
39
|
+
def source_file_path(path)
|
40
|
+
File.join(source_root, path)
|
41
|
+
end
|
42
|
+
|
43
|
+
def destination_file_path(path)
|
44
|
+
repository_operation.renderer.render_string(File.join(destination_root, path))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'pry'
|
5
|
+
|
6
|
+
require 'canpe/repository'
|
7
|
+
|
8
|
+
module Canpe
|
9
|
+
class RepositoryStore
|
10
|
+
attr_reader :path
|
11
|
+
|
12
|
+
LOAD_PATHS = [Dir.pwd, ENV['HOME'], Gem::Specification.find_by_name('canpe').gem_dir]
|
13
|
+
DEFAULT_REPOSITORY_DIR = '.canpe_repos'
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def repository_stores
|
17
|
+
stores = []
|
18
|
+
LOAD_PATHS.each do |path|
|
19
|
+
repository_store_path = File.join(path, DEFAULT_REPOSITORY_DIR)
|
20
|
+
stores << new(path) if File.directory?(repository_store_path)
|
21
|
+
end
|
22
|
+
stores
|
23
|
+
end
|
24
|
+
|
25
|
+
def repository_list
|
26
|
+
@_repository_list ||= repository_stores.map { |store| store.repository_list }.flatten
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(path)
|
31
|
+
@path = path
|
32
|
+
end
|
33
|
+
|
34
|
+
def root_url
|
35
|
+
path
|
36
|
+
end
|
37
|
+
|
38
|
+
def repository_dir
|
39
|
+
DEFAULT_REPOSITORY_DIR
|
40
|
+
end
|
41
|
+
|
42
|
+
def repositories_root
|
43
|
+
File.join(root_url, repository_dir)
|
44
|
+
end
|
45
|
+
|
46
|
+
def repository_list
|
47
|
+
repository_list = []
|
48
|
+
dir_list = Dir.glob(File.join(repositories_root, '*'))
|
49
|
+
dir_list.each do |dir_path|
|
50
|
+
repository_list << Repository.new(File.basename(dir_path), self) if File.directory? dir_path
|
51
|
+
end
|
52
|
+
repository_list
|
53
|
+
end
|
54
|
+
|
55
|
+
def name
|
56
|
+
if path == ENV['HOME']
|
57
|
+
'HOME'
|
58
|
+
else
|
59
|
+
Pathname.new(path).relative_path_from(Pathname.new(ENV['HOME']))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/canpe/runner.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'canpe/repository'
|
3
|
+
require 'canpe/repository_store'
|
4
|
+
require 'canpe/repository_operation'
|
5
|
+
require 'canpe/repository_creator'
|
6
|
+
require 'tilt'
|
7
|
+
require 'pry'
|
8
|
+
|
9
|
+
module Canpe
|
10
|
+
class Runner
|
11
|
+
class << self
|
12
|
+
def generate(repository, options: {})
|
13
|
+
operation = RepositoryOperation.new(repository)
|
14
|
+
operation.prepare_operation(options)
|
15
|
+
repository.file_paths.each { |path| operation.generate_file(path) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def create(repository_name)
|
19
|
+
repository_creator = RepositoryCreator.new(repository_name)
|
20
|
+
repository_creator.execute
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'canpe/template_binding_container'
|
4
|
+
|
5
|
+
module Canpe
|
6
|
+
class TemplateBinding
|
7
|
+
attr_reader :canpe_hash
|
8
|
+
|
9
|
+
def initialize(canpe_hash = {})
|
10
|
+
@canpe_hash = canpe_hash
|
11
|
+
end
|
12
|
+
|
13
|
+
def canpe
|
14
|
+
@_canpe ||= TemplateBindingContainer.new(canpe_hash)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'canpe/template_binding_reflector'
|
2
|
+
|
3
|
+
module Canpe
|
4
|
+
class TemplateBindingContainer
|
5
|
+
attr_reader :hash
|
6
|
+
|
7
|
+
def initialize(hash)
|
8
|
+
@hash = hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](name)
|
12
|
+
if hash.key? name
|
13
|
+
hash[name]
|
14
|
+
else
|
15
|
+
TemplateBindingReflector.new([{method: name, args: []}])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Canpe
|
2
|
+
class TemplateBindingReflector
|
3
|
+
attr_reader :call_list
|
4
|
+
|
5
|
+
def initialize(call_list = [])
|
6
|
+
@call_list = call_list
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(method, *args)
|
10
|
+
Reflector.new(call_list + [{ method: method, args: args }])
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
method_chain = call_list.map do |hash|
|
15
|
+
method_call = "#{hash[:method].to_s}"
|
16
|
+
method_call += "(#{hash[:args].join(', ')})" if hash[:args].size > 0
|
17
|
+
method_call
|
18
|
+
end.join '.'
|
19
|
+
"Error: #{method_chain}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'canpe/template_binding'
|
2
|
+
|
3
|
+
module Canpe
|
4
|
+
class TemplateRenderer
|
5
|
+
attr_accessor :repository_operation, :injected_hash
|
6
|
+
|
7
|
+
def initialize(repository_operation, hash = {}.with_indifferent_access)
|
8
|
+
@repository_operation = repository_operation
|
9
|
+
@injected_hash = hash
|
10
|
+
end
|
11
|
+
|
12
|
+
def repository
|
13
|
+
repository_operation.repository
|
14
|
+
end
|
15
|
+
|
16
|
+
def prepare(options)
|
17
|
+
if options.key? :variables
|
18
|
+
injected_hash.merge!(options[:variables])
|
19
|
+
return
|
20
|
+
end
|
21
|
+
|
22
|
+
if repository.binding_options['variables'].blank?
|
23
|
+
return puts 'skip variable injection.'
|
24
|
+
end
|
25
|
+
|
26
|
+
puts 'you need to set variables to generate codes!'
|
27
|
+
repository.binding_options['variables'].each.with_index(1) do |entry, index|
|
28
|
+
puts "#{index}: #{entry['name']} (#{entry['type']}) "
|
29
|
+
end
|
30
|
+
|
31
|
+
puts ''
|
32
|
+
puts 'If you want to stop setting array, let it blank and press enter.'
|
33
|
+
|
34
|
+
hash = {}
|
35
|
+
|
36
|
+
repository.binding_options['variables'].each do |entry|
|
37
|
+
if entry['type'] == 'string'
|
38
|
+
print "#{entry['name']} ?) "
|
39
|
+
hash[entry['name']] = STDIN.gets.chomp
|
40
|
+
elsif entry['type'] == 'array'
|
41
|
+
array = []
|
42
|
+
loop do
|
43
|
+
print "#{entry['name']}[#{array.size}] ?) "
|
44
|
+
input = STDIN.gets.chomp
|
45
|
+
|
46
|
+
if input.present?
|
47
|
+
array << input
|
48
|
+
else
|
49
|
+
break
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
hash[entry['name']] = array
|
54
|
+
end
|
55
|
+
end
|
56
|
+
injected_hash.merge!(hash)
|
57
|
+
|
58
|
+
puts "finished variable settings: #{hash}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def render_string(str)
|
62
|
+
template = Tilt::ERBTemplate.new(trim: false) { str.to_s }
|
63
|
+
template.render(Canpe::TemplateBinding.new(injected_hash))
|
64
|
+
end
|
65
|
+
|
66
|
+
def render_file(path)
|
67
|
+
Tempfile.new.tap do |file|
|
68
|
+
template = Tilt::ERBTemplate.new(path, trim: false)
|
69
|
+
file.write(template.render(Canpe::TemplateBinding.new(injected_hash)))
|
70
|
+
file.rewind
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: canpe
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yoshinori Kosaka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.20'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.20'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: tilt
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.0.8
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.0.8
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.15'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.15'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry-byebug
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.5'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.5'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: awesome_print
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '1.8'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '1.8'
|
139
|
+
description: ''
|
140
|
+
email:
|
141
|
+
- yoshinori.ksk@gmail.com
|
142
|
+
executables:
|
143
|
+
- canpe
|
144
|
+
extensions: []
|
145
|
+
extra_rdoc_files: []
|
146
|
+
files:
|
147
|
+
- ".canpe_repos/default_repository/binding.yml"
|
148
|
+
- ".canpe_repos/default_repository/templates/.canpe_repos/<%= canpe[:repository_name]
|
149
|
+
%>/binding.yml"
|
150
|
+
- ".canpe_repos/default_repository/templates/.canpe_repos/<%= canpe[:repository_name]
|
151
|
+
%>/templates/sample.html.erb"
|
152
|
+
- ".gitignore"
|
153
|
+
- ".rspec"
|
154
|
+
- ".travis.yml"
|
155
|
+
- CODE_OF_CONDUCT.md
|
156
|
+
- Gemfile
|
157
|
+
- LICENSE.txt
|
158
|
+
- README.md
|
159
|
+
- Rakefile
|
160
|
+
- bin/canpe
|
161
|
+
- bin/console
|
162
|
+
- bin/setup
|
163
|
+
- canpe.gemspec
|
164
|
+
- lib/canpe.rb
|
165
|
+
- lib/canpe/command.rb
|
166
|
+
- lib/canpe/file_manipulation.rb
|
167
|
+
- lib/canpe/repository.rb
|
168
|
+
- lib/canpe/repository_creator.rb
|
169
|
+
- lib/canpe/repository_operation.rb
|
170
|
+
- lib/canpe/repository_operation_context.rb
|
171
|
+
- lib/canpe/repository_store.rb
|
172
|
+
- lib/canpe/runner.rb
|
173
|
+
- lib/canpe/template_binding.rb
|
174
|
+
- lib/canpe/template_binding_container.rb
|
175
|
+
- lib/canpe/template_binding_reflector.rb
|
176
|
+
- lib/canpe/template_renderer.rb
|
177
|
+
- lib/canpe/version.rb
|
178
|
+
homepage: https://github.com/ykosaka/canpe
|
179
|
+
licenses:
|
180
|
+
- MIT
|
181
|
+
metadata: {}
|
182
|
+
post_install_message:
|
183
|
+
rdoc_options: []
|
184
|
+
require_paths:
|
185
|
+
- lib
|
186
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
187
|
+
requirements:
|
188
|
+
- - ">="
|
189
|
+
- !ruby/object:Gem::Version
|
190
|
+
version: '0'
|
191
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
192
|
+
requirements:
|
193
|
+
- - ">="
|
194
|
+
- !ruby/object:Gem::Version
|
195
|
+
version: '0'
|
196
|
+
requirements: []
|
197
|
+
rubyforge_project:
|
198
|
+
rubygems_version: 2.6.8
|
199
|
+
signing_key:
|
200
|
+
specification_version: 4
|
201
|
+
summary: customizable template generator written by Ruby.
|
202
|
+
test_files: []
|