@preventive/deptree 1.3.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/bin/compare.js +5 -2
  2. package/bin/deptree.js +66 -19
  3. package/cocoapods.d.ts +372 -0
  4. package/cocoapods.js +6 -0
  5. package/package.json +24 -6
  6. package/pnpm.d.ts +52 -75
  7. package/soldeer.d.ts +6 -3
  8. package/src/cargo/members.js +3 -2
  9. package/src/cargo/tree.js +2 -2
  10. package/src/cargo/workspace.js +3 -6
  11. package/src/cocoapods/archive.js +223 -0
  12. package/src/cocoapods/clean.js +186 -0
  13. package/src/cocoapods/fnmatch.js +198 -0
  14. package/src/cocoapods/inputs.js +80 -0
  15. package/src/cocoapods/install.js +61 -0
  16. package/src/cocoapods/podspec.js +203 -0
  17. package/src/cocoapods/podtree.js +131 -0
  18. package/src/cocoapods/source.js +90 -0
  19. package/src/cocoapods/tree.js +184 -0
  20. package/src/cocoapods/unicode.js +17 -0
  21. package/src/github.js +92 -0
  22. package/src/glob.js +2 -1
  23. package/src/hash.js +1 -34
  24. package/src/matcher.js +2 -4
  25. package/src/mount.js +3 -6
  26. package/src/npm/compat.js +3 -4
  27. package/src/npm/manifests.js +1 -1
  28. package/src/npm/tree.js +9 -8
  29. package/src/patch.js +9 -5
  30. package/src/pnpm/bins.js +12 -11
  31. package/src/pnpm/commands.js +5 -15
  32. package/src/pnpm/directories.js +250 -0
  33. package/src/pnpm/extensions.js +76 -0
  34. package/src/pnpm/filename.js +4 -4
  35. package/src/pnpm/frozen.js +3 -2
  36. package/src/pnpm/glob.js +1 -1
  37. package/src/pnpm/graph.js +7 -9
  38. package/src/pnpm/hoist.js +0 -36
  39. package/src/pnpm/hoisted.js +3 -3
  40. package/src/pnpm/hook.js +13 -21
  41. package/src/pnpm/inputs.js +7 -16
  42. package/src/pnpm/install.js +3 -4
  43. package/src/pnpm/lazy.js +18 -18
  44. package/src/pnpm/local.js +29 -15
  45. package/src/pnpm/minimatch.js +2 -2
  46. package/src/pnpm/nm-hoist.js +1 -1
  47. package/src/pnpm/npm-packlist.js +1 -1
  48. package/src/pnpm/overrides.js +3 -4
  49. package/src/pnpm/package.js +17 -9
  50. package/src/pnpm/packlist.js +1 -2
  51. package/src/pnpm/patches.js +15 -13
  52. package/src/pnpm/production.js +1 -1
  53. package/src/pnpm/projects.js +14 -40
  54. package/src/pnpm/readers.js +6 -29
  55. package/src/pnpm/settings.js +14 -37
  56. package/src/pnpm/tree.js +34 -24
  57. package/src/pnpm/uptodate.js +12 -13
  58. package/src/pnpm/workspace.js +8 -79
  59. package/src/pnpm/write.js +2 -1
  60. package/src/soldeer/tree.js +2 -2
  61. package/src/soldeer/zip.js +11 -42
  62. package/src/tarball.js +1 -3
  63. package/src/yarn1/hoist.js +4 -2
  64. package/src/yarn1/manifest.js +3 -2
  65. package/src/yarn1/requests.js +1 -1
  66. package/src/yarn1/resolve.js +16 -6
  67. package/src/yarn1/tree.js +12 -8
  68. package/src/soldeer/git.js +0 -67
