change to use --input|-i for the CSDL input

This commit is contained in:
Sam Xu 2018-01-04 15:50:18 -08:00
parent 0ef1a7aaeb
commit 85a36aaef5
5 changed files with 55 additions and 86 deletions

View file

@ -105,7 +105,7 @@ namespace OoasGui
fileTextBox.Text = openFileDialog.FileName;
csdlRichTextBox.Text = text;
Settings.ServiceRoot = new Uri(openFileDialog.FileName);
Convert();
}
catch (Exception ex)
@ -146,6 +146,7 @@ namespace OoasGui
string csdl = reader.ReadToEnd();
LoadEdm(url, csdl);
csdlRichTextBox.Text = FormatXml(csdl);
Settings.ServiceRoot = requestUri;
Convert();
}
catch(Exception ex)

View file

@ -36,14 +36,9 @@ namespace OoasUtil
public bool CanContinue { get { return _continue; } }
/// <summary>
/// Input file with the CSDL
/// Input FilePath or Url with the CSDL.
/// </summary>
public string InputFile { get; private set; }
/// <summary>
/// Input Url with the CSDL
/// </summary>
public string InputUri { get; private set; }
public string Input { get; private set; }
/// <summary>
/// Output format.
@ -58,13 +53,7 @@ namespace OoasUtil
/// <summary>
/// Is input Url
/// </summary>
public bool IsUrl
{
get
{
return InputUri != null;
}
}
public bool IsLocalFile { get; private set; }
/// <summary>
/// Process the arguments.
@ -95,18 +84,9 @@ namespace OoasUtil
PrintUsage();
return true;
case "--url":
case "-u":
if (!ProcessUrl(_args[i+1]))
{
return false;
}
i++;
break;
case "--file":
case "-f":
if (!ProcessFile(_args[i + 1]))
case "--input":
case "-i":
if (!ProcessInput(_args[i+1]))
{
return false;
}
@ -156,48 +136,20 @@ namespace OoasUtil
Format = OpenApiFormat.Json;
}
ValidateArguments();
_continue = true;
return true;
_continue = ValidateArguments();
return _continue;
}
private bool ProcessUrl(string uri)
private bool ProcessInput(string input)
{
if (InputUri != null)
if (Input != null)
{
Console.WriteLine("[Error:] Multiple [--url|-u] are not allowed.\n");
Console.WriteLine("[Error:] Multiple [--input|-i] are not allowed.\n");
PrintUsage();
return false;
}
if (InputFile != null)
{
Console.WriteLine("[Error:] Already [--file|-f] is used.\n");
PrintUsage();
return false;
}
InputUri = uri;
return true;
}
private bool ProcessFile(string file)
{
if (InputFile != null)
{
Console.WriteLine("[Error:] Multiple [--file|-f] are not allowed.\n");
PrintUsage();
return false;
}
if (InputUri != null)
{
Console.WriteLine("[Error:] Already [--url|-u] is used.\n");
PrintUsage();
return false;
}
InputFile = file;
Input = input;
return true;
}
@ -229,18 +181,19 @@ namespace OoasUtil
private bool ValidateArguments()
{
if (String.IsNullOrEmpty(InputUri) && String.IsNullOrEmpty(InputFile))
if (String.IsNullOrEmpty(Input))
{
Console.WriteLine("[Error:] At least one of [--url|-u|--file|-f] is required.\n");
Console.WriteLine("[Error:] At least one of [--input|-i] is required.\n");
PrintUsage();
return false;
}
if (!String.IsNullOrEmpty(InputFile))
IsLocalFile = IsLocalPath(Input);
if (IsLocalFile)
{
if (!File.Exists(InputFile))
if (!File.Exists(Input))
{
Console.WriteLine("[Error]: File (" + InputFile + ") is not existed.\n");
Console.WriteLine("[Error]: File (" + Input + ") is not existed.\n");
return false;
}
}
@ -263,15 +216,14 @@ namespace OoasUtil
sb.Append("\nOptions:\n");
sb.Append(" --help|-h\t\t\tDisplay help.\n");
sb.Append(" --version|-v\t\t\tDisplay version.\n");
sb.Append(" --url|-u ServiceUrl\t\tSet the OData Service Url.\n");
sb.Append(" --file|-f CsdlFile\t\tSet the CSDL file name.\n");
sb.Append(" --output|-o CsdlFile\t\tSet the output file name.\n");
sb.Append(" --input|-i CsdlFileOrUrl\t\tSet the CSDL file name or the OData Service Url.\n");
sb.Append(" --output|-o OutputFile\t\tSet the output file name.\n");
sb.Append(" --json|-j\t\t\tSet the output format as JSON.\n");
sb.Append(" --yaml|-y\t\t\tSet the output format as YAML.\n");
sb.Append("\nExamples:\n");
sb.Append(" OoasUtil.exe -y -u http://services.odata.org/TrippinRESTierService -o trip.yaml\n");
sb.Append(" OoasUtil.exe -j -u c:\\csdl.xml -o trip.json\n");
sb.Append(" OoasUtil.exe -y -i http://services.odata.org/TrippinRESTierService -o trip.yaml\n");
sb.Append(" OoasUtil.exe -j -i c:\\csdl.xml -o trip.json\n");
Console.WriteLine(sb.ToString());
}
@ -287,5 +239,26 @@ namespace OoasUtil
{
Console.WriteLine(version.ToString());
}
private static bool IsLocalPath(string path)
{
bool ret = true;
try
{
ret = new Uri(path).IsFile;
}
catch
{
if (path.StartsWith("http://") ||
path.StartsWith(@"http:\\") ||
path.StartsWith("https://") ||
path.StartsWith(@"https:\\"))
{
return false;
}
}
return ret;
}
}
}

