字節跳動的私有庫公開了可以看我這篇博客部署:字節跳動flutter私有庫unpub
目標
- 搭建私有pub服務器
- 發布私有庫到自建服務器
- 引用私有pub服務器上的私有庫
步驟
- 部署pub_server服務端
- 去除發布到私有庫時的google權限校驗(執行pub publish命令時)
- pubspec.yaml中引用私有庫
方法
1.部署pub_server服務端
按照如下命令下載pub_server源碼、拉取依賴庫、啟動服務
git clone https://github.com/dart-archive/pub_server.git
...
cd pub_server/
...
pub get
...
dart example/example.dart -d /tmp/package-db
因為pub_server是使用Dart語言編寫,因此該服務的啟動需要依賴dart環境,所以請先確保Dart運行環境已正確安裝。
其中dart example/example.dart -d /tmp/package-db 這個命令是啟動服務的,后面的/tmp/package-db
就是pub推上來的包儲存的位置
其中dart example/example.dart -d /tmp/package-db
命令中的/tmp/package-db
是存放上傳package
和plugin
的地址,可以按照自己的要求確定。
當在終端中看到以下內容,說明pub_server運行正常:
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替換為本機真實的ip地址和可用端口。不能使用localhost字符串,使用localhost的話其他機器無法通過ip訪問。
若采用修改客戶端PUB_HOSTED_URL環境變量的方式引用私有倉庫,則需要將example/example.dart文件中的pubDartLangOrg常量的值改為國內pub鏡像地址,比如“https://pub.flutter-io.cn”。
修改localhost和8080端口號,可以在example/example.dart文件中搜索相關文字進行修改。
2.去除google權限校驗
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)
校驗代碼
源代碼:
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
網絡上的文章有說在bin/lib目錄下執行該命令的,其實該命令的執行目錄并不重要,在哪里執行都可以;重要的是命令中的~/pub/bin/pub.dart,因為該路徑指定的文件中包含main()方法。
所以當你的Terminal中出現類似../lib/pub.dart: Warning: Interpreting this as package URI, 'package:pub/pub.dart'.這樣的錯誤時,你就要確認一下自己的命令中指定的目錄是否正確。
在pub的源代碼中,bin目錄和lib目錄中都有pub.dart文件,但只有bin目錄中的是包含main()方法的。
2.5. 替換snapshot文件或修改配置
網絡上一般的做法是將上一步生成的my.pub.snapshot文件拷貝到dart-sdk/bin/snapshots目錄下,并且使用文本編輯工具將dart-sdk/bin目錄下pub文件中的pub.dart.snapshot修改為my.pub.snapshot。
其實以上做法可能只是原作者的個人習慣,實際上只要將上一步的snapshot文件命名為pub.dart.snapshot
,并替換掉dart-sdk/bin/snapshots
目錄下的同名文件即可。
如果你是直接安裝的
Flutter
,dart-sdk
在flutter/bin/cache
目錄下。
3.發布私有庫到私有pub_server
主要是修改pubspec.yaml中的publish_to:
到自己的私有pub_server pubspec.yaml編寫規則
ps:注意每次提交version不能重復 version編寫規則
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
根目錄下執行pub publish
,此時不會再要求你做google
校驗,最終看到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
服務會找不到對應的package
,你會在pub_server
的終端看到類似這樣的日志:2022-03-01T08:49:42.888997 0:00:00.000319 GET [404] /api/packages/user_info
方法2:將環境變量設置為私有Pub服務地址export PUB_HOSTED_URL=http://localhost:8080,然后在pubspec.yaml文件中正常引用package:
dependencies:
user_info_s: ^0.0.1
若使用該方法,則需要將example/example.dart文件中的pubDartLangOrg常量的值改為國內pub鏡像地址,比如“https://pub.flutter-io.cn”。
因為當私有Pub倉庫中找不到請求的package時,程序會到pubDartLangOrg指定的pub倉庫中去下載用到的package到私有倉庫中。
若在使用過程中出現錯誤,可以使用flutter pub get -v
命令查看具體的明細日志。
參考鏈接:https://blog.csdn.net/blog_jihq/article/details/115380948