Skip to content

Commit

Permalink
Implemented adapter for excavate gem (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx authored Jun 22, 2024
1 parent 9a91d5b commit 2567990
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 23 deletions.
23 changes: 14 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ require "fileutils"
namespace :build do
desc "Download cacert.pem"
task :download_cacert do
url = URI("https://curl.se/ca/cacert.pem")
FileUtils.mkdir_p("lib/cert")
Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == "https") do |http|
request = Net::HTTP::Get.new url
http.request request do |response|
open "lib/cert/cacert.pem.mozilla", "w" do |io|
response.read_body do |chunk|
io.write chunk
unless File.exist?("lib/cert/cacert.pem.mozilla")

url = URI("https://curl.se/ca/cacert.pem")
FileUtils.mkdir_p("lib/cert")
Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == "https") do |http|
request = Net::HTTP::Get.new url
http.request request do |response|
open "lib/cert/cacert.pem.mozilla", "w" do |io|
response.read_body do |chunk|
io.write chunk
end
end
end
end
Expand All @@ -52,7 +55,9 @@ end

task build: "build:download_cacert"

task default: ["build:download_cacert", :spec]
task spec: "build:download_cacert"

task default: :spec

RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new
5 changes: 3 additions & 2 deletions lib/tebako-runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# - an option to implement adapter 'AFTER'
module TebakoRuntime
PRE_REQUIRE_MAP = {
"excavate" => "tebako-runtime/pre/excavate",
"seven_zip_ruby" => "tebako-runtime/pre/seven-zip"
}.freeze

Expand Down Expand Up @@ -79,10 +80,10 @@ def self.process_all(name)
# It targets ffi-compiler/ffi-compiler2 that use some functions of
# deployed ffi to process other gems
# THis approach is not compatible with tebako on Windows because ffi
# is deployed with (inplib) reference to target tebako package that is
# is deployed with (implib) reference to target tebako package that is
# not available at deploy time
def self.process_pass_through(name)
if name == "ffi" && RUBY_PLATFORM =~ /msys|mingw|cygwin/
if name == "ffi" && RUBY_PLATFORM =~ /mswin|mingw/
puts "Replacing ffi ffi-platform-stub" if log_enabled
res = original_require "tebako-runtime/pass-through/ffi-platform-stub"
else
Expand Down
2 changes: 1 addition & 1 deletion lib/tebako-runtime/memfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
# Module TebakoRuntime
# Methods to extract files from memfs to temporary folder
module TebakoRuntime
COMPILER_MEMFS = RUBY_PLATFORM =~ /msys|mingw|cygwin/ ? "A:/__tebako_memfs__" : "/__tebako_memfs__"
COMPILER_MEMFS = RUBY_PLATFORM =~ /mswin|mingw/ ? "A:/__tebako_memfs__" : "/__tebako_memfs__"
COMPILER_MEMFS_LIB_CACHE = Pathname.new(Dir.mktmpdir("tebako-runtime-"))

class << self
Expand Down
31 changes: 31 additions & 0 deletions lib/tebako-runtime/pre/excavate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# Copyright (c) 2024 [Ribose Inc](https://www.ribose.com).
# All rights reserved.
# This file is a part of tebako
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# For some reason on Windows an attempt to require "seven_zip_ruby" from excavate fails
# I cannot debug it effectively because of https://github.com/tamatebako/tebako/issues/119

require "seven_zip_ruby"
5 changes: 3 additions & 2 deletions lib/tebako-runtime/pre/seven-zip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
module TebakoRuntime
sevenz_lib = RUBY_PLATFORM.downcase.match(/mswin|mingw/) ? "7z*.dll" : "7z.so"
sevenz_path = File.join(full_gem_path("seven-zip"), "lib", "seven_zip_ruby", sevenz_lib)
sevenz_paths = Dir.glob(sevenz_path)
sevenz_new_folder = COMPILER_MEMFS_LIB_CACHE / "seven_zip_ruby"
FileUtils.mkdir_p(sevenz_new_folder)
Dir.glob(sevenz_path).each do |file|
sevenz_paths.each do |file|
FileUtils.cp(file, sevenz_new_folder)
end
$LOAD_PATH.unshift(COMPILER_MEMFS_LIB_CACHE)
$LOAD_PATH.unshift(COMPILER_MEMFS_LIB_CACHE.to_s)
end
2 changes: 1 addition & 1 deletion lib/tebako-runtime/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
# POSSIBILITY OF SUCH DAMAGE.

