Skip to content

Commit

Permalink
Merge pull request #16 from Qiwen-Yu/issue15
Browse files Browse the repository at this point in the history
fix issue 15 on supporting config feature
  • Loading branch information
hphan9 authored Oct 15, 2021
2 parents 7ee9652 + e1ac107 commit 761020c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
64 changes: 46 additions & 18 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.IO;
using McMaster.Extensions.CommandLineUtils;
using Newtonsoft.Json.Linq;

namespace shinny_ssg
{
static class Globals
Expand All @@ -24,8 +26,10 @@ static int Main(string[] args)
//help option
app.HelpOption();
app.VersionOption("-v|--version", "0.1", "Shinny SSG 0.1");
var inputFileOption = app.Option<string>("-i|--input", "Input file/folder to convert source file to HTML", CommandOptionType.SingleValue)
.IsRequired();
//config Option
var configOption = app.Option<string>("-c|--config", "Configuration JSON File", CommandOptionType.SingleValue);
// 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 @@ -34,28 +38,52 @@ static int Main(string[] args)

app.OnExecute(() =>
{
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()))
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")))
{
destination = outputOption.Value();
//start working with config File here
string jsonString = File.ReadAllText(configName);
JObject jObj = JObject.Parse(jsonString);
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
else if (inputValue != null)
{
//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(""))
Globals.cssUrl = cssOption.HasValue() ? cssOption.Value() : null;
Globals.langAtr = langOption.HasValue() ? langOption.Value() : null;
if (outputOption.HasValue() && Directory.Exists(outputOption.Value()))
{
file.Delete();
destination = outputOption.Value();
}
foreach (DirectoryInfo dir in di.GetDirectories())
else
{
dir.Delete(true);
}
//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);
}

}
inputname = inputValue;
}
else
{
throw new Exception("Either Input or Config File must have a valid value ");
}


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 @@ -73,13 +101,14 @@ static int Main(string[] args)
{
var f = new Subfolder();
f.CreateFolder(inputname, destination);

}
else
{
Console.WriteLine("Input Path is not valid.");
}



});

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

}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 761020c

Please sign in to comment.