Module: Pray::FormatManifest

Defined in:
lib/pray/format_manifest.rb

Class Method Summary collapse

Class Method Details

.ambiguous_exports_for_roles(selected_exports, exports, roles) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/pray/format_manifest.rb', line 225

def ambiguous_exports_for_roles(selected_exports, exports, roles)
  ambiguous = []
  roles.each do |role|
    matching = selected_exports.select do |export_name|
      export = exports[export_name]
      export && Destination.export_kind_matches_role?(export.kind, role)
    end
    next unless matching.length > 1

    matching.each do |export_name|
      ambiguous << export_name unless ambiguous.include?(export_name)
    end
  end
  ambiguous
end

.apply_format_hints(packages, hints) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/pray/format_manifest.rb', line 144

def apply_format_hints(packages, hints)
  packages.each do |package|
    hint = hints[package.name]
    if hint
      hint.roles.each do |role|
        package.roles << role unless package.roles.include?(role)
      end
      package.file ||= hint.file_path
      if package.exports.empty? && !hint.exports.empty?
        package.exports = hint.exports.dup
      end
    end
    if package.file && !package.roles.include?("file")
      package.roles << "file"
    end
  end
end

.classify_format_hints(project) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pray/format_manifest.rb', line 19

def classify_format_hints(project)
  hints = {}
  project.packages.each do |package|
    roles = []
    file_path = package.declaration.file
    package.selected_exports.each do |export_name|
      export = package.spec.exports[export_name]
      next unless export

      %w[fragment folder file].each do |role|
        if Destination.export_kind_matches_role?(export.kind, role) && !roles.include?(role)
          roles << role
        end
      end
      if file_path.nil? && export.kind == "file"
        file_path = export.default_path || export_name
      end
    end
    hints[package.declaration.name] = PackageFormatHint.new(
      roles: roles,
      file_path: file_path,
      exports: ambiguous_exports_for_roles(package.selected_exports, package.spec.exports, roles)
    )
  end
  hints
end

.clone_package(package) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/pray/format_manifest.rb', line 245

def clone_package(package)
  package.dup.tap do |copy|
    copy.exports = package.exports.dup
    copy.targets = package.targets.dup
    copy.features = package.features.dup
    copy.groups = package.groups.dup
    copy.roles = package.roles.dup
  end
end

.deep_clone_manifest(manifest) ⇒ Object



241
242
243
# File 'lib/pray/format_manifest.rb', line 241

def deep_clone_manifest(manifest)
  Marshal.load(Marshal.dump(manifest))
end


57
58
59
60
61
62
63
64
65
66
# File 'lib/pray/format_manifest.rb', line 57

def format_recommended(manifest, hints)
  recommended = recommend_manifest(manifest, hints)
  text = FormatSerialize.serialize_recommended(recommended)
  reparsed = Pray.parse_manifest(text)
  if reparsed.manifest_hash != recommended.manifest_hash
    raise Error.manifest("formatted Prayfile did not round-trip to an equivalent manifest")
  end

  text
end

.has_migratable_legacy_targets?(manifest) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'lib/pray/format_manifest.rb', line 68

def has_migratable_legacy_targets?(manifest)
  manifest.targets.any? do |target|
    !target.scoped && target.mode == "legacy" &&
      (!target.outputs.empty? || !target.skills.empty?)
  end
end

.locals_for_compose(locals) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/pray/format_manifest.rb', line 191

def locals_for_compose(locals)
  before = []
  after = []
  locals.each do |local|
    next if local.bound

    if %w[start before].include?(local.position)
      before << local
    else
      after << local
    end
  end
  before + after
end

.mark_package_bound(packages, name, role) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/pray/format_manifest.rb', line 217

def mark_package_bound(packages, name, role)
  package = packages.find { |entry| entry.name == name }
  return unless package

  package.bound = true
  package.roles << role unless package.roles.include?(role)
end

.migrate_legacy_manifest(manifest, hints) ⇒ Object



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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/pray/format_manifest.rb', line 75

