Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
idelvall committed Dec 11, 2023
1 parent 6943cf9 commit 40a4041
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 83 deletions.
71 changes: 16 additions & 55 deletions gradle/Earthfile
Original file line number Diff line number Diff line change
@@ -1,61 +1,22 @@
VERSION --global-cache 0.7

# INIT stores the configuration required for the other functions in the filesystem, and installs required dependencies.
# - cache_id: Overrides default ID of the global cache. Its value is exported to the build environment under the entry: $EARTHLY_GRADLE_HOME_CACHE_ID
INIT:
COMMAND
IF [ "$GRADLE_USER_HOME" = "" ]
ENV GRADLE_USER_HOME="$HOME/.gradle"
END
RUN if [ -n "$EARTHLY_GRADLE_HOME_CACHE_ID" ]; then \
echo "+INIT has already been called in this build environment" ; \
exit 1; \
fi
RUN mkdir -p /tmp/earthly/cfg

# EARTHLY_GRADLE_HOME_CACHE_ID
ARG EARTHLY_TARGET_PROJECT_NO_TAG
ARG OS_RELEASE=$(md5sum /etc/os-release | cut -d ' ' -f 1)
ENV EARTHLY_GRADLE_HOME_CACHE_ID ="${EARTHLY_TARGET_PROJECT_NO_TAG}#${OS_RELEASE}#earthly-gradle-cache"

# RUN_WITH_CACHE runs the passed command with the gradle caches mounted.
# Notice that in order to run this function, +INIT must be called first. This function exports the target cache mount ID under the env entry: $EARTHLY_GRADLE_PROJECT_CACHE_ID.
# GRADLE_GET_MOUNT_CACHE sets the following entries in the calling environment:
# EARTHLY_GRADLE_USER_HOME_CACHE: Code of the mount cache for the gradle home.
# EARTHLY_GRADLE_PROJECT_CACHE: Code of the mount cache for the .gradle folder.
# Example:
# DO gradle+GRADLE_GET_MOUNT_CACHE
# RUN --mount=$EARTHLY_GRADLE_USER_HOME_CACHE --mount=$EARTHLY_GRADLE_PROJECT_CACHE gradle --no-daemon build
# Arguments:
# - command (required): Command to run, can be any expression.
# - gradle_home_cache_id: ID of the gradle cache mount. By default: $EARTHLY_GRADLE_HOME_CACHE_ID as exported by +INIT
# - project_cache_id: ID of the target cache mount. By default: ${EARTHLY_GRADLE_HOME_CACHE_ID}#${EARTHLY_TARGET_NAME}
# - with_docker: Runs the command within a WITH DOCKER element parametrized with this value
#
RUN_WITH_CACHE:
# - cache_prefix: To be used in both caches. By default: ${EARTHLY_TARGET_PROJECT_NO_TAG}#${OS_RELEASE}#earthly-gradle-cache
GRADLE_GET_MOUNT_CACHE:
COMMAND
DO +CHECK_INITED
ARG --required command
ARG EARTHLY_TARGET_NAME
ARG gradle_home_cache_id = $EARTHLY_GRADLE_HOME_CACHE_ID
ARG project_cache_id="${EARTHLY_GRADLE_HOME_CACHE_ID}#${EARTHLY_TARGET_NAME}"
ARG with_docker
IF [ -n "$with_docker" ]
WITH DOCKER $with_docker
RUN --mount=type=cache,mode=0777,id=$gradle_home_cache_id,sharing=shared,target=$GRADLE_USER_HOME \
--mount=type=cache,mode=0777,id=$project_cache_id,sharing=locked,target=.gradle \
set -e; \
mkdir -p $GRADLE_USER_HOME; \
printf "Running:\n $command\n"; \
eval $command
END
ELSE
RUN --mount=type=cache,mode=0777,id=$gradle_home_cache_id,sharing=shared,target=$GRADLE_USER_HOME \
--mount=type=cache,mode=0777,id=$project_cache_id,sharing=locked,target=.gradle \
set -e; \
mkdir -p $GRADLE_USER_HOME; \
printf "Running:\n $command\n"; \
eval $command
ARG EARTHLY_TARGET_PROJECT_NO_TAG
ARG OS_RELEASE=$(md5sum /etc/os-release | cut -d ' ' -f 1)
ARG cache_prefix = "${EARTHLY_TARGET_PROJECT_NO_TAG}#${OS_RELEASE}#earthly-gradle-cache"
IF [ "$GRADLE_USER_HOME" = "" ]
ENV GRADLE_USER_HOME="$HOME/.gradle"
END
ENV EARTHLY_GRADLE_PROJECT_CACHE_ID=$project_cache_id

