基于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时就会从私有仓库拉取组件了。

参考资源

相关推荐
Victor3562 小时前
Netty(20)如何实现基于Netty的WebSocket服务器?
后端
缘不易2 小时前
Springboot 整合JustAuth实现gitee授权登录
spring boot·后端·gitee
Kiri霧2 小时前
Range循环和切片
前端·后端·学习·golang
WizLC2 小时前
【Java】各种IO流知识详解
java·开发语言·后端·spring·intellij idea
Victor3562 小时前
Netty(19)Netty的性能优化手段有哪些?
后端
爬山算法2 小时前
Netty(15)Netty的线程模型是什么?它有哪些线程池类型?
java·后端
白宇横流学长3 小时前
基于SpringBoot实现的冬奥会科普平台设计与实现【源码+文档】
java·spring boot·后端
Python编程学习圈3 小时前
Asciinema - 终端日志记录神器,开发者的福音
后端
bing.shao3 小时前
Golang 高并发秒杀系统踩坑
开发语言·后端·golang
壹方秘境3 小时前
一款方便Java开发者在IDEA中抓包分析调试接口的插件
后端