@@ -0,0 +1,203 @@
1
+ // A podspec as CocoaPods reads one from trunk's JSON, Specification.from_hash,
2
+ // and what its Consumer makes of the attributes that say which of a pod's
3
+ // files are used, on a platform: each spec's own value, the platform's laid
4
+ // over it, and for an inherited attribute the parent's under both. What
5
+ // CocoaPods would fail on, and what one release reads otherwise than
6
+ // another, is refused.
7
+
8
+ import { DeptreeError, quote } from '../error.js'
9
+ import { UNSAFE } from '../mount.js'
10
+ import { CASE_UNSTABLE } from './unicode.js'
11
+
12
+ // The platforms CocoaPods takes, by the names a podspec gives them, and
13
+ // all of them, in its order, for a spec that names none; Platform takes
14
+ // another case of a name, or `xros`, from 1.16 alone.
15
+ const PLATFORM_NAMES = { __proto__: null, osx: 'osx', macos: 'osx', ios: 'ios', tvos: 'tvos', visionos: 'visionos', watchos: 'watchos' }
16
+ const PLATFORMS = ['osx', 'ios', 'tvos', 'visionos', 'watchos']
17
+ // What Version.create takes of a deployment target, as trunk has them.
18
+ const DEPLOYMENT_TARGET = /^\d+(?:\.\d+)*$/u
19
+
20
+ // The keys from_hash takes the specs inside one from, each a list.
21
+ const CHILDREN = ['subspecs', 'testspecs', 'appspecs']
22
+
23
+ // The file patterns, each a list, none inherited.
24
+ const LISTS = ['source_files', 'exclude_files', 'resources', 'preserve_paths', 'vendored_frameworks', 'vendored_libraries']
25
+ // One path each, inherited: a subspec takes its parent's where it has none.
26
+ const PATHS = ['prefix_header_file', 'module_map']
27
+
28
+ export const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value)
29
+ // Given at all: a podspec leaves a value out as undefined, null or false.
30
+ export const given = (value) => value !== undefined && value !== null && value !== false
31
+ // Text a pattern or a path can be: not what reads otherwise than it is
32
+ // written, nor what CocoaPods compares by a case Ruby maps otherwise in one
33
+ // release than another.
34
+ const isText = (value) => typeof value === 'string' && value.isWellFormed() && !UNSAFE.test(value) && !CASE_UNSTABLE.test(value)
35
+
36
+ function readJson(bytes, where) {
37
+ let json
38
+ try {
39
+ json = JSON.parse(new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes))
40
+ } catch {
41
+ throw new DeptreeError('its podspec is not JSON, as trunk serves one', where)
42
+ }
43
+ if (!isObject(json)) throw new DeptreeError('its podspec is not a JSON object', where)
44
+ return json
45
+ }
46
+
47
+ // A spec and those inside it, each with its name in full, `Root/Sub`, and
48
+ // its parent. `at` is where it is in the podspec, for a refusal.
49
+ function specOf(hash, parent, at, where) {
50
+ const name = hash.name
51
+ if (!isText(name) || name === '' || name.includes('/')) throw new DeptreeError(`${at}name is not a spec's name`, where)
52
+ const spec = { name: parent === undefined ? name : `${parent.name}/${name}`, attributes: hash, parent, children: [], at }
53
+ for (const key of CHILDREN) {
54
+ const list = hash[key]
55
+ if (list === undefined || list === null) continue
56
+ if (!Array.isArray(list)) throw new DeptreeError(`${at}${key} is not a list of specs`, where)
57
+ list.forEach((child, index) => {
58
+ if (!isObject(child)) throw new DeptreeError(`${at}${key}[${index}] is not a spec`, where)
59
+ spec.children.push(specOf(child, spec, `${at}${key}[${index}].`, where))
60
+ })
61
+ }
62
+ return spec
63
+ }
64
+
65
+ // The root spec of `bytes`, held to be the lockfile's: of `name` at
66
+ // `version`, as Pod::Version writes it back.
67
+ export function readPodspec(bytes, name, version, where) {
68
+ const root = specOf(readJson(bytes, where), undefined, '', where)
69
+ if (root.name !== name) throw new DeptreeError(`its podspec is of ${quote(root.name)}`, where)
70
+ if (root.attributes.version !== version) throw new DeptreeError(`its podspec is of version ${typeof root.attributes.version === 'string' ? quote(root.attributes.version) : 'none'}`, where)
71
+ return root
72
+ }
73
+
74
+ // Every spec, the root first, then each inside one after it, depth first,
75
+ // as recursive_subspecs lists them.
76
+ export function allSpecs(root) {
77
+ return [root, ...root.children.flatMap((child) => allSpecs(child))]
78
+ }
79
+
80
+ export function specNamed(root, name) {
81
+ return allSpecs(root).find((spec) => spec.name === name)
82
+ }
83
+
84
+ // Specification#platform_hash: a name, a list of names, or names to
85
+ // deployment targets; anything else, none.
86
+ function platformNames(spec, where) {
87
+ const value = spec.attributes.platforms
88
+ const named = typeof value === 'string' ? [[value, null]] : Array.isArray(value) ? value.map((name) => [name, null]) : isObject(value) ? Object.entries(value) : []
89
+ return named.map(([name, target]) => {
90
+ if (typeof name !== 'string' || PLATFORM_NAMES[name] === undefined) throw new DeptreeError(`${spec.at}platforms names ${typeof name === 'string' ? quote(name) : 'what is no name'}, not a platform CocoaPods takes alike in each release`, where)
91
+ if (target !== null && !(typeof target === 'string' && DEPLOYMENT_TARGET.test(target))) throw new DeptreeError(`${spec.at}platforms.${name} is not a deployment target`, where)
92
+ return PLATFORM_NAMES[name]
93
+ })
94
+ }
95
+
96
+ // available_platforms: its own, else its parent's, else all of them.
97
+ export function platformsOf(spec, where) {
98
+ for (let at = spec; at !== undefined; at = at.parent) {
99
+ const names = platformNames(at, where)
100
+ if (names.length > 0) return [...new Set(names)]
101
+ }
102
+ return PLATFORMS
103
+ }
104
+
105
+ // The hash of a platform's values where a spec gives one, as the Consumer
106
+ // reads it; anything else but null or false CocoaPods fails on, or reads as
107
+ // a string.
108
+ function platformHash(spec, platform, where) {
109
+ const value = spec.attributes[platform]
110
+ if (!given(value)) return undefined
111
+ if (!isObject(value)) throw new DeptreeError(`${spec.at}${platform} is not a hash of values for the platform`, where)
112
+ return value
113
+ }
114
+
115
+ // prepare_value of a list: a value as a list of itself, nulls dropped;
116
+ // each has to be a pattern, as File.fnmatch takes nothing else.
117
+ function patternsOf(value, what, where) {
118
+ if (value === undefined || value === null) return []
119
+ const list = Array.isArray(value) ? value.filter((item) => item !== null) : [value]
120
+ for (const item of list) if (!isText(item)) throw new DeptreeError(`${what} is not a file pattern, or a list of them`, where)
121
+ return list
122
+ }
123
+
124
+ // The raw value of `key` on `platform`, its own and the platform's.
125
+ function rawValues(spec, key, platform, where) {
126
+ return [spec.attributes[key], platformHash(spec, platform, where)?.[key]]
127
+ }
128
+
129
+ function listOf(spec, key, platform, where) {
130
+ const [own, onPlatform] = rawValues(spec, key, platform, where)
131
+ return [...patternsOf(own, `${spec.at}${key}`, where), ...patternsOf(onPlatform, `${spec.at}${platform}.${key}`, where)]
132
+ }
133
+
134
+ // A path given as it is joined to the pod's directory, as Pathname#+ joins
135
+ // it: one this keeps as it is, within the directory.
136
+ function checkPath(path, what, where) {
137
+ if (!isText(path) || path === '' || path.split('/').some((segment) => segment === '' || segment === '.' || segment === '..')) {
138
+ throw new DeptreeError(`${what} is not a path within the pod, as CocoaPods joins it to the pod's directory`, where)
139
+ }
140
+ return path
141
+ }
142
+
143
+ // prefix_header_file and module_map: a path, or false or null for none,
144
+ // the platform's over the spec's, the spec's over its parent's.
145
+ function pathOf(spec, key, platform, where) {
146
+ const [own, onPlatform] = rawValues(spec, key, platform, where)
147
+ const value = onPlatform ?? own ?? (spec.parent === undefined ? undefined : pathOf(spec.parent, key, platform, where))
148
+ if (!given(value)) return undefined
149
+ if (typeof value !== 'string') throw new DeptreeError(`${spec.at}${key} is ${JSON.stringify(value)}, which CocoaPods fails on`, where)
150
+ return checkPath(value, `${spec.at}${key}`, where)
151
+ }
152
+
153
+ // _prepare_on_demand_resources: patterns, or a hash of its `paths` and a
154
+ // `category`, which has to be a name.
155
+ function onDemandPatterns(patterns, what, where) {
156
+ if (typeof patterns === 'string' || Array.isArray(patterns)) return patternsOf(patterns, what, where)
157
+ if (!isObject(patterns)) throw new DeptreeError(`${what} is not file patterns, or a hash of them, which CocoaPods fails on`, where)
158
+ if (Object.hasOwn(patterns, 'category') && typeof patterns.category !== 'string') throw new DeptreeError(`${what}.category is not a name, which CocoaPods fails on`, where)
159
+ return patternsOf(patterns.paths, `${what}.paths`, where)
160
+ }
161
+
162
+ // resource_bundles and on_demand_resources: a hash, the platform's merged
163
+ // into the spec's, by name, each its patterns as `prepare` reads them. Of
164
+ // a name in both, the bundle has the patterns of both; of an on-demand
165
+ // resource, CocoaPods fails, as it adds the two hashes as if strings.
166
+ function hashOf(spec, key, platform, where, prepare) {
167
+ const result = new Map()
168
+ const whats = [`${spec.at}${key}`, `${spec.at}${platform}.${key}`]
169
+ for (const [index, value] of rawValues(spec, key, platform, where).entries()) {
170
+ if (value === undefined || value === null) continue
171
+ if (!isObject(value)) throw new DeptreeError(`${whats[index]} is not a hash, which CocoaPods fails on`, where)
172
+ for (const [name, patterns] of Object.entries(value)) {
173
+ const what = `${whats[index]}[${quote(name)}]`
174
+ if (result.has(name) && prepare === onDemandPatterns) throw new DeptreeError(`${what} is given for the spec and for ${platform} too, which CocoaPods fails on`, where)
175
+ result.set(name, [...result.get(name) ?? [], ...prepare(patterns, what, where)])
176
+ }
177
+ }
178
+ return [...result.values()].flat()
179
+ }
180
+
181
+ // What a spec's Consumer on `platform` gives of each attribute that names
182
+ // files: the lists of patterns, those of its resource bundles and its
183
+ // on-demand resources, and the prefix header's and module map's paths.
184
+ export function consumerOf(spec, platform, where) {
185
+ const values = {}
186
+ for (const key of LISTS) values[key] = listOf(spec, key, platform, where)
187
+ values.resource_bundles = hashOf(spec, 'resource_bundles', platform, where, patternsOf)
188
+ values.on_demand_resources = hashOf(spec, 'on_demand_resources', platform, where, onDemandPatterns)
189
+ for (const key of PATHS) values[key] = pathOf(spec, key, platform, where)
190
+ return values
191
+ }
192
+
193
+ // The root's license file, as RootAttributesAccessors#license reads it: of
194
+ // a hash alone, where its `text`, which CocoaPods strips, is text.
195
+ export function licenseFileOf(root, where) {
196
+ const license = root.attributes.license
197
+ if (!isObject(license)) return undefined
198
+ if (given(license.text) && typeof license.text !== 'string') throw new DeptreeError('license.text is not text, which CocoaPods fails on', where)
199
+ const file = license.file
200
+ if (!given(file)) return undefined
201
+ if (typeof file !== 'string') throw new DeptreeError('license.file is not a path, which CocoaPods fails on', where)
202
+ return checkPath(file, 'license.file', where)
203
+ }
@@ -0,0 +1,131 @@
1
+ // A pod's directory, held in memory as CocoaPods finds it on disk: what
2
+ // is there, what a path comes to through the links on the way, and what
3
+ // FileUtils.rm_rf leaves of it.
4
+
5
+ import { utf8fromStringLoose } from '@exodus/bytes/utf8.js'
6
+ import { DeptreeError, quote } from '../error.js'
7
+ import { fold } from '../mount.js'
8
+ import { CASE_UNSTABLE } from './unicode.js'
9
+
10
+ // The links a path is followed through before it is taken to lead nowhere,
11
+ // as each gives up with ELOOP; the longest path, in bytes, and the longest
12
+ // name in it, that a call takes, which a path in the pod is shorter than
13
+ // with the pod's directory in front of it.
14
+ const LIMITS = {
15
+ linux: { links: 40, path: 4096, name: 255 },
16
+ darwin: { links: 32, path: 1024, name: 255 },
17
+ }
18
+
19
+ const parentOf = (path) => (path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : '')
20
+ const join = (dir, name) => (dir === '' ? name : `${dir}/${name}`)
21
+
22
+ // A pod's directory: its paths from there, each a directory, a file with
23
+ // its data and mode, or a link with its target, as given; every directory
24
+ // on the way to one is one of them. `folded` where it is on macOS, which
25
+ // finds a name whatever its case and normalization: two that are one name
26
+ // there are refused, as git's checkout would write one of them alone.
27
+ export class PodTree {
28
+ constructor({ dirs, files, links }, folded, where) {
29
+ this.where = where
30
+ this.limits = LIMITS[folded ? 'darwin' : 'linux']
31
+ this.key = folded ? fold : (path) => path
32
+ this.nodes = new Map()
33
+ const add = (path, node) => {
34
+ for (let at = parentOf(path); at !== '' && !this.nodes.has(at); at = parentOf(at)) this.nodes.set(at, { type: 'directory' })
35
+ this.nodes.set(path, node)
36
+ }
37
+ for (const dir of dirs) add(dir, { type: 'directory' })
38
+ for (const [path, { data, mode }] of files) add(path, { type: 'file', data, mode })
39
+ for (const [path, target] of links) add(path, { type: 'symlink', target })
40
+ // Each path by its key, as a lookup finds it.
41
+ this.byKey = new Map()
42
+ for (const [path, node] of this.nodes) {
43
+ this.check(path, node.target)
44
+ const other = this.byKey.get(this.key(path))
45
+ if (other !== undefined) throw new DeptreeError(`${quote(other)} and ${quote(path)} are one name on macOS`, where)
46
+ this.byKey.set(this.key(path), path)
47
+ }
48
+ }
49
+
50
+ // What CocoaPods fails on, a path too long or with a name too long, and
51
+ // what it reads by case otherwise in one release than another.
52
+ check(path, target) {
53
+ const bytes = utf8fromStringLoose(path).length
54
+ if (bytes >= this.limits.path || path.split('/').some((name) => utf8fromStringLoose(name).length > this.limits.name)) {
55
+ throw new DeptreeError(`${quote(path)} is longer than a path or a name can be, and CocoaPods fails on it`, this.where)
56
+ }
57
+ if (CASE_UNSTABLE.test(path) || (target !== undefined && CASE_UNSTABLE.test(target))) {
58
+ throw new DeptreeError(`${quote(path)} has a letter whose case each release of Ruby maps otherwise, by which CocoaPods compares names`, this.where)
59
+ }
60
+ }
61
+
62
+ get paths() {
63
+ return [...this.nodes.keys()]
64
+ }
65
+
66
+ // The node `path` comes to, links followed, or undefined where it comes
67
+ // to nothing. A link that leads out of the pod's directory reads as
68
+ // whatever is there on the machine, which is refused.
69
+ resolve(path) {
70
+ // The names still to follow, the next one last.
71
+ const pending = path.split('/').toReversed()
72
+ let at = ''
73
+ let followed = 0
74
+ while (pending.length > 0) {
75
+ const name = pending.pop()
76
+ if (name === '' || name === '.') continue
77
+ if (name === '..') {
78
+ if (at === '') throw new DeptreeError(`${quote(path)} leads out of the pod's directory through a link`, this.where)
79
+ at = parentOf(at)
80
+ continue
81
+ }
82
+ const next = this.byKey.get(this.key(join(at, name)))
83
+ if (next === undefined) return undefined
84
+ const node = this.nodes.get(next)
85
+ if (node.type === 'symlink') {
86
+ // A link to nothing at all, which leads nowhere.
87
+ if (++followed > this.limits.links || node.target === '') return undefined
88
+ if (node.target.startsWith('/')) throw new DeptreeError(`${quote(next)} links to the absolute ${quote(node.target)}`, this.where)
89
+ pending.push(...node.target.split('/').toReversed())
90
+ continue
91
+ }
92
+ if (node.type === 'file' && pending.length > 0) return undefined
93
+ at = next
94
+ }
95
+ return at === '' ? { path: '', node: { type: 'directory' } } : { path: at, node: this.nodes.get(at) }
96
+ }
97
+
98
+ // File.directory? and File.exist?, which follow links.
99
+ isDirectory(path) {
100
+ return this.resolve(path)?.node.type === 'directory'
101
+ }
102
+
103
+ exists(path) {
104
+ return this.resolve(path) !== undefined
105
+ }
106
+
107
+ // FileUtils.rm_rf of each of `removed`: it, and all under it where it is
108
+ // a directory, as nothing is under a link.
109
+ removeAll(removed) {
110
+ for (const path of this.paths) {
111
+ let at = path
112
+ while (at !== '' && !removed.has(at)) at = parentOf(at)
113
+ if (at === '') continue
114
+ this.nodes.delete(path)
115
+ this.byKey.delete(this.key(path))
116
+ }
117
+ }
118
+
119
+ // The tree as writeFiles writes it.
120
+ contents() {
121
+ const dirs = []
122
+ const files = new Map()
123
+ const links = new Map()
124
+ for (const [path, node] of this.nodes) {
125
+ if (node.type === 'directory') dirs.push(path)
126
+ else if (node.type === 'file') files.set(path, { data: node.data, mode: node.mode })
127
+ else links.set(path, node.target)
128
+ }
129
+ return { dirs, files, links }
130
+ }
131
+ }
@@ -0,0 +1,90 @@
1
+ // Where a pod is downloaded from: its podspec's `source`, as
2
+ // cocoapods-downloader takes it, held to what is fetched here alike.
3
+
4
+ import { DeptreeError, quote } from '../error.js'
5
+ import { githubRepoOf } from '../github.js'
6
+ import { given, isObject } from './podspec.js'
7
+
8
+ // cocoapods-downloader's strategies; a source names one of them alone.
9
+ const STRATEGIES = ['git', 'hg', 'http', 'scp', 'svn']
10
+ // What its Git takes beside the URL; anything else it refuses.
11
+ const GIT_OPTIONS = new Set(['git', 'tag', 'commit', 'branch', 'submodules'])
12
+ const COMMIT = /^[\da-f]{40}$/u
13
+ // What its Http, a RemoteFile, takes beside the URL; anything else it
14
+ // refuses.
15
+ const HTTP_OPTIONS = new Set(['http', 'type', 'flatten', 'sha1', 'sha256', 'headers'])
16
+ // The hash RemoteFile checks a download against, :sha256 before :sha1, by
17
+ // the lowercase hex Ruby's hexdigest writes, which it compares as text: a
18
+ // sha256 alone is taken, as two archives can be made to share a sha1.
19
+ const SHA256 = /^[\da-f]{64}$/u
20
+ // RemoteFile's types, by :type or the end of the URL's path: tars it
21
+ // extracts with `tar xf` and flattens by default, a zip with `unzip`, which
22
+ // it does not flatten, and a dmg it mounts, not read here.
23
+ const TARS = new Set(['tgz', 'tar', 'tbz', 'txz'])
24
+ const TYPES = new Set([...TARS, 'zip', 'dmg'])
25
+ const URL_TYPES = [[/\.zip$/u, 'zip'], [/\.(?:tgz|tar\.gz)$/u, 'tgz'], [/\.tar$/u, 'tar'], [/\.(?:tbz|tar\.bz2)$/u, 'tbz'], [/\.(?:txz|tar\.xz)$/u, 'txz'], [/\.dmg$/u, 'dmg']]
26
+ // https://github.com/<owner>/<repo>/ and a path under it, as upstream's
27
+ // getPodArchive fetches one: no query, fragment, credentials or port, and
28
+ // each segment as its buildUrl lets one through.
29
+ const LOGIN = /^(?=.{1,39}$)[a-z\d](?:-?[a-z\d])*$/iu
30
+ const REPO_NAME = /^(?!\.\.?$)[\w.-]{1,100}$/u
31
+ const SEGMENT = /^(?!\.\.?$)(?:[\w.~@-]|%[\dA-F]{2})+$/u
32
+ function isGitHubUrl(url) {
33
+ const parsed = URL.parse(url)
34
+ if (parsed?.href !== url || parsed.origin !== 'https://github.com' || parsed.username !== '' || parsed.password !== '' || parsed.search !== '' || parsed.hash !== '') return false
35
+ const segments = parsed.pathname.slice(1).split('/')
36
+ return segments.length >= 3 && LOGIN.test(segments[0]) && REPO_NAME.test(segments[1]) && segments.every((segment) => SEGMENT.test(segment))
37
+ }
38
+
39
+ // A refusal of where a pod is downloaded from, which buildCocoaPodsTree's
40
+ // `partial` leaves the pod to CocoaPods for; a DeptreeError otherwise.
41
+ export class SourceRefusal extends DeptreeError {}
42
+
43
+ // The podspec's source: git, a GitHub repository by its URL and the full
44
+ // commit to check out of it; or :http, an archive on GitHub over https,
45
+ // `tar xf` or `unzip` extracts, and the hash the podspec holds it to. A
46
+ // tag is refused, as the lockfile holds no commit for one and GitHub can
47
+ // move it. A prepare_command, a script CocoaPods runs in the download, is
48
+ // refused, as is any other source, each with a SourceRefusal.
49
+ export function sourceOf(podspec, where) {
50
+ if (given(podspec.attributes.prepare_command)) throw new SourceRefusal('a prepare_command, a script CocoaPods runs in the download, is not supported', where)
51
+ const source = podspec.attributes.source
52
+ if (!isObject(source)) throw new SourceRefusal('its podspec has no source to download it from', where)
53
+ const strategies = STRATEGIES.filter((strategy) => Object.hasOwn(source, strategy))
54
+ if (strategies.length !== 1) throw new SourceRefusal('its podspec\'s source names no one way to download it, which CocoaPods fails on', where)
55
+ if (strategies[0] === 'http') return httpSourceOf(source, where)
56
+ if (strategies[0] !== 'git') throw new SourceRefusal(`a source by :${strategies[0]} is not supported: only git from GitHub, and :http from GitHub, are`, where)
57
+ const other = Object.keys(source).find((key) => !GIT_OPTIONS.has(key))
58
+ if (other !== undefined) throw new SourceRefusal(`its source has :${other}, which cocoapods-downloader refuses of git`, where)
59
+ const { git: url, tag, commit, branch, submodules } = source
60
+ const repo = typeof url === 'string' ? githubRepoOf(url) : undefined
61
+ if (repo === undefined) throw new SourceRefusal(`a git source from ${typeof url === 'string' ? quote(url) : 'nowhere'}, not a GitHub repository over https or ssh, is not supported`, where)
62
+ if (given(submodules)) throw new SourceRefusal('a git source with its submodules is not supported: GitHub\'s tarball leaves each empty', where)
63
+ if (given(branch)) throw new SourceRefusal('a git source by a branch, which locks no commit, is not supported', where)
64
+ // CocoaPods records no commit for a tag, which GitHub can move.
65
+ if (given(tag)) throw new SourceRefusal('a git source by a tag, for which neither the lockfile nor the podspec holds a commit, is not supported: only one by a full commit is', where)
66
+ if (!(typeof commit === 'string' && COMMIT.test(commit))) throw new SourceRefusal(given(commit) ? `${quote(String(commit))} is not a full commit, as is supported` : 'a git source by no commit is not supported', where)
67
+ return { type: 'git', url, repo, commit }
68
+ }
69
+
70
+ // An :http source: its URL, on GitHub over https; what RemoteFile takes it
71
+ // to be, and so the name it downloads it under, `file.<type>`, in the
72
+ // directory it extracts it into; whether it flattens it, by :flatten where
73
+ // that is given, as Ruby reads a value, and else by its type, a tar and not
74
+ // a zip; and the sha256 it checks it against. A download held to no hash,
75
+ // or to a sha1 alone, is refused, as is one with :headers, which curl would
76
+ // send.
77
+ function httpSourceOf(source, where) {
78
+ const other = Object.keys(source).find((key) => !HTTP_OPTIONS.has(key))
79
+ if (other !== undefined) throw new SourceRefusal(`its source has :${other}, which cocoapods-downloader refuses of :http`, where)
80
+ const { http: url, type: named, flatten, headers } = source
81
+ if (typeof url !== 'string' || !isGitHubUrl(url)) throw new SourceRefusal(`an :http source from ${typeof url === 'string' ? quote(url) : 'nowhere'}, not https://github.com/<owner>/<repo>/ and a path of letters, digits and \`_.~@-%\` under it, is not supported`, where)
82
+ if (given(headers)) throw new SourceRefusal('an :http source with :headers, which curl sends with the request, is not supported', where)
83
+ const type = given(named) ? named : URL_TYPES.find(([pattern]) => pattern.test(URL.parse(url).pathname))?.[1]
84
+ if (typeof type !== 'string' || !TYPES.has(type)) throw new SourceRefusal(`${given(named) ? `its :type ${quote(String(named))}` : 'its URL'} names no type of archive cocoapods-downloader extracts`, where)
85
+ if (type === 'dmg') throw new SourceRefusal('an :http source of a dmg, which hdiutil mounts, is not supported: only a zip, and tgz, tar, tbz and txz, which `tar xf` extracts, are', where)
86
+ if (!given(source.sha256)) throw new SourceRefusal(given(source.sha1) ? 'an :http source held to a :sha1 alone, which two archives can be made to share, is not supported: only one held to a :sha256 is' : 'an :http source with no :sha256 to hold the download to is not supported', where)
87
+ const { sha256 } = source
88
+ if (!(typeof sha256 === 'string' && SHA256.test(sha256))) throw new SourceRefusal('its :sha256 is not one in lowercase hex, which no download matches', where)
89
+ return { type: 'http', url, archive: type, file: `file.${type}`, flatten: Object.hasOwn(source, 'flatten') ? given(flatten) : TARS.has(type), sha256 }
90
+ }
@@ -0,0 +1,184 @@
1
+ // The pods `pod install` downloads into Pods/ from a Podfile.lock.
2
+
3
+ import { utf8fromString } from '@exodus/bytes/utf8.js'
4
+ import { parsePodfileLock } from '@preventive/lockfile/cocoapods.js'
5
+ import { getPodArchive, getPodspec } from '@preventive/upstream/cocoapods.js'
6
+ import { Vfs } from '@preventive/vfs'
7
+ import { eachConcurrently } from '../concurrent.js'
8
+ import { DeptreeError, quote } from '../error.js'
9
+ import { checkCloned, checkoutOf } from '../github.js'
10
+ import { sha1Hex } from '../hash.js'
11
+ import { checkNoDir, fold, makeDirs, mount, writeFiles } from '../mount.js'
12
+ import { archiveOf } from './archive.js'
13
+ import { checkHost, flagsOf, inputsOf, platformsOf } from './inputs.js'
14
+ import { installPod, planPod } from './install.js'
15
+ import { readPodspec, specNamed } from './podspec.js'
16
+ import { SourceRefusal, sourceOf } from './source.js'
17
+
18
+ const GITHUB = 'github must be a GitHub client from createClient, which each pod is fetched through'
19
+ const GITHUB_METHODS = ['getRepo', 'compareCommits', 'getCommitTree', 'getRepoTreeTarball']
20
+
21
+ // Trunk, as SPEC REPOS names it from CocoaPods 1.8, or the git repository
22
+ // trunk's podspecs are kept in, as it named it before; trunk's CDN serves
23
+ // the same files.
24
+ const isTrunk = (repo) => repo === 'trunk' || /^https:\/\/github\.com\/cocoapods\/specs(?:\.git)?$/iu.test(repo)
25
+
26
+ // What CocoaPods writes in Pods/ beside the pods, which a pod's directory
27
+ // would be one with: Pods.xcodeproj among the projects of
28
+ // generate_multiple_pod_projects, each `<name>.xcodeproj`.
29
+ const GENERATED = ['Headers', 'Local Podspecs', 'Manifest.lock', 'Target Support Files']
30
+ const isGenerated = (name, key) => GENERATED.some((other) => key(other) === key(name)) || key(name).endsWith(key('.xcodeproj'))
31
+
32
+ const checkNoPods = checkNoDir('Pods', 'a Pods directory')
33
+
34
+ const about = (name) => `pods[${quote(name)}]`
35
+
36
+ // A root CocoaPods downloads into Pods/, true: one from trunk, or by
37
+ // :podspec, which CocoaPods downloads by its podspec's source, but where
38
+ // it resolved that to a commit, CHECKOUT OPTIONS', which it downloads by
39
+ // those. One by :path, which it reads where it is, false; any other,
40
+ // refused.
41
+ function isDownloaded(root, folded) {
42
+ const where = about(root.name)
43
+ if (root.external?.type === 'path') return false
44
+ if (root.external?.type === 'podspec' && root.checkout !== undefined) throw new SourceRefusal('a pod by :podspec that CocoaPods downloads by its CHECKOUT OPTIONS is not supported', where)
45
+ if (root.external !== undefined && root.external.type !== 'podspec') throw new SourceRefusal(`a pod from an external source by :${root.external.type} is not supported: only those from trunk, by :podspec and by :path are`, where)
46
+ if (root.external === undefined && !isTrunk(root.repo)) throw new SourceRefusal(`a pod from the spec repo ${quote(root.repo)} is not supported: only those from trunk are`, where)
47
+ if (isGenerated(root.name, folded ? fold : (name) => name)) throw new DeptreeError('a pod named as what CocoaPods writes beside the pods in Pods/ is not supported', where)
48
+ // PodDirCleaner globs the pod's directory by its path as it is, which
49
+ // then matches what it should not, or nothing.
50
+ if (/[*?[\]{}]/u.test(root.name)) throw new DeptreeError('a name with a glob\'s `*`, `?`, `[`, `]`, `{` or `}` in it, which CocoaPods cleans the pod by, is not supported', where)
51
+ // ASCII, as the lockfile reader holds a pod's name to.
52
+ if (root.name.length > 255) throw new DeptreeError('a name too long for a directory, which CocoaPods fails to make', where)
53
+ return true
54
+ }
55
+
56
+ function refuse(detail, where) {
57
+ throw new DeptreeError(detail, where)
58
+ }
59
+
60
+ // What `attempt` gives, or comes to where it is async, or with `partial`,
61
+ // where it refuses where the pod is downloaded from, undefined, its refusal
62
+ // kept in `skipped` by the pod's name; any other refusal stands.
63
+ function unlessSkipped(attempt, root, skipped) {
64
+ const skip = (error) => {
65
+ if (skipped === undefined || !(error instanceof SourceRefusal)) throw error
66
+ skipped.set(root.name, error.message)
67
+ return undefined
68
+ }
69
+ try {
70
+ const result = attempt()
71
+ return result instanceof Promise ? result.catch(skip) : result
72
+ } catch (error) {
73
+ return skip(error)
74
+ }
75
+ }
76
+
77
+ // The podspec of a pod by :podspec, as CocoaPods wrote it in Pods/Local
78
+ // Podspecs, held to the lockfile's checksum, which is its sha1.
79
+ async function localPodspec(root, podspecs, where) {
80
+ const bytes = podspecs(root.name)
81
+ if (bytes === undefined) throw new SourceRefusal(`its podspec, the JSON CocoaPods writes as Pods/Local Podspecs/${root.name}.podspec.json, is not given`, where)
82
+ const sum = await sha1Hex(bytes)
83
+ if (sum !== root.checksum) throw new DeptreeError(`its podspec given is not the one the lockfile has the checksum of: its sha1 is ${sum}, not ${root.checksum}`, where)
84
+ return bytes
85
+ }
86
+
87
+ // Each root's podspec, its source, and what of its download it keeps,
88
+ // planPod's, all before any download, so that a pod refused fails the call
89
+ // before any tarball or archive is fetched. A pod skipped for its source
90
+ // has no plan, its podspec held to the lockfile all the same.
91
+ async function planAll(roots, { github, podspecs, platforms }, skipped) {
92
+ const plans = new Map()
93
+ await eachConcurrently(roots, async (root) => {
94
+ const where = about(root.name)
95
+ const bytes = root.external === undefined ? await getPodspec(root.name, root.version, root.checksum) : await unlessSkipped(() => localPodspec(root, podspecs, where), root, skipped)
96
+ if (bytes === undefined) return
97
+ const podspec = readPodspec(bytes, root.name, root.version, where)
98
+ const source = unlessSkipped(() => sourceOf(podspec, where), root, skipped)
99
+ if (source === undefined) return
100
+ const specs = root.pods.map((name) => specNamed(podspec, name) ?? refuse(`its podspec has no ${quote(name)}, which the lockfile installs`, where))
101
+ const files = planPod(podspec, specs, platforms, where)
102
+ if (source.type === 'git') await checkCloned(github, source.repo, source.commit, where)
103
+ plans.set(root.name, { root, files, source })
104
+ }, (root) => about(root.name))
105
+ return plans
106
+ }
107
+
108
+ // What a download comes to, each fetched once, for every pod of it: a
109
+ // checkout, by its repository and commit; an archive, by its hash and what
110
+ // RemoteFile makes of it.
111
+ const downloadOf = ({ source }) => (source.type === 'git' ? `git ${source.repo}@${source.commit}` : `http ${source.sha256} ${source.file} ${source.flatten}`)
112
+ async function contentsOf({ root, source }, github) {
113
+ const where = about(root.name)
114
+ if (source.type === 'git') return await checkoutOf(github, source.repo, source.commit, where)
115
+ return await archiveOf(await getPodArchive(source.url, source.sha256), source, where)
116
+ }
117
+
118
+ // What CocoaPods keeps of each pod's download, held only while the pods of
119
+ // it are installed.
120
+ async function installAll(plans, github, options) {
121
+ const byDownload = Map.groupBy(plans.values(), downloadOf)
122
+ const installed = new Map()
123
+ await eachConcurrently(byDownload.values(), async (group) => {
124
+ const contents = await contentsOf(group[0], github)
125
+ // An archive is no clone, with no .git for CocoaPods to copy.
126
+ const keepsGit = options.keepsGit && group[0].source.type === 'git'
127
+ for (const plan of group) installed.set(plan.root.name, installPod(contents, plan.files, { ...options, keepsGit, where: about(plan.root.name) }))
128
+ }, ([{ root }]) => about(root.name))
129
+ return installed
130
+ }
131
+
132
+ // A pod written, as CocoaPodsInstalled has it: from git, by the commit
133
+ // checked out; by :http, by the URL and the sha256 it is held to.
134
+ function installedOf({ name, version, checksum, pods }, { source }) {
135
+ const pod = { path: `Pods/${name}`, name, version, checksum, pods: [...pods] }
136
+ if (source.type === 'git') return { ...pod, git: source.url, commit: source.commit }
137
+ return { ...pod, http: source.url, sha256: source.sha256 }
138
+ }
139
+
140
+ // Pods/Manifest.lock, which CocoaPods writes as it writes Podfile.lock;
141
+ // the lockfile reader holds the lockfile to be as CocoaPods writes it, so
142
+ // it is the lockfile's bytes: its text as UTF-8, of no lone surrogate, as
143
+ // the reader refuses one.
144
+ function writeManifest(vfs, lockfile, stats) {
145
+ const data = utf8fromString(lockfile)
146
+ makeDirs(vfs, 'Pods')
147
+ vfs.writeFile('/Pods/Manifest.lock', data)
148
+ stats.files++
149
+ stats.bytes += data.length
150
+ }
151
+
152
+ export async function buildCocoaPodsTree(options) {
153
+ const all = options ?? {}
154
+ const { vfs: into, github } = all
155
+ if (into !== undefined && !(into instanceof Vfs)) throw new TypeError('vfs must be a Vfs, or left out')
156
+ if (github !== undefined && GITHUB_METHODS.some((name) => typeof github?.[name] !== 'function')) throw new TypeError(GITHUB)
157
+ const inputs = inputsOf(all)
158
+ const platforms = platformsOf(all.platforms)
159
+ const { manifest, partial } = flagsOf(all)
160
+ const host = checkHost(all.host)
161
+ const folded = host.os === 'darwin'
162
+ // 1.13 to 1.15 copy a download into their cache with FileUtils.cp_r,
163
+ // which copies the clone's .git; 1.16 on, with rsync, which leaves it out.
164
+ const keepsGit = Number(host.cocoapods.split('.')[1]) < 16
165
+ // Refused before anything is fetched; mount checks again.
166
+ if (into !== undefined) checkNoPods(into, folded)
167
+ const lock = await parsePodfileLock(inputs.lockfile, inputs.podfile === undefined ? {} : { podfile: inputs.podfile })
168
+ const roots = Object.values(lock.roots)
169
+ const skipped = partial ? new Map() : undefined
170
+ const fetched = roots.filter((root) => unlessSkipped(() => isDownloaded(root, folded), root, skipped))
171
+ if (github === undefined && fetched.length > 0) throw new TypeError(GITHUB)
172
+ const plans = await planAll(fetched, { github, podspecs: inputs.podspecs, platforms }, skipped)
173
+ const contents = await installAll(plans, github, { folded, keepsGit })
174
+ // In the lockfile's order, whatever order the fetches finished in.
175
+ const downloaded = fetched.filter(({ name }) => plans.has(name))
176
+ const vfs = new Vfs()
177
+ const stats = { pods: roots.length, downloaded: downloaded.length, files: 0, bytes: 0, links: 0 }
178
+ if (downloaded.length > 0) makeDirs(vfs, 'Pods')
179
+ for (const { name } of downloaded) writeFiles(vfs, `Pods/${name}`, contents.get(name), stats)
180
+ if (manifest) writeManifest(vfs, inputs.lockfile, stats)
181
+ const installed = downloaded.map((root) => installedOf(root, plans.get(root.name)))
182
+ const left = roots.filter(({ name }) => skipped?.has(name)).map(({ name, version }) => ({ name, version, reason: skipped.get(name) }))
183
+ return { vfs: mount(vfs, into, folded, checkNoPods), stats, installed, skipped: left }
184
+ }
@@ -0,0 +1,17 @@
1
+ // Characters whose case Ruby's String#downcase and #upcase map otherwise
2
+ // in one release than another, and than String#toLowerCase and
3
+ // #toUpperCase here: cased letters encoded after Unicode 12.1, which Ruby
4
+ // 2.6 reads, and older ones that map to them. Each is refused where
5
+ // CocoaPods compares a name by its case, as which release reads it is not
6
+ // known. Made from Unicode 17's DerivedAge.txt, the code points Node maps
7
+ // to another case whose age, or that of what they map to, is past 12.1;
8
+ // they take in every code point Ruby 3.3 maps otherwise than Node, but
9
+ // for the final sigma, which clean.js reads as Ruby does.
10
+ export const CASE_UNSTABLE = new RegExp(`[${[
11
+ String.raw`\u{19B}\u{264}\u{1C89}-\u{1C8A}\u{2C2F}\u{2C5F}\u{A7C0}-\u{A7C1}`,
12
+ String.raw`\u{A7C7}-\u{A7DC}\u{A7F5}-\u{A7F6}\u{10570}-\u{1057A}`,
13
+ String.raw`\u{1057C}-\u{1058A}\u{1058C}-\u{10592}\u{10594}-\u{10595}`,
14
+ String.raw`\u{10597}-\u{105A1}\u{105A3}-\u{105B1}\u{105B3}-\u{105B9}`,
15
+ String.raw`\u{105BB}-\u{105BC}\u{10D50}-\u{10D65}\u{10D70}-\u{10D85}`,
16
+ String.raw`\u{16EA0}-\u{16EB8}\u{16EBB}-\u{16ED3}`,
17
+ ].join('')}]`, 'u')