devbin 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ lib_path = File.expand_path("../lib", __dir__)
5
+ $:.unshift(lib_path) if !$:.include?(lib_path)
6
+ require "devbin/cli"
7
+
8
+ Signal.trap("INT") do
9
+ warn("\n#{caller.join("\n")}: interrupted")
10
+ exit(1)
11
+ end
12
+
13
+ begin
14
+ Devbin::CLI.start
15
+ rescue Devbin::CLI::Error => err
16
+ puts "ERROR: #{err.message}"
17
+ exit 1
18
+ end
@@ -0,0 +1,6 @@
1
+ require "devbin/version"
2
+
3
+ module Devbin
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+
5
+ module Devbin
6
+ # Handle the application command line parsing
7
+ # and the dispatch to various command objects
8
+ #
9
+ # @api public
10
+ class CLI < Thor
11
+ # Error raised by this runner
12
+ Error = Class.new(StandardError)
13
+
14
+ desc "version", "devbin version"
15
+ def version
16
+ require_relative "version"
17
+ puts "v#{Devbin::VERSION}"
18
+ end
19
+ map %w(--version -v) => :version
20
+
21
+ require_relative "commands/rails"
22
+ register Devbin::Commands::Rails, "rails", "rails [SUBCOMMAND]", "Control the Rails application"
23
+ end
24
+ end
@@ -0,0 +1,166 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module Devbin
6
+ class Command
7
+ extend Forwardable
8
+
9
+ def_delegators :command, :run
10
+
11
+ # Execute this command
12
+ #
13
+ # @api public
14
+ def execute(*)
15
+ raise(
16
+ NotImplementedError,
17
+ "#{self.class}##{__method__} must be implemented"
18
+ )
19
+ end
20
+
21
+ # The external commands runner
22
+ #
23
+ # @see http://www.rubydoc.info/gems/tty-command
24
+ #
25
+ # @api public
26
+ def command(**options)
27
+ require "tty-command"
28
+ TTY::Command.new(options)
29
+ end
30
+
31
+ # The cursor movement
32
+ #
33
+ # @see http://www.rubydoc.info/gems/tty-cursor
34
+ #
35
+ # @api public
36
+ def cursor
37
+ require "tty-cursor"
38
+ TTY::Cursor
39
+ end
40
+
41
+ # Open a file or text in the user's preferred editor
42
+ #
43
+ # @see http://www.rubydoc.info/gems/tty-editor
44
+ #
45
+ # @api public
46
+ def editor
47
+ require "tty-editor"
48
+ TTY::Editor
49
+ end
50
+
51
+ # File manipulation utility methods
52
+ #
53
+ # @see http://www.rubydoc.info/gems/tty-file
54
+ #
55
+ # @api public
56
+ def generator
57
+ require "tty-file"
58
+ TTY::File
59
+ end
60
+
61
+ # Terminal output paging
62
+ #
63
+ # @see http://www.rubydoc.info/gems/tty-pager
64
+ #
65
+ # @api public
66
+ def pager(**options)
67
+ require "tty-pager"
68
+ TTY::Pager.new(options)
69
+ end
70
+
71
+ # Terminal platform and OS properties
72
+ #
73
+ # @see http://www.rubydoc.info/gems/tty-pager
74
+ #
75
+ # @api public
76
+ def platform
77
+ require "tty-platform"
78
+ TTY::Platform.new
79
+ end
80
+
81
+ # The interactive prompt
82
+ #
83
+ # @see http://www.rubydoc.info/gems/tty-prompt
84
+ #
85
+ # @api public
86
+ def prompt(**options)
87
+ require "tty-prompt"
88
+ TTY::Prompt.new(options)
89
+ end
90
+
91
+ # Get terminal screen properties
92
+ #
93
+ # @see http://www.rubydoc.info/gems/tty-screen
94
+ #
95
+ # @api public
96
+ def screen
97
+ require "tty-screen"
98
+ TTY::Screen
99
+ end
100
+
101
+ # The unix which utility
102
+ #
103
+ # @see http://www.rubydoc.info/gems/tty-which
104
+ #
105
+ # @api public
106
+ def which(*args)
107
+ require "tty-which"
108
+ TTY::Which.which(*args)
109
+ end
110
+
111
+ # Check if executable exists
112
+ #
113
+ # @see http://www.rubydoc.info/gems/tty-which
114
+ #
115
+ # @api public
116
+ def exec_exist?(*args)
117
+ require "tty-which"
118
+ TTY::Which.exist?(*args)
119
+ end
120
+
121
+ def pastel
122
+ @pastel ||=
123
+ begin
124
+ require "pastel"
125
+ Pastel.new
126
+ end
127
+ end
128
+
129
+ def find_pwd(file_or_directory_name)
130
+ path = [".", file_or_directory_name]
131
+ file = nil
132
+ results = Dir[path.join("/")]
133
+ file = results[0]
134
+ return path[0..-2] unless results.empty?
135
+ 3.times do
136
+ path.unshift("..")
137
+ results = Dir[path.join("/")]
138
+ file = results[0]
139
+ return path[0..-2] unless results.empty?
140
+ end
141
+ []
142
+ end
143
+
144
+ def docker_sync_pwd
145
+ @docker_sync_pwd ||=
146
+ begin
147
+ path = find_pwd("docker-sync.yml")
148
+ if path.empty?
149
+ fail "Cannot find the `docker-sync.yml' file"
150
+ end
151
+ path.join("/")
152
+ end
153
+ end
154
+
155
+ def docker_pwd
156
+ @docker_pwd ||=
157
+ begin
158
+ path = find_pwd("docker")
159
+ if path.empty?
160
+ fail "Cannot find the `docker' folder"
161
+ end
162
+ path.push("docker").join("/")
163
+ end
164
+ end
165
+ end
166
+ end
@@ -0,0 +1 @@
1
+ #
File without changes
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+
5
+ module Devbin
6
+ module Commands
7
+ class Rails < Thor
8
+
9
+ namespace :rails
10
+
11
+ map "s" => "server"
12
+ map "c" => "console"
13
+ map "a" => "attach"
14
+
15
+ desc "server [APP_NAME]", "Start the Rails server"
16
+ method_option :help, aliases: "-h", type: :boolean,
17
+ desc: "Display usage information"
18
+ method_option :detach, aliases: "-d", type: :boolean, required: false, default: false,
19
+ desc: "Start the server with detached mode"
20
+ def server(app_name)
21
+ if options[:help]
22
+ invoke :help, ["server"]
23
+ else
24
+ require_relative "rails/server"
25
+ Devbin::Commands::Rails::Server.new(app_name, options).execute
26
+ end
27
+ end
28
+
29
+ desc "attach APP_NAME", "Attach to the rails server"
30
+ method_option :help, aliases: "-h", type: :boolean,
31
+ desc: "Display usage information"
32
+ def attach(app_name)
33
+ if options[:help]
34
+ invoke :help, ["attach"]
35
+ else
36
+ require_relative "rails/attach"
37
+ Devbin::Commands::Rails::Attach.new(app_name, options).execute
38
+ end
39
+ end
40
+
41
+ desc "console APP_NAME", "Start the rails console"
42
+ method_option :help, aliases: "-h", type: :boolean,
43
+ desc: "Display usage information"
44
+ def console(app_name)
45
+ if options[:help]
46
+ invoke :help, ["console"]
47
+ else
48
+ require_relative "rails/console"
49
+ Devbin::Commands::Rails::Console.new(app_name, options).execute
50
+ end
51
+ end
52
+
53
+ desc "stop APP_NAME", "Stop the rails application"
54
+ method_option :help, aliases: "-h", type: :boolean,
55
+ desc: "Display usage information"
56
+ method_option :all, aliases: "-a", type: :boolean, required: false, default: false,
57
+ desc: "Stop all applications"
58
+ def stop(app_name = "")
59
+ if options[:help]
60
+ invoke :help, ["stop"]
61
+ else
62
+ require_relative "rails/stop"
63
+ Devbin::Commands::Rails::Stop.new(app_name, options).execute
64
+ end
65
+ end
66
+
67
+ desc "restart APP_NAME", "Restart docker-sync stack and the application"
68
+ method_option :help, aliases: "-h", type: :boolean,
69
+ desc: "Display usage information"
70
+ method_option :detach, aliases: "-d", type: :boolean, required: false, default: false,
71
+ desc: "Restart the server with detached mode"
72
+ def restart(app_name)
73
+ if options[:help]
74
+ invoke :help, ["restart"]
75
+ else
76
+ require_relative "rails/restart"
77
+ Devbin::Commands::Rails::Restart.new(app_name, options).execute
78
+ end
79
+ end
80
+
81
+ desc "off", "Close all active containers and go home"
82
+ method_option :help, aliases: "-h", type: :boolean,
83
+ desc: "Display usage information"
84
+ def off(*)
85
+ if options[:help]
86
+ invoke :help, ["off"]
87
+ else
88
+ require_relative "rails/off"
89
+ Devbin::Commands::Rails::Off.new(options).execute
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../command"
4
+
5
+ module Devbin
6
+ module Commands
7
+ class Rails
8
+ class Attach < Devbin::Command
9
+ def initialize(app_name, options)
10
+ @app_name = app_name
11
+ @options = options
12
+ end
13
+
14
+ def execute(input: $stdin, output: $stdout)
15
+ container_id, _err = run "docker-compose ps -q #{@app_name}", chdir: docker_pwd
16
+ puts pastel.green(
17
+ "Remember to use ",
18
+ pastel.yellow.on_bright_black.bold("Ctrl + C"),
19
+ " to detach from container ( Overrided Ctrl + P Ctrl + Q to work with VSCode )"
20
+ )
21
+ pid = Process.fork {
22
+ exec "docker attach #{container_id.strip} --detach-keys='ctrl-c'"
23
+ }
24
+ Process.wait pid
25
+ output.puts "OK"
26
+ exit 0
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../command"
4
+
5
+ module Devbin
6
+ module Commands
7
+ class Rails
8
+ class Console < Devbin::Command
9
+ def initialize(app_name, options)
10
+ @app_name = app_name
11
+ @options = options
12
+ end
13
+
14
+ def execute(input: $stdin, output: $stdout)
15
+ Dir.chdir(docker_pwd) do
16
+ pid = Process.fork {
17
+ exec "docker-compose exec #{@app_name} bundle exec rails c"
18
+ }
19
+ Process.wait pid
20
+ output.puts "OK"
21
+ exit 0
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../command"
4
+
5
+ module Devbin
6
+ module Commands
7
+ class Rails
8
+ class Off < Devbin::Command
9
+ def initialize(options)
10
+ @options = options
11
+ end
12
+
13
+ def execute(input: $stdin, output: $stdout)
14
+ require_relative "stop"
15
+ Devbin::Commands::Rails::Stop.new(@app_name, {all: true}).execute
16
+ output.puts "OK"
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../command"
4
+
5
+ module Devbin
6
+ module Commands
7
+ class Rails
8
+ class Restart < Devbin::Command
9
+ def initialize(app_name, options)
10
+ @app_name = app_name
11
+ @options = options
12
+ end
13
+
14
+ def execute(input: $stdin, output: $stdout)
15
+ require_relative "stop"
16
+ Devbin::Commands::Rails::Stop.new(@app_name, {all: false}).execute
17
+ run "docker-sync start", chdir: docker_sync_pwd
18
+ require_relative "server"
19
+ Devbin::Commands::Rails::Server.new(@app_name, {detach: @optons && @options[:detach]}).execute
20
+ output.puts "OK"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../command"
4
+
5
+ module Devbin
6
+ module Commands
7
+ class Rails
8
+ class Server < Devbin::Command
9
+ def initialize(app_name, options)
10
+ @app_name = app_name
11
+ @options = options
12
+ end
13
+
14
+ def execute(input: $stdin, output: $stdout)
15
+ # Command logic goes here ...
16
+ begin
17
+ run "docker-sync start", chdir: docker_sync_pwd
18
+ rescue TTY::Command::ExitError => e
19
+ unless e.message =~ /warning\s+docker-sync\salready\sstarted\sfor\sthis\sconfiguration/
20
+ raise e
21
+ end
22
+ end
23
+ run "docker-compose up -d #{@app_name}", chdir: docker_pwd
24
+ unless @options[:detach]
25
+ require_relative "attach"
26
+ Devbin::Commands::Rails::Attach.new(@app_name, {}).execute
27
+ end
28
+ output.puts "OK"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end