mygen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1d28a17ba4b0b74637b09ba1583cfb393d54f3c1
4
+ data.tar.gz: f9d44da3c8ba7feb944570c2f65b83e9cf9211da
5
+ SHA512:
6
+ metadata.gz: fc22d1ca38bf3a1892707b989340c0b253e61a5bd40534b59849818bf2d0a12167c9b349c7e967c1660a01585dbc524e19128c7f189897d77890ad1f52082370
7
+ data.tar.gz: a67afc5299bc615248c9e4b38e6f074cdd91c9c809b34f30ef49154f109d41020d16b06b2cab4f9c72d9bd6ddcf52a27c6541b7c52992784c372e93dfdfd3b5f
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .byebug_history
@@ -0,0 +1 @@
1
+ 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mygen.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kristian Rasmussen
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,41 @@
1
+ # Mygen
2
+
3
+ A code generator
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mygen'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mygen
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ myg --help
25
+ ```
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/iamkristian/mygen.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mygen"
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 "irb"
14
+ IRB.start
data/bin/myg ADDED
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mygen'
4
+ require 'gli'
5
+ require 'mygen'
6
+ require 'byebug'
7
+
8
+ include GLI::App
9
+
10
+ program_desc 'A simple code generator'
11
+
12
+ version Mygen::VERSION
13
+
14
+ subcommand_option_handling :normal
15
+ arguments :strict
16
+
17
+ desc "Dry run, doesn't change files"
18
+ switch :s
19
+
20
+
21
+ desc 'Describe some flag here'
22
+ default_value 'the default'
23
+
24
+
25
+ desc 'Apply an arbitrary generator'
26
+ arg_name 'The generator to run'
27
+ command :generate do |c|
28
+ c.desc 'Describe a flag to generate'
29
+ c.flag :f
30
+ Mygen::Plugins.load.each do |p|
31
+ plugin = p.new
32
+ c.desc plugin.description
33
+ c.arg 'name'
34
+ c.command plugin.generator_name.to_sym do |s|
35
+ plugin.plugin_commands(s) if plugin.respond_to?(:plugin_commands)
36
+ s.action do |global_options, options, args|
37
+ plugin.dry_run = global_options[:s]
38
+ plugin.name = args[0]
39
+ plugin.dest_dir = args[0]
40
+ plugin.call if plugin.respond_to?(:call)
41
+ end
42
+ end
43
+ end
44
+ c.action do |global_options,options,args|
45
+
46
+ # Your command logic here
47
+ # If you have any errors, just raise them
48
+ # raise "that command made no sense"
49
+
50
+ puts "generate command ran"
51
+ end
52
+ end
53
+
54
+ pre do |global,command,options,args|
55
+ # Pre logic here
56
+ # Return true to proceed; false to abort and not call the
57
+ # chosen command
58
+ # Use skips_pre before a command to skip this block
59
+ # on that command only
60
+ true
61
+ end
62
+
63
+ post do |global,command,options,args|
64
+ # Post logic here
65
+ # Use skips_post before a command to skip this
66
+ # block on that command only
67
+ end
68
+
69
+ on_error do |exception|
70
+ # Error logic here
71
+ # return false to skip default error handling
72
+ true
73
+ end
74
+
75
+ exit run(ARGV)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require "mygen/version"
2
+ require "mygen/plugins"
3
+ require "mygen/plugins/mygen_generator"
4
+ require "mygen/generator"
5
+ require "erb"
6
+
7
+ module Mygen
8
+ def self.root
9
+ File.dirname __dir__
10
+ end
11
+ end
@@ -0,0 +1,107 @@
1
+ module Mygen
2
+ class Generator
3
+ attr_accessor :name, :dest_dir, :dry_run, :template_source_dir
4
+
5
+ def initialize
6
+ @template_source_dir = File.join(ENV['HOME'], ".mygen", "plugins", generator_name, "templates")
7
+ end
8
+
9
+ def self.descendants
10
+ ObjectSpace.each_object(Class).select { |klass| klass < self }
11
+ end
12
+
13
+ def description
14
+ "Plugin has no description"
15
+ end
16
+
17
+ def generator_name
18
+ self.class.name.gsub(/::/, '/').
19
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
20
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
21
+ tr("-", "_").
22
+ split('/').last.
23
+ downcase
24
+ end
25
+
26
+ def fileutils
27
+ dry_run ? FileUtils::DryRun : FileUtils::Verbose
28
+ end
29
+
30
+ def template_files(path = template_source_dir)
31
+ Dir.glob(File.join(path, "**/*")).select { |f| File.file? f }
32
+ end
33
+
34
+ def template_dirs(path = template_source_dir)
35
+ Dir.glob(File.join(path, "**/*")).select { |f| File.directory? f }
36
+ end
37
+
38
+ def internal_template_files
39
+ template_files(internal_template_source_dir)
40
+ end
41
+
42
+ def internal_template_source_dir
43
+ File.join(Mygen.root, "templates", generator_name)
44
+ end
45
+
46
+ def make_template_tree(internal = false)
47
+ @template_source_dir = internal_template_source_dir if internal
48
+ fileutils.rm_rf(dest_dir) if File.exist?(dest_dir)
49
+ fileutils.cp_r(template_source_dir, dest_dir)
50
+ end
51
+
52
+ # rename directories that should be filtered, from __name
53
+ # files should be from the destination, so no dirs needs to be filtered
54
+ # and only files need to be processed.
55
+ #
56
+ def parse_templates(bindings)
57
+ template_dirs(File.join(dest_dir)).each do |dir|
58
+ dest = file_destination(dir, bindings)
59
+ move_file_in_place(dir, dest)
60
+ end
61
+ # Filter files with erb
62
+ template_files(File.join(dest_dir)).each do |file|
63
+ dest = file_destination(file, bindings)
64
+ # This is where you parse the erb files and fill in the contens
65
+ if file.end_with? 'erb'
66
+ parse(file, bindings)
67
+ end
68
+ move_file_in_place(file, dest)
69
+ end
70
+ end
71
+
72
+ def parse(file, bindings)
73
+ erb = ERB.new(File.read(file))
74
+ result = erb.result bindings
75
+ File.open(file, "w") { |f| f.write(result) }
76
+ end
77
+
78
+ def move_file_in_place(src, dest)
79
+ sf = File.absolute_path(src)
80
+ df = File.absolute_path(dest)
81
+ fileutils.mv(sf, df) unless sf == df
82
+ end
83
+
84
+ def parse_and_place_file(file, dest, bindings)
85
+ erb = ERB.new(File.read(file))
86
+ erb.result bindings
87
+ end
88
+
89
+ def file_destination(file, bindings)
90
+ # subtract template_source_dir from path, add dest_dir and substitute filenames
91
+ new_file = file.gsub(template_source_dir, '')
92
+ replaced_filename(new_file, bindings)
93
+ end
94
+
95
+ private
96
+
97
+ def replaced_filename(file, bindings)
98
+ elements = File.basename(file).split('.')
99
+ elements.pop if elements.last == 'erb'
100
+ dirname = File.dirname(file)
101
+ f = elements.map do |element|
102
+ element.start_with?('__') ? eval( element.sub(/^__/, ''), bindings) : element
103
+ end.join('.')
104
+ File.join(dirname, f)
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,23 @@
1
+ module Mygen
2
+ #
3
+ # Load plugins from a dir (default to $HOME/.mygen/plugins)
4
+ # Add all subdirs to $LOAD_PATH, and get ancestors of the generator class
5
+ # And register them in the myg shell command
6
+ #
7
+ module Plugins
8
+ def self.load(path = File.join(ENV['HOME'], ".mygen", "plugins"))
9
+ dirs = Dir.glob(File.join(path, "**/*.rb"))
10
+ dirs.each do |d|
11
+ register_plugin(d)
12
+ end
13
+ Generator.descendants
14
+ end
15
+
16
+ def self.register_plugin(plugin)
17
+ dir = File.dirname(plugin)
18
+ $LOAD_PATH.unshift(dir) unless $LOAD_PATH.include?(dir)
19
+ klass = File.basename(plugin, '.rb').downcase
20
+ require klass
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ require 'mygen/generator'
2
+ module Mygen
3
+ module Plugins
4
+ class MygenGenerator < Mygen::Generator
5
+ def description
6
+ "Generates a new mygen generator"
7
+ end
8
+
9
+ def call
10
+ puts "create generator named #{name}"
11
+ make_template_tree(true)
12
+ @bleh = "fish"
13
+ @name = name
14
+ b = binding
15
+ parse_templates(b)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Mygen
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mygen/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mygen"
8
+ spec.version = Mygen::VERSION
9
+ spec.authors = ["Kristian Rasmussen"]
10
+ spec.email = ["me@krx.io"]
11
+
12
+ spec.summary = %q{Code generator for generating project structures,
13
+ for various languages}
14
+ spec.description = %q{Code generator that helps you generating project
15
+ structures, filtering files. Plugable in a git like manner.}
16
+ spec.homepage = "https://github.com/iamkristian/mygen"
17
+ spec.license = "MIT"
18
+
19
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
20
+ # delete this section to allow pushing this gem to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
25
+ end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ spec.bindir = "bin"
29
+ spec.executables = spec.files.grep(%r{^bin/myg}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.11"
33
+ spec.add_development_dependency "rspec", "~> 3.2"
34
+ spec.add_development_dependency "gli", "~> 2.13"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "pry", "~> 0.10"
37
+ spec.add_development_dependency "pry-byebug", "~> 3.3"
38
+ end
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mygen"
@@ -0,0 +1,3 @@
1
+ # <%= name %>
2
+
3
+ This describes the generator <%= name %>
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mygen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kristian Rasmussen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-13 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: gli
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.13'
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: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
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: '3.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.3'
97
+ description: |-
98
+ Code generator that helps you generating project
99
+ structures, filtering files. Plugable in a git like manner.
100
+ email:
101
+ - me@krx.io
102
+ executables:
103
+ - myg
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - ".gitignore"
108
+ - ".ruby-version"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/myg
115
+ - bin/setup
116
+ - lib/mygen.rb
117
+ - lib/mygen/generator.rb
118
+ - lib/mygen/plugins.rb
119
+ - lib/mygen/plugins/mygen_generator.rb
120
+ - lib/mygen/version.rb
121
+ - mygen.gemspec
122
+ - templates/mygen_generator/Gemfile
123
+ - templates/mygen_generator/README.md.erb
124
+ - templates/mygen_generator/lib/__name.rb.erb
125
+ - templates/mygen_generator/lib/__name/README.md
126
+ homepage: https://github.com/iamkristian/mygen
127
+ licenses:
128
+ - MIT
129
+ metadata:
130
+ allowed_push_host: https://rubygems.org
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.4.5
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Code generator for generating project structures, for various languages
151
+ test_files: []