crudboy 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: d6dc8118318a3ba0b12135bedf428cd4a56fd39aaab7cb3389cc5cacb5713c10
4
+ data.tar.gz: 2cb2c93182d2f582e6829e23fe6e506704dd0d1083bc232bef9402dfb16b9fc3
5
+ SHA512:
6
+ metadata.gz: 00a0f03e7788ecf4972ca98fa3a310658b1e0ba868e723146ea84f6754ebc5a4043f0aad31457065938129a2932cc309863b2dbcabe431694be4610b4b8d2b18
7
+ data.tar.gz: 2603679bc0997601dab38ef1619bc3f7979aa5db433f7d06281e926448682dcdfe0556ce49874d3c98d78252dd3be810d8ae96bb75af082bb18328e259991ebc
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea
10
+ /examples/
11
+ *.gem
@@ -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 liuxiang@ktjr.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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in crudboy.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ crudboy (0.1.0)
5
+ activerecord (>= 6.0.3, < 6.2.0)
6
+ activesupport (~> 6.0.3)
7
+ composite_primary_keys (~> 12.0.3)
8
+ mysql2 (~> 0.5.3)
9
+ net-ssh-gateway (~> 2.0.0)
10
+ pry (~> 0.13.1)
11
+ pry-byebug (~> 3.9.0)
12
+ pry-doc (~> 1.1.0)
13
+ rainbow (~> 3.0.0)
14
+ sqlite3 (~> 1.4)
15
+ table_print (~> 1.5.6)
16
+ terminal-table (~> 1.8.0)
17
+
18
+ GEM
19
+ remote: https://rubygems.org/
20
+ specs:
21
+ activemodel (6.0.3.5)
22
+ activesupport (= 6.0.3.5)
23
+ activerecord (6.0.3.5)
24
+ activemodel (= 6.0.3.5)
25
+ activesupport (= 6.0.3.5)
26
+ activesupport (6.0.3.5)
27
+ concurrent-ruby (~> 1.0, >= 1.0.2)
28
+ i18n (>= 0.7, < 2)
29
+ minitest (~> 5.1)
30
+ tzinfo (~> 1.1)
31
+ zeitwerk (~> 2.2, >= 2.2.2)
32
+ byebug (11.1.3)
33
+ coderay (1.1.3)
34
+ composite_primary_keys (12.0.9)
35
+ activerecord (~> 6.0.0)
36
+ concurrent-ruby (1.1.8)
37
+ i18n (1.8.9)
38
+ concurrent-ruby (~> 1.0)
39
+ method_source (1.0.0)
40
+ minitest (5.14.4)
41
+ mysql2 (0.5.3)
42
+ net-ssh (6.1.0)
43
+ net-ssh-gateway (2.0.0)
44
+ net-ssh (>= 4.0.0)
45
+ pry (0.13.1)
46
+ coderay (~> 1.1)
47
+ method_source (~> 1.0)
48
+ pry-byebug (3.9.0)
49
+ byebug (~> 11.0)
50
+ pry (~> 0.13.0)
51
+ pry-doc (1.1.0)
52
+ pry (~> 0.11)
53
+ yard (~> 0.9.11)
54
+ rainbow (3.0.0)
55
+ rake (12.3.3)
56
+ sqlite3 (1.4.2)
57
+ table_print (1.5.7)
58
+ terminal-table (1.8.0)
59
+ unicode-display_width (~> 1.1, >= 1.1.1)
60
+ thread_safe (0.3.6)
61
+ tzinfo (1.2.9)
62
+ thread_safe (~> 0.1)
63
+ unicode-display_width (1.7.0)
64
+ yard (0.9.26)
65
+ zeitwerk (2.4.2)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ crudboy!
72
+ rake (~> 12.0)
73
+
74
+ BUNDLED WITH
75
+ 2.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Liu Xiang
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,12 @@
1
+ # Crudboy
2
+
3
+ ## Installation
4
+
5
+ Execute:
6
+
7
+ $ gem install crudboy
8
+
9
+ If system permission problems occurs, try with sudo:
10
+
11
+ $ sudo gem install crudboy
12
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "crudboy"
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(__FILE__)
data/bin/setup ADDED
@@ -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
data/crudboy.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ require_relative 'lib/crudboy/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "crudboy"
5
+ spec.version = Crudboy::VERSION
6
+ spec.authors = ["Liu Xiang"]
7
+ spec.email = ["liuxiang921@gmail.com"]
8
+
9
+ spec.summary = %{CRUD code generator using Rails ActiveRecord}
10
+ spec.description = %{CRUD code generator using Rails ActiveRecord}
11
+ spec.homepage = "https://github.com/lululau/crudboy"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
14
+
15
+
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency 'mysql2', '~> 0.5.3'
27
+ # spec.add_dependency 'pg', '>= 0.18', '< 2.0'
28
+ spec.add_dependency 'sqlite3', '~> 1.4'
29
+ spec.add_dependency 'activerecord', '>= 6.0.3', '< 6.2.0'
30
+ spec.add_dependency 'composite_primary_keys', '~> 12.0.3'
31
+ spec.add_dependency 'activesupport', '~> 6.0.3'
32
+ spec.add_dependency 'net-ssh-gateway', '~> 2.0.0'
33
+ spec.add_dependency 'pry', '~> 0.13.1'
34
+ spec.add_dependency 'pry-byebug', '~> 3.9.0'
35
+ spec.add_dependency 'pry-doc', '~> 1.1.0'
36
+ spec.add_dependency 'rainbow', '~> 3.0.0'
37
+ spec.add_dependency 'terminal-table', '~> 1.8.0'
38
+ spec.add_dependency 'table_print', '~> 1.5.6'
39
+ end
data/exe/crudboy ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ CrudboySetsidWrrapper = File.dirname(File.absolute_path(__FILE__)) + '/crudboy_setsid_wrapper'
4
+
5
+ require "crudboy"
6
+
7
+ Crudboy::Cli.start
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ Process.setsid
4
+
5
+ exec(*ARGV)
data/lib/crudboy.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'table_print'
2
+ require 'crudboy/version'
3
+ require 'crudboy/id'
4
+ require 'crudboy/ext'
5
+ require 'crudboy/helper'
6
+ require 'crudboy/template_context'
7
+ require 'crudboy/column'
8
+ require 'crudboy/model'
9
+ require 'crudboy/template'
10
+ require 'crudboy/bundle'
11
+ require 'crudboy/ssh_proxy'
12
+ require 'crudboy/app'
13
+ require 'crudboy/cli'
14
+
15
+ module Crudboy
16
+ end
@@ -0,0 +1,96 @@
1
+ require 'net/ssh/gateway'
2
+
3
+ module Crudboy
4
+ class App
5
+
6
+ class << self
7
+ attr_accessor :env, :prompt, :instance, :connect_options
8
+
9
+ def config
10
+ @@effective_config
11
+ end
12
+ end
13
+
14
+ def initialize(options)
15
+ require 'active_support/all'
16
+ require 'active_record'
17
+ require 'composite_primary_keys'
18
+ require "crudboy/connection"
19
+ require "crudboy/definition"
20
+ @options = options
21
+ App.env = @options.env
22
+ App.connect_options = connect_options
23
+ Connection.open(App.connect_options)
24
+ @definition = Definition.new(effective_config)
25
+ @context = TemplateContext.new(@definition)
26
+ load_initializer!
27
+ App.instance = self
28
+ end
29
+
30
+ def connect_options
31
+ connect_conf = effective_config.slice(:adapter, :host, :username,
32
+ :password, :database, :encoding,
33
+ :pool, :port, :socket)
34
+ if effective_config[:ssh].present?
35
+ connect_conf.merge!(start_ssh_proxy!)
36
+ end
37
+
38
+ connect_conf
39
+ end
40
+
41
+ def load_initializer!
42
+ return unless effective_config[:initializer]
43
+ initializer_file = File.expand_path(effective_config[:initializer])
44
+ unless File.exists?(initializer_file)
45
+ STDERR.puts "Specified initializer file not found, #{effective_config[:initializer]}"
46
+ exit(1)
47
+ end
48
+ load(initializer_file)
49
+ end
50
+
51
+ def start_ssh_proxy!
52
+ ssh_config = effective_config[:ssh]
53
+ local_ssh_proxy_port = Crudboy::SSHProxy.connect(
54
+ ssh_config.slice(:host, :user, :port, :password).merge(
55
+ forward_host: effective_config[:host],
56
+ forward_port: effective_config[:port],
57
+ local_port: ssh_config[:local_port]))
58
+
59
+ {
60
+ host: '127.0.0.1',
61
+ port: local_ssh_proxy_port
62
+ }
63
+ end
64
+
65
+ def config
66
+ @config ||= YAML.load(IO.read(File.expand_path(@options.config_file))).with_indifferent_access
67
+ end
68
+
69
+ def selected_config
70
+ if @options.env.present? && !config[@options.env].present?
71
+ STDERR.puts "Specified ENV `#{@options.env}' not exists"
72
+ end
73
+ if env = @options.env
74
+ config[env]
75
+ else
76
+ {}
77
+ end
78
+ end
79
+
80
+ def effective_config
81
+ @@effective_config ||= nil
82
+ unless @@effective_config
83
+ @@effective_config = selected_config.deep_merge(@options.to_h)
84
+ if @@effective_config[:adapter].blank?
85
+ @@effective_config[:adapter] = 'sqlite3'
86
+ end
87
+ @@effective_config[:database] = File.expand_path(@@effective_config[:database]) if @@effective_config[:adapter] == 'sqlite3'
88
+ end
89
+ @@effective_config
90
+ end
91
+
92
+ def run!
93
+ Bundle.new(@options.template_bundle, @options.template_args, @options.destination, @context).render!
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,79 @@
1
+ require 'find'
2
+ require 'net/ssh/gateway'
3
+ require 'ostruct'
4
+
5
+ module Crudboy
6
+ class Bundle
7
+ attr_accessor :path, :config, :result, :destination, :templates, :context
8
+
9
+ def initialize_path(path)
10
+ # TODO
11
+ path
12
+ end
13
+
14
+ def parse_options(options)
15
+ load_option_definitions
16
+ return unless Object::const_defined?('TEMPLATE_OPTIONS')
17
+ OpenStruct.new.tap do |result|
18
+
19
+ TEMPLATE_OPTIONS.each do |definition|
20
+ definition[:default].try do |default|
21
+ result[definition[:name]] = default
22
+ end
23
+ end
24
+
25
+ OptionParser.new do |opts|
26
+
27
+ opts.banner = "Template Options:\n\n"
28
+
29
+ TEMPLATE_OPTIONS.each do |definition|
30
+ opts.on(*definition.values_at(:short, :long, :description)) do |value|
31
+ result[definition[:name]] = value
32
+ end
33
+ end
34
+ opts.on('', '--help', 'Prints this help') do
35
+ puts opts
36
+ exit
37
+ end
38
+ end.parse!(options)
39
+ end
40
+ end
41
+
42
+ def load_option_definitions
43
+ File.join(@path, "options.rb").try do |option_definitions_file|
44
+ load(option_definitions_file)
45
+ end
46
+ end
47
+
48
+ def initialize(path, options, destination, context)
49
+ @path = initialize_path(path)
50
+ @destination = destination
51
+ @options = parse_options(options)
52
+ @context = context
53
+ @context.bundle_options = @options
54
+ @templates = initialize_templates
55
+ end
56
+
57
+ def load_initializer!
58
+ "#{@path}/init.rb".tap do |initializer_file|
59
+ load(initializer_file) if File.exist?(initializer_file)
60
+ end
61
+ end
62
+
63
+ def initialize_templates
64
+ templates_path= File.join(@path, 'templates')
65
+ Find.find(templates_path).map do |file_path|
66
+ base_path = file_path.delete_prefix(templates_path)
67
+ Template.new(file_path, base_path, @context)
68
+ end
69
+ end
70
+
71
+ def render!
72
+ load_initializer!
73
+ templates.each do |template|
74
+ template.render!(@destination)
75
+ end
76
+ end
77
+
78
+ end
79
+ end