CHECK_INITED:
COMMAND
RUN if [ ! -n "$EARTHLY_GRADLE_HOME_CACHE_ID" ]; then \
echo "+INIT has not been called yet in this build environment" ; \
exit 1; \
fi;
ENV EARTHLY_GRADLE_CACHE_PREFIX=$cache_prefix
ENV EARTHLY_GRADLE_USER_HOME_CACHE="type=cache,mode=0777,id=$cache_prefix,sharing=shared,target=$GRADLE_USER_HOME"
ENV EARTHLY_GRADLE_PROJECT_CACHE="type=cache,mode=0777,id=${cache_prefix}#project,sharing=shared,target=.gradle"
38 changes: 10 additions & 28 deletions gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,20 @@ IMPORT github.com/earthly/lib/gradle:<version/commit> AS gradle
```
> :warning: Due to [this issue](https://github.com/earthly/earthly/issues/3490), make sure to enable `--global-cache` in the calling Earthfile, as shown above.
## +INIT
## +GRADLE_GET_MOUNT_CACHE

This function stores the configuration required by the other functions in the build environment filesystem, and installs required dependencies.
This function sets the following entries in the calling environment, so they can be used later on `RUN` commands
- `$EARTHLY_GRADLE_USER_HOME_CACHE`: Code of the mount cache for the gradle home.
- `$EARTHLY_GRADLE_PROJECT_CACHE`: Code of the mount cache for the .gradle folder.

It must be called once per build environment, to avoid passing repetitive arguments to the functions called after it, and to install required dependencies before the source files are copied from the build context.
### Arguments:
- `cache_prefix`: To be used in both caches. By default: `${EARTHLY_TARGET_PROJECT_NO_TAG}#${OS_RELEASE}#earthly-gradle-cache`

### Usage

Call once per build environment:
```earthfile
DO gradle+INIT ...
### Example:
```earthly
DO gradle+GRADLE_GET_MOUNT_CACHE
RUN --mount=$EARTHLY_GRADLE_USER_HOME_CACHE --mount=$EARTHLY_GRADLE_PROJECT_CACHE gradle --no-daemon build
```

### Arguments
#### `cache_id`
Overrides default ID of the global `$GRADLE_USER_HOME` cache. Its value is exported to the build environment under the entry: `$EARTHLY_GRADLE_HOME_CACHE_ID`.

## +RUN_WITH_CACHE

`+RUN_WITH_CACHE` runs the passed command with the GRADLE caches mounted.

Notice that in order to run this function, [+INIT](#init) must be called first. This function exports the target cache mount ID under the env entry: `$EARTHLY_GRADLE_PROJECT_CACHE_ID`.

### Arguments
#### `command (required)`
Command to run, can be any expression.

#### `gradle_home_cache_id`
ID of the gradle home cache mount. By default: `$EARTHLY_GRADLE_HOME_CACHE_ID` as exported by `+INIT`

#### `project_cache_id`
ID of the target cache mount. By default: `${EARTHLY_GRADLE_HOME_CACHE_ID}#${EARTHLY_TARGET_NAME}`

## Example
See [earthly-intellij-plugin](https://github.com/earthly/earthly-intellij-plugin/blob/main/Earthfile) for a complete example.

0 comments on commit 40a4041

Please sign in to comment.