Skip to content

Commit

Permalink
fix(plugins): ensure stateless npm plugin (#845)
Browse files Browse the repository at this point in the history
Work around an issue that regressed NPM-based projects
on the Launchpad auto-build system. The issue was that the
Launchpad auto-build system executed pull and build steps
separately, thereby losing the shared state between them.
  • Loading branch information
liushuyu authored Sep 13, 2024
1 parent e80504c commit fab2ecc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
37 changes: 19 additions & 18 deletions craft_parts/plugins/npm_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ def get_build_environment(self) -> Dict[str, str]:
@override
def get_pull_commands(self) -> List[str]:
"""Return a list of commands to run during the pull step."""
return []

@override
def get_build_commands(self) -> List[str]:
"""Return a list of commands to run during the build step."""
cmd = []
options = cast(NpmPluginProperties, self._options)
if options.npm_include_node:
arch = self._get_architecture()
Expand All @@ -291,25 +297,30 @@ def get_pull_commands(self) -> List[str]:
self._node_binary_path = os.path.join(
self._part_info.part_cache_dir, file_name
)
return [
cmd += [
dedent(
f"""\
if [ ! -f "{self._node_binary_path}" ]; then
mkdir -p "{self._part_info.part_cache_dir}"
curl --retry 5 -s "{checksum_uri}" -o "{self._part_info.part_cache_dir}"/SHASUMS256.txt
curl --retry 5 -s "{node_uri}" -o "{self._node_binary_path}"
fi
cd "{self._part_info.part_cache_dir}"
pushd "{self._part_info.part_cache_dir}"
sha256sum --ignore-missing --strict -c SHASUMS256.txt
popd
"""
)
]
return []

@override
def get_build_commands(self) -> List[str]:
"""Return a list of commands to run during the build step."""
cmd = [
if self._node_binary_path is not None:
cmd += [
dedent(
f"""\
tar -xzf "{self._node_binary_path}" -C "${{CRAFT_PART_INSTALL}}/" \
--no-same-owner --strip-components=1
"""
),
]
cmd += [
dedent(
"""\
NPM_VERSION="$(npm --version)"
Expand All @@ -322,14 +333,4 @@ def get_build_commands(self) -> List[str]:
"""
)
]
if self._node_binary_path is not None:
cmd.insert(
0,
dedent(
f"""\
tar -xzf "{self._node_binary_path}" -C "${{CRAFT_PART_INSTALL}}/" \
--no-same-owner --strip-components=1
"""
),
)
return cmd
11 changes: 5 additions & 6 deletions tests/unit/plugins/test_npm_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,16 @@ def test_get_build_commands_include_node_true(
]
plugin = NpmPlugin(properties=properties, part_info=part_info)

assert plugin.get_pull_commands() == [
assert plugin.get_pull_commands() == []

assert plugin.get_build_commands() == [
f'if [ ! -f "{part_info.part_cache_dir}/node-v20.13.1-linux-x64.tar.gz" ]; then\n'
f' mkdir -p "{part_info.part_cache_dir}"\n'
f' curl --retry 5 -s "https://nodejs.org/dist/v20.13.1/SHASUMS256.txt" -o "{part_info.part_cache_dir}"/SHASUMS256.txt\n'
f' curl --retry 5 -s "https://nodejs.org/dist/v20.13.1/node-v20.13.1-linux-x64.tar.gz" -o "{part_info.part_cache_dir}/node-v20.13.1-linux-x64.tar.gz"\n'
"fi\n"
f'cd "{part_info.part_cache_dir}"\n'
"sha256sum --ignore-missing --strict -c SHASUMS256.txt\n"
]

assert plugin.get_build_commands() == [
f'pushd "{part_info.part_cache_dir}"\n'
"sha256sum --ignore-missing --strict -c SHASUMS256.txt\npopd\n",
f'tar -xzf "{part_info.part_cache_dir}/node-v20.13.1-linux-x64.tar.gz"'
' -C "${CRAFT_PART_INSTALL}/" --no-same-owner '
"--strip-components=1\n",
Expand Down

0 comments on commit fab2ecc

Please sign in to comment.