This repository was archived by the owner on Mar 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrequire2lib.rb
executable file
·151 lines (116 loc) · 4.32 KB
/
require2lib.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require "ev/ftools"
require "rbconfig"
require "rubyscript2exe"
exit if __FILE__ == $0
module RUBYSCRIPT2EXE
end
module REQUIRE2LIB
JUSTRUBYLIB = ARGV.include?("--require2lib-justrubylib")
JUSTSITELIB = ARGV.include?("--require2lib-justsitelib")
RUBYGEMS = (not JUSTRUBYLIB)
VERBOSE = ARGV.include?("--require2lib-verbose")
QUIET = (ARGV.include?("--require2lib-quiet") and not VERBOSE)
LOADED = []
ARGV.delete_if{|arg| arg =~ /^--require2lib-/}
ORGDIR = Dir.pwd
THISFILE = File.expand_path(__FILE__)
LIBDIR = File.expand_path((ENV["REQUIRE2LIB_LIBDIR"] or "."))
LOADSCRIPT = File.expand_path((ENV["REQUIRE2LIB_LOADSCRIPT"] or "."))
RUBYLIBDIR = Config::CONFIG["rubylibdir"]
SITELIBDIR = Config::CONFIG["sitelibdir"]
at_exit do
Dir.chdir(ORGDIR)
REQUIRE2LIB.gatherlibs
end
def self.gatherlibs
$stderr.puts "Gathering files..." unless QUIET
File.makedirs(LIBDIR)
if RUBYGEMS
begin
Gem.dir
rubygems = true
rescue NameError
rubygems = false
end
else
rubygems = false
end
pureruby = true
if rubygems
require "fileutils" # Hack ???
requireablefiles = []
Dir.mkdir(File.expand_path("rubyscript2exe.gems", LIBDIR))
Dir.mkdir(File.expand_path("rubyscript2exe.gems/gems", LIBDIR))
Dir.mkdir(File.expand_path("rubyscript2exe.gems/specifications", LIBDIR))
Gem.loaded_specs.each do |key, gem|
$stderr.puts "Found gem #{gem.name} (#{gem.version})." if VERBOSE
fromdir = File.join(gem.installation_path, "specifications")
todir = File.expand_path("rubyscript2exe.gems/specifications", LIBDIR)
fromfile = File.join(fromdir, "#{gem.full_name}.gemspec")
tofile = File.join(todir, "#{gem.full_name}.gemspec")
File.copy(fromfile, tofile)
fromdir = gem.full_gem_path
todir = File.expand_path(File.join("rubyscript2exe.gems/gems", gem.full_name), LIBDIR)
Dir.copy(fromdir, todir)
Dir.find(todir).each do |file|
if File.file?(file)
gem.require_paths.each do |lib|
unless lib.empty?
lib = File.expand_path(lib, todir)
lib = lib + "/"
requireablefiles << file[lib.length..-1] if file =~ /^#{lib.gsub('+', '\+')}/
end
end
end
end
end
end
($" + LOADED).each do |req|
catch :found do
$:.each do |lib|
fromfile = File.expand_path(req, lib)
tofile = File.expand_path(req, LIBDIR)
if File.file?(fromfile)
unless fromfile == tofile or fromfile == THISFILE
unless (rubygems and requireablefiles.include?(req)) # ??? requireablefiles might be a little dangerous.
if (not JUSTRUBYLIB and not JUSTSITELIB) or
(JUSTRUBYLIB and fromfile.include?(RUBYLIBDIR)) or
(JUSTSITELIB and fromfile.include?(SITELIBDIR))
$stderr.puts "Found #{fromfile} ." if VERBOSE
File.makedirs(File.dirname(tofile)) unless File.directory?(File.dirname(tofile))
File.copy(fromfile, tofile)
pureruby = false unless req =~ /\.(rbw?|ruby)$/i
else
$stderr.puts "Skipped #{fromfile} ." if VERBOSE
end
end
end
throw :found
end
end
#$stderr.puts "Can't find #{req} ." unless req =~ /^ev\// or QUIET
#$stderr.puts "Can't find #{req} ." unless req =~ /^(\w:)?[\/\\]/ or QUIET
end
end
$stderr.puts "Not all required files are pure Ruby." unless pureruby if VERBOSE
unless LOADSCRIPT == ORGDIR
File.open(LOADSCRIPT, "w") do |f|
f.puts "module RUBYSCRIPT2EXE"
f.puts " REQUIRE2LIB_FROM_APP={}"
RUBYSCRIPT2EXE.class_variables.each do |const|
const = const[2..-1]
f.puts " REQUIRE2LIB_FROM_APP[:#{const}]=#{RUBYSCRIPT2EXE.send(const).inspect}"
end
f.puts " REQUIRE2LIB_FROM_APP[:rubygems]=#{rubygems.inspect}"
f.puts "end"
end
end
end
end
module Kernel
alias :require2lib_load :load
def load(filename, wrap=false)
REQUIRE2LIB::LOADED << filename unless REQUIRE2LIB::LOADED.include?(filename)
require2lib_load(filename, wrap)
end
end