libinject 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.
Files changed (3) hide show
  1. data/lib/libinject.rb +74 -0
  2. data/lib/libinject.rb~ +74 -0
  3. metadata +38 -0
data/lib/libinject.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'rbconfig'
2
+
3
+ module LibInject
4
+ Version = '0.1.0'
5
+
6
+ def self.lib_inject( io )
7
+ contents = ''
8
+ io.each do |line| contents << line; end
9
+ InjectResult.new( contents ).to_s
10
+ end
11
+
12
+ class InjectResult
13
+ def initialize( contents )
14
+ @contents = contents.clone
15
+ @injected = {}
16
+ end
17
+
18
+ def to_s; InjectedFile.new( @contents, @injected ).to_s; end
19
+ end
20
+
21
+ class InjectedFile
22
+ @@extensions = [ '', '.rb', '.bundle', '.h' ]
23
+ config = Config::CONFIG
24
+ @@load_dirs = [ config['rubylibdir'], config['archdir'] ]
25
+
26
+ def initialize( contents, injected )
27
+ @contents, @injected = contents.clone, injected
28
+ end
29
+
30
+ def expand( libname, full_path )
31
+ @injected[full_path] = true
32
+ header_line = "# v" + ( '---------v' * 7 ) + "\n"
33
+ header =
34
+ "\n#{ header_line }# LibInject: begin '" + libname +
35
+ "' library injection\n#{ header_line }\n"
36
+ footer_line = "# ^" + ( '---------^' * 7 ) + "\n"
37
+ footer =
38
+ "#{ footer_line }# LibInject: end '#{ libname }' library injection\n" +
39
+ footer_line
40
+ to_inject = File.open( full_path ) do |f| f.gets( nil ); end
41
+ header + InjectedFile.new( to_inject, @injected ).to_s + "\n" + footer
42
+ end
43
+
44
+ def full_path( libname )
45
+ possible_paths = $:.map { |dir| @@extensions.map { |ext|
46
+ File.join( dir, libname + ext )
47
+ } }.flatten
48
+ possible_paths.find { |path| File.exist? path }
49
+ end
50
+
51
+ def maybe_inject( match, libname )
52
+ eval match
53
+ if @@load_dirs.any? { |load_dir| @@extensions.any? { |extension|
54
+ File.exist?( File.join( load_dir, libname + extension ) )
55
+ } }
56
+ match
57
+ else
58
+ full_path = full_path libname
59
+ if @injected[full_path]
60
+ ''
61
+ else
62
+ expand( libname, full_path )
63
+ end
64
+ end
65
+ end
66
+
67
+ def to_s
68
+ @contents.gsub!( /^[^\n#]*require ('|")(.*)("|')/ ) { |match|
69
+ maybe_inject( match, $2 )
70
+ }
71
+ @contents
72
+ end
73
+ end
74
+ end
data/lib/libinject.rb~ ADDED
@@ -0,0 +1,74 @@
1
+ require 'rbconfig'
2
+
3
+ module LibInject
4
+ VERSION = '0.1.0'
5
+
6
+ def self.lib_inject( io )
7
+ contents = ''
8
+ io.each do |line| contents << line; end
9
+ InjectResult.new( contents ).to_s
10
+ end
11
+
12
+ class InjectResult
13
+ def initialize( contents )
14
+ @contents = contents.clone
15
+ @injected = {}
16
+ end
17
+
18
+ def to_s; InjectedFile.new( @contents, @injected ).to_s; end
19
+ end
20
+
21
+ class InjectedFile
22
+ @@extensions = [ '', '.rb', '.bundle', '.h' ]
23
+ config = Config::CONFIG
24
+ @@load_dirs = [ config['rubylibdir'], config['archdir'] ]
25
+
26
+ def initialize( contents, injected )
27
+ @contents, @injected = contents.clone, injected
28
+ end
29
+
30
+ def expand( libname, full_path )
31
+ @injected[full_path] = true
32
+ header_line = "# v" + ( '---------v' * 7 ) + "\n"
33
+ header =
34
+ "\n#{ header_line }# LibInject: begin '" + libname +
35
+ "' library injection\n#{ header_line }\n"
36
+ footer_line = "# ^" + ( '---------^' * 7 ) + "\n"
37
+ footer =
38
+ "#{ footer_line }# LibInject: end '#{ libname }' library injection\n" +
39
+ footer_line
40
+ to_inject = File.open( full_path ) do |f| f.gets( nil ); end
41
+ header + InjectedFile.new( to_inject, @injected ).to_s + "\n" + footer
42
+ end
43
+
44
+ def full_path( libname )
45
+ possible_paths = $:.map { |dir| @@extensions.map { |ext|
46
+ File.join( dir, libname + ext )
47
+ } }.flatten
48
+ possible_paths.find { |path| File.exist? path }
49
+ end
50
+
51
+ def maybe_inject( match, libname )
52
+ eval match
53
+ if @@load_dirs.any? { |load_dir| @@extensions.any? { |extension|
54
+ File.exist?( File.join( load_dir, libname + extension ) )
55
+ } }
56
+ match
57
+ else
58
+ full_path = full_path libname
59
+ if @injected[full_path]
60
+ ''
61
+ else
62
+ expand( libname, full_path )
63
+ end
64
+ end
65
+ end
66
+
67
+ def to_s
68
+ @contents.gsub!( /^[^\n#]*require ('|")(.*)("|')/ ) { |match|
69
+ maybe_inject( match, $2 )
70
+ }
71
+ @contents
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,38 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.6
3
+ specification_version: 1
4
+ name: libinject
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2005-08-11
8
+ summary: LibInject is a developer tool for injecting external dependencies into a Ruby file.
9
+ require_paths:
10
+ - lib
11
+ email: sera@fhwang.net
12
+ homepage: http://libinject.rubyforge.org/
13
+ rubyforge_project:
14
+ description: LibInject is a developer tool for injecting external dependencies into a Ruby file.
15
+ autorequire: libinject
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ authors:
28
+ - Francis Hwang
29
+ files:
30
+ - lib/libinject.rb
31
+ - lib/libinject.rb~
32
+ test_files: []
33
+ rdoc_options: []
34
+ extra_rdoc_files: []
35
+ executables: []
36
+ extensions: []
37
+ requirements: []
38
+ dependencies: []