
目錄
一、ohpm-repo搭建私倉(cāng)
二、發(fā)布HAR包到ohpm-repo私倉(cāng)
三、發(fā)布成功后HAR包的導(dǎo)入和使用
對(duì)于個(gè)人構(gòu)建的HAR靜態(tài)共享包,可以發(fā)布到官方OpenHarmony三方庫(kù)中心倉(cāng),供他人使用。如果是公司業(yè)務(wù)相關(guān)的庫(kù),并不想被外部人員使用,可以在公司內(nèi)部使用ohpm-repo搭建一個(gè)私有倉(cāng)庫(kù)供內(nèi)部人員使用,一來(lái)比較安全,二來(lái)集成也比較快。
一、ohpm-repo搭建私倉(cāng)
1、node環(huán)境安裝
ohpm-repo 依賴(lài)于 node 運(yùn)行,支持 node.js 18.x 及以上版本,請(qǐng)?zhí)崆鞍惭b nodejs,并完成環(huán)境變量的配置。Node.js安裝請(qǐng)參考Node.js官方網(wǎng)站。
終端輸入以下命令校驗(yàn)node安裝版本 node -v
<git> node -v
v18.15.0
2、官方下載中心下載 ohpm-repo 私倉(cāng)工具包。

3、解壓下載好的ohpm-repo 私倉(cāng)工具包,獲取以下文件目錄:

4、將上圖bin目錄的路徑配置到系統(tǒng)環(huán)境變量path中:
- mac中打開(kāi)環(huán)境變量配置文件
vim ~/.zshrc
- 將以下代碼粘到配置文件中
export PATH="/Users/xxx/ohpm-repo-5.0.7.0/bin:$PATH"
- 執(zhí)行命令保存配置文件
source ~/.zshrc
5、執(zhí)行以下命令,查詢(xún)版本:
<git> ohpm-repo -v
5.0.7
6、修改私倉(cāng)工具包中的配置 conf/config.yaml
路徑為:/Users/xxx/ohpm-repo-5.0.7.0/conf/config.yaml
我這里使用默認(rèn)本機(jī)數(shù)據(jù)調(diào)試,沒(méi)有做修改。參考官方:配置文件
listen: localhost:8088 # 建議修改為具體的ip:port
.
.
.
7、執(zhí)行安裝命令:
ohpm-repo install
輸出以下信息表示安裝成功:
<git> ohpm-repo install
[2024-11-15T15:16:07.410] [WARN] default - "listen" protocol is set to 'http' in "config.yaml" file, which is insecure, advise to use the more secure 'https' protocol instead.
[2024-11-15T15:16:07.425] [INFO] default - initialize encryption component successfully.
[2024-11-15T15:16:07.426] [INFO] default - initialize "file database" successfully.
[2024-11-15T15:16:07.428] [INFO] default - initialize "file storage" successfully.
[2024-11-15T15:16:07.433] [INFO] default - insert repo success: repo's id is 5cdc28ee00c1428991f25b82f48c3741.
[2024-11-15T15:16:07.434] [INFO] default - insert systemSecurity config success: support anonymous access to ohpm-repo.
[2024-11-15T15:16:07.434] [INFO] console - install successfully.
[2024-11-15T15:16:07.445] [INFO] default - set "deploy_root" environment variables successfully: "OHPM_REPO_DEPLOY_ROOT = /Users/xxx/ohpm-repo".
[2024-11-15T15:16:07.445] [INFO] console - to make the environment variables of "deploy_root" take effect, please run the refresh command: "source ~/.zshrc " or ". ~/.zshrc".
mac 會(huì)自動(dòng)設(shè)置環(huán)境變量
export OHPM_REPO_DEPLOY_ROOT=/Users/wxq/ohpm-repo
所以需要再執(zhí)行一下環(huán)境變量保存命令:
source ~/.zshrc
8、執(zhí)行啟動(dòng)命令:
ohpm-repo start
輸出以下信息表示啟動(dòng)成功:
<git> ohpm-repo start
[2024-11-15T15:18:34.073] [WARN] default - "listen" protocol is set to 'http' in "config.yaml" file, which is insecure, advise to use the more secure 'https' protocol instead.
[2024-11-15T15:18:34.074] [INFO] default - config file path: "/Users/xxx/ohpm-repo/conf/config.yaml".
[2024-11-15T15:18:34.076] [INFO] default - initialize "file database" successfully.
[2024-11-15T15:18:34.076] [INFO] default - initialize "file storage" successfully.
[2024-11-15T15:18:34.089] [INFO] console - http address - localhost:8088 - ohpm-repo/5.0.7.
此時(shí)在瀏覽器打開(kāi) http://localhost:8088/,會(huì)看到構(gòu)建好的私倉(cāng)頁(yè)面:

