Skip to content

Commit 3048baa

Browse files
committed
graphql ok
1 parent 4dd2973 commit 3048baa

File tree

8 files changed

+161
-5
lines changed

8 files changed

+161
-5
lines changed

Scripts/graphql/.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"request": "launch",
1111
"preLaunchTask": "build",
1212
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/graphql.dll",
13-
"args": ["243"],
13+
"args": ["107"],
1414
"cwd": "${workspaceFolder}",
1515
"console": "internalConsole",
1616
"stopAtEntry": false,

Scripts/graphql/CSharpCode.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace graphql
2+
{
3+
public class CSharpCode
4+
{
5+
public string MethodName {get;set;}
6+
}
7+
}

Scripts/graphql/Leetcode.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace graphql
99
{
1010
public class Leetcode
1111
{
12-
public string BaseUrl => "https://leetcode.com";
12+
public static string BaseUrl => "https://leetcode.com";
1313
public async Task<QuestionStat> GetAllAsync()
1414
{
1515
var leetcodeApi = RestService.For<ILeetcode>(BaseUrl);
@@ -32,6 +32,9 @@ query getQuestionDetail($titleSlug: String!) {
3232
difficulty
3333
categoryTitle
3434
codeDefinition
35+
sampleTestCase
36+
metaData
37+
questionDetailUrl
3538
}
3639
3740
}",

Scripts/graphql/MethodData.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json;
4+
5+
namespace graphql
6+
{
7+
public class MethodData
8+
{
9+
[JsonProperty("name")]
10+
public string Name{get;set;}
11+
[JsonProperty("params")]
12+
public List<TypeName> Params {get;set;}
13+
[JsonProperty("return")]
14+
public TypeName Return {get; set;}
15+
}
16+
public class TypeName
17+
{
18+
public string name {get;set;}
19+
public string type {get;set;}
20+
21+
public bool dealloc {get;set;}
22+
}
23+
}

Scripts/graphql/Program.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ static void Main(string[] args)
3434
var statStatusPair = questionStat.StatStatusPairs.Where(x=>x.Stat.QuestionId==questionId).FirstOrDefault();
3535
if(statStatusPair != null)
3636
{
37-
var QuestionDetail = lc.GetLeetcodeAsync(statStatusPair.Stat.QuestionTitleSlug).Result;
38-
Console.WriteLine(QuestionDetail.ToJson());
37+
var questionDetail = lc.GetLeetcodeAsync(statStatusPair.Stat.QuestionTitleSlug).Result;
38+
TemplateOpt temp = new TemplateOpt(questionDetail);
39+
temp.Save();
3940
}
4041
}
4142

Scripts/graphql/QuestionDetail.cs

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3-
3+
using System.Linq;
44
using System.Globalization;
5+
using System.Text.RegularExpressions;
56
using Newtonsoft.Json;
67
using Newtonsoft.Json.Converters;
78
namespace graphql
@@ -29,14 +30,50 @@ public partial class QuestionDetail
2930
[JsonProperty("codeDefinition")]
3031
public string CodeDefinition { get; set; }
3132

33+
[JsonProperty("sampleTestCase")]
34+
public string SampleTestCase {get; set;}
35+
36+
[JsonProperty("metaData")]
37+
public string MetaData {get; set;}
38+
3239
[JsonProperty("questionDetailUrl")]
3340
public string QuestionDetailUrl{get;set;}
41+
42+
public string QuestionName => QuestionTitle.Trim().Replace(" ", "").Replace("(", "").Replace(")", "").Replace(",", "").Replace("'", "").Replace("-", "");
43+
public string QuestionUrl => $"{Leetcode.BaseUrl}{QuestionDetailUrl}";
3444
public List<CodeDefinition> CodeDefinitions => JsonConvert.DeserializeObject<List<CodeDefinition>>(CodeDefinition);
45+
46+
public string[] ContentLines => HtmlToText(Content).Split('\n');
47+
public MethodData MethodData => JsonConvert.DeserializeObject<MethodData>(MetaData);
48+
49+
public string CSharpCode => CodeDefinitions.Where(x=>x.Value=="csharp").First().DefaultCode;
3550
}
3651

