shelley 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ca77da0be4417eacb0a9fb6372f53a20b37b5e954184ee39203edb99eac6b500
4
+ data.tar.gz: b393d7034752f6f78c7b7f83b77572b1aa8fedd49d958a9068f52a22bc333178
5
+ SHA512:
6
+ metadata.gz: 5c1dc1c8e19862b99dd32f8291e618e9697f9d007bc0cbcb595c7a02cf429abb76d26a0e647421b99aabd58ea1dece3d6d27c97fd8e8e7c84f363c62e3f3db78
7
+ data.tar.gz: 19b81645ce0ae60236c9e0117b8b6397a639569256fb313b585b464598c5195f724cc33d57b47363aa537db2610df670fa28e21bf769c5fefe94ae4870a0f7fa
@@ -0,0 +1,51 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.16.6
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in shelley.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ shelley (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.0)
32
+ shelley!
33
+
34
+ BUNDLED WITH
35
+ 1.16.6
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 TODO: Write your name
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,138 @@
1
+ # Shelley
2
+
3
+ A gem for converting your classes and method into a shell supporting nested commands, autocomplete and history.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'shelley'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install shelley
20
+
21
+ ## Usage
22
+
23
+ ### A simple shell
24
+
25
+ ```ruby
26
+ class Calculator
27
+ def initialize
28
+ @value = 0
29
+ end
30
+
31
+ def add(n)
32
+ @value += n.to_i
33
+ end
34
+
35
+ def subtract(n)
36
+ @value -= n.to_i
37
+ end
38
+
39
+ def multiply_by(n)
40
+ @value *= n.to_i
41
+ end
42
+
43
+ def divide_by(n)
44
+ @value /= n.to_i
45
+ end
46
+
47
+ def result
48
+ puts @value
49
+ end
50
+ end
51
+
52
+ command_registry = Shelley::CommandRegistry.new
53
+ command_registry.add_command(Calculator.new)
54
+ shell = Shelley::InteractiveShell.new(command_registry)
55
+ shell.start
56
+ ```
57
+
58
+ Sample output
59
+
60
+ ```
61
+ > add 3
62
+ > result
63
+ 3
64
+ > add 5
65
+ > subtract 2
66
+ > result
67
+ 6
68
+ > divide_by 3
69
+ > result
70
+ 2
71
+ > multiply_by 10
72
+ > result
73
+ 20
74
+ > divide_by 0
75
+ divided by 0
76
+ >
77
+ ```
78
+
79
+ ### A shell with subcommands
80
+
81
+ ```ruby
82
+ class Timer
83
+ def initialize
84
+ @started_at = nil
85
+ @elapsed = 0
86
+ end
87
+
88
+ def start
89
+ return unless @started_at.nil?
90
+ @started_at = Time.now
91
+ end
92
+
93
+ def stop
94
+ return if @started_at.nil?
95
+ @elapsed += Time.now - @started_at
96
+ @started_at = nil
97
+ end
98
+
99
+ def elapsed
100
+ elapsed = @elapsed
101
+ elapsed += (Time.now - @started_at) unless @started_at.nil?
102
+ puts elapsed
103
+ end
104
+ end
105
+
106
+ command_registry = Shelley::CommandRegistry.new
107
+ command_registry.add_command(Calculator.new, 'calc')
108
+ command_registry.add_command(Timer.new, 'timer')
109
+ shell = Shelley::InteractiveShell.new(command_registry)
110
+ shell.start
111
+ ```
112
+
113
+ Sample output
114
+
115
+ ```
116
+ > calc add 1
117
+ > calc add 3
118
+ > timer start
119
+ > calc subtract 2
120
+ > calc result
121
+ 2
122
+ > timer elapsed
123
+ 10.156443773
124
+ > timer stop
125
+ > timer elapsed
126
+ 12.239438967
127
+ > timer elapsed
128
+ 12.239438967
129
+ > calc result
130
+ 2
131
+ ```
132
+
133
+ ## TODO
134
+
135
+ * Support for adding single methods
136
+ * Automatic argument convertion
137
+ * Argument autocomplete
138
+ * Refactor `InteractiveShell` and `CommandRegistry` in order to not expose the internal state of `CommandRegistry`
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'shelley'
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__)
@@ -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,7 @@
1
+ require 'readline'
2
+
3
+ module Shelley
4
+ require 'shelley/version'
5
+ require 'shelley/registry'
6
+ require 'shelley/shells'
7
+ end
@@ -0,0 +1,116 @@
1
+ module Shelley
2
+ # A node of the command tree
3
+ class CommandNode
4
+ attr_reader :children, :parent
5
+ attr_accessor :name
6
+ attr_accessor :command
7
+
8
+ def initialize(parent = nil)
9
+ @children = []
10
+ @parent = parent
11
+ end
12
+
13
+ def ensure_exists(*path)
14
+ return self if path.count.zero?
15
+ curr_name = path.shift
16
+ node = @children.find { |n| n.name == curr_name }
17
+ if node.nil?
18
+ node = CommandNode.new(self)
19
+ node.name = curr_name
20
+ @children << node
21
+ end
22
+ return node if path.count.zero?
23
+ node.ensure_exists(*path)
24
+ end
25
+
26
+ def node_by_path(*path)
27
+ return self if path.count.zero?
28
+ curr_name = path.shift
29
+ node = @children.find { |n| n.name == curr_name }
30
+ return nil if node.nil?
31
+ return node if path.count.zero?
32
+ node.node_by_path(*path)
33
+ end
34
+
35
+ def full_path
36
+ curr_node = self
37
+ path = []
38
+ until curr_node.parent.nil?
39
+ path << curr_node.name
40
+ curr_node = curr_node.parent
41
+ end
42
+ path.reverse
43
+ end
44
+
45
+ def to_s
46
+ "CommandNode(name=#{name}, command=#{command}, children={#{children.map(&:to_s)}})"
47
+ end
48
+ end
49
+
50
+ # A command
51
+ class Command
52
+ def initialize(method)
53
+ @method = method
54
+ end
55
+
56
+ def execute(*args)
57
+ @method.call(*args)
58
+ rescue StandardError => msg
59
+ raise CommandError, msg
60
+ end
61
+ end
62
+
63
+ # An error raised by the CommandRegistry if a command fails
64
+ class CommandError < StandardError
65
+ end
66
+
67
+ # A shell
68
+ class CommandRegistry
69
+ def initialize
70
+ @tree = CommandNode.new
71
+ end
72
+
73
+ # Returns the command tree
74
+ def commands_tree
75
+ @tree
76
+ end
77
+
78
+ def command?(*path)
79
+ !@tree.node_by_path(*path).nil?
80
+ end
81
+
82
+ # Adds a command to the shell
83
+ def add_command(instance, *path)
84
+ node = @tree.ensure_exists(*path)
85
+ instance.class.public_instance_methods(false).each do |method_name|
86
+ child = node.ensure_exists(method_name.to_s)
87
+ child.command = Command.new(instance.method(method_name))
88
+ end
89
+ end
90
+
91
+ def raise_error(command)
92
+ raise CommandError, "Cannot find command \"#{command}\""
93
+ end
94
+
95
+ # Executes a command
96
+ def execute_command(command)
97
+ tokens = command.split
98
+ node = @tree
99
+ until tokens.count.zero?
100
+ prev_node = node
101
+ curr_name = tokens.shift
102
+ node = node.node_by_path(curr_name)
103
+ if node.nil?
104
+ raise_error(command) if prev_node.command.nil?
105
+ tokens.insert(0, curr_name)
106
+ prev_node.command.execute(*tokens)
107
+ return
108
+ elsif tokens.count.zero?
109
+ node.command.execute
110
+ return
111
+ end
112
+ end
113
+ raise_error(command)
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,62 @@
1
+ module Shelley
2
+ # An interactive shell, supporting history and autocompletion
3
+ class InteractiveShell
4
+ attr_accessor :prompt
5
+
6
+ def initialize(command_registry)
7
+ @command_registry = command_registry
8
+ @prompt = '> '
9
+ end
10
+
11
+ # Retrieves candidate nodes for a given path
12
+ def candidate_nodes(*path)
13
+ return @command_registry.commands_tree.children if path.count.zero?
14
+ last_name = path.pop
15
+ node = @command_registry.commands_tree.node_by_path(*path)
16
+ return [] if node.nil?
17
+ last_node = node.node_by_path(last_name)
18
+ return last_node.children unless last_node.nil?
19
+ node.children.select { |n| n.name =~ /^#{Regexp.escape(last_name)}/ }
20
+ end
21
+
22
+ # Tries to autocomplete a lines
23
+ def autocomplete(line)
24
+ candidate_nodes(*line.split).map(&:name).sort
25
+ end
26
+
27
+ # Starts the shell
28
+ def start
29
+ Readline.completion_append_character = ' '
30
+ Readline.completion_proc = lambda do |_line|
31
+ autocomplete(Readline.line_buffer)
32
+ end
33
+ while (line = Readline.readline(@prompt, true))
34
+ begin
35
+ @command_registry.execute_command(line)
36
+ rescue StandardError => msg
37
+ puts msg
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ # A non-interactive shell. Executes given commands sequentially
44
+ class NonInteractiveShell
45
+ def initialize(command_registry, commands)
46
+ @command_registry = command_registry
47
+ @commands = commands
48
+ end
49
+
50
+ # Starts the shell
51
+ def start
52
+ @commands.each do |command|
53
+ begin
54
+ puts "> #{command}"
55
+ @command_registry.execute_command(command)
56
+ rescue StandardError => msg
57
+ puts msg
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module Shelley
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path('lib', __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shelley/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'shelley'
8
+ spec.version = Shelley::VERSION
9
+ spec.authors = ['Lorenzo Benvenuti']
10
+ spec.email = ['lorenzo.benvenuti@gmail.com']
11
+
12
+ spec.summary = 'Convert your ruby code into a shell'
13
+ spec.description = 'Shelley allows to convert your classes and method into a shell supporting nested commands, autocomplete and history.'
14
+ spec.homepage = 'https://github.com/lorenzobenvenuti/shelley'
15
+ spec.license = 'MIT'
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(__dir__)) 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_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ end
data/test.rb ADDED
@@ -0,0 +1,57 @@
1
+ require_relative 'lib/shelley'
2
+
3
+ class Calculator
4
+ def initialize
5
+ @value = 0
6
+ end
7
+
8
+ def add(n)
9
+ @value += n.to_i
10
+ end
11
+
12
+ def subtract(n)
13
+ @value -= n.to_i
14
+ end
15
+
16
+ def multiply_by(n)
17
+ @value *= n.to_i
18
+ end
19
+
20
+ def divide_by(n)
21
+ @value /= n.to_i
22
+ end
23
+
24
+ def result
25
+ puts @value
26
+ end
27
+ end
28
+
29
+ class Timer
30
+ def initialize
31
+ @started_at = nil
32
+ @elapsed = 0
33
+ end
34
+
35
+ def start
36
+ return unless @started_at.nil?
37
+ @started_at = Time.now
38
+ end
39
+
40
+ def stop
41
+ return if @started_at.nil?
42
+ @elapsed += Time.now - @started_at
43
+ @started_at = nil
44
+ end
45
+
46
+ def elapsed
47
+ elapsed = @elapsed
48
+ elapsed += (Time.now - @started_at) unless @started_at.nil?
49
+ puts elapsed
50
+ end
51
+ end
52
+
53
+ command_registry = Shelley::CommandRegistry.new
54
+ command_registry.add_command(Calculator.new, 'calc')
55
+ command_registry.add_command(Timer.new, 'timer')
56
+ shell = Shelley::InteractiveShell.new(command_registry)
57
+ shell.start
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shelley
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lorenzo Benvenuti
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-01 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Shelley allows to convert your classes and method into a shell supporting
56
+ nested commands, autocomplete and history.
57
+ email:
58
+ - lorenzo.benvenuti@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/shelley.rb
74
+ - lib/shelley/registry.rb
75
+ - lib/shelley/shells.rb
76
+ - lib/shelley/version.rb
77
+ - shelley.gemspec
78
+ - test.rb
79
+ homepage: https://github.com/lorenzobenvenuti/shelley
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.7.6
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Convert your ruby code into a shell
103
+ test_files: []