Skip to content

Commit

Permalink
download
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai Stoichitescu committed May 31, 2022
1 parent 378987e commit 853ffc1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 51 deletions.
78 changes: 37 additions & 41 deletions js/private/npm_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -210,54 +210,50 @@ _EXTRACT_TO_DIRNAME = "package"
_LINK_JS_PACKAGE_BZL_FILENAME = "link_js_package.bzl"

def _impl(rctx):
# Quick hack so we get our packages from the sources and not through the wire
if "/.pnpm/" in rctx.attr.integrity:
rctx.symlink(rctx.attr.integrity, _EXTRACT_TO_DIRNAME)
else:
numeric_version = pnpm_utils.strip_peer_dep_version(rctx.attr.version)

rctx.download(
output = _TARBALL_FILENAME,
url = "https://registry.npmjs.org/{0}/-/{1}-{2}.tgz".format(
rctx.attr.package,
# scoped packages contain a slash in the name, which doesn't appear in the later part of the URL
rctx.attr.package.split("/")[-1],
numeric_version,
),
integrity = rctx.attr.integrity,
)
numeric_version = pnpm_utils.strip_peer_dep_version(rctx.attr.version)

rctx.download(
output = _TARBALL_FILENAME,
url = "https://registry.npmjs.org/{0}/-/{1}-{2}.tgz".format(
rctx.attr.package,
# scoped packages contain a slash in the name, which doesn't appear in the later part of the URL
rctx.attr.package.split("/")[-1],
numeric_version,
),
integrity = rctx.attr.integrity,
)

mkdir_args = ["mkdir", "-p", _EXTRACT_TO_DIRNAME] if not repo_utils.is_windows(rctx) else ["cmd", "/c", "if not exist {extract_to_dirname} (mkdir {extract_to_dirname})".format(_EXTRACT_TO_DIRNAME = _EXTRACT_TO_DIRNAME.replace("/", "\\"))]
result = rctx.execute(mkdir_args)
if result.return_code:
msg = "mkdir %s failed: \nSTDOUT:\n%s\nSTDERR:\n%s" % (_EXTRACT_TO_DIRNAME, result.stdout, result.stderr)
fail(msg)
mkdir_args = ["mkdir", "-p", _EXTRACT_TO_DIRNAME] if not repo_utils.is_windows(rctx) else ["cmd", "/c", "if not exist {extract_to_dirname} (mkdir {extract_to_dirname})".format(_EXTRACT_TO_DIRNAME = _EXTRACT_TO_DIRNAME.replace("/", "\\"))]
result = rctx.execute(mkdir_args)
if result.return_code:
msg = "mkdir %s failed: \nSTDOUT:\n%s\nSTDERR:\n%s" % (_EXTRACT_TO_DIRNAME, result.stdout, result.stderr)
fail(msg)

# npm packages are always published with one top-level directory inside the tarball, tho the name is not predictable
# so we use tar here which takes a --strip-components N argument instead of rctx.download_and_extract
untar_args = ["tar", "-xf", _TARBALL_FILENAME, "--strip-components", str(1), "-C", _EXTRACT_TO_DIRNAME]
# npm packages are always published with one top-level directory inside the tarball, tho the name is not predictable
# so we use tar here which takes a --strip-components N argument instead of rctx.download_and_extract
untar_args = ["tar", "-xf", _TARBALL_FILENAME, "--strip-components", str(1), "-C", _EXTRACT_TO_DIRNAME]

if repo_utils.is_linux(rctx):
# Some packages have directory permissions missing the executable bit, which prevents GNU tar from
# extracting files into the directory. Delay permission restoration for directories until all files
# have been extracted. We assume that any linux platform is using GNU tar and has this flag available.
untar_args.append("--delay-directory-restore")
if repo_utils.is_linux(rctx):
# Some packages have directory permissions missing the executable bit, which prevents GNU tar from
# extracting files into the directory. Delay permission restoration for directories until all files
# have been extracted. We assume that any linux platform is using GNU tar and has this flag available.
untar_args.append("--delay-directory-restore")

result = rctx.execute(untar_args)
result = rctx.execute(untar_args)
if result.return_code:
msg = "tar %s failed: \nSTDOUT:\n%s\nSTDERR:\n%s" % (_EXTRACT_TO_DIRNAME, result.stdout, result.stderr)
fail(msg)

if not repo_utils.is_windows(rctx):
# Some packages have directory permissions missing executable which
# make the directories not listable. Fix these cases in order to be able
# to execute the copy action. https://stackoverflow.com/a/14634721
chmod_args = ["chmod", "-R", "a+X", _EXTRACT_TO_DIRNAME]
result = rctx.execute(chmod_args)
if result.return_code:
msg = "tar %s failed: \nSTDOUT:\n%s\nSTDERR:\n%s" % (_EXTRACT_TO_DIRNAME, result.stdout, result.stderr)
msg = "chmod %s failed: \nSTDOUT:\n%s\nSTDERR:\n%s" % (_EXTRACT_TO_DIRNAME, result.stdout, result.stderr)
fail(msg)

if not repo_utils.is_windows(rctx):
# Some packages have directory permissions missing executable which
# make the directories not listable. Fix these cases in order to be able
# to execute the copy action. https://stackoverflow.com/a/14634721
chmod_args = ["chmod", "-R", "a+X", _EXTRACT_TO_DIRNAME]
result = rctx.execute(chmod_args)
if result.return_code:
msg = "chmod %s failed: \nSTDOUT:\n%s\nSTDERR:\n%s" % (_EXTRACT_TO_DIRNAME, result.stdout, result.stderr)
fail(msg)

pkg_json_path = paths.join(_EXTRACT_TO_DIRNAME, "package.json")

pkg_json = json.decode(rctx.read(pkg_json_path))
Expand Down
11 changes: 1 addition & 10 deletions js/private/translate_pnpm_lock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -364,16 +364,7 @@ def link_js_packages():
# Workaround for third party packages
# so they are brought in from the workspace
# instead of being downloaded again.
# integrity = package_info.get("integrity")
integrity = paths.join(
_user_workspace_root(rctx),
"common",
"temp",
"node_modules",
".pnpm",
pnpm_utils.virtual_store_name(name, pnpm_version),
"node_modules",
name)
integrity = package_info.get("integrity")
transitive_closure = package_info.get("transitiveClosure")

if rctx.attr.prod and dev:
Expand Down

0 comments on commit 853ffc1

Please sign in to comment.