Skip to content

Commit

Permalink
Add version support for the Black action pt.2
Browse files Browse the repository at this point in the history
Since we're moving to a composite based action, quite a few changes
were made. 1) Support was added for all OSes (Windows was painful).
2) Isolation from the rest of the workflow had to be done manually
with a virtual environment.

Other noteworthy changes:

- Since we can just put all of our action's code in action.yml, I
  got rid of the action/ directory and embedded the entrypoint in
  the YAML file.
- Renamed `black_version` to `version` to better fit the existing
  input naming scheme.
- Added support for log groups, this makes our action's output a
  bit more fancy (I may or may have not added some debug output too)
  • Loading branch information
ichard26 committed May 18, 2021
1 parent 55a46b8 commit 38a3716
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 26 deletions.
82 changes: 73 additions & 9 deletions action.yml
Expand Up @@ -2,24 +2,88 @@ name: "Black"
description: "The uncompromising Python code formatter."
author: "Łukasz Langa and contributors to Black"
inputs:
black_args:
description: "Black input arguments."
options:
description:
"Options passed to black. Use `black --help` to see available options. Default:
'--check'"
required: false
default: "--check --diff"
src:
description: "Source to run black. Default: '.'"
required: false
default: "."
version:
description: 'Python Version specifier (PEP440) - e.g. "==20.8b1"'
required: false
default: ""
black_version:
description: 'Version of black to use. E.g "==20.8b1"'
black_args:
description: "[DEPRECATED] Black input arguments."
required: false
default: ""
branding:
color: "black"
icon: "check-circle"

runs:
using: composite
steps:
- run: pip install --upgrade --no-cache-dir black${{inputs.black_version}}
- id: black-venv
run: |
echo "::group:: Create a virtual environment for Black"
if [ "$RUNNER_OS" == "Windows" ]; then
bindir="Scripts"
else
bindir="bin"
fi
# TODO: check if this line is necessary
actionpath='${{github.action_path}}'
envpath="${{ github.action_path }}/.black-venv"
python -m venv $envpath
# Please don't ask me questions on why this specific form is necessary so the
# output doesn't up mangled. I really don't know why, but I don't want to
# dump even more hours on trying to understand it. BUT I will blame Windows
# for wasting a few hours of my life. ::set-output:: is weird... - @ichard26
printf "%s" "$envpath/$bindir/"
printf "%s\n" "::set-output name=venv-bin::"
echo
echo "created virtual environment at '$envpath'"
echo "::endgroup::"
shell: bash

- run: |
echo "::group:: Install Black"
if [ "$RUNNER_OS" == "Windows" ]; then
suffix=".exe"
else
suffix=""
fi
if [ -z "${{ inputs.version }}" ]; then
pipcmd="python$suffix -m pip install black[colorama]"
else
pipcmd="python$suffix -m pip install black[colorama]==${{inputs.version}}"
fi
echo "running from virtual environment '$pipcmd'"
echo
${{ steps.black-venv.outputs.venv-bin }}$pipcmd
echo "::endgroup::"
shell: bash
- id: run-black
run:
${{ github.action_path }}/action/entrypoint.sh ${{ inputs.configuration }} ${{
inputs.sortPaths }}
- run: |
echo "::group:: Run Black"
if [ -n "${{ inputs.black_args }}" ]; then
echo '::warning::Input `with.black_args` is deprecated. Use `with.options` and `with.src` instead.'
${{ steps.black-venv.outputs.venv-bin }}black ${{ inputs.black_args }}
echo "::endgroup::"
fi
${{ steps.black-venv.outputs.venv-bin }}black ${{ inputs.options }} ${{ inputs.src }}
echo "::endgroup::"
shell: bash
17 changes: 0 additions & 17 deletions action/entrypoint.sh

This file was deleted.

0 comments on commit 38a3716

Please sign in to comment.