DEV Community

HarmonyOS
HarmonyOS

Posted on

Obtaining HAP Package Build Time

Read the original article:Obtaining HAP Package Build Time

Context

This document explains how to retrieve the build time of HAP packages through Hvigor builds.

Description

How to obtain the build time of HAP packages via Hvigor builds?

Hvigor allows developers to implement their own plugins, where developers can define custom build logic and share it with others.
Hvigor primarily provides two approaches for plugin development:

Solution

Manually insert the build time into a custom JSON file within the afterNodeEvaluate callback function and save it to the rawfile directory. The file can later be read. The hvigorfile.ts under the HAP module is as follows:

import { hapTasks } from '@ohos/hvigor-ohos-plugin';
import { hvigor, HvigorNode, HvigorPlugin } from '@ohos/hvigor';
import * as fs from 'fs';
import path from 'path';

export function customPlugin(): HvigorPlugin {
  return {
    pluginId: 'customPlugin',
    async apply(node: HvigorNode): Promise<void> {
      // Callback function after node evaluation
      hvigor.afterNodeEvaluate((hvigorNode) => {
        // Ensure the directory exists
        const resourcesDir = path.join(__dirname, 'src/main/resources/rawfile');
        if (!fs.existsSync(resourcesDir)) {
          fs.mkdirSync(resourcesDir, { recursive: true });
        }
        // Write build time to JSON file
        const now = new Date();
        const year = now.getFullYear();
        const month = String(now.getMonth() + 1).padStart(2, '0');
        const date = String(now.getDate()).padStart(2, '0');
        const hours = String(now.getHours()).padStart(2, '0');
        const minutes = String(now.getMinutes()).padStart(2, '0');
        const seconds = String(now.getSeconds()).padStart(2, '0');
        const buildTime = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
        const buildInfo = { 'buildTime': buildTime };
        fs.writeFileSync(
          path.join(resourcesDir, 'build_hap_info.json'),
          JSON.stringify(buildInfo, null, 2)
        );
      })
    }
  };
};

export default {
  system: hapTasks,
  plugins: [customPlugin()]
}
Enter fullscreen mode Exit fullscreen mode

Code Execution Screenshot

imag1.png

Constraints & Limitations

  • This example supports API Version 19 Release and above.
  • This example supports HarmonyOS 5.1.1 Release SDK and above.
  • This example requires DevEco Studio 5.1.1 Release or later for compilation and execution.

Key Takeaways

  • Build time can be captured using the afterNodeEvaluate callback in Hvigor.
  • The build time can be stored in a JSON file within the rawfile directory for later retrieval.

Reference Link

Written by Taskhyn Maksim

Top comments (0)