DEV Community

ZHZL-m
ZHZL-m

Posted on

【Journey of HarmonyOS Next】DevEco Studio User Guide (32) -> Build the system lifecycle

Image description

1 -> Engineering Structure Definition

Hvigor parses the project into a tree structure, the project is the root node of the tree, each module in the project is the leaf node of the tree, the tree is up to two layers, the module cannot contain other modules, and the project or module is collectively referred to as a node in the definition of Hvigor.

Image description

At the beginning of the initial construction phase, a tree structure will be constructed to store the project structure of the project through the configuration in the project-level build-profile.json5 file, and the project-level build-profile.json5 file and the hvigorconfig.ts file can be configured with multiple modules.

2 -> Hvigor script file

In the lifecycle of the build, Hvigor uses two script files to complete the registration of plugins, tasks, and lifecycle hooks:

hvigorconfig.ts: This file only exists in the root directory in the whole project, it is not a necessary file for construction and does not exist by default, you can create it yourself if necessary, this file is parsed and executed earlier, and can be used to manipulate some data at the beginning of the Hvigor life cycle.
hvigorfile.ts: This file is a copy under each node, which is a necessary file for the build, and can be used to register operations such as plugins, tasks, and lifecycle hooks.

3 -> Task and task dependency graphs

Hvigor is an automated construction of a project based on tasks, and a task is the basic unit of work in the Hvigor build process, which defines the specific work that needs to be performed when building a project. Tasks can complete a variety of operations, such as source code compilation tasks, packaging tasks, or signing tasks. The execution logic of each task is provided by the plugin, which can be the default task logic provided by the hvigor-ohos-plugin, or you can customize it.

It should be noted that there are dependencies on tasks, Hvigor will build a task dependency graph before executing any task, and all tasks will form a directed acyclic graph (DAG), as shown in the following example diagram, the dependencies between tasks are represented by arrows:

Image description

Both the hvigor-ohos-plugin and the build scripts in the hvigorfile.ts file will affect the task dependency graph through the task dependency mechanism.

4 -> hvigor-ohos-plugin

hvigor-ohos-plugin is the default build plug-in, which provides business logic support for the completion of tasks, such as providing Hap, Har and Hsp packaging services for Hvigor, and the specific execution logic of each task is provided by different plug-ins in this module.

4.1 -> Relationship between Hvigor and hvigor-ohos-plugin

Hvigor provides task management mechanisms such as task registration and configuration management, which is responsible for controlling the task execution process, but does not include the specific business logic of each task, which is provided by hvigor-ohos-plugin.

The relationship between Hvigor and hvigor-ohos-plugin can be illustrated by the following figure: Hvigor accepts the registration of tasks, arranges the order of task execution, and calls the plug-ins in hvigor-ohos-plugin in order to execute the tasks. If you customize your own task logic plugin and register it, hvigor-ohos-plugin will also call your personalized plugin to complete the compilation and build process.

In the process of Hvigor executing the build, hvigor-ohos-plugin will register the task with Hvigor, and Hvigor will call the corresponding plug-in to execute the corresponding task according to the built task execution directed graph, and after completing a series of tasks such as compilation, packaging, and signing, Hvigor will officially complete the build.

Image description

5-> Hvigor life cycle

The lifecycle shows how the Hvigor build system can go through a complete build process. Hvigor's compilation and build process has three distinct phases, which are initialized, configured, and executed, and Hvigor runs these stages sequentially.

5.1 -> Initialization

illustrate

The main purpose of this phase is to initialize the compilation parameters of the project and construct a tree data model of the project structure, with each node being a HvigorNode object.

Set the build parameters of Hvigor according to the command parameters and the configuration in the hvigor-config.json5 file, and construct the hvigor object and hvigorConfig object:
hvigor objects are destroyed throughout the hvigor lifecycle, from the very beginning of creation to the end of the build;
The hvigorConfig object is used to represent hvigor's abstraction of the project structure, is a simple configuration object of hvigor that is used to dynamically add or remove nodes, and it also holds a description object for each node (nodeDescriptor object).
Create a rootNodeDescriptor instance from the build-profile.json5 file in the root directory of the project, and initialize the NodeDescriptor object instances of all modules in the project through the modules field.
Execute a hvigorconfig.ts file in the root directory of the project, where you can register hooks for the lifecycle through hvigor's APIs or perform other processes at the start of the build.
Based on the node description object, an instance of the HvigorNode object for each node is constructed.

5.2 -> Configuration

illustrate

At the beginning of this phase, all nodes are loaded, but there are no plugins, tasks, and DAGs in each node, and the main purpose of this phase is to load these contents.

Execute the hvigorfile.ts files in each node, add the plugin for each node (register the task with Hvigor), execute the apply method of the plugin, and add the context of the plugin.
Based on the plugin and task loaded in the previous step, a DAG graph is constructed based on the dependencies of the task execution.

5.3 -> Execution

Execute the selected task.
Dependencies between tasks determine the order in which tasks are executed.
Tasks can be executed in parallel.

5.4 -> Lifecycle and hook points

In the lifecycle of Hvigor, the following multiple hook points are available for you to call certain logic at the corresponding time.

In the image below, all the wireframes marked in green are hook points that can be used.

Image description

Top comments (0)