ohpm-repo 首次啟動(dòng)時(shí),默認(rèn)創(chuàng)建一個(gè)管理員賬號(hào),賬號(hào)名稱(chēng):admin,密碼:12345Qq! 。該賬號(hào)在首次登錄時(shí),需要修改其密碼,請(qǐng)修改密碼后,重新登錄該賬號(hào):

二、發(fā)布HAR包到ohpm-repo私倉(cāng)
提示:發(fā)布HAR包到ohpm-repo和發(fā)布到官方OpenHarmony三方庫(kù)中心倉(cāng)是一樣的邏輯。
1、創(chuàng)建發(fā)布組織
當(dāng)想要上傳一個(gè)含有組織(例如@wxqos/wxqcolor)的包時(shí),在ohpm-repo中需要先創(chuàng)建出該組織(例如wxqos)才能進(jìn)行上傳。和發(fā)布到官方OpenHarmony三方庫(kù)中心倉(cāng)不同的是,這個(gè)組織創(chuàng)建后不用審核,直接就可以使用。具體的申請(qǐng),請(qǐng)參考官方文檔:組織管理。

2、利用工具ssh-keygen生成公、私鑰,可通過(guò)執(zhí)行以下命令來(lái)生成公鑰和私鑰:
ssh-keygen -m PEM -t RSA -b 4096 -f ~/.ssh_ohpm/mykey
在命令執(zhí)行過(guò)程中,會(huì)讓你輸入一個(gè)密碼,這個(gè)密碼是后面用來(lái)上傳包用的,一定要記?。。?!
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
如果本地 ~/ 路徑下沒(méi)有隱藏文件夾.ssh_ohpm,需要先手動(dòng)創(chuàng)建一個(gè):cd到 ~/ 下,執(zhí)行 mkdir .ssh_ohpm。
執(zhí)行成功后會(huì)生成以下私鑰和公鑰文件:

官方說(shuō)明:
- ~/.ssh_ohpm/mykey 為私鑰文件 mykey 的文件路徑,按照實(shí)際情況指定。指定的私鑰存儲(chǔ)目錄必須存在?!窘忉?zhuān)荷傻膍ykey為私鑰,存儲(chǔ)到本地】
- 追加了.pub后綴的相應(yīng)公鑰文件會(huì)存放在和私鑰相同的目錄下?!窘忉?zhuān)荷傻膍ykey.pub為公鑰,后面會(huì)將公鑰內(nèi)容復(fù)制到OpenHarmony三方庫(kù)中心倉(cāng)官網(wǎng)中個(gè)人信息下,參考步驟3】
- OHPM包管理器只支持加密密鑰認(rèn)證,請(qǐng)?jiān)谏晒借€時(shí)輸入密碼。【解釋?zhuān)壕褪巧厦嬲f(shuō)的密碼,后面上傳包的時(shí)候會(huì)用】
3、登錄OpenHarmony三方庫(kù)中心倉(cāng)官網(wǎng),單擊主頁(yè)右上角的個(gè)人中心,新增OHPM公鑰,將公鑰文件(mykey.pub)的內(nèi)容粘貼到公鑰輸入框中。