def migrate_legacy_manifest(manifest, hints)
  next_manifest = Manifest.new(
    prayfile_version: manifest.prayfile_version,
    sources: manifest.sources.map { |source| source.dup },
    targets: [],
    packages: manifest.packages.map { |package| clone_package(package) },
    local: manifest.local.map { |local| local.dup },
    symbols: manifest.symbols.dup,
    render: manifest.render.dup
  )

  apply_format_hints(next_manifest.packages, hints)

  compose_paths = unique_paths(
    manifest.targets.flat_map { |target| target.outputs.map { |path| [path, target.name] } }
  )
  tree_paths = unique_paths(
    manifest.targets.flat_map { |target| target.skills.map { |path| [path, target.name] } }
  )

  compose_paths.each do |path, target_names|
    target = Destination.new_destination_target("compose", path)
    locals_for_compose(next_manifest.local).each do |local|
      Destination.bind_local_entry(target, local.path)
      entry = next_manifest.local.find { |candidate| candidate.path == local.path }
      entry.bound = true if entry
    end
    packages_for_role(next_manifest.packages, "fragment", target_names).each do |package|
      Destination.bind_package_entry(target, package.name)
      mark_package_bound(next_manifest.packages, package.name, "fragment")
    end
    next_manifest.targets << target
  end

  tree_paths.each do |path, target_names|
    target = Destination.new_destination_target("tree", path)
    packages_for_role(next_manifest.packages, "folder", target_names).each do |package|
      Destination.bind_package_entry(target, package.name)
      mark_package_bound(next_manifest.packages, package.name, "folder")
    end
    next_manifest.targets << target
  end

  next_manifest.packages.each do |package|
    next unless package.file

    package.bound = true
    package.roles << "file" unless package.roles.include?("file")
  end
  next_manifest.local.each do |local|
    local.position = "after" if local.bound
  end

  manifest.targets.each do |target|
    next unless FormatSerialize.target_has_extras?(target)

    next_manifest.targets << ManifestTarget.new(
      name: target.name,
      commands: target.commands.dup,
      rules: target.rules.dup,
      max_bytes: target.max_bytes,
      mode: "legacy",
      scoped: false
    )
  end

  next_manifest
end

.omit_context_resolved_exports(manifest) ⇒ Object



162
163
164
165
166
# File 'lib/pray/format_manifest.rb', line 162

def omit_context_resolved_exports(manifest)
  manifest.packages.each do |package|
    package.exports.clear if package.bound && package.exports.length <= 1
  end
end

.omit_default_sources(manifest) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/pray/format_manifest.rb', line 168

def omit_default_sources(manifest)
  sole_source = (manifest.sources.length == 1) ? manifest.sources.first.name : nil
  source_names = manifest.sources.map(&:name).to_set
  manifest.packages.each do |package|
    source = package.source
    next unless source

    matches_sole = sole_source == source
    namespace = package.name.split("/", 2).first
    matches_namespace = namespace == source && source_names.include?(source)
    package.source = nil if matches_sole || matches_namespace
  end
end

.packages_for_role(packages, role, target_names) ⇒ Object



206
207
208
209
210
211
212
213
214
215
# File 'lib/pray/format_manifest.rb', line 206

def packages_for_role(packages, role, target_names)
  packages.select do |package|
    next false if package.file
    if !package.targets.empty? && package.targets.none? { |name| target_names.include?(name) }
      next false
    end

    package.roles.include?(role)
  end
end

.recommend_manifest(manifest, hints) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/pray/format_manifest.rb', line 46

def recommend_manifest(manifest, hints)
  recommended = if has_migratable_legacy_targets?(manifest)
    migrate_legacy_manifest(manifest, hints)
  else
    deep_clone_manifest(manifest)
  end
  omit_context_resolved_exports(recommended)
  omit_default_sources(recommended)
  recommended
end

.unique_paths(items) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/pray/format_manifest.rb', line 182

def unique_paths(items)
  map = {}
  items.each do |path, target_name|
    map[path] ||= Set.new
    map[path] << target_name
  end
  map.sort.map { |path, names| [path, names] }
end

.uses_destination_dsl?(manifest) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/pray/format_manifest.rb', line 13

def uses_destination_dsl?(manifest)
  manifest.targets.any? { |target| target.scoped || target.mode != "legacy" } ||
    manifest.packages.any? { |package| package.bound || package.file } ||
    manifest.local.any?(&:bound)
end