diff --git a/README.md b/README.md index bd00b9b85..8cbca093a 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,13 @@ The correctness of the TOML parsing and decoding is not yet entirely guaranteed ## Contributing All kinds of contributions are welcome. Make sure to read the [CONTRIBUTING.md](CONTRIBUTING.md) first! + +## Use it in Github Workflow + +To use taplo in your Github workflow, you can add a step : +```yaml + uses: tamasfe/taplo +``` +Setup the configuration in a file on your repository: [Documentation](https://taplo.tamasfe.dev/configuration/file.html). +And you can configure what running with action inputs: `format`, `format_write_changes`, `lint` and `version`. +`format_write_changes` don't update the commits, should be used with additional step if changes should be integrated (tool like autofix.ci) \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 000000000..ecc5f424e --- /dev/null +++ b/action.yml @@ -0,0 +1,57 @@ +name: "Taplo Action" +author: "Gwen Lg" +description: "Run Taplo for TOML formatting or/and lint check." + +inputs: + format: + description: "Check formatting TOML files" + required: false + default: true + + format_write_changes: + description: "Write the formatting changes to the repository" + required: false + default: false + + lint: + description: "Check TOML files with lints" + required: false + default: false + + version: + description: "Version of Taplo" + required: false + default: "0.10.0" + +runs: + using: 'composite' + steps: + - name: Install Taplo + id: install + shell: bash + env: + INSTALL_DIR: . + INPUT_VERSION: ${{ inputs.version }} + INPUT_FILES: ${{ inputs.files }} + run: $GITHUB_ACTION_PATH/action/install.sh + + - name: Format with Taplo + if: ${{ inputs.format == 'true' }} + id: format + shell: bash + if: ${{ inputs.format }} + run: | + if ! ${{ inputs.format_write_changes }}; then + ARGS=" --check --diff" + fi + ${taplo_cmd} format ${ARGS} + + - name: Lint with Taplo + if: ${{ inputs.lint == 'true' }} + id: lint + shell: bash + run: ${taplo_cmd} lint + +branding: + icon: "type" + color: "red" diff --git a/action/install.sh b/action/install.sh new file mode 100755 index 000000000..42172a7a7 --- /dev/null +++ b/action/install.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +set -eu + +SOURCE_DIR="$(dirname -- ${BASH_SOURCE[0]:-$0})" + +log() { + echo -e "$1" >&2 +} + +_DEFAULT_INSTALL_DIR=${HOME}/bin +_INSTALL_DIR=${INSTALL_DIR:-${_DEFAULT_INSTALL_DIR}} +CMD_NAME="taplo" +COMMAND="${_INSTALL_DIR}/${CMD_NAME}" + +TARGET=${INPUT_FILES:-"."} +if [[ -z $(ls ${TARGET} 2>/dev/null) ]]; then + log "ERROR: Input files (${TARGET}) not found" + exit 1 +fi + +if [[ ! -x ${COMMAND} ]]; then + VERSION=${INPUT_VERSION:-"0.9.3"} + if [[ "$(uname -m)" == "arm64" || "$(uname -m)" == "aarch64" ]]; then + ARCH="aarch64" + else + ARCH="x86_64" + fi + UNAME=$(uname -s) + if [[ "$UNAME" == "Darwin" ]]; then + TARGET_FILE="full-darwin-${ARCH}" + FILE_EXT="gz" + elif [[ "$UNAME" == CYGWIN* || "$UNAME" == MINGW* || "$UNAME" == MSYS* ]]; then + TARGET_FILE="windows-${ARCH}" + FILE_EXT="zip" + else + TARGET_FILE="linux-${ARCH}" + FILE_EXT="gz" + fi + FILE_NAME="${CMD_NAME}-${TARGET_FILE}.${FILE_EXT}" + log "Downloading '${CMD_NAME}' v${VERSION}" + wget --progress=dot:mega "https://github.com/tamasfe/taplo/releases/download/${VERSION}/${FILE_NAME}" + + mkdir -p ${_INSTALL_DIR} + if [[ "$FILE_EXT" == "zip" ]]; then + unzip -o "${FILE_NAME}" -d ${_INSTALL_DIR} ${CMD_NAME}.exe + else + gzip -d "${FILE_NAME}" -c >${COMMAND} + chmod +x ${COMMAND} + fi + rm "${FILE_NAME}" +fi +echo "taplo_cmd=${COMMAND}" >>"${GITHUB_ENV}"