0

我是颤振开发的新手。我试图将我的代码作为依赖项分成多个本地包。这是我目前的项目结构:

/packages/ commons:包含常用小部件和实用程序功能的包

/packages/ fruits:包含有关水果的屏幕的包(取决于commons:)

/ main: 取决于commons&fruits

每当我在commons影响fruits包的包中进行依赖项更改时,我必须flutter pub get在三个文件夹(forcommonsfruitsmain-project)中执行才能运行代码。

有什么办法可以将此过程减少为一次“刷新”点击?


  1. 示例commons
flutter pub add fluro
flutter pub get
4

1 回答 1

0

由于我不耐烦了,所以我想为此编写一个小的 shell 脚本。我不知道是否有更简单的方法,但这对我有用。

我的项目具有以下文件夹结构:

project
   pubspec.yaml
   packages/
       package1/
           pubspec.yaml
       package2/
           pubspec.yaml
       package3/
           pubspec.yaml

refresh.bash在项目文件夹中创建了一个文件。这是它的样子:

# open project directory or exit if failed
cd PROJECT_PATH || exit
# check if project contains pubspec.yaml file
if [ -f "pubspec.yaml" ]; then
    # check if packages folder exists
    if [ -d "packages" ]; then
        # open packages folder
        cd "packages" || exit
        # run for all subdirectories of packages folder
        for d in */; do
            # open subdirectory
            cd "$d" || exit
            # check if subdirectory contains pubspec.yaml file
            if [ -f "pubspec.yaml" ]; then
                # run pub get for subdirectory (package)
                flutter pub get
            fi
            # exit subdirectory
            cd ..
        done
        # exit packages directory
        cd ..
    fi

    # run pub get for project directory
    flutter pub get
else
    echo "pubspec.yaml not found"
fi

替换PROJECT_PATH为项目的路径。

为了能够运行这个脚本,我必须让它可执行。这可以通过以下命令完成:

chmod +x refresh.bash

在此之后,我运行脚本(在 bash 终端中):

./refresh.bash

注意:我不知道 shell 脚本是如何工作的,我从Shell Script Cheatsheet中得到了一些提示。所以,如果有任何问题,或者如果它可以更小,请建议我。这是我使用 shell 脚本的第一天。

于 2022-01-13T10:40:33.090 回答