shell 如何在vscode中对Python文件运行格式化脚本?

hgncfbus  于 11个月前  发布在  Shell
关注(0)|答案(1)|浏览(134)

我在本地分支中有一个bash脚本文件,我想运行这个文件,它在Visual Studio中的Python文件上有这些指令https://github.com/quantumlib/Cirq/blob/master/check/format-incremental,但我不知道这意味着什么或如何去做

#!/usr/bin/env bash

################################################################################
# Formats python files that have been modified.
#
# Usage:
#     check/format-incremental [BASE_REVISION] [--apply] [--all]
#
# By default, the script runs flynt to convert old string literal formatting to f-strings
# and analyzes python files that have changed relative to the
# base revision and determines whether they need to be formatted. If any changes
# are needed, it prints the diff and exits with code 1, otherwise it exits with
# code 0.
#
# With '--apply', reformats the files instead of printing the diff and exits
# with code 0.
#
# With '--all', analyzes all python files, instead of only changed files.
#
# You can specify a base git revision to compare against (i.e. to use when
# determining whether or not a file is considered to have "changed"). For
# example, you can compare against 'origin/master' or 'HEAD~1'.
#
# If you don't specify a base revision, the following defaults will be tried, in
# order, until one exists:
#
#     1. upstream/master
#     2. origin/master
#     3. master
#
# If none exists, the script fails.
################################################################################

# Get the working directory to the repo root.
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
cd "${topdir}" || exit $?

# Parse arguments.
only_print=1
only_changed=1
rev=""
for arg in "$@"; do
    if [[ "${arg}" == "--apply" ]]; then
        only_print=0
    elif [[ "${arg}" == "--all" ]]; then
        only_changed=0
    elif [ -z "${rev}" ]; then
        if [ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]; then
            echo -e "\033[31mNo revision '${arg}'.\033[0m" >&2
            exit 1
        fi
        rev="${arg}"
    else
        echo -e "\033[31mToo many arguments. Expected [revision] [--apply] [--all].\033[0m" >&2
        exit 1
    fi
done

typeset -a format_files
if (( only_changed == 1 )); then
    # Figure out which branch to compare against.
    if [ -z "${rev}" ]; then
        if [ "$(git cat-file -t upstream/master 2> /dev/null)" == "commit" ]; then
            rev=upstream/master
        elif [ "$(git cat-file -t origin/master 2> /dev/null)" == "commit" ]; then
            rev=origin/master
        elif [ "$(git cat-file -t master 2> /dev/null)" == "commit" ]; then
            rev=master
        else
            echo -e "\033[31mNo default revision found to compare against. Argument #1 must be what to diff against (e.g. 'origin/master' or 'HEAD~1').\033[0m" >&2
            exit 1
        fi
    fi
    base="$(git merge-base ${rev} HEAD)"
    if [ "$(git rev-parse ${rev})" == "${base}" ]; then
        echo -e "Comparing against revision '${rev}'." >&2
    else
        echo -e "Comparing against revision '${rev}' (merge base ${base})." >&2
        rev="${base}"
    fi

    # Get the modified, added and moved python files.
    IFS=$'\n' read -r -d '' -a format_files < \
        <(git diff --name-only --diff-filter=MAR "${rev}" -- '*.py' ':(exclude)cirq-google/cirq_google/cloud/*' ':(exclude)*_pb2.py')
else
    echo -e "Formatting all python files." >&2
    IFS=$'\n' read -r -d '' -a format_files < \
        <(git ls-files '*.py' ':(exclude)*_pb2.py')
fi

