diff options
author | Jeremy Koritzinsky <jekoritz@microsoft.com> | 2021-09-24 00:34:00 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-24 00:34:00 +0300 |
commit | a526e77b6d5fbf362cd40e442a0ac893e681448f (patch) | |
tree | e898422a1893c02bbaf198b37e523b0e935e1b06 /eng/formatting | |
parent | b0872284c96867c0e6a73e91c3b620d2a26e4b50 (diff) |
Move clang-format download into dotnet/runtime and add docs for setting up auto-formatting in the repository (#59374)
Diffstat (limited to 'eng/formatting')
-rw-r--r-- | eng/formatting/download-tools.ps1 | 46 | ||||
-rwxr-xr-x | eng/formatting/download-tools.sh | 48 | ||||
-rw-r--r-- | eng/formatting/format.sh | 26 |
3 files changed, 120 insertions, 0 deletions
diff --git a/eng/formatting/download-tools.ps1 b/eng/formatting/download-tools.ps1 new file mode 100644 index 00000000000..36959c784f5 --- /dev/null +++ b/eng/formatting/download-tools.ps1 @@ -0,0 +1,46 @@ + +function DownloadClangTool +{ + param ( + [string] + $toolName, + [string] + $downloadOutputPath + ) + + $baseUri = "https://clrjit.blob.core.windows.net/clang-tools/windows" + + if (-not $(ls $downloadOutputPath | Where-Object {$_.Name -eq "$toolName.exe"})) + { + $baseBackoffSeconds = 2; + + $success = $false + for ($i = 0; $i -lt 5; $i++) { + echo "Attempting download of '$baseUri/$toolName.exe'" + $status = Invoke-WebRequest -Uri "$baseUri/$toolName.exe" -OutFile $(Join-Path $downloadOutputPath -ChildPath "$toolName.exe") + if ($status.StatusCode -lt 400) + { + $success = $true + break + } else { + echo "Download attempt $($i+1) failed. Trying again in $($baseBackoffSeconds + $baseBackoffSeconds * $i) seconds" + Start-Sleep -Seconds $($baseBackoffSeconds + $baseBackoffSeconds * $i) + } + } + if (-not $success) + { + Write-Output "Failed to download clang-format" + return 1 + } + } +} + +$downloadPathFolder = Split-Path $PSScriptRoot -Parent | Split-Path -Parent | Join-Path -ChildPath "artifacts" | Join-Path -ChildPath "tools" + +mkdir $downloadPathFolder -ErrorAction SilentlyContinue + +DownloadClangTool "clang-format" "$downloadPathFolder" +DownloadClangTool "clang-tidy" "$downloadPathFolder" + +# Add to path to enable scripts to skip additional downloading steps since the tools will already be on the path. +$env:PATH = "$downloadPathFolder;$env:PATH" diff --git a/eng/formatting/download-tools.sh b/eng/formatting/download-tools.sh new file mode 100755 index 00000000000..4534c41de7b --- /dev/null +++ b/eng/formatting/download-tools.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +set -ue + +source="${BASH_SOURCE[0]}" + +# resolve $source until the file is no longer a symlink +while [[ -h "$source" ]]; do + scriptroot="$( cd -P "$( dirname "$source" )" && pwd )" + source="$(readlink "$source")" + # if $source was a relative symlink, we need to resolve it relative to the path where the + # symlink file was located + [[ $source != /* ]] && source="$scriptroot/$source" +done +scriptroot="$( cd -P "$( dirname "$source" )" && pwd )" + +function DownloadClangTool { + targetPlatform=$(dotnet --info |grep RID:) + targetPlatform=${targetPlatform##*RID:* } + + toolUrl=https://clrjit.blob.core.windows.net/clang-tools/${targetPlatform}/$1 + toolOutput=$2/$1 + + if [[ ! -x "$toolOutput" ]]; then + curl --retry 5 -o "${toolOutput}" "$toolUrl" + chmod 751 $toolOutput + fi + + if [[ ! -x "$toolOutput" ]]; then + echo "Failed to download $1" + exit 1 + fi +} + + +engFolder="$(cd -P "$( dirname "$scriptroot" )" && pwd )" +downloadPathFolder="$(cd -P "$( dirname "$engFolder" )" && pwd )/artifacts/tools" + +mkdir -p "$downloadPathFolder" + +. "$scriptroot/../common/tools.sh" + +InitializeDotNetCli true + +DownloadClangTool "clang-format" "$downloadPathFolder" +DownloadClangTool "clang-tidy" "$downloadPathFolder" + +export PATH=$downloadPathFolder:$PATH diff --git a/eng/formatting/format.sh b/eng/formatting/format.sh new file mode 100644 index 00000000000..5a9edf4c339 --- /dev/null +++ b/eng/formatting/format.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +LC_ALL=C +# Select files to format +NATIVE_FILES=$(git diff --cached --name-only --diff-filter=ACM "*.h" "*.hpp" "*.c" "*.cpp" "*.inl" | sed 's| |\\ |g') +MANAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM "*.cs" "*.vb" | sed 's| |\\ |g') + +exec 1>&2 + +if [[ -n "$NATIVE_FILES" ]]; then + # Format all selected files + echo "$NATIVE_FILES" | cat | xargs | sed -e 's/ /,/g' | xargs "./artifacts/tools/clang-format" -style=file -i + + # Add back the modified files to staging + echo "$NATIVE_FILES" | xargs git add +fi +if [[ -n "$MANAGED_FILES" ]]; then + # Format all selected files + echo "$MANAGED_FILES" | cat | xargs | sed -e 's/ /,/g' | dotnet format --include + + # Add back the modified files to staging + echo "$MANAGED_FILES" | xargs git add +fi + + +exit 0 |