3752
public partial class QuestionDetail
3853
{
3954
public static QuestionDetail FromJson(string json) => JsonConvert.DeserializeObject<QuestionDetail>(json, Converter.Settings);
55+
56+
private static string HtmlToText(string html)
57+
{
58+
const string tagWhiteSpace = @"(>|$)(\W|\n|\r)+<";//matches one or more (white space or line breaks) between '>' and '<'
59+
const string stripFormatting = @"<[^>]*(>|$)";//match any character between '<' and '>', even when end tag is missing
60+
const string lineBreak = @"<(br|BR)\s{0,1}\/{0,1}>";//matches: <br>,<br/>,<br />,<BR>,<BR/>,<BR />
61+
var lineBreakRegex = new Regex(lineBreak, RegexOptions.Multiline);
62+
var stripFormattingRegex = new Regex(stripFormatting, RegexOptions.Multiline);
63+
var tagWhiteSpaceRegex = new Regex(tagWhiteSpace, RegexOptions.Multiline);
64+
65+
var text = html;
66+
//Decode html specific characters
67+
text = System.Net.WebUtility.HtmlDecode(text);
68+
//Remove tag whitespace/line breaks
69+
text = tagWhiteSpaceRegex.Replace(text, "><");
70+
//Replace <br /> with line breaks
71+
text = lineBreakRegex.Replace(text, Environment.NewLine);
72+
//Strip formatting
73+
text = stripFormattingRegex.Replace(text, string.Empty);
74+
75+
return text;
76+
}
4077
}
4178

4279

Scripts/graphql/TemplateOpt.cs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using CmdRunner;
3+
using System.IO;
4+
using Scriban;
5+
using Scriban.Runtime;
6+
using Scriban.Parsing;
7+
namespace graphql
8+
{
9+
public class TemplateOpt
10+
{
11+
private QuestionDetail _detail;
12+
private string _algorithmsPath;
13+
private string _algorithmsTestPath;
14+
private string _readmePath = @"../../README.md";
15+
private string _author;
16+
public string AlgorithmsTmp =>@"
17+
// Source : {{detail.question_url}}
18+
// Author : {{author}}
19+
// Date : {{date}}
20+
21+
/**********************************************************************************
22+
*
23+
{{for contentLine in detail.content_lines}}
24+
* {{contentLine}}
25+
{{end}}
26+
*
27+
*
28+
**********************************************************************************/
29+
using System;
30+
using System.Collections.Generic;
31+
using Algorithms.Utils;
32+
namespace Algorithms
33+
{
34+
public class Solution{{detail.question_id}}
35+
{
36+
public static {{m.return.type}} {{m.name}}({{ for param in m.params }} {{ param.type }} {{ param.name }},{{ end }})
37+
{
38+
throw new NotImplementedException();
39+
}
40+
}
41+
}";
42+
public string AlgorithmsTestTmp =>@"
43+
using System;
44+
using System.Collections.Generic;
45+
using Algorithms;
46+
using Algorithms.Utils;
47+
using Xunit;
48+
namespace AlgorithmsTest
49+
{
50+
public class {{detail.question_name}}Test
51+
{
52+
[Theory]
53+
[InlineData({{detail.SampleTestCase}})]
54+
public void TestMethod({{ for param in m.params }} {{ param.type }} {{ param.name }},{{ end }} {{m.return.type}} output)
55+
{
56+
Assert.Equal(output, Solution{{detail.question_id}}.{{m.name}}({{ for param in m.params }} {{ param.name }},{{ end }}));
57+
}
58+
}
59+
}";
60+
public string ReadmeTmp =>@"|{{detail.question_id}}|[{{detail.question_title}}]({{detail.question_url}}) | [C#](./Algorithms/{{detail.question_name}}.cs)|{{detail.difficulty}}|";
61+
public TemplateOpt(QuestionDetail detail)
62+
{
63+
_detail = detail;
64+
dynamic cmd = new Cmd();
65+
_author = cmd.git.config(get: "user.name");
66+
_algorithmsPath = $"../../Algorithms/{_detail.QuestionName}.cs";
67+
_algorithmsTestPath = $"../../AlgorithmsTest/{_detail.QuestionName}Test.cs";
68+
}
69+
70+
public bool Save()
71+
{
72+
var template = Template.Parse(AlgorithmsTmp);
73+
var algorithmsText = template.Render(new { Author = _author, Date = DateTime.Now, Detail = _detail, M = _detail.MethodData});
74+
File.WriteAllText(_algorithmsPath, algorithmsText);
75+
template = Template.Parse(AlgorithmsTestTmp);
76+
var algorithmsTestText = template.Render(new { Detail = _detail, M = _detail.MethodData});
77+
File.WriteAllText(_algorithmsTestPath, algorithmsTestText);
78+
template = Template.Parse(ReadmeTmp);
79+
var readmeText = template.Render(new { Detail = _detail});
80+
File.AppendAllLines(_readmePath, new[]{readmeText});
81+
return true;
82+
}
83+
}
84+
}

Scripts/graphql/graphql.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageReference Include="GraphQL.Client" Version="1.0.3" />
1111
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
1212
<PackageReference Include="refit" Version="4.3.0" />
13+
<PackageReference Include="Scriban" Version="1.2.0" />
1314
</ItemGroup>
1415

1516
</Project>

0 commit comments

Comments
 (0)