feat: fetch problem by pwsh script.
Remove old C++ fetcher.
This commit is contained in:
89
build.ps1
Executable file
89
build.ps1
Executable file
@@ -0,0 +1,89 @@
|
||||
#!pwsh
|
||||
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory = $true, Position = 0)]
|
||||
[string]$problemId
|
||||
)
|
||||
|
||||
begin
|
||||
{
|
||||
$ErrorActionPreference = 'Stop'
|
||||
}
|
||||
|
||||
end
|
||||
{
|
||||
Write-Host "Try to fetch problem $($problemId)"
|
||||
|
||||
$graphQuery = @'
|
||||
query questionData($titleSlug: String!) {
|
||||
question(titleSlug: $titleSlug) {
|
||||
content
|
||||
stats
|
||||
codeDefinition
|
||||
sampleTestCase
|
||||
metaData
|
||||
}
|
||||
}
|
||||
'@
|
||||
$response = Invoke-RestMethod -Uri "https://leetcode.cn/api/problems/algorithms/" -Method Get
|
||||
$problem = @($response.stat_status_pairs | Where-Object { $_.stat.frontend_question_id -eq $problemId })
|
||||
|
||||
if ($problem.Count -ne 1)
|
||||
{
|
||||
Write-Error "Failed to find target problem $($problemId)"
|
||||
}
|
||||
$problem = $problem[0]
|
||||
|
||||
$problemNumber = "{0:D4}" -f ($problem.stat.frontend_question_id -as [int])
|
||||
$filename = "$problemNumber-$($problem.stat.question__title_slug).cpp"
|
||||
|
||||
$existedFile = @(Get-ChildItem ./src/problems | Where-Object { $_.Name -eq $filename})
|
||||
|
||||
if ($existedFile.Count -gt 0)
|
||||
{
|
||||
Write-Error "Problem $($problem.stat.question__title_slug) has been fetched, see src/problems/$filename."
|
||||
}
|
||||
|
||||
$variables = @{
|
||||
titleSlug = $problem.stat.question__title_slug
|
||||
} | ConvertTo-Json
|
||||
|
||||
$query = @{
|
||||
operationName = "questionData"
|
||||
variables = $variables
|
||||
query = $graphQuery
|
||||
}
|
||||
|
||||
Write-Host "Try to fetch details of $($problem.stat.question__title_slug)"
|
||||
$response = Invoke-RestMethod -Uri "https://leetcode.cn/graphql" -Body $query -Method Post
|
||||
|
||||
$codeDefinition = @($response.data.question.codeDefinition | ConvertFrom-Json | Where-Object { $_.value -eq "cpp" })
|
||||
if ($codeDefinition.Count -ne 1)
|
||||
{
|
||||
Write-Error "Failed to find C++ code definition"
|
||||
}
|
||||
$testcaseName = "P$($problem.stat.frontend_question_id)"
|
||||
|
||||
$outputCode = @"
|
||||
/**
|
||||
* [$($problem.stat.frontend_question_id)] $($problem.stat.question__title_slug)
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
using namespace std;
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
$($codeDefinition[0].defaultCode)
|
||||
|
||||
// submission codes end
|
||||
|
||||
TEST($testcaseName, Test1)
|
||||
{
|
||||
}
|
||||
"@
|
||||
|
||||
Set-Content -Path "./src/problems/$($filename)" -Value $outputCode
|
||||
Write-Host "Saved as $filename."
|
||||
}
|
||||
Reference in New Issue
Block a user