ruby-app-gen 0.1.1

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: 2df760d07f0f14ec5f85340e4599dad4da908121ed72cc1252666aa2ef799fb3
4
+ data.tar.gz: 0f7f37c37beee202797d50b55cdb600abd907a6c3021ad0c0232d8182e72abd4
5
+ SHA512:
6
+ metadata.gz: 560e374acf0a85b4bdcb0427f1b038e12f1c92d51644e8a7f38afe522c71f2d0a4621d42825d46b28428dcaf7eaf83fd2811c683120e23cf0a16eed91098bf96
7
+ data.tar.gz: 997541049be92bca9978ce938540b379d4638cf25315cffe8bd86126362a98df3c225ac03577348375f0ece42b265d8ada6bcc5bce6174c548134b07762843c5
data/bin/app-gen ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ libx = File.expand_path('../lib', __dir__)
6
+ $LOAD_PATH.unshift(libx) unless $LOAD_PATH.include?(libx)
7
+
8
+ require 'app_gen'
9
+
10
+ AppGen::App.start(ARGV)
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+
5
+ require 'active_support/core_ext/string'
6
+ require 'erubi'
7
+ require 'thor'
8
+
9
+ module AppGen
10
+ class App < Thor
11
+ desc 'generate APP_NAME PATH', 'generate a ruby application skeleton'
12
+ option :force, type: :boolean, desc: 'overrides all the application files is exist'
13
+ option :database, type: :boolean, default: true, desc: 'generates DB related files'
14
+ def generate(app_name, path)
15
+ @app_name = app_name
16
+ @app_root = File.join(path, app_name)
17
+ @root = File.expand_path('../..', __dir__)
18
+
19
+ create_directories
20
+ create_files
21
+ puts 'Complete!'
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :app_name, :app_root, :root
27
+
28
+ def terminate!(error_code: 1, error_message: nil)
29
+ puts error_message if error_message
30
+ exit error_code
31
+ end
32
+
33
+ def app_filename
34
+ @app_filename ||= app_name.underscore
35
+ end
36
+
37
+ def app_classname
38
+ @app_classname ||= app_filename.camelize
39
+ end
40
+
41
+ def templates_path
42
+ File.join(root, 'lib/templates')
43
+ end
44
+
45
+ def ask_yes_no(prompt)
46
+ %w[Y y yes Yes YES].include?(ask("#{prompt} [Y/n]:"))
47
+ end
48
+
49
+ def check_directories
50
+ return unless Dir.exists?(app_root)
51
+ return if options.force?
52
+ return if ask_yes_no("Directory #{app_root} already exists. Do you want to continue?")
53
+
54
+ terminate!(error_message: 'Skipped!')
55
+ end
56
+
57
+ def db_directories
58
+ return [] unless options.database?
59
+
60
+ [
61
+ File.join(app_root, 'db/config'),
62
+ File.join(app_root, 'db/migrate')
63
+ ]
64
+ end
65
+
66
+ def app_directories
67
+ [
68
+ app_root,
69
+ File.join(app_root, 'bin'),
70
+ File.join(app_root, "lib/#{app_filename}")
71
+ ]
72
+ end
73
+
74
+ def create_directories
75
+ check_directories
76
+
77
+ (app_directories + db_directories).each do |dir|
78
+ FileUtils.mkdir_p(dir)
79
+ end
80
+ end
81
+
82
+ def create_files
83
+ files = {
84
+ 'Gemfile' => 'Gemfile',
85
+ 'Rakefile' => 'Rakefile',
86
+ 'bin/console' => 'bin/console',
87
+ 'lib/app.rb' => "lib/#{app_filename}.rb",
88
+ 'lib/app/app.rb' => "lib/#{app_filename}/app.rb"
89
+ }
90
+ files['db/config/database.yml'] = 'db/config/database.yml' if options.database?
91
+
92
+ files.each do |template, file|
93
+ create_app_file(template, file)
94
+ end
95
+
96
+ FileUtils.chmod 0755, app_file_path('bin/console')
97
+ end
98
+
99
+ def read_template(template_filename)
100
+ File.read(File.join(templates_path, template_filename))
101
+ rescue Errno::ENOENT => e
102
+ terminate!(error_message: "Can't read #{template_filename} template")
103
+ end
104
+
105
+ def template_data_to_src(template_data)
106
+ template_src = Erubi::Engine.new(template_data).src
107
+ eval(template_src)
108
+ end
109
+
110
+ def app_file_path(file)
111
+ File.join(app_root, file)
112
+ end
113
+
114
+ def create_app_file(template, file)
115
+ app_file_path = app_file_path(file)
116
+ if !File.exists?(app_file_path) ||
117
+ options.force? ||
118
+ ask_yes_no("#{file} already exists, do you want to override it?")
119
+ src = template_data_to_src(read_template("#{template}.erb"))
120
+ File.open(app_file_path, 'wb') { |file| file << src }
121
+ else
122
+ puts "Skipping #{file}"
123
+ end
124
+ end
125
+ end
126
+ end
data/lib/app_gen.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV['APP_ENV'] ||= 'development'
4
+ ENV['RACK_ENV'] = ENV['APP_ENV']
5
+
6
+ require 'rubygems'
7
+ require 'bundler/setup'
8
+
9
+ require 'zeitwerk'
10
+
11
+ loader = Zeitwerk::Loader.new
12
+ loader.push_dir(__dir__)
13
+ loader.enable_reloading if ENV['APP_ENV'] == 'development'
14
+ loader.setup
15
+
16
+ loader.eager_load unless %w[test development].include?(ENV['APP_ENV'])
17
+
18
+ module AppGen
19
+ # no op
20
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ <%- if options.database? -%>
8
+ gem 'activerecord', require: 'active_record'
9
+ <%- end -%>
10
+ gem 'irb'
11
+ gem 'rake'
12
+ <%- if options.database? -%>
13
+ gem 'sqlite3'
14
+ <%- end -%>
15
+ gem 'zeitwerk'
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ libx = File.expand_path('./lib', __dir__)
4
+ $LOAD_PATH.unshift(libx) unless $LOAD_PATH.include?(libx)
5
+
6
+ require '<%= app_filename -%>'
7
+ <%- if options.database? -%>
8
+
9
+ task :environment do
10
+ # no op
11
+ end
12
+
13
+ namespace :db do
14
+ task :load_config do
15
+ # Make sure the application was initialized - i.e. database was configured
16
+ <%= app_classname %>::App.instance
17
+ end
18
+ end
19
+
20
+ load 'active_record/railties/databases.rake'
21
+ <%- end -%>
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ libx = File.expand_path('../lib', __dir__)
6
+ $LOAD_PATH.unshift(libx) unless $LOAD_PATH.include?(libx)
7
+
8
+ require '<%= app_filename %>'
9
+
10
+ <%= app_classname %>::App.instance.start
@@ -0,0 +1,7 @@
1
+ development:
2
+ adapter: sqlite3
3
+ database: db/<%= app_filename %>.db
4
+
5
+ test:
6
+ adapter: sqlite3
7
+ database: db/<%= app_filename %>_test.db
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= app_classname %>
4
+ class App
5
+ include Singleton
6
+
7
+ attr_reader :env, :root
8
+
9
+ def initialize
10
+ @env = ENV['APP_ENV']
11
+ @root = File.expand_path('../..', __dir__)
12
+ <%- if options.database? -%>
13
+ config_database
14
+ <%- end -%>
15
+ end
16
+
17
+ def start
18
+ IRB.start
19
+ end
20
+ <%- if options.database? -%>
21
+
22
+ private
23
+
24
+ def database_path
25
+ @database_path ||= File.join(root, 'db')
26
+ end
27
+
28
+ def migrations_paths
29
+ @migrations_paths ||= File.join(database_path, 'migrate')
30
+ end
31
+
32
+ def database_config
33
+ @database_config ||= begin
34
+ db_config_path = File.expand_path('config/database.yml', database_path)
35
+ YAML.load_file(db_config_path)
36
+ end
37
+ end
38
+
39
+ def config_database
40
+ ActiveRecord::Tasks::DatabaseTasks.env = env
41
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths = migrations_paths
42
+ ActiveRecord::Tasks::DatabaseTasks.db_dir = database_path
43
+ ActiveRecord::Tasks::DatabaseTasks.root = root
44
+
45
+ ActiveRecord::Base.schema_format = :sql
46
+ ActiveRecord::Base.configurations = database_config
47
+ ActiveRecord::Base.establish_connection
48
+ end
49
+ <%- end -%>
50
+ end
51
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV['APP_ENV'] ||= 'development'
4
+ ENV['RACK_ENV'] = ENV['APP_ENV']
5
+
6
+ require 'rubygems'
7
+ require 'bundler/setup'
8
+
9
+ Bundler.require('default', ENV['APP_ENV'])
10
+
11
+ loader = Zeitwerk::Loader.new
12
+ loader.push_dir(__dir__)
13
+ loader.enable_reloading if ENV['APP_ENV'] == 'development'
14
+ loader.setup
15
+
16
+ loader.eager_load unless %w[test development].include?(ENV['APP_ENV'])
17
+
18
+ module <%= app_classname %>
19
+ # no op
20
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-app-gen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dima Lunich
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: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: erubi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: zeitwerk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.5'
69
+ description: A simple gem to generate a ruby app
70
+ email: dima.lunich@gmail.com
71
+ executables:
72
+ - app-gen
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - bin/app-gen
77
+ - lib/app_gen.rb
78
+ - lib/app_gen/app.rb
79
+ - lib/templates/Gemfile.erb
80
+ - lib/templates/Rakefile.erb
81
+ - lib/templates/bin/console.erb
82
+ - lib/templates/db/config/database.yml.erb
83
+ - lib/templates/lib/app.rb.erb
84
+ - lib/templates/lib/app/app.rb.erb
85
+ homepage: https://github.com/lunich/ruby-app-gen
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.2.15
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: AppGen ruby gem
108
+ test_files: []