Skip to content

Commit 81729b7

Browse files
committed
Merge branch 'master' of https://github.com/AlDanial/cloc
2 parents 79ba110 + 3be454d commit 81729b7

File tree

2 files changed

+257
-0
lines changed

2 files changed

+257
-0
lines changed

.github/workflows/release-ready.yml

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Create release
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
jobs:
8+
create_release:
9+
if: ${{ github.event.pull_request.merged == true && startsWith(github.head_ref, 'release-v') }}
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
15+
with:
16+
ref: "master"
17+
18+
- name: Extract version from branch name
19+
id: get_version
20+
run: |
21+
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
22+
if [[ "$BRANCH" =~ release-v([0-9]+\.[0-9]+) ]]; then
23+
VERSION="${BASH_REMATCH[1]}"
24+
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
25+
echo "DISPLAY_VERSION=v$VERSION" >> $GITHUB_OUTPUT
26+
else
27+
echo "Branch name does not match expected format (release-vX.XX)!" && exit 1
28+
fi
29+
30+
- name: Package repository as tar.gz
31+
run: |
32+
VER="${{ steps.get_version.outputs.VERSION }}"
33+
git archive --format=tar.gz -o cloc-$VER.tar.gz HEAD
34+
35+
- name: Get artifact url
36+
id: get_artifact_url
37+
env:
38+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
run: |
40+
ARTIFACT_COMMENT=$(gh pr view ${{ github.event.pull_request.number }} --json comments -q '
41+
.comments
42+
| sort_by(.createdAt)
43+
| reverse
44+
| map(select(.body | contains("**Built executable artifact:**")))
45+
| .[0].body
46+
')
47+
if [ -z "$ARTIFACT_COMMENT" ]; then
48+
echo "No matching comment found."
49+
exit 1
50+
fi
51+
MATCHING_URLS=$(echo "$ARTIFACT_COMMENT" | grep -oP 'https:\/\/[^\s\)]+')
52+
if [ -z "$MATCHING_URLS" ]; then
53+
echo "No URL found in the comment."
54+
exit 1
55+
fi
56+
ARTIFACT_URL=$(echo "$MATCHING_URLS" | head -n 1)
57+
RUN_ID=$(echo "$ARTIFACT_URL" | grep -oP '/actions/runs/\K[0-9]+')
58+
echo "RUN_ID=$RUN_ID" >> $GITHUB_OUTPUT
59+
echo "Artifact URL: $ARTIFACT_URL"
60+
61+
- name: Download artifact
62+
uses: actions/download-artifact@v4
63+
with:
64+
name: cloc-${{ steps.get_version.outputs.VERSION }}-executable
65+
github-token: ${{ secrets.GITHUB_TOKEN }}
66+
run-id: ${{ steps.get_artifact_url.outputs.RUN_ID }}
67+
68+
- name: Prepare renamed release assets
69+
run: |
70+
VER="${{ steps.get_version.outputs.VERSION }}"
71+
cp cloc cloc-$VER.pl
72+
cp Unix/NEWS release_notes-$VER.txt
73+
74+
- name: Create GitHub release with assets
75+
uses: softprops/action-gh-release@v2
76+
with:
77+
tag_name: "${{ steps.get_version.outputs.DISPLAY_VERSION }}"
78+
name: "${{ steps.get_version.outputs.DISPLAY_VERSION }}"
79+
body: "${{ github.event.pull_request.body }}"
80+
token: ${{ secrets.GITHUB_TOKEN }}
81+
files: |
82+
cloc-${{ steps.get_version.outputs.VERSION }}.tar.gz
83+
README.md
84+
cloc-${{ steps.get_version.outputs.VERSION }}.pl
85+
cloc-${{ steps.get_version.outputs.VERSION }}.exe
86+
release_notes-${{ steps.get_version.outputs.VERSION }}.txt

.github/workflows/release-staging.yml

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
name: Build Windows executable
2+
3+
on:
4+
pull_request:
5+
types: [labeled]
6+
7+
jobs:
8+
staging_build:
9+
if: ${{ github.event.label.name == 'release-ready' }}
10+
runs-on: windows-latest
11+
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
15+
with:
16+
ref: ${{ github.head_ref }}
17+
18+
# Branch name must be in the format "release-vX.XX"
19+
- name: Extract version from branch name
20+
id: get_version
21+
shell: pwsh
22+
run: |
23+
$branch = "${{ github.head_ref }}"
24+
if (-not $branch) { $branch = "${{ github.ref }}" }
25+
if ($branch -match 'release-v(?<ver>[0-9]+\.[0-9]+)') {
26+
$ver = $Matches["ver"]
27+
echo "VERSION_NUM=$ver" >> $env:GITHUB_OUTPUT
28+
echo "DISPLAY_VERSION=v$ver" >> $env:GITHUB_OUTPUT
29+
Write-Host "Extracted version: $ver"
30+
}
31+
else {
32+
Write-Error "Branch name does not match expected format (release-vX.XX)."
33+
}
34+
35+
- name: Update version in cloc files
36+
shell: pwsh
37+
run: |
38+
$ver = "${{ steps.get_version.outputs.VERSION_NUM }}"
39+
Write-Host "Updating version in cloc and Unix/cloc to '$ver'"
40+
$replaceVer = 'my $VERSION = "' + $ver + '";'
41+
(Get-Content cloc) -replace 'my \$VERSION = ".*";', $replaceVer | Set-Content cloc
42+
(Get-Content Unix/cloc) -replace 'my \$VERSION = ".*";', $replaceVer | Set-Content Unix/cloc
43+
44+
- name: Update README.md with version and date
45+
shell: pwsh
46+
run: |
47+
$ver = "${{ steps.get_version.outputs.VERSION_NUM }}"
48+
$disp = "${{ steps.get_version.outputs.DISPLAY_VERSION }}"
49+
$currentDate = (Get-Date).ToString("MMM. d, yyyy")
50+
Write-Host "Updating README.md with version $disp and date $currentDate"
51+
(Get-Content README.md) `
52+
-replace 'Latest release:\s+v\d+\.\d+\s+\(.*\)', "Latest release: $disp ($currentDate)" `
53+
-replace 'badge/version-\d+\.\d+', "badge/version-$ver" `
54+
-replace 'cloc-\d+\.\d+\.pl', "cloc-$ver.pl" `
55+
-replace 'cloc-\d+\.\d+\.exe(?!:)', "cloc-$ver.exe" `
56+
-replace 'pp -M Win32::LongPath -M Encode::Unicode -M Digest::MD5 -c -x -o cloc-\d+\.\d+\.exe', "pp -M Win32::LongPath -M Encode::Unicode -M Digest::MD5 -c -x -o cloc-$ver.exe" `
57+
-replace '<tt>cloc-\d+\.\d+\.exe</tt>', "<tt>cloc-$ver.exe</tt>" | Set-Content README.md
58+
59+
- name: Install or upgrade Strawberry Perl
60+
shell: pwsh
61+
run: |
62+
if (Get-Command perl -ErrorAction SilentlyContinue) {
63+
# see https://github.com/StrawberryPerl/Perl-Dist-Strawberry/issues/19#issuecomment-2401043349
64+
choco uninstall strawberryperl -y
65+
}
66+
choco install strawberryperl --no-progress -y
67+
perl -V
68+
cpan -v
69+
70+
- name: Install CPAN dependencies
71+
shell: pwsh
72+
run: |
73+
cpan -i App::cpanminus
74+
cpanm Digest::MD5
75+
cpanm Regexp::Common
76+
cpanm Algorithm::Diff
77+
cpanm PAR::Packer
78+
cpanm Win32::LongPath || cpanm -n Win32::LongPath
79+
80+
- name: Build executable
81+
shell: pwsh
82+
run: |
83+
$ver = "${{ steps.get_version.outputs.VERSION_NUM }}"
84+
pp -M Win32::LongPath -M Encode::Unicode -M Digest::MD5 -c -x -o "cloc-$ver.exe" cloc
85+
86+
- name: Upload executable to VirusTotal and get analysis URL
87+
id: vt_upload
88+
shell: pwsh
89+
run: |
90+
$ver = "${{ steps.get_version.outputs.VERSION_NUM }}"
91+
$apiKey = "${{ secrets.VIRUSTOTAL_API_KEY }}"
92+
$exeFile = "cloc-$ver.exe"
93+
$absoluteFilePath = (Get-Location).Path + "\" + $exeFile
94+
Write-Host "Uploading $absoluteFilePath to VirusTotal..."
95+
$headers=@{}
96+
$headers.Add("accept", "application/json")
97+
$headers.Add("content-type", "multipart/form-data")
98+
$headers.Add("x-apikey", $apiKey)
99+
$response = Invoke-RestMethod -Uri "https://www.virustotal.com/api/v3/files" `
100+
-Method POST `
101+
-Headers $headers `
102+
-Form @{ file = Get-Item -Path $absoluteFilePath }
103+
$vtAnalysisId = $response.data.id
104+
$response2 = Invoke-WebRequest -Uri "https://www.virustotal.com/api/v3/analyses/$vtAnalysisId" -Method GET -Headers $headers
105+
$vtId = ($response2.Content | ConvertFrom-Json).data.links.item.Split("/")[-1]
106+
$vtUrl = "https://www.virustotal.com/gui/file/$vtId"
107+
echo "VT_URL=$vtUrl" >> $env:GITHUB_OUTPUT
108+
Write-Host "VirusTotal analysis available at $vtUrl"
109+
110+
- name: Upload artifact
111+
uses: actions/upload-artifact@v4
112+
id: upload-artifact
113+
with:
114+
name: cloc-${{ steps.get_version.outputs.VERSION_NUM }}-executable
115+
path: cloc-${{ steps.get_version.outputs.VERSION_NUM }}.exe
116+
117+
- name: Post comment with artifact and VirusTotal URL
118+
uses: peter-evans/create-or-update-comment@v4
119+
with:
120+
issue-number: ${{ github.event.pull_request.number }}
121+
body: |
122+
**Built executable artifact:**
123+
[Download the executable artifact from this workflow run](${{ steps.upload-artifact.outputs.artifact-url }})
124+
125+
**VirusTotal Analysis:** ${{ steps.vt_upload.outputs.VT_URL }}
126+
127+
- name: Clean up local exe
128+
shell: pwsh
129+
run: |
130+
$ver = "${{ steps.get_version.outputs.VERSION_NUM }}"
131+
Remove-Item "cloc-$ver.exe"
132+
133+
- name: Update README recent versions entry
134+
shell: pwsh
135+
run: |
136+
$ver = "${{ steps.get_version.outputs.VERSION_NUM }}"
137+
$vtUrl = "${{ steps.vt_upload.outputs.VT_URL }}"
138+
$readme = Get-Content README.md -Raw
139+
$pattern = 'The entries for recent versions are:\s*\r?\n\s*\r?\n'
140+
$replacement = "The entries for recent versions are:`n`ncloc-$ver.exe:`n$vtUrl`n`n"
141+
$newReadme = [regex]::Replace($readme, $pattern, $replacement)
142+
Set-Content -Path README.md -Value $newReadme
143+
(Get-Content README.md -Raw) -replace '\r?\n\s*\r?\n$', "" | Set-Content README.md
144+
145+
- name: Update Unix/NEWS with release notes
146+
shell: pwsh
147+
run: |
148+
$ver = "${{ steps.get_version.outputs.VERSION_NUM }}"
149+
$currentDate = (Get-Date).ToString("MMM. d, yyyy")
150+
$event = Get-Content $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json
151+
$prBody = $event.pull_request.body -replace '"', '`"'
152+
$processedNotes = $prBody -replace '- ', ' o ' -replace '[\*\`_]', ''
153+
$newsContent = Get-Content Unix/NEWS -Raw -Encoding UTF8
154+
$header = @(
155+
" Release Notes for cloc version $ver",
156+
" https://github.com/AlDanial/cloc",
157+
" $currentDate"
158+
)
159+
$newNews = $header + "" + $processedNotes + "" + ("=" * 76) + $newsContent
160+
Set-Content -Path Unix/NEWS -Value $newNews -Encoding UTF8
161+
(Get-Content Unix/NEWS -Raw) -replace '\r?\n\s*\r?\n$', "" | Set-Content Unix/NEWS -Encoding UTF8
162+
163+
- name: Commit changes
164+
shell: pwsh
165+
run: |
166+
$ver = "${{ steps.get_version.outputs.VERSION_NUM }}"
167+
git config user.name "github-actions"
168+
git config user.email "actions@github.com"
169+
git add cloc README.md Unix/NEWS Unix/cloc
170+
git commit -m "chore: prepare release version $ver"
171+
git push

0 commit comments

Comments
 (0)