基于unpub的Flutter私有组件库搭建

搭建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文件中的相关配置:

  1. 搜索_getUploaderEmail并修改相关鉴权逻辑
  2. 将上游仓库地址从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:dart_mongo_pass@127.0.0.1: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时就会从私有仓库拉取组件了。

参考资源

相关推荐
_風箏36 分钟前
OpenSSH【安装 03】远程代码执行漏洞CVE-2024-6387修复(cp: 无法创建普通文件“/usr/sbin/sshd“:文本文件忙问题处理)
后端
用户895356032822040 分钟前
Gin 框架核心架构解析
后端·go
我是哪吒44 分钟前
分布式微服务系统架构第164集:架构懂了就来了解数据库存储扩展千亿读写
后端·面试·github
UrbanJazzerati1 小时前
PowerShell 自动化实战:自动化为 Git Staged 内容添加 Issue 注释标记
后端·面试·shell
橙序员小站1 小时前
通过trae开发你的第一个Chrome扩展插件
前端·javascript·后端
wave7771 小时前
feign的bean创建过程-底层请求过程-源码走读
后端·面试
爻渡1 小时前
时延揭密:探索不同函数调用实现背后的性能差异
后端·程序员
will_we2 小时前
Tauri 2 安卓开发初体验
后端
jhfuture2 小时前
从源码分析,为什么要使用TTL(TransmittableThreadLocal)而不是inheritableThreadLocals
后端·面试
cwkiller4 小时前
伏魔挑战赛-ASP/ASP.NET赛道10+绕过样本思路分享
后端