Skip to content

Commit

Permalink
fix bug in supporting config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiwen-Yu committed Oct 14, 2021
1 parent ebdd292 commit e1ac107
Showing 1 changed file with 31 additions and 81 deletions.
112 changes: 31 additions & 81 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.IO;
using McMaster.Extensions.CommandLineUtils;
using Newtonsoft.Json.Ling;
using Newtonsoft.Json.Linq;

namespace shinny_ssg
{
Expand All @@ -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<string>("-c|--config", "Configuration JSON File", CommandOptionType.SingleValue);
var inputFileOption = app.Option<string>("-i|--input", "Input file/folder to convert source file to HTML", CommandOptionType.SingleValue)
.IsRequired();
// input file now is not required
var inputFileOption = app.Option<string>("-i|--input", "Input file/folder to convert source file to HTML", CommandOptionType.SingleValue);
var outputOption = app.Option<string>("-o|--output", "Output folder for converted file/files", CommandOptionType.SingleValue);
var cssOption = app.Option<string>("--stylesheet| -s", "Style Sheet for the converted HTML file", CommandOptionType.SingleValue);
//language option
Expand All @@ -37,101 +38,52 @@ 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<string, JToken> 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(""))
{
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);

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
Expand All @@ -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.");
}

}


});

Expand All @@ -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;
}
Expand All @@ -179,4 +129,4 @@ static int Main(string[] args)
}

}
}
}

0 comments on commit e1ac107

Please sign in to comment.