Skip to content

Commit

Permalink
Merge pull request #56 from fabricioferreira/main
Browse files Browse the repository at this point in the history
Added support to specify a single source file
  • Loading branch information
Ellerbach authored Apr 26, 2024
2 parents c8ef49f + 4671c6f commit 8c5ca12
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
17 changes: 12 additions & 5 deletions src/DocFxOpenApi/DocFxOpenApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ private void RunLogic()
_options.OutputFolder = _options.SpecFolder;
}

_message.Verbose($"Specification folder: {_options.SpecFolder}");
_message.Verbose($"Specification file/folder: {_options.SpecFolder ?? _options.SpecFile}");
_message.Verbose($"Output folder : {_options.OutputFolder}");
_message.Verbose($"Verbose : {_options.Verbose}");

if (!Directory.Exists(_options.SpecFolder))
if ((_options.SpecFolder ?? _options.SpecFile) == null)
{
_message.Error($"ERROR: Specification folder '{_options.SpecFolder}' doesn't exist.");
_message.Error($"ERROR: Specification folder/file '{_options.SpecSource}' doesn't exist.");
_returnvalue = 1;
return;
}
Expand All @@ -93,9 +93,16 @@ private void RunLogic()

private void ConvertOpenApiFiles()
{
foreach (var extension in _openApiFileExtensions)
if (_options.SpecFolder != null)
{
this.ConvertOpenApiFiles(extension);
foreach (var extension in _openApiFileExtensions)
{
this.ConvertOpenApiFiles(extension);
}
}
else
{
this.ConvertOpenApiFile(_options.SpecFile!);
}
}

Expand Down
40 changes: 36 additions & 4 deletions src/DocFxOpenApi/Domain/CommandlineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@

namespace DocFxOpenApi.Domain
{
using System.IO;
using CommandLine;

/// <summary>
/// Class for command line options.
/// Class for command line options.
/// </summary>
public class CommandlineOptions
{
/// <summary>
/// Gets or sets the folder with specifications.
/// </summary>
[Option('s', "specfolder", Required = true, HelpText = "Folder containing the OpenAPI specification.")]
public string? SpecFolder { get; set; }
[Option('s', "specsource", Required = true, HelpText = "Folder or File containing the OpenAPI specification.")]
public string? SpecSource
{
get => SpecFolder ?? SpecFile;
set => SetSource(value);
}

/// <summary>
/// Gets or sets the output folder.
Expand All @@ -27,5 +32,32 @@ public class CommandlineOptions
/// </summary>
[Option('v', "verbose", Required = false, HelpText = "Show verbose messages.")]
public bool Verbose { get; set; }

/// <summary>
/// Gets the folder with specifications, if the source is a folder.
/// </summary>
public string? SpecFolder { get; private set; }

/// <summary>
/// Gets the file with specifications, if the source is a file.
/// </summary>
public string? SpecFile { get; private set; }

private void SetSource(string? value)
{
if (value == null)
{
return;
}

if (Directory.Exists(value))
{
SpecFolder = value;
}
else if (File.Exists(value))
{
SpecFile = value;
}
}
}
}
}
11 changes: 6 additions & 5 deletions src/DocFxOpenApi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ This tool converts existing [OpenAPI](https://www.openapis.org/) specification f

```text
DocFxOpenApi -s <specs folder> [-o <output folder>] [-v]
-s, --specfolder Required. Folder containing the OpenAPI specification.
-s, --specsource Required. Folder or file containing the OpenAPI specification.
-o, --outputfolder Folder to write the resulting specifications in.
-v, --verbose Show verbose messages.
--help Display this help screen.
--version Display version information.
-v, --verbose Show verbose messages.
--help Display this help screen.
--version Display version information.
```

The tool converts any `*.json`, `*.yaml`, `*.yml` file from the provided specification folder into the output folder. It supports JSON or YAML-format, OpenAPI v2 or v3 (including 3.0.1) format files.
When a folder is provided to the `specsource` parameter, the tool converts all `*.json`, `*.yaml`, `*.yml` files in the folder and its subfolders. When a file is provided, the tool converts only that file.
It supports JSON or YAML-format, OpenAPI v2 or v3 (including 3.0.1) format files.

If the `-o or --outputfolder` is not provided, the output folder is set to the input specs folder.

Expand Down

0 comments on commit 8c5ca12

Please sign in to comment.