Skip to content

Commit d78b979

Browse files
threema-danilothreema-gian
authored andcommitted
flatpak-node-generator: Apply patch for lockfile v3 git sources
See flatpak/flatpak-builder-tools#382
1 parent 8b1ae81 commit d78b979

File tree

2 files changed

+75
-62
lines changed
  • packaging/flatpak/flatpak-builder-tools/flatpak_node_generator/providers

2 files changed

+75
-62
lines changed

packaging/flatpak/flatpak-builder-tools/flatpak_node_generator/providers/__init__.py

+9
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://[email protected]:ianstormtaylor/to-camel-case.git
38+
# with https://[email protected]/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(),

packaging/flatpak/flatpak-builder-tools/flatpak_node_generator/providers/npm.py

+66-62
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,79 @@ 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 = urllib.parse.urlparse(source.from_ or source.original)
451+
452+
# https://github.com/npm/hosted-git-info
453+
hosted_git = [
454+
('@github.com', 'github'),
455+
('@bitbucket.org', 'bitbucket'),
456+
('@gitlab.com', 'gitlab'),
457+
('@gist.github.com', 'gist'),
458+
('@git.sr.ht', 'sourcehut'),
459+
]
460+
for domain, shortcut in hosted_git:
461+
if domain in source_url.netloc.lower():
462+
targets.append(
463+
f"{shortcut}:{source_url.path[1:].replace('.git', '')}"
464+
f'#{source_url.fragment}'
465+
)
466+
break
467+
468+
if (
469+
source_url.scheme.startswith(GIT_URL_PREFIX + 'ssh')
470+
and ':' not in source_url.netloc
471+
):
472+
path_match = re.compile(r'^/([^/]+)(.*)').match(source_url.path)
473+
if path_match:
474+
parent, child = path_match.groups()
475+
source_url = source_url._replace(
476+
netloc=f'{source_url.netloc}:{parent}', path=child
477+
)
478+
elif source_url.scheme.startswith(GIT_URL_PREFIX):
479+
targets.append(source_url.geturl()[len(GIT_URL_PREFIX) :])
480+
481+
targets.append(source_url.geturl())
482+
for t in targets:
483+
data[t] = new_version
484+
data[t.replace('#' + source.commit, '')] = new_version
485+
486+
filename = 'package.json'
487+
target = Path('$FLATPAK_BUILDER_BUILDDIR') / prefix / filename
488+
script = textwrap.dedent(script.lstrip('\n')).strip().replace('\n', '')
489+
json_data = json.dumps(data)
490+
patch_commands[lockfile].append(
491+
'jq'
492+
' --arg buildroot "$FLATPAK_BUILDER_BUILDDIR"'
493+
f' --argjson data {shlex.quote(json_data)}'
494+
f' {shlex.quote(script)} {target}'
495+
f' > {target}.new'
496+
)
497+
patch_commands[lockfile].append(f'mv {target}{{.new,}}')
494498

495499
patch_all_commands: List[str] = []
496500
for lockfile in self.all_lockfiles:

0 commit comments

Comments
 (0)