Skip to content

Commit

Permalink
chore: clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
yanksyoon committed Jan 27, 2025
1 parent 9c2dfb9 commit 7f9aeb7
Show file tree
Hide file tree
Showing 15 changed files with 340 additions and 415 deletions.
58 changes: 26 additions & 32 deletions docs/explanation/extensions/expressjs-framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,6 @@ Ubuntu base and Node combinations
In this section of the document, we will discuss the possible combinations of
Ubuntu bases and possible usages of NPM plugin.

24.04 base and Node from NPM plugin
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following example uses the Ubuntu 24.04 base and Node from the NPM plugin.

.. code-block:: yaml
base: [email protected]
extensions:
- expressjs-framework
parts:
expressjs-framework/install-app:
npm-include-node: true
npm-node-version: 20.12
In this case, the application is installed and run via Node and NPM installed by
the NPM plugin.

24.04 base and Node from Ubuntu packages
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -43,9 +25,6 @@ The following example uses the Ubuntu 24.04 base and Node from Ubuntu packages.
base: [email protected]
extensions:
- expressjs-framework
parts:
expressjs-framework/install-app:
npm-include-node: false
In this case, the application is installed and run via Node and NPM installed by
the Ubuntu packages. The NPM and Node versions are determined by the versions of
Expand All @@ -55,24 +34,23 @@ the corressponding Ubuntu base from the chilsel-slices repository. This
blob/ubuntu-24.04/slices/nodejs.yaml) is an example of the NodeJS version
shipped with the Ubuntu 24.04 base.

Bare base and Node from NPM plugin
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24.04 base and Node from NPM plugin
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following example uses the bare base and Node from the NPM plugin.
The following example uses the Ubuntu 24.04 base and Node from the NPM plugin.

.. code-block:: yaml
base: bare
build-base: [email protected]
base: [email protected]
extensions:
- expressjs-framework
parts:
expressjs-framework/install-app:
npm-include-node: true
npm-node-version: 20.12
In this case, the application is installed and run via Node and NPM installed by
the NPM plugin. For different possible inputs for npm-node-version, refer to the
[NPM plugin documentation](https://documentation.ubuntu.com/rockcraft/en/\
latest/common/craft-parts/reference/plugins/npm_plugin).
the NPM plugin.

Bare base and Node from Ubuntu packages
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -83,9 +61,6 @@ The following example uses the bare base and Node from Ubuntu packages.
base: bare
build-base: [email protected]
parts:
expressjs-framework/install-app:
npm-include-node: false
In this case, the application is installed and run via Node and NPM installed by
the Ubuntu packages. The NPM and Node versions are determined by the versions of
Expand All @@ -98,3 +73,22 @@ shipped with the Ubuntu 24.04 base.
See the NPM version shipped with the corressponding Ubuntu base from the Ubuntu
packages archive from the [Ubuntu packages search](https://packages.ubuntu.com/\
search?suite=default&section=all&arch=any&keywords=npm&searchon=names)

Bare base and Node from NPM plugin
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following example uses the bare base and Node from the NPM plugin.

.. code-block:: yaml
base: bare
build-base: [email protected]
parts:
expressjs-framework/install-app:
npm-include-node: true
npm-node-version: 20.12
In this case, the application is installed and run via Node and NPM installed by
the NPM plugin. For different possible inputs for npm-node-version, refer to the
[NPM plugin documentation](https://documentation.ubuntu.com/rockcraft/en/\
latest/common/craft-parts/reference/plugins/npm_plugin).
16 changes: 0 additions & 16 deletions docs/tutorial/code/expressjs/package.json

This file was deleted.

69 changes: 44 additions & 25 deletions rockcraft/extensions/expressjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,6 @@ def is_experimental(base: str | None) -> bool:
"""Check if the extension is in an experimental state."""
return True

@override
def get_part_snippet(self) -> dict[str, Any]:
"""Return the part snippet to apply to existing parts.
This is unused but is required by the ABC.
"""
return {}

@override
def get_parts_snippet(self) -> dict[str, Any]:
"""Return the parts to add to parts.
This is unused but is required by the ABC.
"""
return {}

