搭建Flutter私有组件库指南
对于技术团队来说,将私有软件包发布在公网上往往不是最佳选择,特别是当这些组件涉及核心业务逻辑时。与iOS开发者使用AppUploader这样的专业工具来管理应用发布类似,Flutter团队也需要建立自己的私有组件库来保证代码安全和发布流程可控。
本文将基于unpub来介绍如何搭建Flutter私有仓库以及如何使用它发布私有包。
一、服务器端安装
1. 安装Flutter/Dart环境
对于Linux服务器,参照官网文档进行安装即可。
2. 配置环境变量
首先确定当前使用的shell类型:
bash
echo $SHELL
根据输出结果(/bin/zsh或/bin/bash等),配置Flutter环境变量:
bash
# zsh配置
echo "export FLUTTER_ROOT=<your flutter installation dir>" >> ~/.zshrc
echo "export PATH=$FLUTTER_ROOT/bin:$PATH" >> ~/.zshrc
source ~/.zshrc
# bash配置
echo "export FLUTTER_ROOT=<your flutter installation dir>" >> ~/.bashrc
echo "export PATH=$FLUTTER_ROOT/bin:$PATH" >> ~/.bashrc
source ~/.bashrc
如果需要使用国内镜像加速访问:
bash
# zsh配置
echo "export PUB_HOSTED_URL=https://pub.flutter-io.cn" >> ~/.zshrc
echo "export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn" >> ~/.zshrc
source ~/.zshrc
# bash配置
echo "export PUB_HOSTED_URL=https://pub.flutter-io.cn" >> ~/.bashrc
echo "export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn" >> ~/.bashrc
source ~/.bashrc
3. 安装MongoDB
推荐使用Docker安装MongoDB,便于迁移和备份:
yaml
version: '2'
services:
dart-mongo:
image: mongo:4.4.18
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: dart_mongo
MONGO_INITDB_ROOT_PASSWORD: dart_mongo_pass
MONGO_INITDB_DATABASE: dart_pub
ports:
-"127.0.0.1:27019:27017"
volumes:
-./dbdata:/data/db
4. 全局安装unpub
bash
flutter pub global activate unpub
5. 修改unpub配置
找到unpub安装目录,修改lib/src/app.dart文件中的相关配置:
- 搜索
_getUploaderEmail
并修改相关鉴权逻辑 - 将上游仓库地址从
https://pub.dev
替换为https://pub.flutter-io.cn
6. 重新激活unpub
bash
flutter pub global deactivate unpub
flutter pub global activate unpub
7. 启动服务器
bash
flutter pub global run 'unpub:unpub' -p 8080 --database 'mongodb://dart_mongo:[email protected]:27019/dart_pub?authSource=admin'
启动后可通过http://ip:8080
访问。
8. (可选)配置域名
使用Nginx进行代理:
nginx
server {
listen 80 ;
server_name flutter-pub.xxx.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl http2;
server_name flutter-pub.xxx.com;
access_log /var/log/nginx/unpub.log;
error_log /var/log/nginx/unpub_error.log;
ssl_certificate ssl/xxx.crt;
ssl_certificate_key ssl/xxx.key;
gzip on;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
}
}
二、客户端配置
1. 跳过谷歌鉴权(仅发布组件时需要)
bash
git clone https://github.com/ameryzhu/pub.git
cd pub
flutter pub get
dart --snapshot=pub.dart.snapshot bin/pub.dart
cp pub.dart.snapshot $FLUTTER_ROOT/bin/cache/dart-sdk/bin/snapshots/
cp pub.dart.snapshot $FLUTTER_ROOT/bin/cache/
2. 发布包到私有仓库
在pubspec.yaml中添加发布配置:
yaml
name: flutter_ui
description: A new Flutter package project.
version: 0.0.1
publish_to: https://flutter-pub.xxx.com/
3. 配置拉取组件的仓库
bash
# zsh配置
echo "export PUB_HOSTED_URL=https://flutter-pub.xxx.com" >> ~/.zshrc
source ~/.zshrc
# bash配置
echo "export PUB_HOSTED_URL=https://flutter-pub.xxx.com" >> ~/.bashrc
source ~/.bashrc
配置完成后,使用flutter pub get
时就会从私有仓库拉取组件了。