公鑰添加成功后顯示:

4、打開(kāi)命令行工具,將對(duì)應(yīng)私鑰文件路徑配置到 .ohpmrc 文件中 key_path 字段上。
執(zhí)行以下命令進(jìn)行配置:
ohpm config set key_path ~/.ssh_ohpm/mykey
在ohpmrc文件中會(huì)填充一條內(nèi)容,表示命令執(zhí)行成功:
key_path=/Users/xxx/.ssh_ohpm/mykey

5、登錄OpenHarmony三方庫(kù)中心倉(cāng)官網(wǎng),單擊主頁(yè)右上角的個(gè)人中心,復(fù)制發(fā)布碼,獲取發(fā)布碼并配置到 .ohpmrc 文件中,可執(zhí)行如下命令:
ohpm config set publish_id your_publish_id
執(zhí)行完成后,會(huì)在步驟6中的 ohpmrc文件中會(huì)新增一條內(nèi)容:
publish_id=your_publish_id
將其中的 your_publish_id 替換成你在官網(wǎng)復(fù)制的發(fā)布碼(比如:5UABCDEFGH)即可。
也可以直接執(zhí)行以下命令即可:
ohpm config set publish_id 5UABCDEFGH

6、發(fā)布 HAR包
執(zhí)行 ''ohpm publish <HAR包路徑>'' 命令發(fā)布 HAR包,<HAR包路徑> 指向的文件后綴需為 .har 文件的具體路徑。例如執(zhí)行以下命令:
ohpm config set publish_registry http://localhost:8088/repos/ohpm
ohpm publish /Users/xxx/WXQHMProject/wxqcolor/build/default/outputs/default/wxqcolor.har
或在命令行中配置參數(shù) --publish_registry 使用,例如以下命令:
ohpm publish /Users/xxx/WXQHMProject/wxqcolor/build/default/outputs/default/wxqcolor.har --publish_registry http://localhost:8088/repos/ohpm
注意:其中會(huì)讓你輸入一個(gè)密碼,就是上面說(shuō)的上傳包的密碼。
終端打印以下信息就表示發(fā)布完成了:
<git> ohpm publish /Users/wxq/DevEcoStudioProjects/WXQColorHM/wxqcolor/build/default/outputs/default/wxqcolor.har
registry:http://localhost:8088/repos/ohpm/
package:@wxqos/wxqcolor@1.0.0
=== Harball Contents ===
519B BuildProfile.ets
66B CHANGELOG.md
198B Index.ets
587B LICENSE
1004B README.md
27B ResourceTable.txt
500B build-profile.json5
0B consumer-rules.txt
234B hvigorfile.ts
93.3kB img.png
1008B obfuscation-rules.txt
300B oh-package.json5
593B src/main/module.json
274B src/main/ets/components/MainPage.ets
1.1kB src/main/ets/components/WXQColor.ets
96B src/main/resources/base/element/string.json
96B src/main/resources/en_US/element/string.json
96B src/main/resources/zh_CN/element/string.json
=== Harball Details ===
name: @wxqos/wxqcolor
version: 1.0.0
filename: @wxqos/wxqcolor-1.0.0.har
package size: 95.7 kB
unpacked size: 99.8 kB
shasum: TKIo4iIPMFmZMfG2MVlUPqH0O4U=
integrity: sha512-yEZi/38AayT9zGjJxJWJxviuB9F4F7W6O9se2Ok49CYxVGiPDRSP+1qSWjRdj9WN9N9sv3++t6pOCRJl4foqYw==
total files: 18
ohpm WARN: The HAR package to be uploaded contains source code, which may cause code asset leakage. Please abort if you do not want to procceed.
what is your passphrase of the private key: *******
+@wxqos/wxqcolor 1.0.0
之后就可以在私倉(cāng)中搜到了:

