code_output 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.rvmrc +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +3 -0
- data/MIT-LICENSE +18 -0
- data/README.md +59 -0
- data/Rakefile +7 -0
- data/bin/code_output +18 -0
- data/code_output.gemspec +23 -0
- data/lib/code_output/version.rb +3 -0
- data/lib/code_output.rb +71 -0
- data/spec/assets/catch.dump.rb +8 -0
- data/spec/assets/catch.output.rb +4 -0
- data/spec/assets/catch.rb +8 -0
- data/spec/assets/coffee_machine.dump.rb +28 -0
- data/spec/assets/coffee_machine.output.rb +5 -0
- data/spec/assets/coffee_machine.rb +28 -0
- data/spec/assets/inheritance.dump.rb +17 -0
- data/spec/assets/inheritance.output.rb +3 -0
- data/spec/assets/inheritance.rb +17 -0
- data/spec/assets/simple.dump.rb +3 -0
- data/spec/assets/simple.output.rb +3 -0
- data/spec/assets/simple.rb +3 -0
- data/spec/code_output_spec.rb +68 -0
- data/spec/spec_helper.rb +9 -0
- metadata +134 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm 1.9.3
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
2
|
+
a copy of this software and associated documentation files (the
|
3
|
+
"Software"), to deal in the Software without restriction, including
|
4
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
5
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
6
|
+
permit persons to whom the Software is furnished to do so, subject to
|
7
|
+
the following conditions:
|
8
|
+
|
9
|
+
The above copyright notice and this permission notice shall be
|
10
|
+
included in all copies or substantial portions of the Software.
|
11
|
+
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
13
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
14
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
15
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
16
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
17
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
18
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# CodeOutput [![Build Status](https://secure.travis-ci.org/infertux/code_output.png)](http://travis-ci.org/#!/infertux/code_output)
|
2
|
+
|
3
|
+
Inject a script output into the source file as comments.
|
4
|
+
This is particularly useful to generate code examples.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
### Command line
|
9
|
+
|
10
|
+
```bash
|
11
|
+
code_output example.rb
|
12
|
+
```
|
13
|
+
|
14
|
+
### Library
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
# example.rb
|
18
|
+
|
19
|
+
class Example
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
p __method__
|
23
|
+
end
|
24
|
+
|
25
|
+
p __method__
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
Example.new
|
30
|
+
```
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
parser = CodeOutput::Parser.new "example.rb"
|
34
|
+
parser.run # executes the script
|
35
|
+
|
36
|
+
parser.raw_output #=> "nil\n:initialize\n"
|
37
|
+
parser.file_with_output #=> "# example.rb\n\nclass Example\n\n def initialize\n p __method__ #=> :initialize\n end\n\n p __method__ #=> nil\n\nend\n\nExample.new\n"
|
38
|
+
|
39
|
+
parser.dump 'example_with_output.rb' # writes file_with_output into the given file
|
40
|
+
```
|
41
|
+
|
42
|
+
## Installation
|
43
|
+
|
44
|
+
Add this line to your application's Gemfile:
|
45
|
+
|
46
|
+
gem 'code_output'
|
47
|
+
|
48
|
+
And then execute:
|
49
|
+
|
50
|
+
$ bundle
|
51
|
+
|
52
|
+
Or install it yourself as:
|
53
|
+
|
54
|
+
$ gem install code_output
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
MIT
|
59
|
+
|
data/Rakefile
ADDED
data/bin/code_output
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
lib = File.expand_path('../../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'code_output'
|
7
|
+
|
8
|
+
if ARGV.size != 1
|
9
|
+
$stderr.puts "#{File.basename(__FILE__)} takes exactly one argument."
|
10
|
+
exit 1
|
11
|
+
end
|
12
|
+
|
13
|
+
filename = ARGV[0]
|
14
|
+
|
15
|
+
parser = CodeOutput::Parser.new filename
|
16
|
+
parser.run
|
17
|
+
puts parser.file_with_output
|
18
|
+
|
data/code_output.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'code_output/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "code_output"
|
8
|
+
gem.version = CodeOutput::VERSION
|
9
|
+
gem.authors = ["Cédric Félizard"]
|
10
|
+
gem.email = ["cedric@felizard.fr"]
|
11
|
+
gem.description = %q{Inject a script output into the source file as comments.}
|
12
|
+
gem.summary = gem.description
|
13
|
+
gem.homepage = "https://github.com/infertux/code_output"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rake'
|
21
|
+
gem.add_development_dependency 'rspec'
|
22
|
+
gem.add_development_dependency 'simplecov'
|
23
|
+
end
|
data/lib/code_output.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require "code_output/version"
|
2
|
+
require "stringio"
|
3
|
+
|
4
|
+
module CodeOutput
|
5
|
+
|
6
|
+
class Parser
|
7
|
+
|
8
|
+
def initialize filename
|
9
|
+
@filename = File.expand_path(filename)
|
10
|
+
raise IOError, "`#{filename}' is not readable" unless File.readable? filename
|
11
|
+
|
12
|
+
@old_stdout = $stdout
|
13
|
+
|
14
|
+
$stdout = StringIO.new
|
15
|
+
$stdout.instance_eval do
|
16
|
+
class << self
|
17
|
+
attr_accessor :outputs
|
18
|
+
|
19
|
+
alias_method :old_write, :write
|
20
|
+
|
21
|
+
def write string
|
22
|
+
(@outputs ||= []) << { line: caller.first, string: string }
|
23
|
+
|
24
|
+
old_write string
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def capture_output
|
31
|
+
load @filename
|
32
|
+
|
33
|
+
$stdout.close_write
|
34
|
+
@output_stream = $stdout
|
35
|
+
@output_stream.rewind
|
36
|
+
|
37
|
+
$stdout = @old_stdout
|
38
|
+
end
|
39
|
+
alias_method :run, :capture_output
|
40
|
+
|
41
|
+
def dump filename
|
42
|
+
File.open(filename, 'w') do |file|
|
43
|
+
file.write file_with_output
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def raw_output
|
48
|
+
@output_stream.string
|
49
|
+
end
|
50
|
+
|
51
|
+
def file_with_output
|
52
|
+
inject_output unless @file_with_output
|
53
|
+
@file_with_output.join
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
def inject_output
|
58
|
+
@file_with_output = IO.readlines(@filename)
|
59
|
+
|
60
|
+
@output_stream.outputs.each do |output|
|
61
|
+
next if output[:string] == "\n"
|
62
|
+
|
63
|
+
match = output[:line].match(/\A#{@filename}:(?<line_no>\d+):/)
|
64
|
+
line_no = match[:line_no].to_i - 1
|
65
|
+
@file_with_output[line_no].rstrip!
|
66
|
+
@file_with_output[line_no] << " \#=> #{output[:string]}\n"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class CoffeeMachine
|
2
|
+
|
3
|
+
# block
|
4
|
+
# comment
|
5
|
+
|
6
|
+
def initialize # inline comment
|
7
|
+
p __method__ #=> :initialize
|
8
|
+
# noop
|
9
|
+
# noop
|
10
|
+
end
|
11
|
+
|
12
|
+
def make!
|
13
|
+
p __method__ #=> :make!
|
14
|
+
puts "DONE" #=> DONE
|
15
|
+
end
|
16
|
+
|
17
|
+
def sugar?
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
p __method__ #=> nil
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
cm = CoffeeMachine.new
|
26
|
+
cm.make!
|
27
|
+
p cm.sugar? # nope #=> false
|
28
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class CoffeeMachine
|
2
|
+
|
3
|
+
# block
|
4
|
+
# comment
|
5
|
+
|
6
|
+
def initialize # inline comment
|
7
|
+
p __method__
|
8
|
+
# noop
|
9
|
+
# noop
|
10
|
+
end
|
11
|
+
|
12
|
+
def make!
|
13
|
+
p __method__
|
14
|
+
puts "DONE"
|
15
|
+
end
|
16
|
+
|
17
|
+
def sugar?
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
p __method__
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
cm = CoffeeMachine.new
|
26
|
+
cm.make!
|
27
|
+
p cm.sugar? # nope
|
28
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
def assets
|
5
|
+
dump_files = File.expand_path("../assets/*.dump.rb", __FILE__)
|
6
|
+
|
7
|
+
Dir[dump_files].each do |dump_file|
|
8
|
+
asset = {
|
9
|
+
dump_file: dump_file,
|
10
|
+
source_file: dump_file.sub(/.dump(.rb)\Z/, '\1'),
|
11
|
+
output_file: dump_file.sub(/.dump(.rb)\Z/, '.output\1')
|
12
|
+
}
|
13
|
+
|
14
|
+
yield asset, File.basename(asset[:source_file])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe CodeOutput do
|
19
|
+
|
20
|
+
describe "#initialize" do
|
21
|
+
it "raises IOError if the input file is not readable" do
|
22
|
+
expect {
|
23
|
+
CodeOutput::Parser.new '/etc/shadow'
|
24
|
+
}.to raise_error IOError
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
[
|
29
|
+
[:file_with_output, :dump_file],
|
30
|
+
[:raw_output, :output_file]
|
31
|
+
].each do |method, file|
|
32
|
+
|
33
|
+
describe "##{method}" do
|
34
|
+
assets do |asset, filename|
|
35
|
+
it "returns the expected output for #{filename}" do
|
36
|
+
parser = CodeOutput::Parser.new asset[:source_file]
|
37
|
+
parser.run
|
38
|
+
|
39
|
+
got = parser.__send__ method
|
40
|
+
expected = File.open(asset[file]).read
|
41
|
+
|
42
|
+
got.should eq expected
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#dump" do
|
50
|
+
assets do |asset, filename|
|
51
|
+
it "writes the expected contents for #{filename}" do
|
52
|
+
parser = CodeOutput::Parser.new asset[:source_file]
|
53
|
+
parser.run
|
54
|
+
|
55
|
+
temp_file = Tempfile.new('tmp')
|
56
|
+
|
57
|
+
begin
|
58
|
+
parser.dump(temp_file.path)
|
59
|
+
|
60
|
+
FileUtils.identical?(asset[:dump_file], temp_file).should be_true
|
61
|
+
ensure
|
62
|
+
temp_file.unlink
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: code_output
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cédric Félizard
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: simplecov
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Inject a script output into the source file as comments.
|
63
|
+
email:
|
64
|
+
- cedric@felizard.fr
|
65
|
+
executables:
|
66
|
+
- code_output
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- .rspec
|
72
|
+
- .rvmrc
|
73
|
+
- .travis.yml
|
74
|
+
- Gemfile
|
75
|
+
- MIT-LICENSE
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- bin/code_output
|
79
|
+
- code_output.gemspec
|
80
|
+
- lib/code_output.rb
|
81
|
+
- lib/code_output/version.rb
|
82
|
+
- spec/assets/catch.dump.rb
|
83
|
+
- spec/assets/catch.output.rb
|
84
|
+
- spec/assets/catch.rb
|
85
|
+
- spec/assets/coffee_machine.dump.rb
|
86
|
+
- spec/assets/coffee_machine.output.rb
|
87
|
+
- spec/assets/coffee_machine.rb
|
88
|
+
- spec/assets/inheritance.dump.rb
|
89
|
+
- spec/assets/inheritance.output.rb
|
90
|
+
- spec/assets/inheritance.rb
|
91
|
+
- spec/assets/simple.dump.rb
|
92
|
+
- spec/assets/simple.output.rb
|
93
|
+
- spec/assets/simple.rb
|
94
|
+
- spec/code_output_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
homepage: https://github.com/infertux/code_output
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.24
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Inject a script output into the source file as comments.
|
120
|
+
test_files:
|
121
|
+
- spec/assets/catch.dump.rb
|
122
|
+
- spec/assets/catch.output.rb
|
123
|
+
- spec/assets/catch.rb
|
124
|
+
- spec/assets/coffee_machine.dump.rb
|
125
|
+
- spec/assets/coffee_machine.output.rb
|
126
|
+
- spec/assets/coffee_machine.rb
|
127
|
+
- spec/assets/inheritance.dump.rb
|
128
|
+
- spec/assets/inheritance.output.rb
|
129
|
+
- spec/assets/inheritance.rb
|
130
|
+
- spec/assets/simple.dump.rb
|
131
|
+
- spec/assets/simple.output.rb
|
132
|
+
- spec/assets/simple.rb
|
133
|
+
- spec/code_output_spec.rb
|
134
|
+
- spec/spec_helper.rb
|