Skip to content

Commit

Permalink
Additional adapter for excavate gem
Browse files Browse the repository at this point in the history
(this is a workaround, pending tamatebako/tebako#119)
  • Loading branch information
maxirmx committed Jun 22, 2024
1 parent 55528b7 commit 609ec4f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 24 deletions.
19 changes: 11 additions & 8 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 Down
7 changes: 4 additions & 3 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 All @@ -54,7 +55,7 @@ def self.full_gem_path(gem)
end

def self.log_enabled
@log_enabled ||= true
@log_enabled ||= false
end

def self.process(name, map, title)
Expand All @@ -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"
3 changes: 1 addition & 2 deletions lib/tebako-runtime/pre/seven-zip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ module TebakoRuntime
sevenz_new_folder = COMPILER_MEMFS_LIB_CACHE / "seven_zip_ruby"
FileUtils.mkdir_p(sevenz_new_folder)
sevenz_paths.each do |file|
puts "#{file} ==> #{sevenz_new_folder}"
FileUtils.cp(file, sevenz_new_folder)
end
$LOAD_PATH.unshift(COMPILER_MEMFS_LIB_CACHE)
$LOAD_PATH.unshift(COMPILER_MEMFS_LIB_CACHE.to_s)
end
17 changes: 8 additions & 9 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,13 +181,13 @@ 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) do |source, destination|
if RUBY_PLATFORM =~ /msys|mingw|cygwin/
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
Expand All @@ -198,9 +197,9 @@ def tmpdir_name
end.and_call_original
end

require "excavate"
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
1 change: 0 additions & 1 deletion tebako-runtime.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rubocop-rspec", "~> 2.23"
spec.add_development_dependency "rubocop-rubycw", "~> 0.1"

spec.add_development_dependency "excavate", "~> 0.3"
spec.add_development_dependency "ffi", "~> 1.15"
spec.add_development_dependency "mn2pdf", "~> 1.79"
spec.add_development_dependency "mnconvert", "~> 1.54"
Expand Down

0 comments on commit 609ec4f

Please sign in to comment.