neri 0.9.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/neri/runtime.rb CHANGED
@@ -1,79 +1,91 @@
1
1
  require "neri/version"
2
+ require "pathname"
2
3
 
3
- alias _neri_orig_require require
4
- alias _neri_orig_load load
4
+ module Kernel
5
+ unless defined?(neri_original_require)
6
+ alias neri_original_require require
7
+ private :neri_original_require
8
+ end
5
9
 
6
- def require(feature)
7
- Neri.require(feature)
8
- end
10
+ unless defined?(neri_original_load)
11
+ alias neri_original_load load
12
+ private :neri_original_load
13
+ end
14
+
15
+ def require(feature)
16
+ Neri.require(feature)
17
+ end
9
18
 
10
- def load(file, priv = false)
11
- Neri.load(file, priv)
19
+ def load(file, priv = false)
20
+ Neri.load(file, priv)
21
+ end
12
22
  end
13
23
 
14
24
  module Neri
15
25
  BLOCK_LENGTH = 32
16
26
  @datafile = nil
17
- @system_dir = nil
18
- @files = {}
19
- @fullpath_files = nil
20
- @current_directory = nil
27
+ @file_informations = {}
28
+ @fullpath_files = {}
29
+ @virtual_files = {}
30
+ @virtual_directory = "."
21
31
  @xor = nil
22
32
 
23
33
  class << self
24
34
  def datafile=(datafile)
25
35
  @datafile = datafile.encode(Encoding::UTF_8)
26
- @system_dir = File.dirname(File.expand_path(@datafile)) + File::SEPARATOR
27
36
  files_length = File.binread(@datafile, BLOCK_LENGTH).to_i
28
37
  files_str = read(files_length, BLOCK_LENGTH)
29
38
  pos = files_length + BLOCK_LENGTH
30
39
  pos += BLOCK_LENGTH - pos % BLOCK_LENGTH unless pos % BLOCK_LENGTH == 0
31
40
  files_str.force_encoding(Encoding::UTF_8)
32
- files_str.split("\n").each do |line|
41
+ files_str.each_line do |line|
33
42
  filename, length, offset = line.split("\t")
34
- @files[filename] = [length.to_i, offset.to_i + pos]
43
+ @file_informations[filename] = [length.to_i, offset.to_i + pos]
35
44
  end
36
- @current_directory = nil
45
+ @fullpath_files = @file_informations.transform_keys { |k| File.expand_path(k) }
37
46
  end
38
47
 
39
48
  def key=(key)
40
- @xor = key.scan(/../).map { |a| a.to_i(16) }.pack("c*")
49
+ @xor ||= key.scan(/../).map { |a| a.to_i(16) }.pack("c*")
41
50
  end
42
51
 
43
- def require(feature)
44
- feature = feature.encode(Encoding::UTF_8)
45
- filepath = nil
46
- (feature.start_with?(/[a-z]:/i, "\\", "/", ".") ? [""] : load_path).each do |path|
47
- ["", ".rb"].each do |ext|
48
- tmp_path = path + feature + ext
49
- filepath ||= tmp_path if exist_in_datafile?(tmp_path)
50
- end
51
- end
52
-
53
- return _neri_orig_require(feature) unless filepath
54
- return false if $LOADED_FEATURES.index(filepath)
52
+ def virtual_directory=(path)
53
+ @virtual_directory = path
54
+ @virtual_files = @file_informations.transform_keys { |k| File.expand_path(k, path) }
55
+ end
55
56
 
56
- code = load_code(filepath)
57
- eval(code, TOPLEVEL_BINDING, filepath)
58
- $LOADED_FEATURES.push(filepath)
57
+ def require(feature)
58
+ feature_path = Pathname.new(feature.encode(Encoding::UTF_8))
59
+ feature_path = feature_path.sub_ext(".rb") if feature_path.extname == ""
60
+ return neri_original_require(feature) if feature_path.extname == ".so"
61
+
62
+ path_str = if feature_path.absolute? || feature.start_with?(".")
63
+ path_in_datafile(feature_path)
64
+ else
65
+ search_in_load_path(feature_path)
66
+ end
67
+
68
+ return neri_original_require(feature) unless path_str
69
+ return false if $LOADED_FEATURES.index(path_str)
70
+
71
+ code = load_code(path_str)
72
+ eval(code, TOPLEVEL_BINDING, path_str)
73
+ $LOADED_FEATURES.push(path_str)
59
74
  true
60
75
  end
61
76
 
62
77
  def load(file, priv = false)
