rubyx 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/README.md +42 -5
  4. data/Rakefile +3 -0
  5. data/bin/rubyx +76 -16
  6. data/rubyx.gemspec +2 -2
  7. metadata +14 -21
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8fcac96e9bd388b3bc5c2c860a3af698259c83a1
4
+ data.tar.gz: f26a3fe4e6b2398860c15727a56109aa259f72d5
5
+ SHA512:
6
+ metadata.gz: 5b8824d4db4843f6d31bec860498c5606f2ad0b60cd5471eed193d9dd7b0ad11b5a001a099fd531d7147283805e3272534a0daa4348b37e4de0f91a21306d7f2
7
+ data.tar.gz: cae64896eee7b31c89ec1324c989e850d627092473a9cc674efc9be1ff550099082bf7d59e8eec01adc7b02dd5aa39aed5692ba337c73a514b32f509188ad64c
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  /Gemfile.lock
2
+ /pkg
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
- # Ruby X
1
+ # Rubyx [![Gem Version](https://badge.fury.io/rb/rubyx.svg)](http://badge.fury.io/rb/rubyx)
2
2
 
3
- A tool to run a Ruby script while showing the executed command, just like
4
- `bash -x`.
3
+ A tool to run a Ruby script while showing the executed lines of code.
5
4
 
6
5
  ## Installation
7
6
 
@@ -11,8 +10,46 @@ $ gem install rubyx
11
10
 
12
11
  ## Usage
13
12
 
