From 8f383060dd246032c5f54b85ec14c7c28a4e1ea1 Mon Sep 17 00:00:00 2001 From: Jakub Kocka Date: Tue, 30 Apr 2024 12:36:00 +0200 Subject: [PATCH] feat: Added pre-commit hook of PowerShell scrip checker --- .github/workflows/pre_commit_check.yml | 2 ++ .gitlab/CODEOWNERS | 1 + .gitlab/ci/pre_commit.yml | 33 +++++++++++++++++++ .pre-commit-config.yaml | 5 +++ .../en/contribute/install-pre-commit-hook.rst | 7 ---- .../contribute/install-pre-commit-hook.rst | 6 ---- export.ps1 | 8 ++--- 7 files changed, 45 insertions(+), 17 deletions(-) diff --git a/.github/workflows/pre_commit_check.yml b/.github/workflows/pre_commit_check.yml index 859f567807..d1d51bedaa 100644 --- a/.github/workflows/pre_commit_check.yml +++ b/.github/workflows/pre_commit_check.yml @@ -40,3 +40,5 @@ jobs: echo "" exit 1 fi + # Run pre-commit for PowerShell scripts check + pre-commit run --hook-stage manual check-powershell-scripts --from-ref base_ref --to-ref pr_ref --show-diff-on-failure diff --git a/.gitlab/CODEOWNERS b/.gitlab/CODEOWNERS index e59da32928..0d61759339 100644 --- a/.gitlab/CODEOWNERS +++ b/.gitlab/CODEOWNERS @@ -170,6 +170,7 @@ /docs/**/api-reference/system/ @esp-idf-codeowners/system /docs/**/security/ @esp-idf-codeowners/security /docs/**/migration-guides/ @esp-idf-codeowners/docs @esp-idf-codeowners/all-maintainers +/docs/**/contribute/install-pre-commit-hook.rst @esp-idf-codeowners/ci @esp-idf-codeowners/tools /examples/README.md @esp-idf-codeowners/docs @esp-idf-codeowners/ci /examples/**/*.py @esp-idf-codeowners/ci @esp-idf-codeowners/tools diff --git a/.gitlab/ci/pre_commit.yml b/.gitlab/ci/pre_commit.yml index fbcd965fed..49449106e7 100644 --- a/.gitlab/ci/pre_commit.yml +++ b/.gitlab/ci/pre_commit.yml @@ -45,3 +45,36 @@ check_pre_commit: paths: - .cache/submodule_archives policy: pull + +check_powershell: + extends: + - .before_script:minimal + stage: pre_check + image: docker:latest + services: + - docker:dind + tags: + - dind + - amd64 + needs: + - pipeline_variables + variables: + # cache pre_commit + PRE_COMMIT_HOME: "$CI_PROJECT_DIR/.cache/pre-commit" + rules: + - changes: + - "*.ps1" + script: + - apk add python3 + - apk add py3-pip + - pip install pre-commit --break-system-packages + - pre-commit run --hook-stage manual check-powershell-scripts --files $MODIFIED_FILES + cache: + - key: pre_commit-cache-${LATEST_GIT_TAG} + paths: + - .cache/pre-commit + policy: pull + - key: submodule-cache-${LATEST_GIT_TAG} + paths: + - .cache/submodule_archives + policy: pull diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 08a5656cd8..c4f9e8990a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -224,6 +224,11 @@ repos: name: shellcheck dash (export.sh) args: ['--shell', 'dash', '-x'] files: 'export.sh' + - repo: https://github.com/espressif/esp-pwsh-check + rev: v1.0.1 + hooks: + - id: check-powershell-scripts + stages: [manual] - repo: https://github.com/espressif/esp-idf-sbom.git rev: v0.13.0 hooks: diff --git a/docs/en/contribute/install-pre-commit-hook.rst b/docs/en/contribute/install-pre-commit-hook.rst index 79858ac7b3..98d76a84a3 100644 --- a/docs/en/contribute/install-pre-commit-hook.rst +++ b/docs/en/contribute/install-pre-commit-hook.rst @@ -3,13 +3,6 @@ Install Pre-commit Hook for ESP-IDF Project :link_to_translation:`zh_CN:[中文]` -Required Dependency -------------------- - -Python 3.8.* or above. This is our recommended Python version for ESP-IDF developers. - -If you still have Python versions not compatible, update your Python versions before installing the pre-commit hook. - Install ``pre-commit`` ---------------------- diff --git a/docs/zh_CN/contribute/install-pre-commit-hook.rst b/docs/zh_CN/contribute/install-pre-commit-hook.rst index d098d1d30e..bf4fec4bb2 100644 --- a/docs/zh_CN/contribute/install-pre-commit-hook.rst +++ b/docs/zh_CN/contribute/install-pre-commit-hook.rst @@ -3,12 +3,6 @@ :link_to_translation:`en:[English]` -环境依赖 ---------------- - -我们向 IDF 开发人员推荐 Python 3.8.* 及以上版本。 - -如果你已安装了不兼容的 Python 版本,应在安装 pre-commit 工具前进行更新。 安装 ``pre-commit`` 工具 --------------------------- diff --git a/export.ps1 b/export.ps1 index af35dceb65..fec71aeca7 100644 --- a/export.ps1 +++ b/export.ps1 @@ -26,10 +26,10 @@ foreach ($line in $envars_raw) { $envars_array += (, ($var_name, $var_val)) } -if ($IsWindows -eq $null) { +if ($null -eq $IsWindows) { # $IsWindows was added in PowerShell Core 6 and PowerShell 7 together with multi-platform support. # I.E. if this # internal variable is not set then PowerShell 5 is used and # the platform cannot be # anything else than Windows. - $IsWindows = $true + $Windows = $true } foreach ($pair in $envars_array) { @@ -38,7 +38,7 @@ foreach ($pair in $envars_array) { $var_val = $pair[1].Trim() # trim spaces on the ends of the val if ($var_name -eq "PATH") { # trim "%PATH%" or "`$PATH" - if ($IsWindows) { + if ($IsWindows || $Windows) { $var_val = $var_val.Trim($S + "%PATH%") } else { $var_val = $var_val.Trim($S + "`$PATH") @@ -60,7 +60,7 @@ function parttool.py { &python "$IDF_PATH\components\partition_table\parttool.py #Compare Path's OLD vs. NEW $NEW_PATH = $env:PATH.split($S) | Select-Object -Unique # array without duplicates $dif_Path = Compare-Object -ReferenceObject $OLD_PATH -DifferenceObject $NEW_PATH -PassThru -if ($dif_Path -ne $null) { +if ($null -ne $dif_Path) { Write-Output "`nAdded to PATH`n-------------" Write-Output $dif_Path } else {