Skip to content

Commit 9e4677e

Browse files
committed
node: support git urls for lockfile v2
1 parent 30db60f commit 9e4677e

9 files changed

Lines changed: 280 additions & 66 deletions

File tree

node/flatpak_node_generator/providers/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'git': {},
1717
'git+http': {'scheme': 'http'},
1818
'git+https': {'scheme': 'https'},
19+
'git+ssh': {'scheme': 'https'},
1920
}
2021

2122

@@ -33,6 +34,14 @@ def parse_git_source(self, version: str, from_: Optional[str] = None) -> GitSour
3334
if not new_url.netloc:
3435
path = new_url.path.split('/')
3536
new_url = new_url._replace(netloc=path[0], path='/'.join(path[1:]))
37+
# Replace https://git@github.com:ianstormtaylor/to-camel-case.git
38+
# wth https://git@github.com/ianstormtaylor/to-camel-case.git
39+
# for git+ssh URLs
40+
if ':' in new_url.netloc:
41+
netloc_split = new_url.netloc.split(':')
42+
new_url = new_url._replace(
43+
netloc=netloc_split[0], path=f'/{netloc_split[1]}{new_url.path}'
44+
)
3645

3746
return GitSource(
3847
original=original_url.geturl(),

node/flatpak_node_generator/providers/npm.py

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ def _process_packages_v2(
130130
integrity=integrity, resolved=info['resolved']
131131
)
132132
elif resolved_url.scheme.startswith('git+'):
133-
raise NotImplementedError(
134-
'Git sources in lockfile v2 format are not supported yet'
135-
f' (package {install_path} in {lockfile})'
136-
)
133+
source = self.parse_git_source(info['resolved'])
137134
else:
138135
raise NotImplementedError(
139136
f"Don't know how to handle package {install_path} in {lockfile}"
@@ -425,72 +422,58 @@ def _finalize(self) -> None:
425422
)
426423

427424
if self.git_sources:
428-
# Generate jq scripts to patch the package*.json files.
429-
scripts = {
430-
'package.json': r"""
431-
walk(
432-
if type == "object"
433-
then
434-
to_entries | map(
435-
if (.value | type == "string") and $data[.value]
436-
then .value = "git+file:\($buildroot)/\($data[.value])"
437-
else .
438-
end
439-
) | from_entries
440-
else .
441-
end
442-
)
443-
""",
444-
'package-lock.json': r"""
445-
walk(
446-
if type == "object" and (.version | type == "string") and $data[.version]
447-
then
448-
.version = "git+file:\($buildroot)/\($data[.version])"
449-
else .
450-
end
451-
)
452-
""",
453-
}
425+
# Generate jq script to patch the package.json file
426+
script = r"""
427+
walk(
428+
if type == "object"
429+
then
430+
to_entries | map(
431+
if (.value | type == "string") and $data[.value]
432+
then .value = "git+file:\($buildroot)/\($data[.value])"
433+
else .
434+
end
435+
) | from_entries
436+
else .
437+
end
438+
)
439+
"""
454440

455441
for lockfile, sources in self.git_sources.items():
456442
prefix = self.relative_lockfile_dir(lockfile)
457-
data: Dict[str, Dict[str, str]] = {
458-
'package.json': {},
459-
'package-lock.json': {},
460-
}
443+
data: Dict[str, str] = {}
461444

462445
for path, source in sources.items():
463446
GIT_URL_PREFIX = 'git+'
464447

465448
new_version = f'{path}#{source.commit}'
466-
assert source.from_ is not None
467-
data['package.json'][source.from_] = new_version
468-
data['package-lock.json'][source.original] = new_version
469-
470-
if source.from_.startswith(GIT_URL_PREFIX):
471-
data['package.json'][
472-
source.from_[len(GIT_URL_PREFIX) :]
473-
] = new_version
474-
475-
if source.original.startswith(GIT_URL_PREFIX):
476-
data['package-lock.json'][
477-
source.original[len(GIT_URL_PREFIX) :]
478-
] = new_version
479-
480-
for filename, script in scripts.items():
481-
target = Path('$FLATPAK_BUILDER_BUILDDIR') / prefix / filename
482-
script = (
483-
textwrap.dedent(script.lstrip('\n')).strip().replace('\n', '')
484-
)
485-
json_data = json.dumps(data[filename])
486-
patch_commands[lockfile].append(
487-
'jq'
488-
' --arg buildroot "$FLATPAK_BUILDER_BUILDDIR"'
489-
f' --argjson data {shlex.quote(json_data)}'
490-
f' {shlex.quote(script)} {target}'
491-
f' > {target}.new'
492-
)
493-
patch_commands[lockfile].append(f'mv {target}{{.new,}}')
449+
targets = []
450+
source_url = source.from_ or source.original
451+
452+
if (
453+
source_url.startswith(GIT_URL_PREFIX + 'ssh')
454+
and ':' not in urllib.parse.urlparse(source_url).netloc
455+
):
456+
source_url = re.sub(r'(://[^/]+)/', r'\1:', source_url)
457+
elif source_url.startswith(GIT_URL_PREFIX):
458+
targets.append(source_url[len(GIT_URL_PREFIX) :])
459+
460+
targets.append(source_url)
461+
for t in targets:
462+
data[t] = new_version
463+
data[t.replace('#' + source.commit, '')] = new_version
464+
465+
filename = 'package.json'
466+
target = Path('$FLATPAK_BUILDER_BUILDDIR') / prefix / filename
467+
script = textwrap.dedent(script.lstrip('\n')).strip().replace('\n', '')
468+
json_data = json.dumps(data)
469+
patch_commands[lockfile].append(
470+
'jq'
471+
' --arg buildroot "$FLATPAK_BUILDER_BUILDDIR"'
472+
f' --argjson data {shlex.quote(json_data)}'
473+
f' {shlex.quote(script)} {target}'
474+
f' > {target}.new'
475+
)
476+
patch_commands[lockfile].append(f'mv {target}{{.new,}}')
494477

495478
patch_all_commands: List[str] = []
496479
for lockfile in self.all_lockfiles:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@flatpak-node-generator-tests/git",
3+
"version": "1.0.0",
4+
"lockfileVersion": 1,
5+
"requires": true,
6+
"dependencies": {
7+
"to-camel-case": {
8+
"version": "git+ssh://git@github.com/ianstormtaylor/to-camel-case.git#00a20429b600ddb6e4f8ff5b17c52914f40fe67d",
9+
"from": "git+ssh://git@github.com/ianstormtaylor/to-camel-case.git",
10+
"requires": {
11+
"to-space-case": "^1.0.0"
12+
}
13+
},
14+
"to-capital-case": {
15+
"version": "git+https://git@github.com/ianstormtaylor/to-capital-case.git#b82f61e00e099b01514e25177bb2d56d0f64b157",
16+
"from": "git+https://git@github.com/ianstormtaylor/to-capital-case.git",
17+
"requires": {
18+
"to-space-case": "^1.0.0"
19+
}
20+
},
21+
"to-no-case": {
22+
"version": "git+https://git@github.com/ianstormtaylor/to-no-case.git#9078578dcf394e63f34fd7c6666772192e537b90",
23+
"from": "git+https://git@github.com/ianstormtaylor/to-no-case.git#9078578dcf394e63f34fd7c6666772192e537b90"
24+
},
25+
"to-space-case": {
26+
"version": "git+ssh://git@github.com/ianstormtaylor/to-space-case.git#aa68213d1211745ce7c6c725ba072e6b13bef640",
27+
"from": "git+ssh://git@github.com/ianstormtaylor/to-space-case.git#aa68213d1211745ce7c6c725ba072e6b13bef640",
28+
"requires": {
29+
"to-no-case": "^1.0.0"
30+
}
31+
}
32+
}
33+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"name": "@flatpak-node-generator-tests/git",
3+
"version": "1.0.0",
4+
"lockfileVersion": 2,
5+
"requires": true,
6+
"packages": {
7+
"": {
8+
"name": "@flatpak-node-generator-tests/git",
9+
"version": "1.0.0",
10+
"dependencies": {
11+
"to-camel-case": "git+ssh://git@github.com:ianstormtaylor/to-camel-case.git",
12+
"to-capital-case": "git+https://git@github.com/ianstormtaylor/to-capital-case.git",
13+
"to-no-case": "git+https://git@github.com/ianstormtaylor/to-no-case.git#9078578dcf394e63f34fd7c6666772192e537b90",
14+
"to-space-case": "git+ssh://git@github.com:ianstormtaylor/to-space-case.git#aa68213d1211745ce7c6c725ba072e6b13bef640"
15+
}
16+
},
17+
"node_modules/to-camel-case": {
18+
"version": "1.0.0",
19+
"resolved": "git+ssh://git@github.com/ianstormtaylor/to-camel-case.git#00a20429b600ddb6e4f8ff5b17c52914f40fe67d",
20+
"license": "MIT",
21+
"dependencies": {
22+
"to-space-case": "^1.0.0"
23+
}
24+
},
25+
"node_modules/to-capital-case": {
26+
"version": "1.0.0",
27+
"resolved": "git+https://git@github.com/ianstormtaylor/to-capital-case.git#b82f61e00e099b01514e25177bb2d56d0f64b157",
28+
"license": "MIT",
29+
"dependencies": {
30+
"to-space-case": "^1.0.0"
31+
}
32+
},
33+
"node_modules/to-no-case": {
34+
"version": "1.0.0",
35+
"resolved": "git+https://git@github.com/ianstormtaylor/to-no-case.git#9078578dcf394e63f34fd7c6666772192e537b90",
36+
"integrity": "sha512-E6uMYyFvmgQWszuxWtCz0NN+BgdVvQjVCU6Nnq0BSxi8q/QKRCBs14pGUWbPQghmpjJbdkKgjICDWihePrJGOQ==",
37+
"license": "MIT"
38+
},
39+
"node_modules/to-space-case": {
40+
"version": "1.0.0",
41+
"resolved": "git+ssh://git@github.com/ianstormtaylor/to-space-case.git#aa68213d1211745ce7c6c725ba072e6b13bef640",
42+
"integrity": "sha512-i7ZtWv1WzR6xrLpd5wpSU/RZxoEDaoD3uLU5pDgXEZfUZJh0QpPtjJdLf7i+nr24Z80ls8qYCfBeFpZk6RzXKQ==",
43+
"license": "MIT",
44+
"dependencies": {
45+
"to-no-case": "^1.0.0"
46+
}
47+
}
48+
},
49+
"dependencies": {
50+
"to-camel-case": {
51+
"version": "git+ssh://git@github.com/ianstormtaylor/to-camel-case.git#00a20429b600ddb6e4f8ff5b17c52914f40fe67d",
52+
"from": "to-camel-case@git+ssh://git@github.com:ianstormtaylor/to-camel-case.git",
53+
"requires": {
54+
"to-space-case": "^1.0.0"
55+
}
56+
},
57+
"to-capital-case": {
58+
"version": "git+https://git@github.com/ianstormtaylor/to-capital-case.git#b82f61e00e099b01514e25177bb2d56d0f64b157",
59+
"from": "to-capital-case@git+https://git@github.com/ianstormtaylor/to-capital-case.git",
60+
"requires": {
61+
"to-space-case": "^1.0.0"
62+
}
63+
},
64+
"to-no-case": {
65+
"version": "git+https://git@github.com/ianstormtaylor/to-no-case.git#9078578dcf394e63f34fd7c6666772192e537b90",
66+
"integrity": "sha512-E6uMYyFvmgQWszuxWtCz0NN+BgdVvQjVCU6Nnq0BSxi8q/QKRCBs14pGUWbPQghmpjJbdkKgjICDWihePrJGOQ==",
67+
"from": "to-no-case@git+https://git@github.com/ianstormtaylor/to-no-case.git#9078578dcf394e63f34fd7c6666772192e537b90"
68+
},
69+
"to-space-case": {
70+
"version": "git+ssh://git@github.com/ianstormtaylor/to-space-case.git#aa68213d1211745ce7c6c725ba072e6b13bef640",
71+
"integrity": "sha512-i7ZtWv1WzR6xrLpd5wpSU/RZxoEDaoD3uLU5pDgXEZfUZJh0QpPtjJdLf7i+nr24Z80ls8qYCfBeFpZk6RzXKQ==",
72+
"from": "to-space-case@git+ssh://git@github.com:ianstormtaylor/to-space-case.git#aa68213d1211745ce7c6c725ba072e6b13bef640",
73+
"requires": {
74+
"to-no-case": "^1.0.0"
75+
}
76+
}
77+
}
78+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "@flatpak-node-generator-tests/git",
3+
"version": "1.0.0",
4+
"lockfileVersion": 3,
5+
"requires": true,
6+
"packages": {
7+
"": {
8+
"name": "@flatpak-node-generator-tests/git",
9+
"version": "1.0.0",
10+
"dependencies": {
11+
"to-camel-case": "git+ssh://git@github.com:ianstormtaylor/to-camel-case.git",
12+
"to-capital-case": "git+https://git@github.com/ianstormtaylor/to-capital-case.git",
13+
"to-no-case": "git+https://git@github.com/ianstormtaylor/to-no-case.git#9078578dcf394e63f34fd7c6666772192e537b90",
14+
"to-space-case": "git+ssh://git@github.com:ianstormtaylor/to-space-case.git#aa68213d1211745ce7c6c725ba072e6b13bef640"
15+
}
16+
},
17+
"node_modules/to-camel-case": {
18+
"version": "1.0.0",
19+
"resolved": "git+ssh://git@github.com/ianstormtaylor/to-camel-case.git#00a20429b600ddb6e4f8ff5b17c52914f40fe67d",
20+
"license": "MIT",
21+
"dependencies": {
22+
"to-space-case": "^1.0.0"
23+
}
24+
},
25+
"node_modules/to-capital-case": {
26+
"version": "1.0.0",
27+
"resolved": "git+https://git@github.com/ianstormtaylor/to-capital-case.git#b82f61e00e099b01514e25177bb2d56d0f64b157",
28+
"license": "MIT",
29+
"dependencies": {
30+
"to-space-case": "^1.0.0"
31+
}
32+
},
33+
"node_modules/to-no-case": {
34+
"version": "1.0.0",
35+
"resolved": "git+https://git@github.com/ianstormtaylor/to-no-case.git#9078578dcf394e63f34fd7c6666772192e537b90",
36+
"integrity": "sha512-E6uMYyFvmgQWszuxWtCz0NN+BgdVvQjVCU6Nnq0BSxi8q/QKRCBs14pGUWbPQghmpjJbdkKgjICDWihePrJGOQ==",
37+
"license": "MIT"
38+
},
39+
"node_modules/to-space-case": {
40+
"version": "1.0.0",
41+
"resolved": "git+ssh://git@github.com/ianstormtaylor/to-space-case.git#aa68213d1211745ce7c6c725ba072e6b13bef640",
42+
"integrity": "sha512-i7ZtWv1WzR6xrLpd5wpSU/RZxoEDaoD3uLU5pDgXEZfUZJh0QpPtjJdLf7i+nr24Z80ls8qYCfBeFpZk6RzXKQ==",
43+
"license": "MIT",
44+
"dependencies": {
45+
"to-no-case": "^1.0.0"
46+
}
47+
}
48+
}
49+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "@flatpak-node-generator-tests/git",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"to-camel-case": "git+ssh://git@github.com:ianstormtaylor/to-camel-case.git",
6+
"to-capital-case": "git+https://git@github.com/ianstormtaylor/to-capital-case.git",
7+
"to-no-case": "git+https://git@github.com/ianstormtaylor/to-no-case.git#9078578dcf394e63f34fd7c6666772192e537b90",
8+
"to-space-case": "git+ssh://git@github.com:ianstormtaylor/to-space-case.git#aa68213d1211745ce7c6c725ba072e6b13bef640"
9+
}
10+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"to-camel-case@git+ssh://git@github.com:ianstormtaylor/to-camel-case.git":
6+
version "1.0.0"
7+
resolved "git+ssh://git@github.com:ianstormtaylor/to-camel-case.git#00a20429b600ddb6e4f8ff5b17c52914f40fe67d"
8+
dependencies:
9+
to-space-case "^1.0.0"
10+
11+
"to-capital-case@git+https://git@github.com/ianstormtaylor/to-capital-case.git":
12+
version "1.0.0"
13+
resolved "git+https://git@github.com/ianstormtaylor/to-capital-case.git#b82f61e00e099b01514e25177bb2d56d0f64b157"
14+
dependencies:
15+
to-space-case "^1.0.0"
16+
17+
to-no-case@^1.0.0:
18+
version "1.0.2"
19+
resolved "https://registry.yarnpkg.com/to-no-case/-/to-no-case-1.0.2.tgz#c722907164ef6b178132c8e69930212d1b4aa16a"
20+
integrity sha512-Z3g735FxuZY8rodxV4gH7LxClE4H0hTIyHNIHdk+vpQxjLm0cwnKXq/OFVZ76SOQmto7txVcwSCwkU5kqp+FKg==
21+
22+
"to-no-case@git+https://git@github.com/ianstormtaylor/to-no-case.git#9078578dcf394e63f34fd7c6666772192e537b90":
23+
version "1.0.0"
24+
resolved "git+https://git@github.com/ianstormtaylor/to-no-case.git#9078578dcf394e63f34fd7c6666772192e537b90"
25+
26+
to-space-case@^1.0.0:
27+
version "1.0.0"
28+
resolved "https://registry.yarnpkg.com/to-space-case/-/to-space-case-1.0.0.tgz#b052daafb1b2b29dc770cea0163e5ec0ebc9fc17"
29+
integrity sha512-rLdvwXZ39VOn1IxGL3V6ZstoTbwLRckQmn/U8ZDLuWwIXNpuZDhQ3AiRUlhTbOXFVE9C+dR51wM0CBDhk31VcA==
30+
dependencies:
31+
to-no-case "^1.0.0"
32+
33+
"to-space-case@git+ssh://git@github.com:ianstormtaylor/to-space-case.git#aa68213d1211745ce7c6c725ba072e6b13bef640":
34+
version "1.0.0"
35+
resolved "git+ssh://git@github.com:ianstormtaylor/to-space-case.git#aa68213d1211745ce7c6c725ba072e6b13bef640"
36+
dependencies:
37+
to-no-case "^1.0.0"

node/tests/data/packages/minimal-git/package-lock.v3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"node_modules/nop": {
1515
"version": "1.0.0",
16-
"resolved": "git+ssh://git@github.com/supershabam/nop.git#f110e75f62cfe3bf4468ac3b74e3dc72ab9ae4bf",
16+
"resolved": "git+https://git@github.com/supershabam/nop.git#f110e75f62cfe3bf4468ac3b74e3dc72ab9ae4bf",
1717
"integrity": "sha512-xCwdA7C4QIORvTMytKHMlkEN6axJGimR0gv5vgjKpEKRvQrPOwhjJnrZEcd5g0LP+7IY38+TY7MP59HRY6gcwA==",
1818
"license": "MIT"
1919
}

0 commit comments

Comments
 (0)