Class: Fig::WorkingDirectoryMaintainer

Inherits:
Object
  • Object
show all
Defined in:
lib/fig/working_directory_maintainer.rb

Overview

Copies files from the project directories in FIG_HOME to the user's working directory. It keeps track of which files have already been copied, and which package/versions they came from, and deletes files as necessary to ensure that we never have files from two different versions of the same package in the user's working directory.

Constant Summary collapse

SYMLOOP_MAX =
20

Instance Method Summary collapse

Constructor Details

#initialize(base_dir) ⇒ WorkingDirectoryMaintainer

Returns a new instance of WorkingDirectoryMaintainer.


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fig/working_directory_maintainer.rb', line 20

def initialize(base_dir)
  @base_dir = base_dir
  @package_metadata_by_name = {}
  @local_fig_data_directory = File.join(@base_dir, '.fig')

  if (
        File.exist?(@local_fig_data_directory)        \
    &&  ! File.directory?(@local_fig_data_directory)
  )
    raise Fig::UserInputError.new(
      %Q<"#{@local_fig_data_directory}" exists and it isn't a directory. Are you running inside a repository?>
    )
  end

  @metadata_file = File.join(@local_fig_data_directory, 'retrieve')

  if File.exist?(@metadata_file)
    ()
  end
end

Instance Method Details

#find_package_version_for_file(file) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fig/working_directory_maintainer.rb', line 85

def find_package_version_for_file(file)
  @package_metadata_by_name.each do |name, package_meta|
    package_meta.each_file do |target|
      if File.identical? file, target
        return formatted_meta(package_meta)
      end
    end
  end

  return nil
end

#prepare_for_shutdown(purged_unused_packages) ⇒ Object


97
98
99
100
101
102
103
104
105
# File 'lib/fig/working_directory_maintainer.rb', line 97

def prepare_for_shutdown(purged_unused_packages)
  if purged_unused_packages
    clean_up_unused_packages()
  end

  ()

  return
end

#retrieve(source, relpath) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fig/working_directory_maintainer.rb', line 56

def retrieve(source, relpath)
  resolved_source = source

  # When recursing through a retrieve, if we encounter a symlink that doesn't
  # point to a directory, we copy it as a symlink.  However, if the retrieve
  # path itself is a symlink, we copy the target of the symlink, not the
  # symlink itself.
  if File.exist? resolved_source
    # Ruby v1.8 does not have File.realdirpath().
    traversal_count = 0
    while File.symlink? resolved_source
      resolved_source = File.join(
        File.dirname(resolved_source), File.readlink(resolved_source)
      )
      traversal_count += 1

      if traversal_count > SYMLOOP_MAX
        raise Fig::RepositoryError.new(
          %Q<Could not resolve symlink "#{source}"; symlink chain exceeded #{SYMLOOP_MAX}.>
        )
      end
    end
  end

  copy(resolved_source, relpath)

  return
end

#switch_to_package_version(name, version) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fig/working_directory_maintainer.rb', line 41

def switch_to_package_version(name, version)
  @package_meta = @package_metadata_by_name[name]
  if @package_meta && @package_meta.current_version != version
    clean_up_package_files()
    @package_meta = nil
  end
  if not @package_meta
    @package_meta = (name, version)
  end

  return
end