the_dude 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ script: 'bundle exec rspec'
3
+ rvm:
4
+ - 1.9.3
5
+ gemfile:
6
+ - Gemfile
7
+ notifications:
8
+ recipients:
9
+ - adam@29ways.co.uk
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- the_dude (0.0.3)
4
+ the_dude (0.0.4)
5
5
  colored
6
6
  hirb
7
7
  nokogiri
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # The Dude
2
2
 
3
3
  [![Code Climate](https://codeclimate.com/github/adamphillips/the_dude.png)](https://codeclimate.com/github/adamphillips/the\_dude)
4
+ [![Build Status](https://travis-ci.org/adamphillips/the_dude.png?branch=master)](https://travis-ci.org/adamphillips/the\_dude)
4
5
 
5
6
  The Dude is here to make your terminal life more chilled.
6
7
 
@@ -9,9 +10,11 @@ Alternatively, The Dude already knows how to do a bunch of stuff for you.
9
10
 
10
11
  For example:
11
12
 
12
- $ dude fetch google.co.uk # opens a google.co.uk in lynx
13
- $ dude show me pictures of cool stuff # opens a browser with a google images search for 'cool stuff'
14
- $ dude wassup # starts top
13
+ ```shell
14
+ $ dude fetch google.co.uk # opens a google.co.uk in lynx
15
+ $ dude show me pictures of cool stuff # opens a browser with a google images search for 'cool stuff'
16
+ $ dude wassup # starts top
17
+ ```
15
18
 
16
19
  How can this be useful?
17
20
  - a single dude command can trigger a bunch of other commands. Sure you
@@ -19,22 +22,39 @@ How can this be useful?
19
22
  you're happy writing shell scripts.
20
23
  - for the command-line phobic, dude provides a friendly interface to the
21
24
  command line.
22
- - for those who are at on the command line, there can still be a benefit
25
+ - for those who are at home on the command line, there can still be a benefit
23
26
  in reducing the cognitive overhead required to run scripts.
24
- - Commands can be bundled and shared using gems meaning that command line savvy
27
+ - commands can be bundled and shared using gems meaning that command line savvy
25
28
  devs can set up simpler commands for other team members to use.
26
29
 
27
30
  ## Talking to The Dude
28
31
 
29
- The Dude is still very young and not on rubygems quite yet. For the mean
30
- time, you will need to checkout the repo and build the gem.
32
+ You can install TheDude using
33
+
34
+ ```shell
35
+ $ gem install the_dude
36
+ ```
31
37
 
32
38
  The Dude comes with a 'dude' binary so you can use this to run commands
33
39
 
34
- dude why? # will output 'because'
40
+ ```shell
41
+ $ dude why? # will output 'because'
42
+ ```
35
43
 
36
- Alternatively, you can start the dude inteactively with
44
+ Alternatively, you can start the dude interactively with
37
45
 
38
- dude -i
46
+ ```shell
47
+ $ dude -i
48
+ ```
39
49
 
40
50
  Then you get a dude prompt you can enter commands straight into.
51
+
52
+ ## Installing plugins
53
+
54
+ You can install plugins for TheDude that contain additional commands or add additional functionality. Plugins are bundled as gems. For example to install the trello plugin simply use
55
+
56
+ ```shell
57
+ $ gem install the_dude-trello
58
+ ```
59
+
60
+ You will now be able to use the commands defined in the Trello plugin.
data/bin/dude CHANGED
@@ -1,10 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'the_dude'
3
3
 
4
- #include TheDude::Dsl
5
-
6
4
  DUDERC_PATH = "#{ENV['HOME']}/.duderc"
7
- #load DUDERC_PATH if File.exists? DUDERC_PATH
8
5
  TheDude::Dsl.from_file DUDERC_PATH
9
6
 
10
7
  # Setup rlwrap for readline support
@@ -15,13 +15,32 @@ module TheDude
15
15
  TheDude.say *args
16
16
  end
17
17
 
18
+ # Creates and registers a new variable
19
+ def var *args
20
+ TheDude::Variable.new(*args).register
21
+ end
22
+
18
23
  # Class methods
19
24
  class << self
25
+ # Runs the specified code inside an instance of the DSL. Code can be
26
+ # passed as a string or block. If both are given, the string is
27
+ # ignored.
28
+ #
29
+ # @param [String] text Code to run
30
+ # @param [Block] code Code to run
31
+ def run text=nil, &code
32
+ if block_given?
33
+ new.instance_exec &code
34
+ else
35
+ new.instance_eval text
36
+ end
37
+ end
38
+
20
39
  # Reads in a file and processes it using The Dude Dsl
21
40
  #
22
41
  # @param [String] path Path to the file to read
23
42
  def from_file path
24
- new.instance_eval File.read(path) if File.exists? path
43
+ run File.read(path) if File.exists? path
25
44
  end
26
45
  end
27
46
  end
@@ -25,8 +25,8 @@ module TheDude
25
25
  return @regexp unless @regexp.nil?
26
26
 
27
27
  # If we have a string, escape it turn it into a regex and send it back
28
- @regexp = /^#{Regexp.quote @expression}$/ and return @regexp if @expression.kind_of? String
29
- @regexp = @expression
28
+ @regexp = /^#{Regexp.quote @expression}$/ if @expression.kind_of? String
29
+ @regexp ||= @expression
30
30
 
31
31
  substitute_all_variables
32
32
  check_for_undefined_variables
@@ -55,7 +55,7 @@ module TheDude
55
55
  # [Regexp] Substitues the specified variable for its pattern and converts
56
56
  # the result back to a regex
57
57
  def substitute_variable var
58
- subbed = @regexp.source.gsub(/\:#{var.name}(\s|$)/, "(#{var.pattern.source}) ")
58
+ subbed = @regexp.source.gsub(/\:#{var.name}(\s*)/, "(#{var.pattern.source})\\1")
59
59
  subbed.strip! if subbed
60
60
  Regexp.new subbed
61
61
  end
@@ -19,5 +19,29 @@ TheDude::Command.new /^show me pictures of (.*)/, ->(query){ `open "https://www.
19
19
  TheDude::Command.new /source for :url/, ->(url){ `curl #{url} | highlight --syntax html -O xterm256` }
20
20
  TheDude::Command.new /headers for :url/, ->(url){ `curl -I #{url}` }
21
21
 
22
+ TheDude::Command.new 'list vars' do
23
+ extend Hirb::Console
24
+ say 'Defined variables'
25
+ say table(TheDude.variables.map {|k, v| {var: v.name, regex: v.pattern}})
26
+ end
27
+
28
+ TheDude::Command.new 'list commands' do
29
+ extend Hirb::Console
30
+ say 'Defined commands'
31
+ say table(TheDude.commands.map {|k, v| {expr: v.expression.expression, regex: v.expression.to_regexp}})
32
+ end
33
+
34
+ TheDude::Command.new 'list plugins' do
35
+ extend Hirb::Console
36
+ say 'Loaded plugins'
37
+ say table(TheDude::Plugin.all.map {|v| {name: v.name, gem_name: v.gem_name}})
38
+ end
39
+
40
+ TheDude::Command.new 'what you got' do
41
+ ask 'list vars'
42
+ ask 'list commands'
43
+ ask 'list plugins'
44
+ end
45
+
22
46
  # Load plugins
23
47
  TheDude::Plugin.all.each {|r| require r.gem_name}
@@ -1,3 +1,3 @@
1
1
  module TheDude
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -53,7 +53,18 @@ describe TheDude::Command do
53
53
  end
54
54
  end
55
55
 
56
- context 'when passed an expression question containing a defined variable' do
56
+ context 'when passed an string question containing a defined variable' do
57
+ before :each do
58
+ TheDude::Variable.new(:server, /\S+/).register
59
+ @command = TheDude::Command.new 'connect to :server', ->(server) {'connecting to #{server}'}
60
+ end
61
+
62
+ it 'should convert the expression to a normal regex adding start and end flags' do
63
+ @command.expression.to_regexp.should == /^connect\ to\ (\S+)$/
64
+ end
65
+ end
66
+
67
+ context 'when passed a regex question containing a defined variable' do
57
68
  before :each do
58
69
  TheDude::Variable.new(:server, /\S+/).register
59
70
  @command = TheDude::Command.new /connect to :server/, ->(server) {'connecting to #{server}'}
@@ -1,18 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe TheDude::Dsl do
4
- class Anony
5
- include TheDude::Dsl
6
- end
7
-
8
4
  describe '#ask' do
9
5
  context 'when asked' do
10
6
  context 'and the command exists' do
11
7
  before :each do
12
- Anony.new.command 'hey', 'what'
8
+ TheDude::Dsl.new.command 'hey', 'what'
13
9
  end
14
10
  it 'should perform the requested command' do
15
- Anony.new.ask('hey').should == 'what'
11
+ TheDude::Dsl.new.ask('hey').should == 'what'
16
12
  end
17
13
  end
18
14
  end
@@ -26,7 +22,7 @@ describe TheDude::Dsl do
26
22
 
27
23
  context 'and a string answer' do
28
24
  it 'should register a new dude command' do
29
- Anony.new.command @question, 'what'
25
+ TheDude::Dsl.new.command @question, 'what'
30
26
  TheDude.commands.length.should == 1
31
27
  TheDude.commands[/^hey$/].ask.should == 'what'
32
28
  end
@@ -34,7 +30,7 @@ describe TheDude::Dsl do
34
30
 
35
31
  context 'and a proc answer' do
36
32
  it 'should register a new dude command' do
37
- Anony.new.command @question, ->{'what'}
33
+ TheDude::Dsl.new.command @question, ->{'what'}
38
34
  TheDude.commands.length.should == 1
39
35
  TheDude.commands[/^hey$/].ask.should == 'what'
40
36
  end
@@ -42,7 +38,7 @@ describe TheDude::Dsl do
42
38
 
43
39
  context 'and a block answer' do
44
40
  it 'should register a new dude command' do
45
- Anony.new.command @question do
41
+ TheDude::Dsl.new.command @question do
46
42
  'what'
47
43
  end
48
44
  TheDude.commands.length.should == 1
@@ -51,4 +47,53 @@ describe TheDude::Dsl do
51
47
  end
52
48
  end
53
49
  end
50
+
51
+ describe '#var' do
52
+ context 'when passed a variable name and regex' do
53
+ it 'should register the variable with TheDude' do
54
+ TheDude::Dsl.new.var :something, /cool/
55
+ TheDude.variables.length.should == 1
56
+ TheDude.variables[:something].pattern.should == /cool/
57
+ end
58
+ end
59
+ end
60
+
61
+ describe '.run' do
62
+ context 'when passed a string' do
63
+ before :each do
64
+ TheDude::Dsl.run 'var :server, /\S+/'
65
+ end
66
+
67
+ it 'should execute the string' do
68
+ TheDude.variables.length.should == 1
69
+ TheDude.variables[:server].should be_kind_of TheDude::Variable
70
+ end
71
+ end
72
+
73
+ context 'when passed a block' do
74
+ before :each do
75
+ TheDude::Dsl.run do
76
+ var :server, /\S+/
77
+ end
78
+ end
79
+
80
+ it 'should execute the block' do
81
+ TheDude.variables.length.should == 1
82
+ TheDude.variables[:server].should be_kind_of TheDude::Variable
83
+ end
84
+ end
85
+
86
+ context 'when passed a string and a block' do
87
+ before :each do
88
+ TheDude::Dsl.run do
89
+ var :server, /\S+/
90
+ end
91
+ end
92
+
93
+ it 'should execute the block' do
94
+ TheDude.variables.length.should == 1
95
+ TheDude.variables[:server].should be_kind_of TheDude::Variable
96
+ end
97
+ end
98
+ end
54
99
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: the_dude
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-16 00:00:00.000000000 Z
12
+ date: 2013-05-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -149,6 +149,7 @@ files:
149
149
  - .gitignore
150
150
  - .rspec
151
151
  - .rvmrc
152
+ - .travis.yml
152
153
  - Gemfile
153
154
  - Gemfile.lock
154
155
  - README.md
@@ -196,7 +197,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
196
197
  version: '0'
197
198
  segments:
198
199
  - 0
199
- hash: 4224877102825340562
200
+ hash: 1990469814655058138
200
201
  required_rubygems_version: !ruby/object:Gem::Requirement
201
202
  none: false
202
203
  requirements:
@@ -205,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
205
206
  version: '0'
206
207
  segments:
207
208
  - 0
208
- hash: 4224877102825340562
209
+ hash: 1990469814655058138
209
210
  requirements: []
210
211
  rubyforge_project:
211
212
  rubygems_version: 1.8.25