14
- ```bash
15
- $ rubyx file.rb
13
+ ```
14
+ $ rubyx -h
15
+ Usage: rubyx [options] <file> -- [arguments]
16
+
17
+ Options:
18
+ -i PREFIX Set prefix for source code (default: "")
19
+ -o PREFIX Set prefix for standard output (default: "# => ")
20
+ -h Show this message
21
+ ```
22
+
23
+ ## Example
24
+
25
+ ```
26
+ $ rubyx input.rb > output.rb
27
+ ```
28
+
29
+ In `input.rb`:
30
+
31
+ ```ruby
32
+ def say
33
+ puts 'Hello!'
34
+ end
35
+
36
+ say
37
+
38
+ puts 'Bye!'
39
+ ```
40
+
41
+ In `output.rb`:
42
+
43
+ ```ruby
44
+ def say
45
+ puts 'Hello!'
46
+ end
47
+
48
+ say
49
+ # => Hello!
50
+
51
+ puts 'Bye!'
52
+ # => Bye!
16
53
  ```
17
54
 
18
55
  ## Contributing
data/Rakefile CHANGED
@@ -1 +1,4 @@
1
1
  require 'bundler/gem_tasks'
2
+
3
+ task :default do
4
+ end
data/bin/rubyx CHANGED
@@ -1,31 +1,91 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- def abort
4
- puts 'Usage: rubyx <file> [arguments]'
3
+ require 'tempfile'
4
+ require 'optparse'
5
+
6
+ iprefix = ''
7
+ oprefix = '# => '
8
+
9
+ parser = OptionParser.new(nil, 20) do |o|
10
+ o.banner = 'Usage: rubyx [options] <file> -- [arguments]'
11
+
12
+ o.separator ''
13
+ o.separator 'Options:'
14
+
15
+ o.on('-i PREFIX', %{Set prefix for source code (default: "#{iprefix}")}) do |value|
16
+ iprefix = value
17
+ end
18
+
19
+ o.on('-o PREFIX', %{Set prefix for standard output (default: "#{oprefix}")}) do |value|
20
+ oprefix = value
21
+ end
22
+
23
+ o.on_tail('-h', 'Show this message') do
24
+ puts o
25
+ exit
26
+ end
27
+ end
28
+
29
+ begin
30
+ parser.parse!
31
+ rescue OptionParser::InvalidOption
32
+ puts parser
5
33
  exit 1
6
34
  end
7
35
 
36
+ def abort(message)
37
+ puts message
38
+ exit 1
39
+ end
40
+
41
+ def capture(stream)
42
+ original = stream.dup
43
+ surrogate = Tempfile.new('rubyx')
44
+ stream.reopen(surrogate)
45
+
46
+ yield
47
+
48
+ stream.rewind
49
+ surrogate.readlines
50
+ ensure
51
+ surrogate.close
52
+ surrogate.unlink
53
+ stream.reopen(original)
54
+ end
55
+
56
+ def trace(handler)
57
+ set_trace_func(handler)
58
+ yield
59
+ ensure
60
+ set_trace_func(nil)
61
+ end
62
+
8
63
  filename = ARGV.shift || ''
9
- abort if filename.empty?
64
+ abort(parser) if filename.empty?
10
65
 
11
66
  filename = File.absolute_path(filename)
67
+ abort('The file does not exist.') unless File.exist?(filename)
12
68
 
13
- unless File.exist?(filename)
14
- puts 'The file does not exist.'
15
- exit 1
69
+ tag = Random.new_seed
70
+ last_position = 0
71
+
72
+ handler = Proc.new do |event, source, position|
73
+ if event == 'line' && source == filename && position > last_position
74
+ (last_position..(position - 1)).each { |i| puts "#{tag}#{i}" }
75
+ last_position = position
76
+ end
16
77
  end
17
78
 
18
- lines = File.readlines(filename)
19
- previous = 0
79
+ source = File.readlines(filename)
80
+ output = capture($stdout) { trace(handler) { load(filename) } }
20
81
 
21
- trace = Proc.new do |event, file, current|
22
- if event == 'line' && file == filename && current > previous
23
- (previous..(current - 1)).each do |i|
24
- puts "+ #{lines[i]}"
25
- end
26
- previous = current
82
+ output.map! do |line|
83
+ if line =~ /^#{tag}(?<position>\d+)$/
84
+ line = source[Regexp.last_match(:position).to_i]
85
+ "#{iprefix}#{line}"
86
+ else
87
+ "#{oprefix}#{line}"
27
88
  end
28
89
  end
29
90
 
30
- set_trace_func(trace)
31
- require filename
91
+ puts output.join
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'rubyx'
3
- spec.version = '0.0.1'
3
+ spec.version = '0.0.2'
4
4
  spec.authors = ['Ivan Ukhov']
5
5
  spec.email = ['ivan.ukhov@gmail.com']
6
6
  spec.summary = 'A tool to run a Ruby script while showing the ' \
7
- 'executed commands.'
7
+ 'executed lines of code.'
8
8
  spec.description = spec.summary
9
9
  spec.homepage = 'https://github.com/IvanUkhov/rubyx'
10
10
  spec.license = 'MIT'
metadata CHANGED
@@ -1,49 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ivan Ukhov
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-08-01 00:00:00.000000000 Z
11
+ date: 2014-08-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.6'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '1.6'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: '10.0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: '10.0'
46
- description: A tool to run a Ruby script while showing the executed commands.
41
+ description: A tool to run a Ruby script while showing the executed lines of code.
47
42
  email:
48
43
  - ivan.ukhov@gmail.com
49
44
  executables:
@@ -51,7 +46,7 @@ executables:
51
46
  extensions: []
52
47
  extra_rdoc_files: []
53
48
  files:
54
- - .gitignore
49
+ - ".gitignore"
55
50
  - Gemfile
56
51
  - LICENSE.txt
57
52
  - README.md
@@ -61,27 +56,25 @@ files:
61
56
  homepage: https://github.com/IvanUkhov/rubyx
62
57
  licenses:
63
58
  - MIT
59
+ metadata: {}
64
60
  post_install_message:
65
61
  rdoc_options: []
66
62
  require_paths:
67
63
  - lib
68
64
  required_ruby_version: !ruby/object:Gem::Requirement
69
- none: false
70
65
  requirements:
71
- - - ! '>='
66
+ - - ">="
72
67
  - !ruby/object:Gem::Version
73
68
  version: 1.9.3
74
69
  required_rubygems_version: !ruby/object:Gem::Requirement
75
- none: false
76
70
  requirements:
77
- - - ! '>='
71
+ - - ">="
78
72
  - !ruby/object:Gem::Version
79
73
  version: '0'
80
74
  requirements: []
81
75
  rubyforge_project:
82
- rubygems_version: 1.8.23.2
76
+ rubygems_version: 2.2.2
83
77
  signing_key:
84
- specification_version: 3
85
- summary: A tool to run a Ruby script while showing the executed commands.
78
+ specification_version: 4
79
+ summary: A tool to run a Ruby script while showing the executed lines of code.
86
80
  test_files: []
87
- has_rdoc: