Class: Fig::Repository

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

Overview

Overall management of a repository. Handles local operations itself; defers remote operations to others.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(os, local_repository_dir, remote_repository_url, application_config, remote_repository_user = nil, update = false, update_if_missing = true) ⇒ Repository

Returns a new instance of Repository.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fig/repository.rb', line 17

def initialize(os, local_repository_dir, remote_repository_url, application_config, remote_repository_user=nil, update=false, update_if_missing=true)
  @os = os
  @local_repository_dir = local_repository_dir
  @remote_repository_url = remote_repository_url
  @remote_repository_user = remote_repository_user
  @application_config = application_config
  @update = update
  @update_if_missing = update_if_missing
  @parser = Parser.new(@application_config)

  @overrides = {}
  if File.exist?('fig.properties')
    File.readlines('fig.properties').each do |line|
      descriptor, path = line.strip.split('=')
      @overrides[descriptor] = path
    end
  end
end

Class Method Details

.is_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/fig/repository.rb', line 13

def self.is_url?(url)
  not (/ftp:\/\/|http:\/\/|file:\/\/|ssh:\/\// =~ url).nil?
end

Instance Method Details

#bundle_resources(package_statements) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fig/repository.rb', line 96

def bundle_resources(package_statements)
  resources = []
  new_package_statements = package_statements.reject do |statement|
    if statement.is_a?(Package::Resource) && !Repository.is_url?(statement.url)
      resources << statement.url
      true
    else
      false
    end
  end
  if resources.size > 0
    resources = expand_globs_from(resources)
    file = 'resources.tar.gz'
    @os.create_archive(file, resources)
    new_package_statements.unshift(Package::Archive.new(file))
    at_exit { File.delete(file) }
  end
  new_package_statements
end

#clean(package_name, version_name) ⇒ Object



36
37
38
39
40
# File 'lib/fig/repository.rb', line 36

def clean(package_name, version_name)
  dir = File.join(@local_repository_dir, package_name)
  dir = File.join(dir, version_name) if version_name
  FileUtils.rm_rf(dir)
end

#list_packagesObject



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

def list_packages
  results = []
  if File.exist?(@local_repository_dir)
    @os.list(@local_repository_dir).each do |package_name|
      @os.list(File.join(@local_repository_dir, package_name)).each do |version_name|
        results << "#{package_name}/#{version_name}"
      end
    end
  end
  results
end

#list_remote_packagesObject



54
55
56
# File 'lib/fig/repository.rb', line 54

def list_remote_packages
  @os.download_list(@remote_repository_url)
end

#load_package(package_name, version_name) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/fig/repository.rb', line 116

def load_package(package_name, version_name)
  Logging.debug "Considering #{package_name}/#{version_name}."
  if @update || (@update_if_missing && package_missing?(package_name, version_name))
    update_package(package_name, version_name)
  end
  read_local_package(package_name, version_name)
end

#local_dir_for_package(package_name, version_name) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/fig/repository.rb', line 168

def local_dir_for_package(package_name, version_name)
  descriptor = "#{package_name}/#{version_name}"
  dir = @overrides[descriptor]
  if dir
    Logging.info "override: #{descriptor}=#{dir}"
  else
    dir = File.join(@local_repository_dir, package_name, version_name)
  end
  dir
end

#publish_package(package_statements, package_name, version_name, local_only) ⇒ Object



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
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fig/repository.rb', line 58

def publish_package(package_statements, package_name, version_name, local_only)
  temp_dir = temp_dir_for_package(package_name, version_name)
  @os.clear_directory(temp_dir)
  local_dir = local_dir_for_package(package_name, version_name)
  @os.clear_directory(local_dir)
  fig_file = File.join(temp_dir, '.fig')
  content = bundle_resources(package_statements).map do |statement|
    if statement.is_a?(Package::Publish)
      nil
    elsif statement.is_a?(Package::Archive) || statement.is_a?(Package::Resource)
      if statement.is_a?(Package::Resource) && !Repository.is_url?(statement.url)
        archive_name = statement.url
        archive_remote = "#{remote_dir_for_package(package_name, version_name)}/#{statement.url}"
      else
        archive_name = statement.url.split('/').last
        archive_remote = "#{remote_dir_for_package(package_name, version_name)}/#{archive_name}"
      end
      if Repository.is_url?(statement.url)
        archive_local = File.join(temp_dir, archive_name)
        @os.download(statement.url, archive_local)
      else
        archive_local = statement.url
      end
      @os.upload(archive_local, archive_remote, @remote_repository_user) unless local_only
      @os.copy(archive_local, local_dir + '/' + archive_name)
      if statement.is_a?(Package::Archive)
        @os.unpack_archive(local_dir, archive_name)
      end
      statement.class.new(archive_name).unparse('')
    else
      statement.unparse('')
    end
  end.select {|s|not s.nil?}
  @os.write(fig_file, content.join("\n").strip)
  @os.upload(fig_file, remote_fig_file_for_package(package_name, version_name), @remote_repository_user) unless local_only
  @os.copy(fig_file, local_fig_file_for_package(package_name, version_name))
end

#read_local_package(package_name, version_name) ⇒ Object



142
143
144
145
# File 'lib/fig/repository.rb', line 142

def read_local_package(package_name, version_name)
  dir = local_dir_for_package(package_name, version_name)
  read_package_from_directory(dir, package_name, version_name)
end

#read_package_from_directory(dir, package_name, version_name) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fig/repository.rb', line 147

def read_package_from_directory(dir, package_name, version_name)
  file = File.join(dir, '.fig')
  if not File.exist?(file)
    file = File.join(dir, 'package.fig')
  end
  if not File.exist?(file)
    Logging.fatal "Fig file not found for package: #{file}"
    raise RepositoryError.new
  end
  read_package_from_file(file, package_name, version_name)
end

#read_package_from_file(file_name, package_name, version_name) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/fig/repository.rb', line 159

def read_package_from_file(file_name, package_name, version_name)
  if not File.exist?(file_name)
    Logging.fatal "Package not found: #{package_name}/#{version_name}"
    raise RepositoryError.new
  end
  content = File.read(file_name)
  return @parser.parse_package(package_name, version_name, File.dirname(file_name), content)
end

#update_package(package_name, version_name) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fig/repository.rb', line 128

def update_package(package_name, version_name)
  remote_fig_file = remote_fig_file_for_package(package_name, version_name)
  local_fig_file = local_fig_file_for_package(package_name, version_name)
  begin
    if @os.download(remote_fig_file, local_fig_file)
      install_package(package_name, version_name)
    end
  rescue NotFoundError
    Logging.fatal "Package not found in remote repository: #{package_name}/#{version_name}"
    delete_local_package(package_name, version_name)
    raise RepositoryError.new
  end
end

#updating?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/fig/repository.rb', line 124

def updating?
  return @update || @update_if_missing
end