字節(jié)跳動的私有庫公開了可以看我這篇博客部署:字節(jié)跳動flutter私有庫unpub
目標(biāo)
- 搭建私有pub服務(wù)器
- 發(fā)布私有庫到自建服務(wù)器
- 引用私有pub服務(wù)器上的私有庫
步驟
- 部署pub_server服務(wù)端
- 去除發(fā)布到私有庫時的google權(quán)限校驗(yàn)(執(zhí)行pub publish命令時)
- pubspec.yaml中引用私有庫
方法
1.部署pub_server服務(wù)端
按照如下命令下載pub_server源碼昵时、拉取依賴庫哭当、啟動服務(wù)
git clone https://github.com/dart-archive/pub_server.git
...
cd pub_server/
...
pub get
...
dart example/example.dart -d /tmp/package-db
因?yàn)閜ub_server是使用Dart語言編寫,因此該服務(wù)的啟動需要依賴dart環(huán)境淹父,所以請先確保Dart運(yùn)行環(huán)境已正確安裝促煮。
其中dart example/example.dart -d /tmp/package-db 這個命令是啟動服務(wù)的胳施,后面的/tmp/package-db
就是pub推上來的包儲存的位置
其中dart example/example.dart -d /tmp/package-db
命令中的/tmp/package-db
是存放上傳package
和plugin
的地址花沉,可以按照自己的要求確定况凉。
當(dāng)在終端中看到以下內(nèi)容谨湘,說明pub_server運(yùn)行正常:
Listening on http://localhost:8080
To make the pub client use this repository configure your shell via:
$ export PUB_HOSTED_URL=http://localhost:8080
注意:
正式部署時绽快,請將localhost替換為本機(jī)真實(shí)的ip地址和可用端口。不能使用localhost字符串紧阔,使用localhost的話其他機(jī)器無法通過ip訪問坊罢。
若采用修改客戶端PUB_HOSTED_URL環(huán)境變量的方式引用私有倉庫,則需要將example/example.dart文件中的pubDartLangOrg常量的值改為國內(nèi)pub鏡像地址擅耽,比如“https://pub.flutter-io.cn”活孩。
修改localhost和8080端口號,可以在example/example.dart文件中搜索相關(guān)文字進(jìn)行修改乖仇。
2.去除google權(quán)限校驗(yàn)
2.1. 下載pub源代碼地址:https://github.com/dart-lang/pub
git clone https://github.com/dart-lang/pub
cd pub
pub get
2.2. 找到lish.dart文件(路徑:pub/lib/src/command/lish.dart)
2.3. 主要是去掉外層oauth2.withClient(cache, (client)
和 withAuthenticatedClient(cache, server, (client)
校驗(yàn)代碼
源代碼:
Future<void> _publish(List<int> packageBytes) async {
try {
final officialPubServers = {
'https://pub.dartlang.org',
'https://pub.dev',
if (runningFromTest &&
Platform.environment.containsKey('PUB_HOSTED_URL') &&
Platform.environment['_PUB_TEST_AUTH_METHOD'] == 'oauth2')
Platform.environment['PUB_HOSTED_URL'],
};
if (officialPubServers.contains(server.toString())) {
// Using OAuth2 authentication client for the official pub servers
await oauth2.withClient(cache, (client) {
return _publishUsingClient(packageBytes, client);
});
} else {
// For third party servers using bearer authentication client
await withAuthenticatedClient(cache, server, (client) {
return _publishUsingClient(packageBytes, client);
});
}
} on PubHttpException catch (error) {
var url = error.response.request!.url;
if (Uri.parse(url.origin) == Uri.parse(server.origin)) {
handleJsonError(error.response);
} else {
rethrow;
}
}
}
修改后代碼:
Future<void> _publish(List<int> packageBytes) async {
try {
final officialPubServers = {
'https://pub.dartlang.org',
'https://pub.dev',
if (runningFromTest &&
Platform.environment.containsKey('PUB_HOSTED_URL') &&
Platform.environment['_PUB_TEST_AUTH_METHOD'] == 'oauth2')
Platform.environment['PUB_HOSTED_URL'],
};
if (officialPubServers.contains(server.toString())) {
// Using OAuth2 authentication client for the official pub servers
// await oauth2.withClient(cache, (client) {
// return _publishUsingClient(packageBytes, client);
// });
await _publishUsingClient(packageBytes, http.Client());
} else {
// For third party servers using bearer authentication client
// await withAuthenticatedClient(cache, server, (client) {
// return _publishUsingClient(packageBytes, client);
// });
await _publishUsingClient(packageBytes, http.Client());
}
} on PubHttpException catch (error) {
var url = error.response.request!.url;
if (Uri.parse(url.origin) == Uri.parse(server.origin)) {
handleJsonError(error.response);
} else {
rethrow;
}
}
}
2.4. 可以選擇編譯成自己的snapshot文件憾儒,命令形式如下
dart [--package_root=<path>] --snapshot=<output_file> <dart_file>
編譯生成snapshot文件
dart --snapshot=my.pub.snapshot ~/pub/bin/pub.dart
網(wǎng)絡(luò)上的文章有說在bin/lib目錄下執(zhí)行該命令的询兴,其實(shí)該命令的執(zhí)行目錄并不重要,在哪里執(zhí)行都可以起趾;重要的是命令中的~/pub/bin/pub.dart诗舰,因?yàn)樵撀窂街付ǖ奈募邪琺ain()方法。
所以當(dāng)你的Terminal中出現(xiàn)類似../lib/pub.dart: Warning: Interpreting this as package URI, 'package:pub/pub.dart'.這樣的錯誤時训裆,你就要確認(rèn)一下自己的命令中指定的目錄是否正確眶根。
在pub的源代碼中,bin目錄和lib目錄中都有pub.dart文件缭保,但只有bin目錄中的是包含main()方法的汛闸。
2.5. 替換snapshot文件或修改配置
網(wǎng)絡(luò)上一般的做法是將上一步生成的my.pub.snapshot文件拷貝到dart-sdk/bin/snapshots目錄下蝙茶,并且使用文本編輯工具將dart-sdk/bin目錄下pub文件中的pub.dart.snapshot修改為my.pub.snapshot艺骂。
其實(shí)以上做法可能只是原作者的個人習(xí)慣,實(shí)際上只要將上一步的snapshot文件命名為pub.dart.snapshot
隆夯,并替換掉dart-sdk/bin/snapshots
目錄下的同名文件即可钳恕。
如果你是直接安裝的
Flutter
,dart-sdk
在flutter/bin/cache
目錄下蹄衷。
3.發(fā)布私有庫到私有pub_server
主要是修改pubspec.yaml中的publish_to:
到自己的私有pub_server pubspec.yaml編寫規(guī)則
ps:注意每次提交version不能重復(fù) version編寫規(guī)則
name: user_info_s
description: A new Flutter project.
publish_to: http://localhost:8080 # Remove this line if you wish to publish to pub.dev
然后在package
根目錄下執(zhí)行pub publish
忧额,此時不會再要求你做google
校驗(yàn),最終看到Successfully uploaded package.
4.使用私有pub庫
方法1:在工程的pubspec.yaml
文件中指定私有pub的引用:
user_info_s:
hosted:
name: user_info_s
url: http://localhost:8080
version: ^0.0.1
注意愧口,name
就是package
的名稱睦番,如果填寫的錯誤,pub_server
服務(wù)會找不到對應(yīng)的package
耍属,你會在pub_server
的終端看到類似這樣的日志:2022-03-01T08:49:42.888997 0:00:00.000319 GET [404] /api/packages/user_info
方法2:將環(huán)境變量設(shè)置為私有Pub服務(wù)地址export PUB_HOSTED_URL=http://localhost:8080托嚣,然后在pubspec.yaml文件中正常引用package:
dependencies:
user_info_s: ^0.0.1
若使用該方法,則需要將example/example.dart文件中的pubDartLangOrg常量的值改為國內(nèi)pub鏡像地址厚骗,比如“https://pub.flutter-io.cn”示启。
因?yàn)楫?dāng)私有Pub倉庫中找不到請求的package時,程序會到pubDartLangOrg指定的pub倉庫中去下載用到的package到私有倉庫中领舰。
若在使用過程中出現(xiàn)錯誤夫嗓,可以使用flutter pub get -v
命令查看具體的明細(xì)日志。
參考鏈接:https://blog.csdn.net/blog_jihq/article/details/115380948