From 4f5d1c6d12e14c538d07e4016061fc111101e46f Mon Sep 17 00:00:00 2001 From: Alif Rachmawadi Date: Thu, 13 Jan 2022 10:10:55 +0000 Subject: [PATCH] add cache path option --- .github/workflows/workflow.yml | 3 ++- README.md | 1 + action.yml | 8 +++++-- setup.sh | 44 ++++++++++++++++++++++++---------- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index e805a406..5b707abf 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -85,7 +85,8 @@ jobs: channel: stable flutter-version: 2.5.0 cache: true - cache-key: key-20220110 + cache-key: key-20220113 + cache-path: ${{ runner.tool_cache }}/flutter/2.5.0-stable - name: Run dart --version shell: bash run: dart --version diff --git a/README.md b/README.md index a9566107..26820892 100644 --- a/README.md +++ b/README.md @@ -157,5 +157,6 @@ steps: flutter-version: 2.5.0 cache: true cache-key: flutter # optional, change this to force refresh cache + cache-path: ${{ runner.tool_cache }}/flutter # optional, change this to specify the cache path - run: flutter --version ``` diff --git a/action.yml b/action.yml index a02274cb..99e608d4 100644 --- a/action.yml +++ b/action.yml @@ -19,13 +19,17 @@ inputs: description: 'Identifier for the Flutter SDK cache' required: false default: 'flutter' + cache-path: + description: 'Flutter SDK cache path' + required: false + default: ${{ runner.tool_cache }}/flutter runs: using: 'composite' steps: - if: ${{ inputs.cache == 'true' }} uses: actions/cache@v2 with: - path: ${{ runner.tool_cache }}/flutter + path: ${{ inputs.cache-path }} key: ${{ inputs.cache-key }}-${{ inputs.channel }}-${{ inputs.flutter-version }} - - run: $GITHUB_ACTION_PATH/setup.sh ${{ inputs.channel }} ${{ inputs.flutter-version }} + - run: $GITHUB_ACTION_PATH/setup.sh -c "${{ inputs.cache-path }}" ${{ inputs.channel }} ${{ inputs.flutter-version }} shell: bash diff --git a/setup.sh b/setup.sh index 4cb4e81c..c0e8395f 100755 --- a/setup.sh +++ b/setup.sh @@ -53,28 +53,48 @@ download_archive() { curl --connect-timeout 15 --retry 5 $archive_url >$archive_local if [[ $archive_name == *zip ]]; then - unzip -o "$archive_local" -d "$2" + unzip -o "$archive_local" -d "$HOME" + shopt -s dotglob + mv ${HOME}/flutter/* "$2" + shopt -u dotglob else - tar xf "$archive_local" -C "$2" + tar xf "$archive_local" -C "$2" --strip-components=1 fi rm $archive_local } -CHANNEL="$1" -VERSION="$2" +transform_path() { + if [[ $OS_NAME == windows ]]; then + echo $1 | sed -e 's/^\///' -e 's/\//\\/g' + else + echo $1 + fi +} + +SDK_CACHE="" + +while getopts 'c:' flag; do + case "${flag}" in + c) SDK_CACHE="$(transform_path $OPTARG)" ;; + ?) exit 2 ;; + esac +done + +CHANNEL="${@:$OPTIND:1}" +VERSION="${@:$OPTIND+1:1}" if [[ $OS_NAME == windows ]]; then - FLUTTER_ROOT="${RUNNER_TOOL_CACHE}\\flutter" PUB_CACHE="${USERPROFILE}\\.pub-cache" else - FLUTTER_ROOT="${RUNNER_TOOL_CACHE}/flutter" PUB_CACHE="${HOME}/.pub-cache" fi -if [[ ! -x "${FLUTTER_ROOT}/bin/flutter" ]]; then +mkdir -p "$SDK_CACHE" + +if [[ ! -x "${SDK_CACHE}/bin/flutter" ]]; then if [[ $CHANNEL == master ]]; then - git clone -b master https://github.com/flutter/flutter.git "$RUNNER_TOOL_CACHE/flutter" + git clone -b master https://github.com/flutter/flutter.git "$SDK_CACHE" else VERSION_MANIFEST=$(get_version_manifest $CHANNEL $VERSION) @@ -84,13 +104,13 @@ if [[ ! -x "${FLUTTER_ROOT}/bin/flutter" ]]; then fi ARCHIVE_PATH=$(echo $VERSION_MANIFEST | jq -r '.archive') - download_archive "$ARCHIVE_PATH" "$RUNNER_TOOL_CACHE" + download_archive "$ARCHIVE_PATH" "$SDK_CACHE" fi fi -echo "FLUTTER_ROOT=${FLUTTER_ROOT}" >>$GITHUB_ENV +echo "FLUTTER_ROOT=${SDK_CACHE}" >>$GITHUB_ENV echo "PUB_CACHE=${PUB_CACHE}" >>$GITHUB_ENV -echo "${FLUTTER_ROOT}/bin" >>$GITHUB_PATH -echo "${FLUTTER_ROOT}/bin/cache/dart-sdk/bin" >>$GITHUB_PATH +echo "${SDK_CACHE}/bin" >>$GITHUB_PATH +echo "${SDK_CACHE}/bin/cache/dart-sdk/bin" >>$GITHUB_PATH echo "${PUB_CACHE}/bin" >>$GITHUB_PATH