if (( ${#format_files[@]} == 0 )); then
    echo -e "\033[32mNo files to format\033[0m."
    exit 0
fi

flynt_args=("--quiet" "-f")
if (( only_print == 1 )); then
    flynt_args+=("--dry-run")
fi

flynt "${format_files[@]}" "${flynt_args[@]}"
FLYNTSTATUS=$?

BLACKVERSION="$(black --version)"

echo "Running the black formatter... (version: $BLACKVERSION)"

args=("--color")
if (( only_print == 1 )); then
    args+=("--check" "--diff")
fi

black "${args[@]}" "${format_files[@]}"
BLACKSTATUS=$?

if [[ "$FLYNTSTATUS" != "0" || "$BLACKSTATUS" != "0"  ]]; then
  exit 1
fi
exit 0

字符串
我试过在终端中运行它,但我真的不知道如何在仓库中对我的文件运行这个格式化程序,因为我对编码非常陌生

lqfhib0f

lqfhib0f1#

我假设它是下面的脚本,位于here

#!/usr/bin/env bash

################################################################################
# Formats python files that have been modified.
#
# Usage:
#     check/format-incremental [BASE_REVISION] [--apply] [--all]
#
# By default, the script runs flynt to convert old string literal formatting to f-strings
# and analyzes python files that have changed relative to the
# base revision and determines whether they need to be formatted. If any changes
# are needed, it prints the diff and exits with code 1, otherwise it exits with
# code 0.
#
# With '--apply', reformats the files instead of printing the diff and exits
# with code 0.
#
# With '--all', analyzes all python files, instead of only changed files.
#
# You can specify a base git revision to compare against (i.e. to use when
# determining whether or not a file is considered to have "changed"). For
# example, you can compare against 'origin/master' or 'HEAD~1'.
#
# If you don't specify a base revision, the following defaults will be tried, in
# order, until one exists:
#
#     1. upstream/master
#     2. origin/master
#     3. master
#
# If none exists, the script fails.
################################################################################

# Get the working directory to the repo root.
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
cd "${topdir}" || exit $?

# Parse arguments.
only_print=1
only_changed=1
rev=""
for arg in "$@"; do
    if [[ "${arg}" == "--apply" ]]; then
        only_print=0
    elif [[ "${arg}" == "--all" ]]; then
        only_changed=0
    elif [ -z "${rev}" ]; then
        if [ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]; then
            echo -e "\033[31mNo revision '${arg}'.\033[0m" >&2
            exit 1
        fi
        rev="${arg}"
    else
        echo -e "\033[31mToo many arguments. Expected [revision] [--apply] [--all].\033[0m" >&2
        exit 1
    fi
done

typeset -a format_files
if (( only_changed == 1 )); then
    # Figure out which branch to compare against.
    if [ -z "${rev}" ]; then
        if [ "$(git cat-file -t upstream/master 2> /dev/null)" == "commit" ]; then
            rev=upstream/master
        elif [ "$(git cat-file -t origin/master 2> /dev/null)" == "commit" ]; then
            rev=origin/master
        elif [ "$(git cat-file -t master 2> /dev/null)" == "commit" ]; then
            rev=master
        else
            echo -e "\033[31mNo default revision found to compare against. Argument #1 must be what to diff against (e.g. 'origin/master' or 'HEAD~1').\033[0m" >&2
            exit 1
        fi
    fi
    base="$(git merge-base ${rev} HEAD)"
    if [ "$(git rev-parse ${rev})" == "${base}" ]; then
        echo -e "Comparing against revision '${rev}'." >&2
    else
        echo -e "Comparing against revision '${rev}' (merge base ${base})." >&2
        rev="${base}"
    fi

    # Get the modified, added and moved python files.
    IFS=$'\n' read -r -d '' -a format_files < \
        <(git diff --name-only --diff-filter=MAR "${rev}" -- '*.py' ':(exclude)cirq-google/cirq_google/cloud/*' ':(exclude)*_pb2.py')
else
    echo -e "Formatting all python files." >&2
    IFS=$'\n' read -r -d '' -a format_files < \
        <(git ls-files '*.py' ':(exclude)*_pb2.py')
fi

if (( ${#format_files[@]} == 0 )); then
    echo -e "\033[32mNo files to format\033[0m."
    exit 0
fi

flynt_args=("--quiet" "-f")
if (( only_print == 1 )); then
    flynt_args+=("--dry-run")
fi

flynt "${format_files[@]}" "${flynt_args[@]}"
FLYNTSTATUS=$?

BLACKVERSION="$(black --version)"

echo "Running the black formatter... (version: $BLACKVERSION)"

args=("--color")
if (( only_print == 1 )); then
    args+=("--check" "--diff")
fi

black "${args[@]}" "${format_files[@]}"
BLACKSTATUS=$?

if [[ "$FLYNTSTATUS" != "0" || "$BLACKSTATUS" != "0"  ]]; then
  exit 1
fi
exit 0

字符串
我还假设您运行的是Windows。有问题的脚本是一个shell脚本。为了在Windows上运行shell脚本,您需要安装Windows Subsystem for Linux(WSL)。https://www.thewindowsclub.com/how-to-run-sh-or-shell-script-file-in-windows-10
安装完成后,cd到包含shell脚本的目录,然后运行./format-incremental。在Visual Studio中,您可以使用ctrl + ~打开终端。

相关问题