Module: Pray::Resolve
- Defined in:
- lib/pray/resolve.rb
Class Method Summary collapse
- .build_skill_file_index(spec) ⇒ Object
- .canonical_project_root(manifest_path) ⇒ Object
- .find_prayspec_file(root) ⇒ Object
- .indexed_files_under_prefix(files, prefix) ⇒ Object
- .load_export_bodies(file_bytes, spec, selected_exports) ⇒ Object
- .load_package_file_bytes(root, spec) ⇒ Object
- .lockfile_preferred_version(lockfile, package_name) ⇒ Object
- .missing_local_embed_guidance(path) ⇒ Object
- .project_root_from_manifest(manifest_path) ⇒ Object
- .resolution_may_benefit_from_git_source_refresh?(error) ⇒ Boolean
- .resolve_local_file(project_root, declaration) ⇒ Object
- .resolve_package(project_root, sources, git_sources, user_config, declaration, lockfile, offline: false) ⇒ Object
- .resolve_package_root(project_root, sources, git_sources, user_config, declaration, lockfile = nil) ⇒ Object
- .resolve_package_root_with_metadata(project_root, sources, git_sources, user_config, declaration, lockfile, offline: false) ⇒ Object
- .resolve_project(manifest_path, offline: false, refresh: false, environment: nil) ⇒ Object
- .resolve_project_in_context(manifest_path, project_root, options = ResolveOptions.new) ⇒ Object
- .resolve_project_with_git_refresh_fallback(manifest_path, offline: false, refresh: false, allow_git_refresh_fallback: false, environment: nil) ⇒ Object
- .resolve_project_with_options(manifest_path, options = ResolveOptions.new) ⇒ Object
- .select_exports(declaration, spec) ⇒ Object
- .skill_relative_file(file, skill_prefix) ⇒ Object
- .source_map(sources) ⇒ Object
Class Method Details
.build_skill_file_index(spec) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/pray/resolve.rb', line 364 def build_skill_file_index(spec) index = {} spec.exports.each do |export_name, export| next unless %w[folder skill].include?(export.kind) folder_prefix = export.path.delete_suffix("/") files = indexed_files_under_prefix(spec.files, folder_prefix) index[export_name] = files unless files.empty? end spec.skills.each do |skill_name, skill| next if index.key?(skill_name) skill_prefix = skill.path.delete_suffix("/") files = indexed_files_under_prefix(spec.files, skill_prefix) index[skill_name] = files unless files.empty? end index end |
.canonical_project_root(manifest_path) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/pray/resolve.rb', line 31 def canonical_project_root(manifest_path) root = project_root_from_manifest(manifest_path) return Pathname(root).cleanpath.to_s if Pathname(root).absolute? File.(root) end |
.find_prayspec_file(root) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/pray/resolve.rb', line 284 def find_prayspec_file(root) files = Dir.children(root).filter_map do |entry| path = File.join(root, entry) (File.file?(path) && File.extname(entry) == ".prayspec") ? path : nil end case files.length when 1 then files.first when 0 then raise Error.resolution("no prayspec file found in #{root.inspect}") else raise Error.resolution("multiple prayspec files found in #{root.inspect}") end end |
.indexed_files_under_prefix(files, prefix) ⇒ Object
383 384 385 |
# File 'lib/pray/resolve.rb', line 383 def indexed_files_under_prefix(files, prefix) files.filter_map { |file| skill_relative_file(file, prefix) } end |
.load_export_bodies(file_bytes, spec, selected_exports) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/pray/resolve.rb', line 349 def load_export_bodies(file_bytes, spec, selected_exports) export_bodies = {} selected_exports.each do |export_name| entry = spec.exports[export_name] raise Error.resolution("package #{spec.name} is missing export #{export_name}") unless entry next unless entry.kind == "fragment" bytes = file_bytes[entry.path] raise Error.integrity("package file missing for export #{export_name}: #{entry.path}") unless bytes export_bodies[export_name] = Hashing.normalize_line_endings(bytes.force_encoding(Encoding::UTF_8)) end export_bodies end |
.load_package_file_bytes(root, spec) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/pray/resolve.rb', line 337 def load_package_file_bytes(root, spec) file_bytes = {} spec.files.each do |file| path = File.join(root, file) raise Error.integrity("package file missing: #{file}") unless File.exist?(path) raise Error.integrity("package file is a directory: #{file}") if File.directory?(path) file_bytes[file] = File.binread(path) end file_bytes end |
.lockfile_preferred_version(lockfile, package_name) ⇒ Object
254 255 256 257 258 |
# File 'lib/pray/resolve.rb', line 254 def lockfile_preferred_version(lockfile, package_name) return nil unless lockfile lockfile.package.find { |entry| entry.name == package_name }&.version end |
.missing_local_embed_guidance(path) ⇒ Object
138 139 140 141 |
# File 'lib/pray/resolve.rb', line 138 def (path) "Prayfile lists `local \"#{path}\"` but the file does not exist. " \ "Create the file or remove the entry from Prayfile, then run `pray install`." end |
.project_root_from_manifest(manifest_path) ⇒ Object
26 27 28 29 |
# File 'lib/pray/resolve.rb', line 26 def project_root_from_manifest(manifest_path) parent = File.dirname(manifest_path) (parent.empty? || parent == ".") ? "." : parent end |
.resolution_may_benefit_from_git_source_refresh?(error) ⇒ Boolean
134 135 136 |
# File 'lib/pray/resolve.rb', line 134 def resolution_may_benefit_from_git_source_refresh?(error) error.category == :resolution && error..include?("no registry version") end |
.resolve_local_file(project_root, declaration) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/pray/resolve.rb', line 260 def resolve_local_file(project_root, declaration) path = File.join(project_root, declaration.path) unless File.exist?(path) if declaration.optional return ResolvedLocalFile.new( path: path, manifest_path: declaration.path, content: "", position: declaration.position, optional: true ) end raise Error.resolution((declaration.path)) end ResolvedLocalFile.new( path: path, manifest_path: declaration.path, content: Hashing.normalize_line_endings(File.read(path)), position: declaration.position, optional: declaration.optional ) end |
.resolve_package(project_root, sources, git_sources, user_config, declaration, lockfile, offline: false) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/pray/resolve.rb', line 143 def resolve_package(project_root, sources, git_sources, user_config, declaration, lockfile, offline: false) root, registry_latest_version = ( project_root, sources, git_sources, user_config, declaration, lockfile, offline: offline ) spec_path = find_prayspec_file(root) spec_text = File.read(spec_path) spec = Pray.parse_package_spec(spec_text).canonicalized if spec.name != declaration.name raise Error.resolution( "package path #{root.inspect} declares #{spec.name.inspect}, expected #{declaration.name.inspect}" ) end unless Constraint.version_satisfies(spec.version, declaration.constraint) raise Error.resolution( "package #{declaration.name} version #{spec.version} does not satisfy constraint #{declaration.constraint}" ) end selected_exports = select_exports(declaration, spec) file_bytes = load_package_file_bytes(root, spec) tree_hash = PackageSpec.tree_hash_from_file_bytes(file_bytes) export_bodies = load_export_bodies(file_bytes, spec, selected_exports) skill_files = build_skill_file_index(spec) ResolvedPackage.new( declaration: declaration, root: root, spec: spec, tree_hash: tree_hash, artifact_hash: tree_hash, artifact: "path:#{File.dirname(spec_path)}", selected_exports: selected_exports, source_checksum: tree_hash, export_bodies: export_bodies, skill_files: skill_files, signer_fingerprint: nil, registry_latest_version: registry_latest_version ) end |
.resolve_package_root(project_root, sources, git_sources, user_config, declaration, lockfile = nil) ⇒ Object
250 251 252 |
# File 'lib/pray/resolve.rb', line 250 def resolve_package_root(project_root, sources, git_sources, user_config, declaration, lockfile = nil) (project_root, sources, git_sources, user_config, declaration, lockfile).first end |
.resolve_package_root_with_metadata(project_root, sources, git_sources, user_config, declaration, lockfile, offline: false) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/pray/resolve.rb', line 183 def ( project_root, sources, git_sources, user_config, declaration, lockfile, offline: false ) if (local_path = user_config.local.package[declaration.name]) return [File.(local_path, project_root), nil] end return [File.(declaration.path, project_root), nil] if declaration.path if declaration.source source = sources[declaration.source] raise Error.resolution("unknown source: #{declaration.source}") unless source if (local_path = user_config.local.source[declaration.source]) source_root = File.(local_path, project_root) resolved = Registry.resolve_local_registry_package_root( project_root, "local:#{declaration.source}", source_root, declaration, preferred_version: lockfile_preferred_version(lockfile, declaration.name), offline: offline ) return [resolved.root, resolved.registry_latest_version] end case source.kind when "path" return [File.join(project_root, source.url, declaration.name.tr("/", "-")), nil] when "registry", "static index" resolved = Registry.resolve_registry_package_root( project_root, source.url, declaration, preferred_version: lockfile_preferred_version(lockfile, declaration.name), offline: offline ) return [resolved.root, resolved.registry_latest_version] when "pray_ssh" raise Error.unsupported("pray_ssh sources are not implemented yet in pray-cli Ruby") when "git" checkout = git_sources[source.name] raise Error.resolution("git source #{source.name} was not prepared") unless checkout clone_url = source.url.delete_prefix("git+") distribution_root = GitSources.resolve_distribution_root(checkout.cache_directory, checkout.subdir) source_key = checkout.revision.to_s.empty? ? clone_url : "#{clone_url}@#{checkout.revision}" resolved = Registry.resolve_local_registry_package_root( project_root, source_key, distribution_root, declaration, preferred_version: lockfile_preferred_version(lockfile, declaration.name), offline: offline ) return [resolved.root, resolved.registry_latest_version] else raise Error.unsupported("source kind #{source.kind} not implemented yet") end end if declaration.git || declaration.tarball || declaration.oci raise Error.unsupported("remote sources are not implemented yet") end [File.join(project_root, declaration.name.tr("/", "-")), nil] end |
.resolve_project(manifest_path, offline: false, refresh: false, environment: nil) ⇒ Object
38 39 40 41 |
# File 'lib/pray/resolve.rb', line 38 def resolve_project(manifest_path, offline: false, refresh: false, environment: nil) = ResolveOptions.new(offline: offline, refresh: refresh, environment: environment) (manifest_path, ) end |
.resolve_project_in_context(manifest_path, project_root, options = ResolveOptions.new) ⇒ Object
48 49 50 51 52 53 54 55 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/pray/resolve.rb', line 48 def resolve_project_in_context(manifest_path, project_root, = ResolveOptions.new) user_config = Config.load_user_config lockfile_path = File.join(project_root, "Prayfile.lock") lockfile_hints = File.exist?(lockfile_path) ? Pray.read_lockfile(lockfile_path) : nil manifest_text = Pray.read_manifest_text(manifest_path) manifest = Pray.parse_manifest(manifest_text) manifest.deprecation_warnings.each { |warning| warn(warning) } Environment.validate_environment(manifest, .environment) manifest_hash = manifest.manifest_hash sources = source_map(manifest.sources) git_sources = GitSources.prepare_git_sources( project_root, manifest.sources, lockfile_hints, refresh: .refresh || .refresh_source_revisions ) source_revisions = git_sources.transform_values(&:revision) source_host_keys = Trust.prepare_source_host_keys(manifest.sources) packages = [] seen = {} errors = [] manifest.packages.each do |declaration| package = resolve_package( project_root, sources, git_sources, user_config, declaration, lockfile_hints, offline: .offline ) if seen[package.declaration.name] raise Error.resolution("duplicate package declaration: #{package.declaration.name}") end seen[package.declaration.name] = true packages << package rescue Error => error errors << "#{declaration.name}: #{error.}" end raise Error.resolution(errors.join("\n")) unless errors.empty? local_files = [] local_errors = [] manifest.local.each do |local| local_files << resolve_local_file(project_root, local) rescue Error => error local_errors << "local #{local.path}: #{error.}" end raise Error.resolution(local_errors.join("\n")) unless local_errors.empty? ResolvedProject.new( manifest_path: manifest_path, project_root: project_root, manifest: manifest, manifest_hash: manifest_hash, packages: packages, local_files: local_files, source_revisions: source_revisions, source_host_keys: source_host_keys, environment: .environment ) end |
.resolve_project_with_git_refresh_fallback(manifest_path, offline: false, refresh: false, allow_git_refresh_fallback: false, environment: nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/pray/resolve.rb', line 113 def resolve_project_with_git_refresh_fallback( manifest_path, offline: false, refresh: false, allow_git_refresh_fallback: false, environment: nil ) = ResolveOptions.new(offline: offline, refresh: refresh, environment: environment) (manifest_path, ) rescue Error => error if allow_git_refresh_fallback && !offline && !refresh && resolution_may_benefit_from_git_source_refresh?(error) refreshed = ResolveOptions.new(offline: offline, refresh: true, environment: environment) (manifest_path, refreshed) else raise end end |
.resolve_project_with_options(manifest_path, options = ResolveOptions.new) ⇒ Object
43 44 45 46 |
# File 'lib/pray/resolve.rb', line 43 def (manifest_path, = ResolveOptions.new) project_root = canonical_project_root(manifest_path) resolve_project_in_context(manifest_path, project_root, ) end |
.select_exports(declaration, spec) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/pray/resolve.rb', line 300 def select_exports(declaration, spec) unless declaration.exports.empty? declaration.exports.each do |export| unless spec.exports.key?(export) raise Error.resolution("package #{declaration.name} does not export #{export}") end end return declaration.exports end roles = declaration.roles || [] return spec.exports.keys.sort if roles.empty? && declaration.file.nil? effective_roles = roles.dup effective_roles << "file" if declaration.file && !effective_roles.include?("file") selected = [] effective_roles.each do |role| compatible = spec.exports.filter_map do |name, export| name if Destination.export_kind_matches_role?(export.kind, role) end case compatible.length when 1 selected << compatible.first unless selected.include?(compatible.first) when 0 raise Error.resolution( "package #{declaration.name} has no export compatible with #{role}" ) else raise Error.resolution( "package #{declaration.name} has multiple exports compatible with #{role}; set export: \"name\"" ) end end selected end |
.skill_relative_file(file, skill_prefix) ⇒ Object
387 388 389 390 391 392 393 394 |
# File 'lib/pray/resolve.rb', line 387 def skill_relative_file(file, skill_prefix) return nil unless file.start_with?(skill_prefix) relative = file.delete_prefix(skill_prefix).delete_prefix("/") return nil if relative.empty? || file == skill_prefix relative end |
.source_map(sources) ⇒ Object
296 297 298 |
# File 'lib/pray/resolve.rb', line 296 def source_map(sources) sources.to_h { |source| [source.name, source] } end |