From ebdd29240b8c8d1e82927c405492f3b6903f7aae Mon Sep 17 00:00:00 2001 From: Qiwen Yu Date: Tue, 12 Oct 2021 00:50:14 -0400 Subject: [PATCH 1/2] fix issue 15 on supporting config feature --- Program.cs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 79 insertions(+) diff --git a/Program.cs b/Program.cs index 54430c2..218a5af 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,8 @@ using System; using System.IO; using McMaster.Extensions.CommandLineUtils; +using Newtonsoft.Json.Ling; + namespace shinny_ssg { static class Globals @@ -24,6 +26,7 @@ static int Main(string[] args) //help option app.HelpOption(); app.VersionOption("-v|--version", "0.1", "Shinny SSG 0.1"); + var configOption = app.Option("-c|--config", "Configuration JSON File", CommandOptionType.SingleValue); var inputFileOption = app.Option("-i|--input", "Input file/folder to convert source file to HTML", CommandOptionType.SingleValue) .IsRequired(); var outputOption = app.Option("-o|--output", "Output folder for converted file/files", CommandOptionType.SingleValue); @@ -34,6 +37,79 @@ static int Main(string[] args) app.OnExecute(() => { + var configname = configOption.Value(); + if (File.Exists(configname) && (configname.EndsWith(".json"))) + { + string jsonString = File.ReadAllText(configname); + JObject jObj = JObject.Parse(jsonString); + + foreach (KeyValuePair keyValuePair in jObj) + { + switch (keyValuePair.Key) { + case "input": + var inputname = (string)keyValuePair.Value; + break; + case "output": + var destination = (string)keyValuePair.Value; + break; + case "stylesheet": + Globals.cssUrl = (string)keyValuePair.Value; + break; + case "lang": + Globals.langAtr = (string)keyValuePair.Value; + break; + } + + } + + if (string.IsNullOrEmpty(destination)){ + destination = "\dist" + } + if(Directory.Exists(destination){ + System.IO.DirectoryInfo di = new DirectoryInfo(destination); + foreach (FileInfo file in di.GetFiles("")) + { + file.Delete(); + } + foreach (DirectoryInfo dir in di.GetDirectories()) + { + + + dir.Delete(true); + } + } + + if (File.Exists(inputname) && (inputname.EndsWith(".txt") || inputname.EndsWith(".md"))) + { + //if the file can not read or create , it will never be saved in the destinaiton folder + FileText temp = new FileText(); + if (temp.CreateFile(inputname, destination)) + { + if (temp.SaveFile()) + { + Console.WriteLine($"File {inputname} is converted sucessfull in {destination} folder"); + } + } + + } + else if (Directory.Exists(inputname)) + { + var f = new Subfolder(); + f.CreateFolder(inputname, destination); + + } + else + { + Console.WriteLine("Input Path is not valid."); + } + + } + + + + } + else{ + var inputname = inputFileOption.Value(); Globals.cssUrl = cssOption.HasValue() ? cssOption.Value() : null; Globals.langAtr = langOption.HasValue() ? langOption.Value() : null; @@ -80,6 +156,8 @@ static int Main(string[] args) Console.WriteLine("Input Path is not valid."); } + } + }); app.Execute(args); diff --git a/README.md b/README.md index b570dd0..b7762d3 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Option Function | -i --input | specifies an input file or folder | | -o --output | specifies destination folder | | -l --lang | specifies language atrribute | +| -c --config | accept a file path to a JSON config file | ### Built with From e1ac10709905f247585895662f627898356121bb Mon Sep 17 00:00:00 2001 From: Qiwen Yu Date: Thu, 14 Oct 2021 18:44:37 -0400 Subject: [PATCH 2/2] fix bug in supporting config file --- Program.cs | 112 +++++++++++++++-------------------------------------- 1 file changed, 31 insertions(+), 81 deletions(-) diff --git a/Program.cs b/Program.cs index 218a5af..937a8ba 100644 --- a/Program.cs +++ b/Program.cs @@ -1,7 +1,7 @@ using System; using System.IO; using McMaster.Extensions.CommandLineUtils; -using Newtonsoft.Json.Ling; +using Newtonsoft.Json.Linq; namespace shinny_ssg { @@ -26,9 +26,10 @@ static int Main(string[] args) //help option app.HelpOption(); app.VersionOption("-v|--version", "0.1", "Shinny SSG 0.1"); + //config Option var configOption = app.Option("-c|--config", "Configuration JSON File", CommandOptionType.SingleValue); - var inputFileOption = app.Option("-i|--input", "Input file/folder to convert source file to HTML", CommandOptionType.SingleValue) - .IsRequired(); + // input file now is not required + var inputFileOption = app.Option("-i|--input", "Input file/folder to convert source file to HTML", CommandOptionType.SingleValue); var outputOption = app.Option("-o|--output", "Output folder for converted file/files", CommandOptionType.SingleValue); var cssOption = app.Option("--stylesheet| -s", "Style Sheet for the converted HTML file", CommandOptionType.SingleValue); //language option @@ -37,35 +38,33 @@ static int Main(string[] args) app.OnExecute(() => { - var configname = configOption.Value(); - if (File.Exists(configname) && (configname.EndsWith(".json"))) + var configName = configOption.HasValue() ? configOption.Value() : null; + var inputValue = inputFileOption.HasValue() ? inputFileOption.Value() : null; + + // If config worked + + var inputname = ""; + if (configName != null && File.Exists(configName) && (configName.EndsWith(".json"))) { - string jsonString = File.ReadAllText(configname); + //start working with config File here + string jsonString = File.ReadAllText(configName); JObject jObj = JObject.Parse(jsonString); - - foreach (KeyValuePair keyValuePair in jObj) + inputname = jObj.ContainsKey("input") ? (string)jObj["input"] : ""; + Globals.cssUrl = jObj.ContainsKey("stylesheet") ? (string)jObj["stylesheet"] : null; + Globals.langAtr = jObj.ContainsKey("lang") ? (string)jObj["lang"] : null; + destination = jObj.ContainsKey("lang") ? (string)jObj["output"] : default; + } + else if (inputValue != null) + { + Globals.cssUrl = cssOption.HasValue() ? cssOption.Value() : null; + Globals.langAtr = langOption.HasValue() ? langOption.Value() : null; + if (outputOption.HasValue() && Directory.Exists(outputOption.Value())) { - switch (keyValuePair.Key) { - case "input": - var inputname = (string)keyValuePair.Value; - break; - case "output": - var destination = (string)keyValuePair.Value; - break; - case "stylesheet": - Globals.cssUrl = (string)keyValuePair.Value; - break; - case "lang": - Globals.langAtr = (string)keyValuePair.Value; - break; - } - - } - - if (string.IsNullOrEmpty(destination)){ - destination = "\dist" + destination = outputOption.Value(); } - if(Directory.Exists(destination){ + else + { + //It will delete all file even though the read or write process fail System.IO.DirectoryInfo di = new DirectoryInfo(destination); foreach (FileInfo file in di.GetFiles("")) { @@ -73,65 +72,18 @@ static int Main(string[] args) } foreach (DirectoryInfo dir in di.GetDirectories()) { - - dir.Delete(true); } - } - if (File.Exists(inputname) && (inputname.EndsWith(".txt") || inputname.EndsWith(".md"))) - { - //if the file can not read or create , it will never be saved in the destinaiton folder - FileText temp = new FileText(); - if (temp.CreateFile(inputname, destination)) - { - if (temp.SaveFile()) - { - Console.WriteLine($"File {inputname} is converted sucessfull in {destination} folder"); - } } - - } - else if (Directory.Exists(inputname)) - { - var f = new Subfolder(); - f.CreateFolder(inputname, destination); - + inputname = inputValue; } else { - Console.WriteLine("Input Path is not valid."); - } - + throw new Exception("Either Input or Config File must have a valid value "); } - - } - else{ - - var inputname = inputFileOption.Value(); - Globals.cssUrl = cssOption.HasValue() ? cssOption.Value() : null; - Globals.langAtr = langOption.HasValue() ? langOption.Value() : null; - if (outputOption.HasValue() && Directory.Exists(outputOption.Value())) - { - destination = outputOption.Value(); - } - else - { - //It will delete all file even though the read or write process fail - System.IO.DirectoryInfo di = new DirectoryInfo(destination); - foreach (FileInfo file in di.GetFiles("")) - { - file.Delete(); - } - foreach (DirectoryInfo dir in di.GetDirectories()) - { - dir.Delete(true); - } - - } - if (File.Exists(inputname) && (inputname.EndsWith(".txt") || inputname.EndsWith(".md"))) { //if the file can not read or create , it will never be saved in the destinaiton folder @@ -149,14 +101,13 @@ static int Main(string[] args) { var f = new Subfolder(); f.CreateFolder(inputname, destination); - } else { Console.WriteLine("Input Path is not valid."); } - } + }); @@ -170,7 +121,6 @@ static int Main(string[] args) } catch (Exception ex) { - Console.Error.WriteLine("There is an error with the transfer process"); Console.Error.WriteLine(ex.Message); return -1; } @@ -179,4 +129,4 @@ static int Main(string[] args) } } -} +} \ No newline at end of file