From 2cf6f0d84ff651902f14bcf8c31dc4339c61f77d Mon Sep 17 00:00:00 2001 From: Gwen Lg Date: Sat, 17 May 2025 20:07:05 +0200 Subject: [PATCH 1/3] feat(action): add action for cargo.toml formating --- action.yml | 45 ++++++++++++++++++++++++++++++++++++++++ action/install.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 action.yml create mode 100755 action/install.sh diff --git a/action.yml b/action.yml new file mode 100644 index 000000000..46017db45 --- /dev/null +++ b/action.yml @@ -0,0 +1,45 @@ +name: "Taplo Action" +author: "Gwen Lg" +description: "Run Taplo for TOML formatting" + +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 + + 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 + run: | + if ! ${{ inputs.format_write_changes }}; then + ARGS=" --check --diff" + fi + ${taplo_cmd} format ${ARGS} + +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}" From 5168ef87190e1557c0d7ece57dbe82190af43873 Mon Sep 17 00:00:00 2001 From: Gwen Lg Date: Mon, 26 May 2025 15:06:50 +0200 Subject: [PATCH 2/3] feat(action): add lint functionality in Taplo Github Action --- action.yml | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 46017db45..ecc5f424e 100644 --- a/action.yml +++ b/action.yml @@ -1,6 +1,6 @@ name: "Taplo Action" author: "Gwen Lg" -description: "Run Taplo for TOML formatting" +description: "Run Taplo for TOML formatting or/and lint check." inputs: format: @@ -13,6 +13,11 @@ inputs: required: false default: false + lint: + description: "Check TOML files with lints" + required: false + default: false + version: description: "Version of Taplo" required: false @@ -34,12 +39,19 @@ runs: 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" From 4530fbddfed6aab18469f8104c0577e680ad19fa Mon Sep 17 00:00:00 2001 From: Gwen Lg Date: Mon, 26 May 2025 16:41:36 +0200 Subject: [PATCH 3/3] docs: add an entry for action in README.md TODO: add info in doc website ? --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) 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