-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
246 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# 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. | ||
|
||
require "fileutils" | ||
require_relative "version" | ||
|
||
# Tebako - an executable packager | ||
module Tebako | ||
# Cache management | ||
class CacheManager | ||
def initialize(deps, src_dir, out_dir) | ||
@deps = deps | ||
@src_dir = src_dir | ||
@out_dir = out_dir | ||
end | ||
|
||
def clean_cache | ||
puts "Cleaning tebako packaging environment" | ||
# Using File.join(deps, "") to ensure that the slashes are appropriate | ||
FileUtils.rm_rf([File.join(@deps, ""), File.join(@out_dir, "")], secure: true) | ||
end | ||
|
||
def clean_output | ||
puts "Cleaning CMake cache and Ruby build files" | ||
|
||
nmr = "src/_ruby_*" | ||
nms = "stash_*" | ||
FileUtils.rm_rf(Dir.glob(File.join(@deps, nmr)), secure: true) | ||
FileUtils.rm_rf(Dir.glob(File.join(@deps, nms)), secure: true) | ||
|
||
# Using File.join(output_folder, "") to ensure that the slashes are appropriate | ||
FileUtils.rm_rf(File.join(@out_dir, ""), secure: true) | ||
end | ||
|
||
def ensure_version_file | ||
version_file_path = File.join(@deps, E_VERSION_FILE) | ||
|
||
begin | ||
File.write(version_file_path, version_key) | ||
# puts "Set version information for tebako packaging environment to #{Tebako::VERSION}" | ||
rescue StandardError => e | ||
puts "An error occurred while creating or updating #{E_VERSION_FILE}: #{e.message}" | ||
end | ||
end | ||
|
||
def version_key | ||
@version_key ||= "#{Tebako::VERSION} at #{@src_dir}" | ||
end | ||
|
||
def version_cache | ||
version_file_path = File.join(@deps, E_VERSION_FILE) | ||
file_version = File.open(version_file_path, &:readline).strip | ||
file_version.match(/(?<version>.+) at (?<source>.+)/) | ||
end | ||
|
||
def version_cache_check | ||
match_data = version_cache | ||
return version_unknown unless match_data | ||
|
||
if match_data[:version] != Tebako::VERSION | ||
version_mismatch(match_data[:version]) | ||
elsif match_data[:source] != @src_dir | ||
version_source_mismatch(match_data[:source]) | ||
end | ||
rescue StandardError | ||
version_unknown | ||
end | ||
|
||
def version_mismatch(cached_version) | ||
puts "Tebako cache was created by a gem version #{cached_version} " \ | ||
"and cannot be used for gem version #{Tebako::VERSION}" | ||
clean_cache | ||
end | ||
|
||
def version_source_mismatch(cached_source) | ||
puts "CMake cache was created for a different source directory '#{cached_source}' " \ | ||
"and cannot be used for '#{@src_dir}'" | ||
clean_output | ||
end | ||
|
||
def version_unknown | ||
puts "CMake cache version was not recognized, cleaning up" | ||
clean_cache | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# 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. | ||
|
||
require "yaml" | ||
require "tebako/cache_manager" | ||
|
||
# rubocop:disable Metrics/BlockLength | ||
|
||
RSpec.describe Tebako::CacheManager do | ||
let(:deps) { "/path/to/deps" } | ||
let(:output_folder) { "/path/to/output" } | ||
let(:source) { "/path/to/source" } | ||
let(:cache_manager) { Tebako::CacheManager.new(deps, source, output_folder) } | ||
|
||
it "cleans the cache by removing the appropriate directories" do | ||
expect(FileUtils).to receive(:rm_rf).with([File.join(deps, ""), File.join(output_folder, "")], secure: true) | ||
cache_manager.clean_cache | ||
end | ||
it "cleans the output by removing the appropriate files and directories" do | ||
nmr = "src/_ruby_*" | ||
nms = "stash_*" | ||
expect(FileUtils).to receive(:rm_rf).with(Dir.glob(File.join(deps, nmr)), secure: true) | ||
expect(FileUtils).to receive(:rm_rf).with(Dir.glob(File.join(deps, nms)), secure: true) | ||
expect(FileUtils).to receive(:rm_rf).with(File.join(output_folder, ""), secure: true) | ||
cache_manager.clean_output | ||
end | ||
|
||
describe "#version_key" do | ||
it "returns the correct version key" do | ||
expect(cache_manager.version_key).to eq("#{Tebako::VERSION} at /path/to/source") | ||
end | ||
end | ||
|
||
describe "#version_cache_check" do | ||
context "when version cache is unknown" do | ||
let(:match_data) { nil } | ||
|
||
it "calls version_unknown" do | ||
allow(cache_manager).to receive(:version_cache).and_return(:match_data) | ||
expect(cache_manager).to receive(:version_unknown) | ||
cache_manager.version_cache_check | ||
end | ||
end | ||
|
||
context "when version does not match" do | ||
let(:match_data) { { version: "0.7.0", source: "/path/to/source" } } | ||
it "calls version_mismatch" do | ||
allow(cache_manager).to receive(:version_cache).and_return(match_data) | ||
expect(cache_manager).to receive(:version_mismatch).with("0.7.0") | ||
cache_manager.version_cache_check | ||
end | ||
end | ||
|
||
context "when source does not match" do | ||
let(:match_data) { { version: Tebako::VERSION, source: "/different/source" } } | ||
|
||
it "calls version_source_mismatch" do | ||
allow(cache_manager).to receive(:version_cache).and_return(match_data) | ||
expect(cache_manager).to receive(:version_source_mismatch).with("/different/source") | ||
cache_manager.version_cache_check | ||
end | ||
end | ||
|
||
context "when version and source match" do | ||
let(:match_data) { { version: Tebako::VERSION, source: "/path/to/source" } } | ||
|
||
it "does not call any mismatch methods" do | ||
allow(cache_manager).to receive(:version_cache).and_return(match_data) | ||
expect(self).not_to receive(:version_unknown) | ||
expect(self).not_to receive(:version_mismatch) | ||
expect(self).not_to receive(:version_source_mismatch) | ||
cache_manager.version_cache_check | ||
end | ||
end | ||
end | ||
|
||
describe "#version_mismatch" do | ||
it "prints the correct message and cleans the cache" do | ||
expect(cache_manager).to receive(:puts) | ||
.with("Tebako cache was created by a gem version 0.9.0 and cannot be used for gem version #{Tebako::VERSION}") | ||
expect(cache_manager).to receive(:clean_cache) | ||
cache_manager.version_mismatch("0.9.0") | ||
end | ||
end | ||
|
||
describe "#version_source_mismatch" do | ||
it "prints the correct message and cleans the output" do | ||
expect(cache_manager).to receive(:puts) | ||
.with("CMake cache was created for a different source directory '/different/source' " \ | ||
"and cannot be used for '/path/to/source'") | ||
expect(cache_manager).to receive(:clean_output) | ||
allow(cache_manager).to receive(:source).and_return("/path/to/source") | ||
cache_manager.version_source_mismatch("/different/source") | ||
end | ||
end | ||
describe "#version_unknown" do | ||
it "prints the correct message and cleans the cache" do | ||
expect(cache_manager).to receive(:puts).with("CMake cache version was not recognized, cleaning up") | ||
expect(cache_manager).to receive(:clean_cache) | ||
cache_manager.version_unknown | ||
end | ||
end | ||
end | ||
|
||
# rubocop:enable Metrics/BlockLength |
Oops, something went wrong.