@override
def get_root_snippet(self) -> dict[str, Any]:
"""Fill in some default root components.
Expand Down Expand Up @@ -96,6 +80,22 @@ def get_root_snippet(self) -> dict[str, Any]:
snippet["parts"]["expressjs-framework/runtime"] = runtime_part
return snippet

@override
def get_part_snippet(self) -> dict[str, Any]:
"""Return the part snippet to apply to existing parts.
This is unused but is required by the ABC.
"""
return {}

@override
def get_parts_snippet(self) -> dict[str, Any]:
"""Return the parts to add to parts.
This is unused but is required by the ABC.
"""
return {}

def _check_project(self) -> None:
"""Ensure this extension can apply to the current rockcraft project.
Expand All @@ -112,15 +112,22 @@ def _check_project(self) -> None:
doc_slug="/reference/extensions/expressjs-framework",
logpath_report=False,
)
if "name" not in self._app_package_json:
if "name" not in self._app_package_json or not isinstance(
self._app_package_json["name"], str
):
raise ExtensionError(
"missing application name",
doc_slug="/reference/extensions/expressjs-framework",
logpath_report=False,
)

def _gen_install_app_part(self) -> dict:
"""Generate the install app part using NPM plugin."""
"""Generate the install app part using NPM plugin.
Set the script shell to bash and copy the .npmrc file to the app
directory. This is to ensure that the ExpressJS run in bare container
can use the shell to launch itself.
"""
install_app_part: dict[str, Any] = {
"plugin": "npm",
"source": f"{self.IMAGE_BASE_DIR}/",
Expand All @@ -132,10 +139,10 @@ def _gen_install_app_part(self) -> dict:
f"ln -s /lib/node_modules/{self._app_name} ${{CRAFT_PART_INSTALL}}/app\n"
),
}
build_packages = self._install_app_build_packages
build_packages = self._gen_app_build_packages()
if build_packages:
install_app_part["build-packages"] = build_packages
stage_packages = self._install_app_stage_packages
stage_packages = self._gen_app_stage_packages()
if stage_packages:
install_app_part["stage-packages"] = stage_packages
if self._user_npm_include_node:
Expand All @@ -145,15 +152,13 @@ def _gen_install_app_part(self) -> dict:
)
return install_app_part

@property
def _install_app_build_packages(self) -> list[str]:
def _gen_app_build_packages(self) -> list[str]:
"""Return the build packages for the install app part."""
if self._user_npm_include_node:
return []
return ["nodejs", "npm"]

@property
def _install_app_stage_packages(self) -> list[str]:
def _gen_app_stage_packages(self) -> list[str]:
"""Return the stage packages for the install app part."""
if self._rock_base == "bare":
return [
Expand Down Expand Up @@ -203,7 +208,21 @@ def _app_package_json(self) -> dict:
logpath_report=False,
)
package_json_contents = package_json_file.read_text(encoding="utf-8")
return json.loads(package_json_contents)
try:
app_package_json = json.loads(package_json_contents)
if not isinstance(app_package_json, dict):
raise ExtensionError(
"invalid package.json file",
doc_slug="/reference/extensions/expressjs-framework",
logpath_report=False,
)
return app_package_json
except json.JSONDecodeError as exc:
raise ExtensionError(
"failed to parse package.json file",
doc_slug="/reference/extensions/expressjs-framework",
logpath_report=False,
) from exc

@property
def _app_name(self) -> str:
Expand Down
1 change: 0 additions & 1 deletion rockcraft/templates/expressjs-framework/rockcraft.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: {{name}}
# see {{versioned_url}}/explanation/bases/
# for more information about bases and using 'bare' bases for chiselled rocks
base: [email protected] # as an alternative, a 'bare' base can be used
build-base: [email protected] # build-base is required when the base is bare
version: '0.1' # just for humans. Semantic versioning is recommended
summary: A summary of your ExpresssJS application # 79 char long summary
description: |
Expand Down
41 changes: 0 additions & 41 deletions tests/spread/rockcraft/extension-expressjs/app/app.js

This file was deleted.

90 changes: 0 additions & 90 deletions tests/spread/rockcraft/extension-expressjs/app/bin/www

This file was deleted.

16 changes: 0 additions & 16 deletions tests/spread/rockcraft/extension-expressjs/app/package.json

This file was deleted.

Loading

0 comments on commit 7f9aeb7

Please sign in to comment.