Skip to content

Commit

Permalink
Merge 1c54a95 into 1e3fe4a
Browse files Browse the repository at this point in the history
  • Loading branch information
chidozieononiwu authored Oct 29, 2024
2 parents 1e3fe4a + 1c54a95 commit 582c4f0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ static Task<int> Main(string[] args)
var output = new Option<string>(name: "--output", description: "The output file path.",
getDefaultValue: () => "swagger.json");

var useTagForOutput = new Option<bool>(name: "--use-tag-for-output", description: "If set to true the output file will be named with the readmeTag used for the run",
getDefaultValue: () => false);

var readmeFile = new Option<string>(name: "--readme", "The input readme file.");

var readmeTag = new Option<string>(name: "--tag", description: "Readme tag used to generate swagger apiView",
Expand All @@ -37,6 +40,7 @@ static Task<int> Main(string[] args)
{
swaggers,
output,
useTagForOutput,
packageName,
swaggerLinks,
readmeFile,
Expand All @@ -46,7 +50,7 @@ static Task<int> Main(string[] args)

cmd.Description = "Parse swagger file into codefile.";

cmd.SetHandler(async (IEnumerable<string> swaggerFiles, string outputFile, string package, IEnumerable<string> links, string readme, string tag) =>
cmd.SetHandler(async (IEnumerable<string> swaggerFiles, string outputFile, bool useTagForOutputFileName, string package, IEnumerable<string> links, string readme, string tag) =>
{
var swaggerLinksArray = links.ToList();
Expand All @@ -56,20 +60,20 @@ static Task<int> Main(string[] args)
{
readme = Path.GetFullPath(readme);
}
await HandleGenerateCodeFile(enumerable, outputFile, package, swaggerLinksArray, readme, tag);
}, swaggers, output, packageName, swaggerLinks, readmeFile, readmeTag);
await HandleGenerateCodeFile(enumerable, outputFile, useTagForOutputFileName, package, swaggerLinksArray, readme, tag);
}, swaggers, output, useTagForOutput, packageName, swaggerLinks, readmeFile, readmeTag);

return Task.FromResult(cmd.Invoke(args));
}

static async Task HandleGenerateCodeFile(IEnumerable<string> swaggers, string output, string packageName, List<string> swaggerLinks, string readmeFile, string readmeTag)
static async Task HandleGenerateCodeFile(IEnumerable<string> swaggers, string output, bool useTagForOutput, string packageName, List<string> swaggerLinks, string readmeFile, string readmeTag)
{

var swaggerFilePaths = swaggers.ToList();
if (readmeFile != null)
{
var readmeFileDir = Path.GetDirectoryName(readmeFile);
var swaggerFiles = ReadmeParser.GetSwaggerFilesFromReadme(readmeFile, readmeTag);
var swaggerFiles = ReadmeParser.GetSwaggerFilesFromReadme(readmeFile, ref readmeTag);
swaggerFilePaths = swaggerFilePaths.Concat(swaggerFiles.Select(it => Path.Join(readmeFileDir, it))).ToList();
}

Expand Down Expand Up @@ -114,6 +118,13 @@ static async Task HandleGenerateCodeFile(IEnumerable<string> swaggers, string ou

var codeFile = root.GenerateCodeFile();
var outputFilePath = Path.GetFullPath(output);

if (useTagForOutput)
{
output = $"{readmeTag}.json";
outputFilePath = Path.Combine(Path.GetDirectoryName(outputFilePath), output);
}

await using FileStream writer = File.Open(outputFilePath, FileMode.Create);
Console.WriteLine($"Generate codefile {output} successfully.");
await codeFile.SerializeAsync(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ public static string GetTagFromYamlArguments(string arguments)
return matchResult.Success ? matchResult.Groups[1].Value : "";
}

public static IEnumerable<string> GetSwaggerFilesFromReadme(string readme, string tag)
public static IEnumerable<string> GetSwaggerFilesFromReadme(string readme, ref string tag)
{
ReadmeParser parser = new ReadmeParser(readme);
parser.ParseReadmeConfig();
string readmeTag = tag;

if (tag == "default" && parser.basicConfig != null)
{
readmeTag = parser.basicConfig.tag;
tag = parser.basicConfig.tag;
}

parser.inputSwaggerFilesMap.TryGetValue(readmeTag, out InputSwaggerFiles inputFiles);
parser.inputSwaggerFilesMap.TryGetValue(tag, out InputSwaggerFiles inputFiles);
return inputFiles?.input ?? Enumerable.Empty<string>();
}
private void ParseReadmeConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@ public ReadmeParserTest(ITestOutputHelper output)
public void TestParseReadme()
{
const string readmeFilePath = "./fixtures/apimanagementReadme.md";
var inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, "default");
var tag = "default";
var inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
Assert.Equal(43, inputFile.ToList().Count);
}

[Fact]
public void TestGetSwaggerFileFromReadmeForAppConfiguration()
{
const string readmeFilePath = "./fixtures/appconfigurationreadme.md";
var inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, "default");
var tag = "default";
var inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
var enumerable = inputFile as string[] ?? inputFile.ToArray();
Assert.Equal("Microsoft.AppConfiguration/stable/2022-05-01/appconfiguration.json", enumerable.ToArray()[0]);
Assert.Single(enumerable.ToList());


inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, "package-2020-06-01");
tag = "package-2020-06-01";
inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
enumerable = inputFile as string[] ?? inputFile.ToArray();
Assert.Equal("Microsoft.AppConfiguration/stable/2020-06-01/appconfiguration.json", enumerable.ToArray()[0]);
Assert.Single(enumerable.ToList());
Expand Down Expand Up @@ -77,7 +79,8 @@ public void TestGetTagFromYamlArgumentsInvalidCase()
public void TestOrderedInputFiles()
{
const string readmeFilePath = "./fixtures/unordered.md";
var inputFiles = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, "package-2023-02");
var tag = "package-2023-02";
var inputFiles = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
Assert.Collection(inputFiles, x => Assert.Equal("a.json", x), x => Assert.Equal("z.json", x));
}
}

0 comments on commit 582c4f0

Please sign in to comment.