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
use mruby::load::MrbLoadSources;
use mruby::Mrb;
use mruby::MrbError;
use std::borrow::Cow;
use std::convert::AsRef;
use crate::Gem;
pub fn init(interp: &Mrb) -> Result<(), MrbError> {
Sinatra::init(interp)
}
#[derive(RustEmbed)]
#[folder = "$CARGO_MANIFEST_DIR/vendor/ruby/2.6.0/gems/sinatra-2.0.5/lib"]
struct Sinatra;
impl Sinatra {
fn contents<T: AsRef<str>>(path: T) -> Result<Vec<u8>, MrbError> {
let path = path.as_ref();
let contents = Self::get(path)
.map(Cow::into_owned)
.ok_or_else(|| MrbError::SourceNotFound(path.to_owned()))?;
if path == "sinatra/base.rb" {
let mut string = String::from_utf8(contents)
.map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
string = string.replace("defined?(RUBY_IGNORE_CALLERS)", "false");
string = string.replace("defined? Encoding", "false");
string = string.replace("defined?(RUBY_ENGINE)", "false");
string = string.replace(
r#"class_eval("def #{name}() #{content}; end")"#,
r#"eval("def #{name}() #{content}; end")"#,
);
string = string.replace(
r"map! { |line| line.split(/:(?=\d|in )/, 3)[0,keep] }.",
r"map! { |line| line.split(':', 3)[0,keep] }",
);
string = string.replace(
"reject { |file, *_| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }",
"# reject { |file, *_| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }",
);
string = string.replace("(not_set = true)", "NOT_SET");
string = string.replace(
"raise ArgumentError if block and !not_set",
"not_set = value.not_set?; raise ArgumentError if block and !not_set",
);
string = string.replace("File.fnmatch(pattern, t)", "false");
string = string.replace("builder.use ShowExceptions", "# builder.use ShowExceptions");
string = string.replace(
"dump_errors! boom if settings.dump_errors?",
"# dump_errors! boom if settings.dump_errors?",
);
string = string.replace("&& File.expand_path(File.dirname(app_file))", "");
string = string.replace("root && File.join(root, 'public')", "root + '/public'");
string = string.replace("public_folder && File.exist?(public_folder)", "false");
string = string.replace(
"rescue\n @env['sinatra.error.params'] = @params\n raise",
"rescue => e; @env['sinatra.error.params'] = @params; raise e",
);
Ok(string.into_bytes())
} else {
Ok(contents)
}
}
}
impl Gem for Sinatra {
fn init(interp: &Mrb) -> Result<(), MrbError> {
for source in Self::iter() {
let contents = Self::contents(&source)?;
interp.def_rb_source_file(source, contents)?;
}
Ok(())
}
}