63
- file = file.encode(Encoding::UTF_8)
64
- filepath = nil
65
- paths = file.start_with?(/[a-z]:/i, "\\", "/", ".") ? [""] : load_path + [""]
66
- paths.each do |path|
67
- filepath ||= path + file if exist_in_datafile?(path + file)
68
- end
78
+ file_path = Pathname.new(file.encode(Encoding::UTF_8))
79
+ path_str = search_in_load_path(file_path) if file_path.relative? && !file.start_with?(".")
80
+ path_str ||= path_in_datafile(file_path)
69
81
 
70
- return _neri_orig_load(file, priv) unless filepath
82
+ return neri_original_load(file, priv) unless path_str
71
83
 
72
- code = load_code(filepath)
84
+ code = load_code(path_str)
73
85
  if priv
74
- Module.new.module_eval(code, filepath)
86
+ Module.new.module_eval(code, path_str)
75
87
  else
76
- eval(code, TOPLEVEL_BINDING, filepath)
88
+ eval(code, TOPLEVEL_BINDING, path_str)
77
89
  end
78
90
  true
79
91
  end
@@ -84,32 +96,26 @@ module Neri
84
96
 
85
97
  def file_read(filename, encoding = Encoding::BINARY)
86
98
  filename = filename.encode(Encoding::UTF_8)
87
- str = nil
88
- if exist_in_datafile?(filename)
89
- length, offset = fullpath_files[File.expand_path(filename)]
90
- str = read(length, offset)
91
- else
92
- str = File.binread(filename)
93
- end
99
+ length, offset = file_information(filename)
100
+ str = length ? read(length, offset) : File.binread(filename)
94
101
  str.force_encoding(encoding)
95
102
  end
96
103
 
97
104
  def files
98
- @files.keys
105
+ @file_informations.keys
99
106
  end
100
107
 
101
108
  def exist_in_datafile?(filename)
102
- fullpath_files.key?(File.expand_path(filename.encode(Encoding::UTF_8)))
109
+ file_information(filename) != nil
103
110
  end
104
111
 
105
112
  private
106
113
 
107
- def fullpath_files
108
- if @current_directory != Dir.pwd
109
- @current_directory = Dir.pwd
110
- @fullpath_files = @files.transform_keys { |k| File.expand_path(k) }
111
- end
112
- @fullpath_files
114
+ def file_information(filename)
115
+ fullpath = File.expand_path(filename)
116
+ return @fullpath_files[fullpath] if @fullpath_files.key?(fullpath)
117
+
118
+ @virtual_files[File.expand_path(filename, @virtual_directory)]
113
119
  end
114
120
 
115
121
  def xor(str)
@@ -131,11 +137,22 @@ module Neri
131
137
  xor(File.binread(@datafile, tmp_length, offset))[0, length]
132
138
  end
133
139
 
134
- def load_path
135
- paths = $LOAD_PATH.map { |path| path.encode(Encoding::UTF_8) }
136
- return paths unless @system_dir
140
+ def search_in_load_path(file_path)
141
+ $LOAD_PATH.each do |path_str|
142
+ path_str = path_str.dup.force_encoding(Encoding::UTF_8) if path_str.encoding == Encoding::BINARY
143
+ load_path = Pathname.new(path_str.encode(Encoding::UTF_8))
144
+ candidate_path_str = path_in_datafile(load_path + file_path)
145
+ return candidate_path_str if candidate_path_str
146
+ end
147
+ nil
148
+ end
149
+
150
+ def path_in_datafile(file_path)
151
+ fullpath = File.expand_path(file_path.to_s)
152
+ return fullpath if exist_in_datafile?(fullpath)
137
153
 
138
- paths.map { |path| path.sub(@system_dir, "*neri*#{File::SEPARATOR}") + File::SEPARATOR }
154
+ virtual_path = File.expand_path(file_path.to_s, @virtual_directory)
155
+ exist_in_datafile?(virtual_path) ? virtual_path : nil
139
156
  end
140
157
 
141
158
  def load_code(file)
data/lib/neri/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Neri
2
- VERSION = -"0.9.4"
2
+ VERSION = -"1.0.0"
3
3
  end
data/neri.gemspec CHANGED
@@ -30,6 +30,4 @@ Gem::Specification.new do |spec|
30
30
  spec.bindir = "exe"
31
31
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
32
  spec.require_paths = ["lib"]
33
-
34
- spec.add_dependency "win32api"
35
33
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neri
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nodai2hITC
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-24 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: win32api
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
11
+ date: 2022-01-08 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description: Neri builds Windows batfile or exefile from Ruby script.
28
14
  email:
29
15
  - nodai2h.itc@gmail.com
@@ -70,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
56
  - !ruby/object:Gem::Version
71
57
  version: '0'
72
58
  requirements: []
73
- rubygems_version: 3.2.30
59
+ rubygems_version: 3.3.3
74
60
  signing_key:
75
61
  specification_version: 4
76
62
  summary: One-Click Ruby Application Builder