kanagata 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ed26320199135ecafbb8fc2c82a0153b1d2a852
4
+ data.tar.gz: 0bae52fce364517ebf71f377be0aa2b273c73b43
5
+ SHA512:
6
+ metadata.gz: 422915fb70c6671880640fcf962f5b463e8c3d9d802a36194ac4dc51b853939bafbaf9bf8dde06862a0b105276c30f5448105b044479c3b947e79b88cf8aeda9
7
+ data.tar.gz: ba16b74fd7b57e2c3f54eb6ffae2329022d82d0f5fceb30fe263befb0ec8730bea5495bb14610977ed47472e8e66109d1abcc134b1be21ea2921e7e8a9325577
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
10
+ /.envrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ 2.2.0
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kanagata.gemspec
4
+ gemspec
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kanagata (0.1.0)
5
+ erubis
6
+ thor
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ byebug (4.0.5)
12
+ columnize (= 0.9.0)
13
+ coderay (1.1.0)
14
+ columnize (0.9.0)
15
+ diff-lcs (1.2.5)
16
+ erubis (2.7.0)
17
+ method_source (0.8.2)
18
+ pry (0.10.1)
19
+ coderay (~> 1.1.0)
20
+ method_source (~> 0.8.1)
21
+ slop (~> 3.4)
22
+ pry-byebug (3.1.0)
23
+ byebug (~> 4.0)
24
+ pry (~> 0.10)
25
+ rake (10.4.2)
26
+ rspec (3.2.0)
27
+ rspec-core (~> 3.2.0)
28
+ rspec-expectations (~> 3.2.0)
29
+ rspec-mocks (~> 3.2.0)
30
+ rspec-core (3.2.3)
31
+ rspec-support (~> 3.2.0)
32
+ rspec-expectations (3.2.1)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.2.0)
35
+ rspec-mocks (3.2.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.2.0)
38
+ rspec-support (3.2.2)
39
+ slop (3.6.0)
40
+ thor (0.19.1)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ bundler (~> 1.9)
47
+ kanagata!
48
+ pry-byebug
49
+ rake (~> 10.0)
50
+ rspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kimura Masayuki
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.
@@ -0,0 +1,109 @@
1
+ # Kanagata
2
+
3
+ Kanagata is a file generator based on ERB template files.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'kanagata'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ ```sh
17
+ $ bundle
18
+ ```
19
+
20
+ Or install it yourself as:
21
+
22
+ ```sh
23
+ $ gem install kanagata
24
+ ```
25
+
26
+
27
+ ## Usage
28
+
29
+ ```sh
30
+ $ kanagata -h
31
+ Commands:
32
+ kanagata destroy [RECIPE_NAME] # Destroy files based on recipe and template files
33
+ kanagata generate [RECIPE_NAME] # Generate files based on recipe and template files
34
+ kanagata help [COMMAND] # Describe available commands or one specific command
35
+ kanagata init # Generate sample recipe and template files
36
+ ```
37
+
38
+ ### e.g. .gitconfig
39
+
40
+ You can prepare a recipe file with the following command to set up your gitconfig file:
41
+
42
+ ```sh
43
+ $ cat <<EOF>>.kanagata
44
+ gitconfig:
45
+ templates:
46
+ - path: ~/.gitconfig
47
+ EOF
48
+ ```
49
+
50
+ Next, you can make a template file with the following command:
51
+
52
+ ```sh
53
+ $ mkdir kanagata
54
+ $ cat <<EOF>>kanagata/.kanagata.erb
55
+ [user]
56
+ name = <%= name %>
57
+ email = <%= email %>
58
+ [alias]
59
+ ci = commit -v
60
+ co = checkout
61
+ st = status
62
+ di = diff
63
+ br = branch
64
+ df = diff
65
+ [color]
66
+ diff = auto
67
+ status = auto
68
+ branch = auto
69
+ interactive = auto
70
+ EOF
71
+ ```
72
+
73
+ Your gitconfig file is generated at your $HOME dir after running the following command.
74
+
75
+ ```sh
76
+ $ kanagata generate gitconfig name:'YOUR NAME' email:your_name@exampl.com
77
+ ```
78
+
79
+ If you want to delete kanagata generated files, you can just run `kanagata destroy gitconfig` command.
80
+
81
+ ### To make your original kanagata recipe and template files
82
+
83
+ You can make sample files with the following command:
84
+
85
+ ```sh
86
+ $ kanagata init [YOUR_RECIPE_NAME]
87
+ ```
88
+
89
+ `kanagata init` command generates one directory and two files.
90
+
91
+ - `kanagata' directory is template file directory, so you can store template files in this directory.
92
+ - `.kanagata` file is a recipe file.
93
+ - `kanagata/sample.yml.erb` file is a sample template file.
94
+
95
+
96
+ ## Development
97
+
98
+ After checking out the repository, run `bundle install` to install dependencies. Then, run `rspec` to run the tests.
99
+
100
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
101
+
102
+
103
+ ## Contributing
104
+
105
+ 1. Fork it ( https://github.com/masa0x80/kanagata/fork )
106
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
107
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
108
+ 4. Push to the branch (`git push origin my-new-feature`)
109
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "kanagata"
4
+
5
+ Kanagata::CLI.start
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kanagata/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kanagata"
8
+ spec.version = Kanagata::VERSION
9
+ spec.authors = ["Kimura Masayuki"]
10
+ spec.email = ["masa0x80@gmail.com"]
11
+
12
+ spec.summary = 'Generate files based on recipe and template files.'
13
+ spec.homepage = "https://github.com/masa0x80/kanagata"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+ spec.required_ruby_version = ">= 2.2.0"
21
+
22
+ spec.add_runtime_dependency "thor"
23
+ spec.add_runtime_dependency "erubis"
24
+ spec.add_development_dependency "bundler", "~> 1.9"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "pry-byebug"
28
+ end
@@ -0,0 +1,11 @@
1
+ require "kanagata/version"
2
+ require "kanagata/cli"
3
+ require "kanagata/skelton"
4
+ require 'kanagata/util'
5
+ require "kanagata/base"
6
+ require "kanagata/generator"
7
+ require "kanagata/destroyer"
8
+
9
+ module Kanagata
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,18 @@
1
+ require 'yaml'
2
+ require 'erubis'
3
+ require 'pathname'
4
+ require 'fileutils'
5
+
6
+ module Kanagata
7
+ class Base
8
+ def initialize(target, config_file, attributes = [])
9
+ config = load_and_validate(target, config_file)
10
+ @templates = config['templates']
11
+ @attributes = merge_attributes(config['attributes'] || {}, attributes)
12
+ @templates_dir = config.key?('templates_dir') ? File.expand_path(config['templates_dir']) : File.join(File.expand_path('.'), 'kanagata')
13
+ end
14
+
15
+ private
16
+ include Kanagata::Util
17
+ end
18
+ end
@@ -0,0 +1,36 @@
1
+ require 'thor'
2
+
3
+ module Kanagata
4
+ class CLI < Thor
5
+ desc 'init', 'Generate sample recipe and template files'
6
+ def init(target = 'sample')
7
+ begin
8
+ skelton = Kanagata::Skelton.new(target)
9
+ rescue => e
10
+ say(e.message, :red)
11
+ end
12
+ end
13
+
14
+ option :config, default: '.kanagata'
15
+ desc 'generate [RECIPE_NAME]', 'Generate files based on recipe and template files'
16
+ def generate(target, *attributes)
17
+ begin
18
+ generator = Kanagata::Generator.new(target, options[:config], attributes)
19
+ generator.generate
20
+ rescue => e
21
+ say(e.message, :red)
22
+ end
23
+ end
24
+
25
+ option :config, default: '.kanagata'
26
+ desc 'destroy [RECIPE_NAME]', 'Destroy files based on recipe and template files'
27
+ def destroy(target, *attributes)
28
+ begin
29
+ destroyer = Kanagata::Destroyer.new(target, options[:config], attributes)
30
+ destroyer.destroy
31
+ rescue => e
32
+ say(e.message, :red)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ module Kanagata
2
+ class Destroyer < Kanagata::Base
3
+ def destroy
4
+ @templates.each do |template|
5
+ output_filename = Erubis::Eruby.new(template['name'] || File.basename(template['path'])).result(@attributes)
6
+ output_file = File.expand_path(File.join(File.dirname(template['path']), output_filename))
7
+ begin
8
+ FileUtils.rm(output_file)
9
+ info "remove #{relative_path_of(output_file)}"
10
+ rescue Errno::ENOENT
11
+ info "Already removed: #{relative_path_of(output_file)}"
12
+ rescue => e
13
+ raise "Error: remove #{relative_path_of(output_file)}"
14
+ ensure
15
+ remove_dirs(output_file)
16
+ end
17
+ end
18
+ end
19
+
20
+ def remove_dirs(file)
21
+ relative_path_of(File.dirname(file)).ascend do |dir|
22
+ begin
23
+ dir.rmdir
24
+ info "remove dir #{dir}"
25
+ rescue Errno::ENOENT
26
+ info "Already removed: #{dir}"
27
+ rescue Errno::ENOTEMPTY
28
+ info "#{dir} is not empty"
29
+ rescue => e
30
+ raise "Error: remove dir #{dir}"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ module Kanagata
2
+ class Generator < Kanagata::Base
3
+ def generate
4
+ @templates.each do |template|
5
+ output_filename = Erubis::Eruby.new(template['name'] || File.basename(template['path'])).result(@attributes)
6
+ output_file = File.expand_path(File.join(File.dirname(template['path']), output_filename))
7
+ make_dirs(output_file)
8
+ if File.exist?(output_file)
9
+ info "Already exists: #{relative_path_of(output_file)}"
10
+ else
11
+ begin
12
+ File.open(output_file, 'w') do |file|
13
+ erubis = Erubis::Eruby.new(File.read(File.join(@templates_dir, "#{File.basename(template['path'])}.erb")))
14
+ file.print erubis.result(@attributes)
15
+ info "create #{relative_path_of(output_file)}"
16
+ end
17
+ rescue => e
18
+ FileUtils.rm(output_file)
19
+ raise "Error: create #{relative_path_of(output_file)}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ def make_dirs(file)
26
+ relative_path_of(File.dirname(file)).descend do |dir|
27
+ begin
28
+ dir.mkdir
29
+ info "create dir #{dir}"
30
+ rescue Errno::EEXIST
31
+ info "Already exists: #{dir}"
32
+ rescue => e
33
+ raise "Error: create dir #{dir}"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ require 'fileutils'
2
+ require 'tmpdir'
3
+
4
+ module Kanagata
5
+ class Skelton
6
+ def initialize(target = 'target')
7
+ Dir.mktmpdir do |tmpdir|
8
+ yaml = <<-"EOF"
9
+ skelton:
10
+ attributes:
11
+ target: '#{target}'
12
+ meta_key: '<%= key %>'
13
+ templates_dir: #{File.expand_path('../../../template_samples', __FILE__)}
14
+ templates:
15
+ - path: ./.kanagata
16
+ - path: ./kanagata/sample.yml.erb
17
+ EOF
18
+ config_file = File.join(tmpdir, '.kanagata')
19
+ File.write(config_file, yaml)
20
+ generator = Kanagata::Generator.new('skelton', config_file)
21
+ generator.generate
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ module Kanagata
2
+ module Util
3
+ def load_and_validate(target, config_file)
4
+ yaml = YAML.load_file(File.expand_path(config_file))
5
+ validate(yaml, target)
6
+ config = yaml[target]
7
+ validate(config, 'templates')
8
+ config['templates'].each do |file|
9
+ validate(file, 'path')
10
+ end
11
+ config
12
+ end
13
+
14
+ def merge_attributes(config_attributes, args_attributes)
15
+ args_attributes.each do |value|
16
+ k, v = value.split(':')
17
+ config_attributes[k] = v
18
+ end
19
+ config_attributes
20
+ end
21
+
22
+ def validate(hash, key)
23
+ raise "Error: config file format is invalid -- '#{key}' is not found." unless hash.key?(key)
24
+ end
25
+
26
+ def relative_path_of(file)
27
+ pwd = File.expand_path('.')
28
+ Pathname.new(file).relative_path_from(Pathname.new(pwd))
29
+ end
30
+
31
+ def info(text)
32
+ puts "\t#{text}"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Kanagata
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ <%= target %>:
2
+ attributes:
3
+ key: value
4
+ templates_dir: ./kanagata
5
+ templates:
6
+ - path: ./path/to/file.yml
7
+ name: <%= meta_key %>.yml
@@ -0,0 +1,2 @@
1
+ sample:
2
+ - <%= meta_key %>
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kanagata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kimura Masayuki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-12 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
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: erubis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - masa0x80@gmail.com
100
+ executables:
101
+ - kanagata
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/kanagata
115
+ - kanagata.gemspec
116
+ - lib/kanagata.rb
117
+ - lib/kanagata/base.rb
118
+ - lib/kanagata/cli.rb
119
+ - lib/kanagata/destroyer.rb
120
+ - lib/kanagata/generator.rb
121
+ - lib/kanagata/skelton.rb
122
+ - lib/kanagata/util.rb
123
+ - lib/kanagata/version.rb
124
+ - template_samples/.kanagata.erb
125
+ - template_samples/sample.yml.erb.erb
126
+ homepage: https://github.com/masa0x80/kanagata
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: 2.2.0
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.4.5
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Generate files based on recipe and template files.
150
+ test_files: []