7、發(fā)布動(dòng)態(tài)共享包 HSP 包
動(dòng)態(tài)共享包 HSP 包不能直接發(fā)布在ohpm-repo內(nèi),需要先轉(zhuǎn)化為 .tgz 包,轉(zhuǎn)換方法見(jiàn):編譯HSP模塊。TGZ 包的發(fā)布流程同 HAR 一致。
執(zhí)行 ''ohpm publish <TGZ 包路徑>'' 命令發(fā)布 TGZ 包,< TGZ 包路徑> 指向的文件后綴需為 .tgz 文件的具體路徑。例如執(zhí)行以下命令:
ohpm config set publish_registry <ohpm-repo私倉(cāng)管理地址>/repos/ohpmohpm
publish demo.tgz
或在命令行中配置參數(shù) --publish_registry 使用,例如以下命令:
ohpm publish demo.tgz --publish_registry <ohpm-repo私倉(cāng)管理地址>/repos/ohpm
8、Web頁(yè)面發(fā)布
以上是使用命令行工具發(fā)布,也可以使用Web頁(yè)面發(fā)布。在Web頁(yè)面用管理員賬號(hào)登錄ohpm-repo私倉(cāng)管理地址,在個(gè)人中心 > 倉(cāng)庫(kù)管理中,點(diǎn)擊管理三方包 > 上傳三方包,包的后綴名必須為 .har 或者 .tgz。


三、發(fā)布成功后HAR包的導(dǎo)入和使用
1、從ohpm-repo獲取HAR包 @wxqos/wxqcolor
可以為所有項(xiàng)目配置該私有倉(cāng),例如執(zhí)行以下命令:
ohpm config set registry http://localhost:8088/repos/ohpm
ohpm install
或者在命令行中配置參數(shù) --registry 使用,例如以下命令:
ohpm install @wxqos/wxqcolor --registry http://localhost:8088/repos/ohpm
輸出以下信息,表示導(dǎo)入成功:
<git> ohpm install @wxqos/wxqcolor --registry http://localhost:8088/repos/ohpm
ohpm INFO: MetaDataFetcher fetching meta info of package '@wxqos/wxqcolor' from http://localhost:8088/repos/ohpm/
ohpm INFO: fetch meta info of package '@wxqos/wxqcolor' success http://localhost:8088/repos/ohpm/@wxqos/wxqcolor
oh-package-lock.json5 中自動(dòng)導(dǎo)入信息:
"@wxqos/wxqcolor@1.0.0": {
"name": "@wxqos/wxqcolor",
"version": "1.0.0",
"integrity": "sha512-yEZi/38AayT9zGjJxJWJxviuB9F4F7W6O9se2Ok49CYxVGiPDRSP+1qSWjRdj9WN9N9sv3++t6pOCRJl4foqYw==",
"resolved": "http://localhost:8088/repos/ohpm/@wxqos/wxqcolor/-/@wxqos/wxqcolor-1.0.0.har",
"registryType": "ohpm"
}
項(xiàng)目中導(dǎo)入成功截圖:

2、HAR包的使用
pages引用:
import { WXQColor, firstPublicFuncColor } from '@wxqos/wxqcolor'
代碼使用參考:
this.CustomText('主題-紅色 firstColor', WXQColor.firstColor)
this.CustomText('文字-重要 textMajorColor', WXQColor.textMajorColor)
this.CustomText('激勵(lì)、價(jià)格等高亮 textLightColor', WXQColor.textLightColor)
this.CustomText('文字-不可用 textUnableColor', WXQColor.textUnableColor)
this.CustomText('-------------------------', WXQColor.lineColor)
this.CustomText('自定義內(nèi)部主顏色 firstInternalFuncColor', WXQColor.firstInternalFuncColor())
this.CustomText('自定義外部主顏色 firstPublicFuncColor', firstPublicFuncColor())
使用效果截圖

官方文檔參考:ohpm-repo私倉(cāng)搭建工具