project_generator 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
+ SHA256:
3
+ metadata.gz: 54fbc182db27fe47cc9d3ff624e1d0ec8a545688d6a8dd919ed35ab259f008e3
4
+ data.tar.gz: 5b14bbed47866af1163a9f4aebd659669d680dbed0d4f0910eba4a2c4ad30d5b
5
+ SHA512:
6
+ metadata.gz: d7e5ae0d8febc8dc702a968b0615f08daecec7a685d7094a34d2914a65f04a9e6981e647f3418586ec4efc05078eb6268d24e94b1675b1723de33a6eb9b08b24
7
+ data.tar.gz: ba8bd2953f84e672c0065f17a5b955c17f31baf2bd4125cbb9ecdad1f971b9478956eee2b54e59ae96fa0d162754de8f98609bd7811388588f80893717a9a280
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ ## 0.1.0 (2021-11-09)
6
+
7
+ * Initial release.
8
+ Taken out from `gem_generator`.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Alexander Popov
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,58 @@
1
+ # Project Generator
2
+
3
+ [![Cirrus CI - Base Branch Build Status](https://img.shields.io/cirrus/github/AlexWayfer/project_generator?style=flat-square)](https://cirrus-ci.com/github/AlexWayfer/project_generator)
4
+ [![Codecov branch](https://img.shields.io/codecov/c/github/AlexWayfer/project_generator/main.svg?style=flat-square)](https://codecov.io/gh/AlexWayfer/project_generator)
5
+ [![Code Climate](https://img.shields.io/codeclimate/maintainability/AlexWayfer/project_generator.svg?style=flat-square)](https://codeclimate.com/github/AlexWayfer/project_generator)
6
+ [![Depfu](https://img.shields.io/depfu/AlexWayfer/project_generator?style=flat-square)](https://depfu.com/repos/github/AlexWayfer/project_generator)
7
+ [![Inline docs](https://inch-ci.org/github/AlexWayfer/project_generator.svg?branch=main)](https://inch-ci.org/github/AlexWayfer/project_generator)
8
+ [![Gem](https://img.shields.io/gem/v/project_generator.svg?style=flat-square)](https://rubygems.org/gems/project_generator)
9
+ [![License](https://img.shields.io/github/license/AlexWayfer/project_generator.svg?style=flat-square)](LICENSE.txt)
10
+
11
+ Base for various CLI generation tools.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'project_generator'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ ```shell
24
+ bundle install
25
+ ```
26
+
27
+ Or install it yourself as:
28
+
29
+ ```shell
30
+ gem install project_generator
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ```ruby
36
+ require 'project_generator'
37
+
38
+ # TODO
39
+ ```
40
+
41
+ ## Development
42
+
43
+ After checking out the repo, run `bundle install` to install dependencies.
44
+
45
+ Then, run `bundle exec rspec` to run the tests.
46
+
47
+ To install this gem onto your local machine, run `toys gem install`.
48
+
49
+ To release a new version, run `toys gem release %version%`.
50
+ See how it works [here](https://github.com/AlexWayfer/gem_toys#release).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/AlexWayfer/project_generator).
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'gorilla_patch/inflections'
4
+ require 'memery'
5
+ require 'yaml'
6
+
7
+ module ProjectGenerator
8
+ ## Base CLI command for Project Generator
9
+ class Command < Clamp::Command
10
+ ## Private instance methods for processing template files (copying, renaming, rendering)
11
+ module ProcessFiles
12
+ ## Class for a single object which should be a scope in render
13
+ class RenderVariables
14
+ include Memery
15
+
16
+ using GorillaPatch::Inflections
17
+
18
+ attr_reader :name, :indentation
19
+
20
+ def initialize(name, indentation)
21
+ @name = name
22
+ @indentation = indentation
23
+ end
24
+
25
+ ## `public :binding` and `send :binding` return caller binding
26
+ ## This is from ERB documentation: https://ruby-doc.org/core-2.7.2/Binding.html
27
+ # rubocop:disable Naming/AccessorMethodName
28
+ def get_binding
29
+ binding
30
+ end
31
+ # rubocop:enable Naming/AccessorMethodName
32
+
33
+ memoize def path
34
+ name.tr('-', '/')
35
+ end
36
+
37
+ memoize def title
38
+ name.split(/[-_]/).map(&:camelize).join(' ')
39
+ end
40
+
41
+ memoize def module_name
42
+ path.camelize
43
+ end
44
+
45
+ memoize def modules
46
+ module_name.split('::')
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'process_files/render_variables'
4
+
5
+ module ProjectGenerator
6
+ ## Base CLI command for Project Generator
7
+ class Command < Clamp::Command
8
+ ## Private instance methods for processing template files (copying, renaming, rendering)
9
+ module ProcessFiles
10
+ RENAME_FILES_PLACEHOLDERS = {
11
+ name: 'project_name',
12
+ path: 'project_path'
13
+ }.freeze
14
+
15
+ private
16
+
17
+ def process_files
18
+ copy_files
19
+
20
+ begin
21
+ @render_variables = initialize_render_variables
22
+
23
+ rename_files
24
+
25
+ render_files
26
+ rescue SystemExit => e
27
+ FileUtils.rm_r @directory
28
+ raise e
29
+ end
30
+ end
31
+
32
+ def initialize_render_variables
33
+ self.class::ProcessFiles::RenderVariables.new name, indentation
34
+ end
35
+
36
+ def copy_files
37
+ puts 'Copying files...'
38
+
39
+ FileUtils.cp_r template, @directory
40
+
41
+ FileUtils.rm_rf "#{@directory}/.git"
42
+ end
43
+
44
+ def rename_files
45
+ puts 'Renaming files...'
46
+
47
+ self.class::RENAME_FILES_PLACEHOLDERS.each do |method_name, template_name|
48
+ real_name = @render_variables.public_send(method_name)
49
+
50
+ Dir["#{@directory}/**/*#{template_name}*"].each do |file_name|
51
+ new_file_name =
52
+ @directory + file_name.delete_prefix(@directory).gsub(template_name, real_name)
53
+
54
+ FileUtils.mkdir_p File.dirname new_file_name
55
+
56
+ File.rename file_name, new_file_name
57
+ end
58
+ end
59
+ end
60
+
61
+ def render_files
62
+ puts 'Rendering files...'
63
+
64
+ Dir.glob("#{@directory}/**/*.erb", File::FNM_DOTMATCH).each do |template_file|
65
+ ## Read a template file content and render it
66
+ content =
67
+ ERB.new(File.read(template_file), trim_mode: '-').result(@render_variables.get_binding)
68
+
69
+ ## Replace tabs with spaces if necessary
70
+ ## TODO: Take it out of `.erb` files
71
+ ## TODO: Take number of spaces from `.editorconfig` file
72
+ ## TODO: Don't convert files refined in `.editorconfig`
73
+ ## TODO: Convert spaces to tabs
74
+ content.gsub!(/^\t+/) { |tabs| ' ' * tabs.count("\t") } if indentation == 'spaces'
75
+
76
+ ## Render variables in file name
77
+ real_pathname = Pathname.new(template_file).sub_ext('')
78
+
79
+ ## Rename template file
80
+ File.rename template_file, real_pathname
81
+
82
+ ## Update file content
83
+ File.write real_pathname, content
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler'
4
+ require 'clamp'
5
+ require 'erb'
6
+ require 'fileutils'
7
+ require 'pathname'
8
+ require 'tmpdir'
9
+
10
+ require_relative 'command/process_files'
11
+
12
+ ## https://github.com/mdub/clamp#allowing-options-after-parameters
13
+ Clamp.allow_options_after_parameters = true
14
+
15
+ module ProjectGenerator
16
+ ## Base CLI command for Project Generator
17
+ class Command < Clamp::Command
18
+ include ProcessFiles
19
+
20
+ parameter 'NAME', 'name of a new gem', inheritable: true
21
+ parameter 'TEMPLATE', 'template path of a new gem', inheritable: true
22
+
23
+ option ['-i', '--indentation'], 'TYPE', 'type of indentation (tabs or spaces)',
24
+ default: 'tabs' do |value|
25
+ ## TODO: Add something like `:variants` to Clamp
26
+ unless %w[tabs spaces].include? value
27
+ raise ArgumentError, 'Only `tabs` or `spaces` values acceptable'
28
+ end
29
+
30
+ value
31
+ end
32
+
33
+ option '--git', :flag, 'use TEMPLATE as GitHub path (clone and generate from it)',
34
+ default: false
35
+
36
+ class << self
37
+ def inherited(subcommand)
38
+ super
39
+
40
+ parameters.each do |parameter|
41
+ subcommand.parameters << parameter
42
+ end
43
+ end
44
+ end
45
+
46
+ # def execute
47
+ # check_target_directory
48
+ #
49
+ # refine_template_parameter if git?
50
+ #
51
+ # process_files
52
+ #
53
+ # initialize_git
54
+ #
55
+ # FileUtils.rm_r @git_tmp_dir if git?
56
+ #
57
+ # done
58
+ # end
59
+
60
+ private
61
+
62
+ def check_target_directory
63
+ @directory = File.expand_path name
64
+
65
+ signal_usage_error 'the target directory already exists' if Dir.exist? @directory
66
+ end
67
+
68
+ def refine_template_parameter
69
+ @git_tmp_dir = Dir.mktmpdir
70
+ `git clone -q https://github.com/#{template}.git #{@git_tmp_dir}`
71
+ self.template = File.join @git_tmp_dir, 'template'
72
+ end
73
+
74
+ def initialize_git
75
+ puts 'Initializing git...'
76
+
77
+ Dir.chdir name do
78
+ system 'git init'
79
+ system 'git add .'
80
+ end
81
+ end
82
+
83
+ def done
84
+ puts 'Done.'
85
+
86
+ puts <<~HELP
87
+ To checkout into a new directory:
88
+ cd #{name}
89
+ HELP
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ProjectGenerator
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'project_generator/command'
4
+ require_relative 'project_generator/version'
metadata ADDED
@@ -0,0 +1,286 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: project_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Popov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: alt_memery
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
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'
41
+ - !ruby/object:Gem::Dependency
42
+ name: clamp
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: gorilla_patch
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler-audit
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.9.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.9.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: gem_toys
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.11.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.11.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: toys
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.12.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.12.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: inifile
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '3.9'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.9'
167
+ - !ruby/object:Gem::Dependency
168
+ name: simplecov
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 0.21.0
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 0.21.0
181
+ - !ruby/object:Gem::Dependency
182
+ name: simplecov-cobertura
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '1.4'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '1.4'
195
+ - !ruby/object:Gem::Dependency
196
+ name: rubocop
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: 1.22.0
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: 1.22.0
209
+ - !ruby/object:Gem::Dependency
210
+ name: rubocop-performance
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: '1.0'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: '1.0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: rubocop-rspec
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: '2.0'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - "~>"
235
+ - !ruby/object:Gem::Version
236
+ version: '2.0'
237
+ description: 'Base for various CLI generation tools.
238
+
239
+ '
240
+ email:
241
+ - alex.wayfer@gmail.com
242
+ executables: []
243
+ extensions: []
244
+ extra_rdoc_files: []
245
+ files:
246
+ - CHANGELOG.md
247
+ - LICENSE.txt
248
+ - README.md
249
+ - lib/project_generator.rb
250
+ - lib/project_generator/command.rb
251
+ - lib/project_generator/command/process_files.rb
252
+ - lib/project_generator/command/process_files/render_variables.rb
253
+ - lib/project_generator/version.rb
254
+ homepage: https://github.com/AlexWayfer/project_generator
255
+ licenses:
256
+ - MIT
257
+ metadata:
258
+ bug_tracker_uri: https://github.com/AlexWayfer/project_generator/issues
259
+ changelog_uri: https://github.com/AlexWayfer/project_generator/blob/v0.1.0/CHANGELOG.md
260
+ documentation_uri: http://www.rubydoc.info/gems/project_generator/0.1.0
261
+ homepage_uri: https://github.com/AlexWayfer/project_generator
262
+ source_code_uri: https://github.com/AlexWayfer/project_generator
263
+ wiki_uri: https://github.com/AlexWayfer/project_generator/wiki
264
+ post_install_message:
265
+ rdoc_options: []
266
+ require_paths:
267
+ - lib
268
+ required_ruby_version: !ruby/object:Gem::Requirement
269
+ requirements:
270
+ - - ">="
271
+ - !ruby/object:Gem::Version
272
+ version: '2.6'
273
+ - - "<"
274
+ - !ruby/object:Gem::Version
275
+ version: '4'
276
+ required_rubygems_version: !ruby/object:Gem::Requirement
277
+ requirements:
278
+ - - ">="
279
+ - !ruby/object:Gem::Version
280
+ version: '0'
281
+ requirements: []
282
+ rubygems_version: 3.2.22
283
+ signing_key:
284
+ specification_version: 4
285
+ summary: Base for various CLI generation tools
286
+ test_files: []