imbue Cache with knowledge of whether a target or directory is for a build or install operation

This commit is contained in:
Josh Holtrop 2019-04-22 22:19:39 -04:00
parent c60589a9c9
commit 328babe1f4
3 changed files with 43 additions and 30 deletions

View File

@ -94,11 +94,11 @@ module Rscons
def clean
cache = Cache.instance
# remove all built files
cache.targets.each do |target|
cache.targets(false).each do |target|
FileUtils.rm_f(target)
end
# remove all created directories if they are empty
cache.directories.sort {|a, b| b.size <=> a.size}.each do |directory|
cache.directories(false).sort {|a, b| b.size <=> a.size}.each do |directory|
next unless File.directory?(directory)
if (Dir.entries(directory) - ['.', '..']).empty?
Dir.rmdir(directory) rescue nil

View File

@ -236,9 +236,12 @@ module Rscons
# trigger a rebuild.
# @param deps [Array<String>] List of dependencies for the target.
# @param env [Environment] The {Rscons::Environment}.
# @param options [Hash] Optional arguments.
# @option options [Boolean] :install
# Whether the target is for an install operation.
#
# @return [void]
def register_build(targets, command, deps, env)
def register_build(targets, command, deps, env, options = {})
Array(targets).each do |target|
target_checksum = Rscons.phony_target?(target) ? "" : calculate_checksum(target)
@cache["targets"][get_cache_key(target)] = {
@ -256,41 +259,65 @@ module Rscons
"checksum" => lookup_checksum(dep),
}
end,
"install" => !!options[:install],
}
end
end
# Return a list of targets that have been built.
# Return a list of targets that have been built or installed.
#
# @return [Array<String>] List of targets that have been built.
def targets
@cache["targets"].keys
# @param install [Boolean]
# Whether to return installed targets. If false, will only return normal
# build targets and not install targets.
#
# @return [Array<String>]
# List of build targets that have been built or installed.
def targets(install)
install = !!install
@cache["targets"].select do |key, target|
target["install"] == install
end.map(&:first)
end
# Make any needed directories and record the ones that are created for
# removal upon a "clean" operation.
# Create any needed directory components for a build or install operation.
#
# @param path [String] Directory to create.
# Build directories will be removed if empty upon a "clean" operation.
# Install directories will be removed if empty upon an "uninstall"
# operation.
#
# @param path [String]
# Directory to create.
# @param options [Hash]
# Optional arguments.
# @option options [Boolean] :install
# Whether the directory is for an install operation.
#
# @return [void]
def mkdir_p(path)
def mkdir_p(path, options = {})
parts = path.split(/[\\\/]/)
parts.each_index do |i|
next if parts[i] == ""
subpath = File.join(*parts[0, i + 1])
unless File.exists?(subpath)
FileUtils.mkdir_p(subpath)
@cache["directories"][subpath] = true
@cache["directories"][subpath] = !!options[:install]
end
end
end
# Return a list of directories which were created as a part of the build.
#
# @param install [Boolean]
# Whether to return installed directories. If false, will only return
# normal build directories and not install targets.
#
# @return [Array<String>]
# List of directories which were created as a part of the build.
def directories
@cache["directories"].keys
def directories(install)
install = !!install
@cache["directories"].select do |key, d_install|
d_install == install
end.map(&:first)
end
private

View File

@ -13,13 +13,6 @@ module Rscons
end
end
describe "#targets" do
it "returns a list of targets that are cached" do
cache = {"targets" => {"t1" => {}, "t2" => {}, "t3" => {}}}
expect(build_from(cache).targets).to eq ["t1", "t2", "t3"]
end
end
describe "#mkdir_p" do
it "makes directories and records any created in the cache" do
_cache = {}
@ -35,7 +28,7 @@ module Rscons
expect(FileUtils).to receive(:mkdir_p).with("one/two/four")
cache.mkdir_p("one/two/three")
cache.mkdir_p("one\\two\\four")
expect(cache.directories).to eq ["one/two", "one/two/three", "one/two/four"]
expect(cache.directories(false)).to eq ["one/two", "one/two/three", "one/two/four"]
end
it "handles absolute paths" do
@ -45,14 +38,7 @@ module Rscons
expect(File).to receive(:exists?).with("/one/two").and_return(false)
expect(FileUtils).to receive(:mkdir_p).with("/one/two")
cache.mkdir_p("/one/two")
expect(cache.directories).to eq ["/one/two"]
end
end
describe "#directories" do
it "returns a list of directories that are cached" do
_cache = {"directories" => {"dir1" => true, "dir2" => true}}
expect(build_from(_cache).directories).to eq ["dir1", "dir2"]
expect(cache.directories(false)).to eq ["/one/two"]
end
end