Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Tag for Token File OutPut #9274

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
chidozieononiwu marked this conversation as resolved.
Show resolved Hide resolved
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,22 @@ 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";
chidozieononiwu marked this conversation as resolved.
Show resolved Hide resolved
var inputFiles = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
Assert.Collection(inputFiles, x => Assert.Equal("a.json", x), x => Assert.Equal("z.json", x));
}

[Fact]
public void TestTagRetrievalUsingGetSwaggerFilesFromReadme()
{
string readmeFilePath = "./fixtures/appconfigurationreadme.md";
var tag = "default";
var inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
Assert.Equal("package-2022-05-01", tag);

readmeFilePath = "./fixtures/unordered.md";
tag = "package-2023-02";
inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
Assert.Equal("package-2023-02", tag);
}
}