module TebakoRuntime
VERSION = "0.5.0"
VERSION = "0.5.1"
end
24 changes: 16 additions & 8 deletions spec/runtime_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

# rubocop:disable Metrics/BlockLength
RSpec.describe TebakoRuntime do
let(:tmpd) { tmpdir_name }

it "has a version number" do
expect(TebakoRuntime::VERSION).not_to be nil
end
Expand All @@ -40,16 +42,13 @@ def tmpdir_name
end

it "extracts single file from memfs" do
tmpd = tmpdir_name
test_file = File.join(__dir__, "fixtures", "files", "test1.file")
expect(FileUtils).to receive(:cp_r).with([test_file], tmpd)

TebakoRuntime.extract(test_file, false, tmpd)
end

it "extracts files from memfs by wildcard" do
tmpd = tmpdir_name

test1_file = File.join(__dir__, "fixtures", "files", "test1.file")
test2_file = File.join(__dir__, "fixtures", "files", "test2.file")
test_files = File.join(__dir__, "fixtures", "files", "*.file")
Expand All @@ -60,7 +59,6 @@ def tmpdir_name
end

it "returns unchanged reference to non-memfs file" do
tmpd = tmpdir_name
expect(TebakoRuntime.extract_memfs("#{tmpd}/test.file")).to eq("#{tmpd}/test.file")
end

Expand Down Expand Up @@ -98,10 +96,11 @@ def tmpdir_name

ref = TebakoRuntime.extract_memfs(File.join(TebakoRuntime::COMPILER_MEMFS, "test1.file"), cache_path: cache)
expect(ref).to eq(File.join(cache, "test1.file"))

FileUtils.remove_dir(cache, true)
end

it "returns unchanged reference to non-memfs file with quoted name" do
tmpd = tmpdir_name
expect(TebakoRuntime.extract_memfs("\"#{tmpd}/test.file\"")).to eq("\"#{tmpd}/test.file\"")
end

Expand Down Expand Up @@ -182,16 +181,25 @@ def tmpdir_name
end

it "provides a pre-processor for seven-zip gem" do
sevenz_libs = RUBY_PLATFORM =~ /msys|mingw|cygwin/ ? ["7z.dll", "7z64.dll"] : ["7z.so"]
sevenz_libs = RUBY_PLATFORM =~ /mswin|mingw/ ? ["7z.dll", "7z64.dll"] : ["7z.so"]
sevenz_libs.each do |sevenz_lib|
sevenz_path = File.join(TebakoRuntime.full_gem_path("seven-zip"), "lib", "seven_zip_ruby", sevenz_lib).to_s
sevenz_new_folder = TebakoRuntime::COMPILER_MEMFS_LIB_CACHE / "seven_zip_ruby"

expect(FileUtils).to receive(:cp).with(sevenz_path, sevenz_new_folder).and_call_original
expect(FileUtils).to receive(:cp) do |source, destination|
if RUBY_PLATFORM =~ /mswin|mingw/
expect(source.casecmp(sevenz_path)).to eq(0) # Case-insensitive comparison for Windows
expect(destination.casecmp(sevenz_new_folder)).to eq(0)
else
expect(source).to eq(sevenz_path) # Case-sensitive comparison for other platforms
expect(destination).to eq(sevenz_new_folder)
end
end.and_call_original
end

require "seven_zip_ruby"

expect($LOAD_PATH).to include(TebakoRuntime::COMPILER_MEMFS_LIB_CACHE)
expect($LOAD_PATH).to include(TebakoRuntime::COMPILER_MEMFS_LIB_CACHE.to_s)
end
end
# rubocop:enable Metrics/BlockLength

0 comments on commit 2567990

Please sign in to comment.