View file

@ -12,10 +12,10 @@ namespace OoasUtil
{
static int Main(string[] args)
{
// args = new[] { "--json", "--file", @"E:\work\OneApi Design\test\TripService.OData.xml", "-o", @"E:\work\OneApi Design\test\Trip.json" };
// args = new[] { "--yaml", "--file", @"E:\work\OneApi Design\test\TripService.OData.xml", "-o", @"E:\work\OneApi Design\test\Trip.yaml" };
// args = new[] { "--yaml", "--url", @"http://services.odata.org/TrippinRESTierService", "-o", @"E:\work\OneApi Design\test\TripUrl.yaml" };
// args = new[] { "--json", "--url", @"http://services.odata.org/TrippinRESTierService", "-o", @"E:\work\OneApi Design\test\TripUrl.json" };
// args = new[] { "--json", "--input", @"E:\work\OneApiDesign\test\TripService.OData.xml", "-o", @"E:\work\OneApiDesign\test1\Trip.json" };
// args = new[] { "--yaml", "-i", @"E:\work\OneApiDesign\test\TripService.OData.xml", "-o", @"E:\work\OneApiDesign\test1\Trip.yaml" };
// args = new[] { "--yaml", "--input", @"http://services.odata.org/TrippinRESTierService", "-o", @"E:\work\OneApiDesign\test1\TripUrl.yaml" };
// args = new[] { "--json", "-i", @"http://services.odata.org/TrippinRESTierService", "-o", @"E:\work\OneApiDesign\test1\TripUrl.json" };
ComLineProcesser processer = new ComLineProcesser(args);
if (!processer.Process())
@ -29,13 +29,13 @@ namespace OoasUtil
}
OpenApiGenerator generator;
if (processer.IsUrl)
if (processer.IsLocalFile)
{
generator = new UrlOpenApiGenerator(new Uri(processer.InputUri), processer.Output, processer.Format.Value);
generator = new FileOpenApiGenerator(processer.Input, processer.Output, processer.Format.Value);
}
else
{
generator = new FileOpenApiGenerator(processer.InputFile, processer.Output, processer.Format.Value);
generator = new UrlOpenApiGenerator(new Uri(processer.Input), processer.Output, processer.Format.Value);
}
if (generator.Generate())

View file

@ -25,13 +25,9 @@ Output the "JSON" format Open API document;
Output the "YAML" format Open API document;
### [--url|-u Uri]
### [--input|-i file]
Indicate to get CSDL from Uri.
### [--file|-f file]
Indicate to get CSDL from file.
Indicate to where to get CSDL, from file or from Uri.
### [--output|-o file]
@ -40,7 +36,7 @@ Indicate to output file name.
## Examples
`OoasUtil.exe -j -u http://services.odata.org/TrippinRESTierService -o trip.json`
`OoasUtil.exe -j -i http://services.odata.org/TrippinRESTierService -o trip.json`
The content of `trip.json` is similiar at https://github.com/xuzhg/OData.OpenAPI/blob/master/Microsoft.OData.OpenAPI/Microsoft.OData.OpenAPI.Tests/Resources/TripService.OpenApi.json

View file

@ -56,7 +56,6 @@ namespace OoasUtil
string csdl = reader.ReadToEnd();
return CsdlReader.Parse(XElement.Parse(csdl).CreateReader());
}
protected override OpenApiConvertSettings GetSettings()