lokka-sh 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -42,9 +42,7 @@ Example:
42
42
 
43
43
  ## Acknowledge
44
44
 
45
- This library was allowed to fork jugyo/rails-sh.
46
-
47
- It is gratitude to Mr.jugyo who exhibited the wonderful library.
45
+ This library is fork of jugyo/rails-sh.
48
46
 
49
47
  ## Copyright
50
48
 
data/bin/lokka-sh CHANGED
@@ -1,8 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
- lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
3
- $:.unshift(lib) if File.directory?(lib) && !$:.include?(lib)
4
-
5
- require 'lokka/sh'
2
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
3
+ require 'lokka-sh'
6
4
 
7
5
  puts <<BANNER.gsub("%", "\e[46m \e[0m")
8
6
 
@@ -12,12 +10,14 @@ puts <<BANNER.gsub("%", "\e[46m \e[0m")
12
10
  .%%......%%..%%..%%.%%...%%.%%...%%..%%..............%%..%%..%%.
13
11
  .%%%%%%..%%%%%%..%%..%%..%%..%%..%%..%%...........%%%%...%%..%%.
14
12
  ................................................................
15
- \e[35m#{Lokka::Sh::VERSION.rjust(63, " ")}\e[0m
13
+ \e[35m#{LokkaSh::VERSION.rjust(63, " ")}\e[0m
16
14
 
17
15
  BANNER
18
16
 
19
17
  APP_PATH = File.expand_path('./init')
20
- Lokka::Sh::Color.with(:blue) { "require #{APP_PATH}" }
18
+ LokkaSh::Color.with(:blue) do
19
+ "require #{APP_PATH}"
20
+ end
21
21
  require APP_PATH
22
22
 
23
- Lokka::Sh.start
23
+ LokkaSh::CLI.run
data/lib/lokka-sh.rb ADDED
@@ -0,0 +1,18 @@
1
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
2
+ require 'readline'
3
+ require 'stringio'
4
+ require 'rake/dsl_definition'
5
+ require 'rake'
6
+
7
+ require 'lokka-sh/version'
8
+ require 'lokka-sh/color'
9
+ require 'lokka-sh/prompt'
10
+ require 'lokka-sh/helpers'
11
+ require 'lokka-sh/forkable'
12
+ require 'lokka-sh/sinatra'
13
+ require 'lokka-sh/rake'
14
+ require 'lokka-sh/bundler'
15
+ require 'lokka-sh/command'
16
+ require 'lokka-sh/command/define'
17
+ require 'lokka-sh/cli'
18
+
@@ -0,0 +1,20 @@
1
+ module LokkaSh
2
+ module Bundler
3
+ extend Forkable
4
+
5
+ class << self
6
+ def _invoke(line)
7
+ line ||= 'install'
8
+ command, *args = line.split(/\s+/)
9
+ ARGV.clear
10
+ ARGV.concat args
11
+ require 'bundler/cli'
12
+ ::Bundler::CLI.new.send(command.to_sym)
13
+ end
14
+
15
+ def sub_commands
16
+ %w(exec install update open package config check list show console open viz init gem)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ module LokkaSh
2
+ module CLI
3
+ class << self
4
+ def run
5
+ LokkaSh::Color.with(:cyan) do
6
+ "Lokka.env: #{::Lokka.env}\n`help` to print help"
7
+ end
8
+ Rake.init
9
+ load_commands
10
+ LokkaSh::Prompt.invoke
11
+ end
12
+
13
+ def load_commands
14
+ begin
15
+ lokkashrc = "#{::Lokka.root}/.lokkashrc"
16
+ load lokkashrc
17
+ LokkaSh::Color.with(:blue) do
18
+ "load #{lokkashrc}"
19
+ end
20
+ rescue LoadError => e
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,12 @@
1
+ module LokkaSh
2
+ module Color
3
+ @colors =
4
+ { :r_video => 7, :black => 30, :red => 31, :green => 32, :yellow => 33,
5
+ :blue => 34, :magenta => 35, :cyan => 36, :white => 27, :default => 39,
6
+ :bg_black => 40, :bg_red => 41, :bg_green => 42, :bg_default => 49 }
7
+
8
+ def self.with(name, &block)
9
+ puts "\e[#{@colors[name.to_sym]}m" + yield + "\e[0m"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,59 @@
1
+ module LokkaSh
2
+ module Command
3
+ class << self
4
+ def commands
5
+ @commands ||= {}
6
+ end
7
+
8
+ def define(*names, &block)
9
+ names.each do |name|
10
+ commands[name.to_sym] = block
11
+ completions << name.to_s
12
+ end
13
+ end
14
+
15
+ def find(line)
16
+ if name = line.split(/\s+/, 2)[0]
17
+ commands[name.to_sym]
18
+ else
19
+ nil
20
+ end
21
+ end
22
+
23
+ def command_names
24
+ commands.keys
25
+ end
26
+
27
+ def [](name)
28
+ commands[name.to_sym]
29
+ end
30
+
31
+ def completion_proc
32
+ lambda { |line|
33
+ regex = /#{Regexp.quote(line)}/
34
+ completions.map { |completion|
35
+ case completion
36
+ when String
37
+ completion if completion.match(regex)
38
+ when Proc
39
+ completion.call(line)
40
+ end
41
+ }.compact
42
+ }
43
+ end
44
+
45
+ def completions
46
+ @completions ||= []
47
+ end
48
+
49
+ def completions=(completions)
50
+ @completions = completions
51
+ end
52
+
53
+ def clear
54
+ commands.clear
55
+ completions.clear
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,68 @@
1
+ module LokkaSh::Command
2
+ define 'help' do
3
+ LokkaSh::Color.with(:blue) do
4
+ <<-HELP.gsub(/^ {6}/, '')
5
+
6
+ help -> print help
7
+ console -> starting console
8
+ rake TASK -> execute rake task
9
+ tasks PATTERN -> print rake tasks
10
+ bundle -> execute bundler command
11
+ exit -> exit from lokka-sh
12
+ restart -> restart lokka-sh
13
+ ! -> execute a system command
14
+ eval -> eval as ruby script
15
+ HELP
16
+ end
17
+ end
18
+
19
+ define 'console' do
20
+ LokkaSh::Sinatra.invoke('console')
21
+ end
22
+
23
+ define 'rake' do |arg|
24
+ LokkaSh::Rake.invoke(arg)
25
+ end
26
+
27
+ define 'tasks', 't' do |arg|
28
+ Rake::Task.tasks.each do |task|
29
+ puts task.name
30
+ end
31
+ end
32
+
33
+ LokkaSh::Rake.task_names.map do |name|
34
+ self.completions << "rake #{name}"
35
+ end
36
+
37
+ define 'bundle' do |arg|
38
+ LokkaSh::Bundler.invoke(arg)
39
+ end
40
+
41
+ (LokkaSh::Bundler.sub_commands - ['init']).map do |c|
42
+ self.completions << "bundle #{c}"
43
+ end
44
+
45
+ define '!' do |arg|
46
+ system arg
47
+ end
48
+
49
+ define 'eval' do |arg|
50
+ LokkaSh::Color.with(:blue) { "=> #{eval(arg, binding, __FILE__, __LINE__).inspect}" }
51
+ end
52
+
53
+ define 'log' do |arg|
54
+ LokkaSh::Color.with(:r_video) { "Ctrl-C to quit" }
55
+ system 'tail', '-f', Lokka.root + "tmp/#{(arg || 'development')}.log"
56
+ end
57
+
58
+ self.completions += %w(development test production).map { |i| "log #{i}" }
59
+
60
+ define 'exit' do
61
+ exit
62
+ end
63
+
64
+ define 'restart' do
65
+ puts 'restarting...'
66
+ exec File.expand_path('../../../bin/lokka-sh', __FILE__)
67
+ end
68
+ end
@@ -0,0 +1,34 @@
1
+ module LokkaSh
2
+ module Forkable
3
+ include LokkaSh::Helpers
4
+
5
+ def invoke(line, options = {})
6
+ run_before_fork
7
+ pid = fork do
8
+ run_after_fork
9
+ _invoke(line)
10
+ end
11
+ Process.waitpid(pid)
12
+ end
13
+
14
+ def _invoke
15
+ raise NotImplementedError
16
+ end
17
+
18
+ def before_fork(&block)
19
+ @before_fork = block
20
+ end
21
+
22
+ def after_fork(&block)
23
+ @after_fork = block
24
+ end
25
+
26
+ def run_before_fork(&block)
27
+ @before_fork.call if @before_fork
28
+ end
29
+
30
+ def run_after_fork(&block)
31
+ @after_fork.call if @after_fork
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,58 @@
1
+ module LokkaSh
2
+ module Helpers
3
+ # copy from pry: https://github.com/pry/pry
4
+ #
5
+ # Create scrollable output via less!
6
+ #
7
+ # This command runs `less` in a subprocess, and gives you the IO to its STDIN pipe
8
+ # so that you can communicate with it.
9
+ #
10
+ # Example:
11
+ #
12
+ # lesspipe do |less|
13
+ # 50.times { less.puts "Hi mom!" }
14
+ # end
15
+ #
16
+ # The default less parameters are:
17
+ # * Allow colour
18
+ # * Don't wrap lines longer than the screen
19
+ # * Quit immediately (without paging) if there's less than one screen of text.
20
+ #
21
+ # You can change these options by passing a hash to `lesspipe`, like so:
22
+ #
23
+ # lesspipe(:wrap=>false) { |less| less.puts essay.to_s }
24
+ #
25
+ # It accepts the following boolean options:
26
+ # :color => Allow ANSI colour codes?
27
+ # :wrap => Wrap long lines?
28
+ # :always => Always page, even if there's less than one page of text?
29
+ #
30
+ def lesspipe(*args)
31
+ if args.any? and args.last.is_a?(Hash)
32
+ options = args.pop
33
+ else
34
+ options = {}
35
+ end
36
+
37
+ output = args.first if args.any?
38
+
39
+ params = []
40
+ params << "-R" unless options[:color] == false
41
+ params << "-S" unless options[:wrap] == true
42
+ params << "-F" unless options[:always] == true
43
+ if options[:tail] == true
44
+ params << "+\\>"
45
+ $stderr.puts "Seeking to end of stream..."
46
+ end
47
+ params << "-X"
48
+
49
+ IO.popen("less #{params * ' '}", "w") do |less|
50
+ if output
51
+ less.puts output
52
+ else
53
+ yield less
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,7 +1,6 @@
1
- module Lokka
2
- module Sh
3
- module Prompt
4
- extend self
1
+ module LokkaSh
2
+ module Prompt
3
+ class << self
5
4
 
6
5
  def invoke
7
6
  setup_readline
@@ -14,7 +13,7 @@ module Lokka
14
13
  rescue SystemExit
15
14
  raise
16
15
  rescue Exception => e
17
- Lokka::Sh::Color.with(:bg_red) { "#{e.message}\n#{e.backtrace.join("\n")}" }
16
+ LokkaSh::Color.with(:bg_red) { "#{e.message}\n#{e.backtrace.join("\n")}" }
18
17
  end
19
18
  end
20
19
 
@@ -27,19 +26,20 @@ module Lokka
27
26
 
28
27
  def setup_readline
29
28
  Readline.basic_word_break_characters = ""
30
- Readline.completion_proc = Lokka::Sh::Command.completion_proc
29
+ Readline.completion_proc = LokkaSh::Command.completion_proc
31
30
  end
32
31
 
33
32
  def execute(line)
34
- if command = Lokka::Sh::Command.find(line)
33
+ if command = LokkaSh::Command.find(line)
35
34
  start = Time.now
36
35
  arg = line.split(/\s+/, 2)[1] rescue nil
37
36
  command.call(arg)
38
- Lokka::Sh::Color.with(:blue) { "#{Time.now - start}sec" }
37
+ LokkaSh::Color.with(:blue) { "#{Time.now - start}sec" }
39
38
  else
40
- Lokka::Sh::Color.with(:bg_red) { "Command not found" }
39
+ LokkaSh::Color.with(:bg_red) { "Command not found" }
41
40
  end
42
41
  end
42
+
43
43
  end
44
44
  end
45
45
  end
@@ -0,0 +1,37 @@
1
+ module LokkaSh
2
+ module Rake
3
+ extend Forkable
4
+
5
+ class << self
6
+ def init
7
+ $stdout = StringIO.new
8
+
9
+ before_fork do
10
+ end
11
+ after_fork do
12
+ end
13
+
14
+ ::Rake.application = ::Rake::Application.new
15
+ ::Rake.application.init
16
+ ::Rake.application.load_rakefile
17
+ ensure
18
+ $stdout = STDOUT
19
+ end
20
+
21
+ def _invoke(line)
22
+ line ||= 'default'
23
+ name, *args = line.split(/\s+/)
24
+ args.each do |arg|
25
+ env, value = arg.split('=')
26
+ next unless env && !env.empty? && value && !value.empty?
27
+ ENV[env] = value
28
+ end
29
+ ::Rake.application[name].invoke
30
+ end
31
+
32
+ def task_names
33
+ ::Rake.application.tasks.map{|t| t.name}
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ module LokkaSh
2
+ module Sinatra
3
+ extend Forkable
4
+
5
+ class << self
6
+ def init
7
+ end
8
+
9
+ def _invoke(line)
10
+ case line
11
+ when "console"
12
+ require 'irb'
13
+ IRB.start(Lokka.root)
14
+ else
15
+ raise
16
+ end
17
+ end
18
+
19
+ def reload!
20
+ end
21
+
22
+ def sub_commands
23
+ %w(console)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module LokkaSh
2
+ VERSION = "1.0.4"
3
+ end
data/lokka-sh.gemspec CHANGED
@@ -1,13 +1,13 @@
1
- $:.push File.expand_path("../lib", __FILE__)
2
- require "lokka/sh/version"
1
+ $:.unshift File.expand_path(File.dirname(__FILE__) + "/lib")
2
+ require "lokka-sh/version"
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "lokka-sh"
6
- s.version = Lokka::Sh::VERSION
6
+ s.version = LokkaSh::VERSION
7
7
  s.authors = ["Dach_h"]
8
8
  s.email = ["bunny.hop.md@gmail.com"]
9
9
  s.homepage = ""
10
- s.summary = "excute sub commands of rails quickly."
10
+ s.summary = "excute sub commands of lokka quickly."
11
11
  s.description = s.summary
12
12
  s.rubyforge_project = "lokka-sh"
13
13
  s.files = `git ls-files`.split("\n")
@@ -15,9 +15,10 @@ Gem::Specification.new do |s|
15
15
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
16
  s.require_paths = ["lib"]
17
17
 
18
- s.add_development_dependency 'rake', '>= 0.9.2'
19
- s.add_development_dependency 'rspec', '>= 2.7.0'
20
- s.add_development_dependency 'bundler', '~> 1.0.0'
18
+ s.add_dependency 'rake', '0.9.2'
19
+ s.add_development_dependency 'rake'
20
+ s.add_development_dependency 'rspec'
21
+ s.add_development_dependency 'bundler'
21
22
  end
22
23
 
23
24
 
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe LokkaSh::Command do
4
+ before do
5
+ LokkaSh::Command.clear
6
+ end
7
+
8
+ context 'when define a command' do
9
+ before do
10
+ @block = lambda {}
11
+ LokkaSh::Command.define('foo', &@block)
12
+ end
13
+
14
+ it 'We can find it' do
15
+ LokkaSh::Command.find('foo').should eq(@block)
16
+ end
17
+
18
+ it 'We can find nil with wrong name' do
19
+ LokkaSh::Command.find('bar').should eq(nil)
20
+ end
21
+
22
+ it 'We can get command names' do
23
+ LokkaSh::Command.command_names.should =~ [:foo]
24
+ end
25
+
26
+ describe 'Command.[]' do
27
+ it 'can get a command' do
28
+ LokkaSh::Command['foo'].should eq(@block)
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '.completions' do
34
+ context 'when command does not exist' do
35
+ it 'completions is empty' do
36
+ LokkaSh::Command.completions.should be_empty
37
+ end
38
+ end
39
+
40
+ context 'when commands exist' do
41
+ before do
42
+ LokkaSh::Command.define('foo') {}
43
+ LokkaSh::Command.define('bar') {}
44
+ end
45
+
46
+ it 'completions is empty' do
47
+ LokkaSh::Command.completions.should =~ ['foo', 'bar']
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '.completion_proc' do
53
+ before do
54
+ ['foo', 'lokka generate', 'rake routes', 'rake spec'].each { |c| LokkaSh::Command.completions << c }
55
+ end
56
+
57
+ it 'return completions' do
58
+ LokkaSh::Command.completion_proc.call('foo').should =~ ['foo']
59
+ LokkaSh::Command.completion_proc.call('rake').should =~ ['rake routes', 'rake spec']
60
+ end
61
+
62
+ context 'with blocks for completion' do
63
+ before do
64
+ LokkaSh::Command.completions.clear
65
+ LokkaSh::Command.completions << lambda { |line| 'block' }
66
+ end
67
+
68
+ it 'return completions' do
69
+ LokkaSh::Command.completion_proc.call('foo').should =~ ['block']
70
+ end
71
+ end
72
+ end
73
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,4 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  require 'rspec'
4
- require 'lokka/sh'
5
-
6
- # Requires supporting files with custom matchers and macros, etc,
7
- # in ./support/ and its subdirectories.
8
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
-
10
- RSpec.configure do |config|
11
-
12
- end
4
+ require 'lokka-sh'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lokka-sh
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,42 +9,53 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-07 00:00:00.000000000Z
12
+ date: 2012-03-12 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &2156502560 !ruby/object:Gem::Requirement
16
+ requirement: &2163208040 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - =
20
20
  - !ruby/object:Gem::Version
21
21
  version: 0.9.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2163208040
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2163207420 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
22
33
  type: :development
23
34
  prerelease: false
24
- version_requirements: *2156502560
35
+ version_requirements: *2163207420
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: rspec
27
- requirement: &2156501740 !ruby/object:Gem::Requirement
38
+ requirement: &2163206740 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
31
42
  - !ruby/object:Gem::Version
32
- version: 2.7.0
43
+ version: '0'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *2156501740
46
+ version_requirements: *2163206740
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: bundler
38
- requirement: &2156500800 !ruby/object:Gem::Requirement
49
+ requirement: &2163206180 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
- - - ~>
52
+ - - ! '>='
42
53
  - !ruby/object:Gem::Version
43
- version: 1.0.0
54
+ version: '0'
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *2156500800
47
- description: excute sub commands of rails quickly.
57
+ version_requirements: *2163206180
58
+ description: excute sub commands of lokka quickly.
48
59
  email:
49
60
  - bunny.hop.md@gmail.com
50
61
  executables:
@@ -60,20 +71,20 @@ files:
60
71
  - README.md
61
72
  - Rakefile
62
73
  - bin/lokka-sh
63
- - lib/lokka/sh.rb
64
- - lib/lokka/sh/bundler.rb
65
- - lib/lokka/sh/color.rb
66
- - lib/lokka/sh/command.rb
67
- - lib/lokka/sh/commands.rb
68
- - lib/lokka/sh/forkable.rb
69
- - lib/lokka/sh/helpers.rb
70
- - lib/lokka/sh/prompt.rb
71
- - lib/lokka/sh/rake.rb
72
- - lib/lokka/sh/sinatra.rb
73
- - lib/lokka/sh/version.rb
74
+ - lib/lokka-sh.rb
75
+ - lib/lokka-sh/bundler.rb
76
+ - lib/lokka-sh/cli.rb
77
+ - lib/lokka-sh/color.rb
78
+ - lib/lokka-sh/command.rb
79
+ - lib/lokka-sh/command/define.rb
80
+ - lib/lokka-sh/forkable.rb
81
+ - lib/lokka-sh/helpers.rb
82
+ - lib/lokka-sh/prompt.rb
83
+ - lib/lokka-sh/rake.rb
84
+ - lib/lokka-sh/sinatra.rb
85
+ - lib/lokka-sh/version.rb
74
86
  - lokka-sh.gemspec
75
- - spec/lokka/sh/command_spec.rb
76
- - spec/lokka/sh_spec.rb
87
+ - spec/lokka-sh/command_spec.rb
77
88
  - spec/spec_helper.rb
78
89
  homepage: ''
79
90
  licenses: []
@@ -98,9 +109,8 @@ rubyforge_project: lokka-sh
98
109
  rubygems_version: 1.8.10
99
110
  signing_key:
100
111
  specification_version: 3
101
- summary: excute sub commands of rails quickly.
112
+ summary: excute sub commands of lokka quickly.
102
113
  test_files:
103
- - spec/lokka/sh/command_spec.rb
104
- - spec/lokka/sh_spec.rb
114
+ - spec/lokka-sh/command_spec.rb
105
115
  - spec/spec_helper.rb
106
116
  has_rdoc:
data/lib/lokka/sh.rb DELETED
@@ -1,40 +0,0 @@
1
- require 'readline'
2
- require 'stringio'
3
- require 'rake/dsl_definition'
4
- require 'rake'
5
-
6
- module Lokka
7
- module Sh
8
- autoload :VERSION, "lokka/sh/version"
9
- autoload :Color, "lokka/sh/color"
10
- autoload :Prompt, "lokka/sh/prompt"
11
- autoload :Helpers, "lokka/sh/helpers"
12
- autoload :Forkable, "lokka/sh/forkable"
13
- autoload :Sinatra, "lokka/sh/sinatra"
14
- autoload :Rake, "lokka/sh/rake"
15
- autoload :Command, "lokka/sh/command"
16
- autoload :Bundler, "lokka/sh/bundler"
17
-
18
- def self.start
19
- Color.with(:cyan) { "Lokka.env: #{::Lokka.env}\n`help` to print help" }
20
- add_load_path
21
- Rake.init
22
- load_commands
23
- Prompt.invoke
24
- end
25
-
26
- def self.add_load_path
27
- lib = File.expand_path(File.dirname(__FILE__) + '/../../lib')
28
- $:.unshift(lib) if File.directory?(lib) && !$:.include?(lib)
29
- end
30
-
31
- def self.load_commands
32
- require "lokka/sh/commands"
33
- lokkashrc = "#{::Lokka.root}/.lokkashrc"
34
- load lokkashrc
35
- Color.with(:blue) {"load #{lokkashrc}"}
36
- rescue LoadError
37
- end
38
- end
39
- end
40
-
@@ -1,22 +0,0 @@
1
- module Lokka
2
- module Sh
3
- module Bundler
4
- extend Forkable
5
-
6
- class << self
7
- def _invoke(line)
8
- line ||= 'install'
9
- command, *args = line.split(/\s+/)
10
- ARGV.clear
11
- ARGV.concat args
12
- require 'bundler/cli'
13
- ::Bundler::CLI.new.send(command.to_sym)
14
- end
15
-
16
- def sub_commands
17
- %w(exec install update open package config check list show console open viz init gem)
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,14 +0,0 @@
1
- module Lokka
2
- module Sh
3
- module Color
4
- @colors =
5
- { :r_video => 7, :black => 30, :red => 31, :green => 32, :yellow => 33,
6
- :blue => 34, :magenta => 35, :cyan => 36, :white => 27, :default => 39,
7
- :bg_black => 40, :bg_red => 41, :bg_green => 42, :bg_default => 49 }
8
-
9
- def self.with(name, &block)
10
- puts "\e[#{@colors[name.to_sym]}m" + yield + "\e[0m"
11
- end
12
- end
13
- end
14
- end
@@ -1,61 +0,0 @@
1
- module Lokka
2
- module Sh
3
- module Command
4
- class << self
5
- def commands
6
- @commands ||= {}
7
- end
8
-
9
- def define(*names, &block)
10
- names.each do |name|
11
- commands[name.to_sym] = block
12
- completions << name.to_s
13
- end
14
- end
15
-
16
- def find(line)
17
- if name = line.split(/\s+/, 2)[0]
18
- commands[name.to_sym]
19
- else
20
- nil
21
- end
22
- end
23
-
24
- def command_names
25
- commands.keys
26
- end
27
-
28
- def [](name)
29
- commands[name.to_sym]
30
- end
31
-
32
- def completion_proc
33
- lambda { |line|
34
- regex = /#{Regexp.quote(line)}/
35
- completions.map { |completion|
36
- case completion
37
- when String
38
- completion if completion.match(regex)
39
- when Proc
40
- completion.call(line)
41
- end
42
- }.compact
43
- }
44
- end
45
-
46
- def completions
47
- @completions ||= []
48
- end
49
-
50
- def completions=(completions)
51
- @completions = completions
52
- end
53
-
54
- def clear
55
- commands.clear
56
- completions.clear
57
- end
58
- end
59
- end
60
- end
61
- end
@@ -1,76 +0,0 @@
1
- module Lokka
2
- module Sh
3
- Command.define 'help' do
4
- Color.with(:cyan) do
5
- <<HELP
6
- help -> print help
7
- lokka console -> starting console
8
- rake TASK -> execute rake task
9
- t, tasks PATTERN -> print rake tasks
10
- bundle -> execute bundler command
11
- exit -> exit from lokka-sh
12
- restart -> restart lokka-sh
13
- ! -> execute a system command
14
- eval -> eval as ruby script
15
- TODO: reload
16
- HELP
17
- end
18
- end
19
-
20
- Command.define 'lokka' do |arg|
21
- Lokka::Sh::Sinatra.invoke(arg)
22
- end
23
-
24
- Command.define 'rake' do |arg|
25
- Lokka::Sh::Rake.invoke(arg)
26
- end
27
-
28
- Command.define 'tasks', 't' do |arg|
29
- Rake::Task.tasks.each do |task|
30
- puts task.name
31
- end
32
- end
33
-
34
- Lokka::Sh::Rake.task_names.map do |name|
35
- Command.completions << "rake #{name}"
36
- end
37
-
38
- Command.define 'bundle' do |arg|
39
- Lokka::Sh::Bundler.invoke(arg)
40
- end
41
-
42
- (Lokka::Sh::Bundler.sub_commands - ['init']).map do |c|
43
- Command.completions << "bundle #{c}"
44
- end
45
-
46
- Command.define '!' do |arg|
47
- system arg
48
- end
49
-
50
- Command.define 'eval' do |arg|
51
- Color.with(:blue) { "=> #{eval(arg, binding, __FILE__, __LINE__).inspect}" }
52
- end
53
-
54
- Command.define 'log' do |arg|
55
- Color.with(:r_video) { "Ctrl-C to quit" }
56
- system 'tail', '-f', Lokka.root + "tmp/#{(arg || 'development')}.log"
57
- end
58
-
59
- Command.completions += %w(development test production).map { |i| "log #{i}" }
60
-
61
- Command.define 'exit' do
62
- exit
63
- end
64
-
65
- Command.define 'restart' do
66
- puts 'restarting...'
67
- exec File.expand_path('../../../../bin/lokka-sh', __FILE__)
68
- end
69
-
70
- =begin
71
- Command.define 'reload' do
72
- Lokka::Sh::Sinatra.reload!
73
- end
74
- =end
75
- end
76
- end
@@ -1,36 +0,0 @@
1
- module Lokka
2
- module Sh
3
- module Forkable
4
- include Lokka::Sh::Helpers
5
-
6
- def invoke(line, options = {})
7
- run_before_fork
8
- pid = fork do
9
- run_after_fork
10
- _invoke(line)
11
- end
12
- Process.waitpid(pid)
13
- end
14
-
15
- def _invoke
16
- raise NotImplementedError
17
- end
18
-
19
- def before_fork(&block)
20
- @before_fork = block
21
- end
22
-
23
- def after_fork(&block)
24
- @after_fork = block
25
- end
26
-
27
- def run_before_fork(&block)
28
- @before_fork.call if @before_fork
29
- end
30
-
31
- def run_after_fork(&block)
32
- @after_fork.call if @after_fork
33
- end
34
- end
35
- end
36
- end
@@ -1,60 +0,0 @@
1
- module Lokka
2
- module Sh
3
- module Helpers
4
- # copy from pry: https://github.com/pry/pry
5
- #
6
- # Create scrollable output via less!
7
- #
8
- # This command runs `less` in a subprocess, and gives you the IO to its STDIN pipe
9
- # so that you can communicate with it.
10
- #
11
- # Example:
12
- #
13
- # lesspipe do |less|
14
- # 50.times { less.puts "Hi mom!" }
15
- # end
16
- #
17
- # The default less parameters are:
18
- # * Allow colour
19
- # * Don't wrap lines longer than the screen
20
- # * Quit immediately (without paging) if there's less than one screen of text.
21
- #
22
- # You can change these options by passing a hash to `lesspipe`, like so:
23
- #
24
- # lesspipe(:wrap=>false) { |less| less.puts essay.to_s }
25
- #
26
- # It accepts the following boolean options:
27
- # :color => Allow ANSI colour codes?
28
- # :wrap => Wrap long lines?
29
- # :always => Always page, even if there's less than one page of text?
30
- #
31
- def lesspipe(*args)
32
- if args.any? and args.last.is_a?(Hash)
33
- options = args.pop
34
- else
35
- options = {}
36
- end
37
-
38
- output = args.first if args.any?
39
-
40
- params = []
41
- params << "-R" unless options[:color] == false
42
- params << "-S" unless options[:wrap] == true
43
- params << "-F" unless options[:always] == true
44
- if options[:tail] == true
45
- params << "+\\>"
46
- $stderr.puts "Seeking to end of stream..."
47
- end
48
- params << "-X"
49
-
50
- IO.popen("less #{params * ' '}", "w") do |less|
51
- if output
52
- less.puts output
53
- else
54
- yield less
55
- end
56
- end
57
- end
58
- end
59
- end
60
- end
data/lib/lokka/sh/rake.rb DELETED
@@ -1,39 +0,0 @@
1
- module Lokka
2
- module Sh
3
- module Rake
4
- extend Forkable
5
-
6
- class << self
7
- def init
8
- $stdout = StringIO.new
9
-
10
- before_fork do
11
- end
12
- after_fork do
13
- end
14
-
15
- ::Rake.application = ::Rake::Application.new
16
- ::Rake.application.init
17
- ::Rake.application.load_rakefile
18
- ensure
19
- $stdout = STDOUT
20
- end
21
-
22
- def _invoke(line)
23
- line ||= 'default'
24
- name, *args = line.split(/\s+/)
25
- args.each do |arg|
26
- env, value = arg.split('=')
27
- next unless env && !env.empty? && value && !value.empty?
28
- ENV[env] = value
29
- end
30
- ::Rake.application[name].invoke
31
- end
32
-
33
- def task_names
34
- ::Rake.application.tasks.map{|t| t.name}
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,33 +0,0 @@
1
- module Lokka
2
- module Sh
3
- module Sinatra
4
- extend Forkable
5
-
6
- class << self
7
- def init
8
- before_fork do
9
- end
10
- after_fork do
11
- end
12
- end
13
-
14
- def _invoke(line)
15
- case line
16
- when "console"
17
- require 'irb'
18
- IRB.start(Lokka.root)
19
- else
20
- raise
21
- end
22
- end
23
-
24
- def reload!
25
- end
26
-
27
- def sub_commands
28
- %w(console)
29
- end
30
- end
31
- end
32
- end
33
- end
@@ -1,5 +0,0 @@
1
- module Lokka
2
- module Sh
3
- VERSION = "1.0.3"
4
- end
5
- end
@@ -1,73 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Lokka::Sh::Command do
4
- before do
5
- Lokka::Sh::Command.clear
6
- end
7
-
8
- context 'when define a command' do
9
- before do
10
- @block = lambda {}
11
- Lokka::Sh::Command.define('foo', &@block)
12
- end
13
-
14
- it 'We can find it' do
15
- Lokka::Sh::Command.find('foo').should eq(@block)
16
- end
17
-
18
- it 'We can find nil with wrong name' do
19
- Lokka::Sh::Command.find('bar').should eq(nil)
20
- end
21
-
22
- it 'We can get command names' do
23
- Lokka::Sh::Command.command_names.should =~ [:foo]
24
- end
25
-
26
- describe 'Command.[]' do
27
- it 'can get a command' do
28
- Lokka::Sh::Command['foo'].should eq(@block)
29
- end
30
- end
31
- end
32
-
33
- describe '.completions' do
34
- context 'when command does not exist' do
35
- it 'completions is empty' do
36
- Lokka::Sh::Command.completions.should be_empty
37
- end
38
- end
39
-
40
- context 'when commands exist' do
41
- before do
42
- Lokka::Sh::Command.define('foo') {}
43
- Lokka::Sh::Command.define('bar') {}
44
- end
45
-
46
- it 'completions is empty' do
47
- Lokka::Sh::Command.completions.should =~ ['foo', 'bar']
48
- end
49
- end
50
- end
51
-
52
- describe '.completion_proc' do
53
- before do
54
- ['foo', 'lokka generate', 'rake routes', 'rake spec'].each { |c| Lokka::Sh::Command.completions << c }
55
- end
56
-
57
- it 'return completions' do
58
- Lokka::Sh::Command.completion_proc.call('foo').should =~ ['foo']
59
- Lokka::Sh::Command.completion_proc.call('rake').should =~ ['rake routes', 'rake spec']
60
- end
61
-
62
- context 'with blocks for completion' do
63
- before do
64
- Lokka::Sh::Command.completions.clear
65
- Lokka::Sh::Command.completions << lambda { |line| 'block' }
66
- end
67
-
68
- it 'return completions' do
69
- Lokka::Sh::Command.completion_proc.call('foo').should =~ ['block']
70
- end
71
- end
72
- end
73
- end
@@ -1,5 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Lokka::Sh do
4
- pending
5
- end