基于unpub搭建Flutter私有组件库教程

前端 潘老师 3周前 (04-01) 16 ℃ (0) 扫码查看

对于技术团队而言,将包含核心业务逻辑的Flutter私有组件包发布到公网是不明智的,这就如同iOS开发者借助AppUploader等专业工具管理应用发布一样,Flutter团队也需要构建属于自己的私有组件库,以此保障代码安全,让发布流程尽在掌控。接下来,本文将详细介绍如何基于unpub搭建Flutter私有仓库,并利用其发布私有包。

一、服务器端安装与配置

(一)安装Flutter/Dart环境

如果使用Linux服务器,按照Flutter官方文档的指引,就能顺利完成Flutter/Dart环境的安装。

(二)配置环境变量

  1. 确定当前shell类型:通过执行以下命令,可查看当前使用的shell类型。
echo $SHELL
  1. 配置Flutter环境变量:根据上述命令的输出结果(可能是/bin/zsh或/bin/bash等),进行相应的Flutter环境变量配置。
# 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
  1. 使用国内镜像加速访问(可选):若希望加快访问速度,可配置国内镜像。同样根据shell类型,执行以下命令。
# 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

(三)安装MongoDB

推荐借助Docker安装MongoDB,这种方式在后续的迁移和备份操作中更为便捷。通过以下Docker Compose配置文件,就能轻松完成安装。

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

(四)全局安装unpub

执行以下命令,完成unpub的全局安装。

flutter pub global activate unpub

(五)修改unpub配置

找到unpub的安装目录,对lib/src/app.dart文件进行编辑,主要修改两个关键部分:一是搜索_getUploaderEmail,并调整相关鉴权逻辑;二是将上游仓库地址从https://pub.dev替换为https://pub.flutter-io.cn

(六)重新激活unpub

修改完配置后,需要重新激活unpub,让更改生效。执行以下命令即可。

flutter pub global deactivate unpub
flutter pub global activate unpub

(七)启动服务器

使用以下命令启动unpub服务器。

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即可访问。

(八)配置域名(可选)

若想使用自定义域名访问,可借助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;
  }
}

二、客户端配置

(一)跳过谷歌鉴权(仅发布组件时需要)

在发布组件时,若要跳过谷歌鉴权,可执行以下操作:

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/

(二)发布包到私有仓库

在项目的pubspec.yaml文件中添加发布配置,示例如下:

name: flutter_ui
description: A new Flutter package project.
version: 0.0.1
publish_to: https://flutter-pub.xxx.com/

(三)配置拉取组件的仓库

根据使用的shell类型,执行相应命令,配置从私有仓库拉取组件。

# 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命令时,就会从私有仓库拉取组件了。

在搭建Flutter私有组件库的过程中,若在过程中遇到问题,可参考unpub GitHub仓库、Flutter官方文档以及Docker官方文档寻找解决方案。希望本文能帮助大家顺利搭建属于自己的Flutter私有组件库。


版权声明:本站文章,如无说明,均为本站原创,转载请注明文章来源。如有侵权,请联系博主删除。
本文链接:https://www.panziye.com/front/16748.html
喜欢 (0)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】