Class: Tebako::CacheManager

Inherits:
Object
  • Object
show all
Defined in:
lib/tebako/cache_manager.rb

Overview

Cache management

Constant Summary collapse

E_VERSION_FILE =
".environment.version"

Instance Method Summary collapse

Constructor Details

#initialize(deps, src_dir, out_dir) ⇒ CacheManager



37
38
39
40
41
# File 'lib/tebako/cache_manager.rb', line 37

def initialize(deps, src_dir, out_dir)
  @deps = deps
  @src_dir = src_dir
  @out_dir = out_dir
end

Instance Method Details

#clean_cacheObject



43
44
45
46
47
# File 'lib/tebako/cache_manager.rb', line 43

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

#clean_outputObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tebako/cache_manager.rb', line 49

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

#ensure_version_fileObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/tebako/cache_manager.rb', line 61

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 "#{Tebako::PACKAGING_ERRORS[201]} #{E_VERSION_FILE}: #{e.message}"
  end
end

#version_cacheObject



76
77
78
79
80
# File 'lib/tebako/cache_manager.rb', line 76

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

#version_cache_checkObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tebako/cache_manager.rb', line 82

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

#version_keyObject



72
73
74
# File 'lib/tebako/cache_manager.rb', line 72

def version_key
  @version_key ||= "#{Tebako::VERSION} at #{@src_dir}"
end

#version_mismatch(cached_version) ⇒ Object



95
96
97
98
99
# File 'lib/tebako/cache_manager.rb', line 95

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

#version_source_mismatch(cached_source) ⇒ Object



101
102
103
104
105
# File 'lib/tebako/cache_manager.rb', line 101

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

#version_unknownObject



107
108
109
110
# File 'lib/tebako/cache_manager.rb', line 107

def version_unknown
  puts "CMake cache version was not recognized, cleaning up"
  clean_cache
end