Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update dependencies #1

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
end_of_line = crlf
indent_size = 4
indent_style = space
insert_final_newline = true
tab_width = 4
trim_trailing_whitespace = true

[*.{csproj,config}]
indent_size = 2
indent_style = space
6 changes: 3 additions & 3 deletions packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net45" />
<package id="Newtonsoft.Json.Schema" version="3.0.11" targetFramework="net45" />
<package id="Microsoft.Net.Compilers" version="3.0.0" targetFramework="net45" developmentDependency="true" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net45" />
<package id="Newtonsoft.Json.Schema" version="3.0.13" targetFramework="net45" />
<package id="Microsoft.Net.Compilers" version="3.8.0" targetFramework="net45" developmentDependency="true" />
</packages>
89 changes: 28 additions & 61 deletions src/Scoop.Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,109 +6,78 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;

namespace Scoop
{
public class JsonParserException : Exception
{
namespace Scoop {
public class JsonParserException : Exception {
public string FileName { get; set; }
public JsonParserException(string file, string message) : base(message) { this.FileName = file; }
public JsonParserException(string file, string message, Exception inner) : base(message, inner) { this.FileName = file; }
}

public class Validator
{
public class Validator {
private bool CI { get; set; }
public JSchema Schema { get; private set; }
public FileInfo SchemaFile { get; private set; }
public JObject Manifest { get; private set; }
public FileInfo ManifestFile { get; private set; }
public IList<string> Errors { get; private set; }
public string ErrorsAsString
{
get
{
return String.Join(System.Environment.NewLine, this.Errors);
}
public string ErrorsAsString {
get { return String.Join(System.Environment.NewLine, this.Errors); }
}

private JSchema ParseSchema(string file)
{
try
{
private JSchema ParseSchema(string file) {
try {
return JSchema.Parse(File.ReadAllText(file, System.Text.Encoding.UTF8));
}
catch (Newtonsoft.Json.JsonReaderException e)
{
} catch (Newtonsoft.Json.JsonReaderException e) {
throw new JsonParserException(Path.GetFileName(file), e.Message, e);
}
catch (FileNotFoundException e)
{
} catch (FileNotFoundException e) {
throw e;
}
}

private JObject ParseManifest(string file)
{
try
{
private JObject ParseManifest(string file) {
try {
return JObject.Parse(File.ReadAllText(file, System.Text.Encoding.UTF8));
}
catch (Newtonsoft.Json.JsonReaderException e)
{
} catch (Newtonsoft.Json.JsonReaderException e) {
throw new JsonParserException(Path.GetFileName(file), e.Message, e);
}
catch (FileNotFoundException e)
{
} catch (FileNotFoundException e) {
throw e;
}
}

public Validator(string schemaFile)
{
public Validator(string schemaFile) {
this.SchemaFile = new FileInfo(schemaFile);
this.Errors = new List<string>();
}

public Validator(string schemaFile, bool ci)
{
public Validator(string schemaFile, bool ci) {
this.SchemaFile = new FileInfo(schemaFile);
this.Errors = new List<string>();
this.CI = ci;
}

public bool Validate(string file)
{
public bool Validate(string file) {
this.ManifestFile = new FileInfo(file);
return this.Validate();
}

public bool Validate()
{
if (!this.SchemaFile.Exists)
{
public bool Validate() {
if (!this.SchemaFile.Exists) {
Console.WriteLine("ERROR: Please provide schema.json!");
return false;
}
if (!this.ManifestFile.Exists)
{
if (!this.ManifestFile.Exists) {
Console.WriteLine("ERROR: Please provide manifest.json!");
return false;
}
this.Errors.Clear();
try
{
if (this.Schema == null)
{
try {
if (this.Schema == null) {
this.Schema = this.ParseSchema(this.SchemaFile.FullName);
}
this.Manifest = this.ParseManifest(this.ManifestFile.FullName);
}
catch (FileNotFoundException e)
{
} catch (FileNotFoundException e) {
this.Errors.Add(e.Message);
}
catch (JsonParserException e)
{
} catch (JsonParserException e) {
this.Errors.Add(String.Format("{0}{1}: {2}", (this.CI ? " [*] " : ""), e.FileName, e.Message));
}

Expand All @@ -119,8 +88,7 @@ public bool Validate()

this.Manifest.IsValid(this.Schema, out validationErrors);

if (validationErrors.Count == 0)
{
if (validationErrors.Count == 0) {
return true;
}
traverseErrors(validationErrors, this.CI ? 3 : 1);
Expand All @@ -129,11 +97,10 @@ public bool Validate()
}

public void traverseErrors(IList<ValidationError> errors, int level = 1) {
if(errors == null) {
if (errors == null) {
return;
}
foreach (ValidationError error in errors)
{
foreach (ValidationError error in errors) {
StringBuilder sb = new StringBuilder();
sb.Insert(sb.Length, " ", level * 2);
sb.Insert(sb.Length, this.CI ? "[*] " : "- ");
Expand All @@ -147,13 +114,13 @@ public void traverseErrors(IList<ValidationError> errors, int level = 1) {
sb.Insert(sb.Length, this.CI ? " [^] " : " ");
sb.AppendFormat("Path: {0}/{1}", error.SchemaId, error.ErrorType);

if(!this.CI) {
if (!this.CI) {
sb.Insert(sb.Length, "\n");
}

this.Errors.Add(sb.ToString());

if(error.ChildErrors != null || error.ChildErrors.Count > 0) {
if (error.ChildErrors != null || error.ChildErrors.Count > 0) {
traverseErrors(error.ChildErrors, level + 1);
}
}
Expand Down
31 changes: 11 additions & 20 deletions src/validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@
using System.IO;
using System.Linq;

namespace Scoop
{
public class Program
{
public static int Main(string[] args)
{
namespace Scoop {
public class Program {
public static int Main(string[] args) {
bool ci = String.Format("{0}", Environment.GetEnvironmentVariable("CI")).ToLower() == "true";
bool valid = true;

if (args.Length < 2)
{
if (args.Length < 2) {
Console.WriteLine("Usage: validator.exe <schema> <manifest> [<manifest>...]");
return 1;
}


IList<string> manifests = args.ToList<String>();
String schema = manifests.First();
manifests.RemoveAt(0);
String combinedArgs = String.Join("", manifests);
if(combinedArgs.Contains("*") || combinedArgs.Contains("?")) {
if (combinedArgs.Contains("*") || combinedArgs.Contains("?")) {
try {
var path = new Uri(Path.Combine(Directory.GetCurrentDirectory(), combinedArgs)).LocalPath;
var drive = Path.GetPathRoot(path);
Expand All @@ -37,25 +32,21 @@ public static int Main(string[] args)
}

Scoop.Validator validator = new Scoop.Validator(schema, ci);
foreach(var manifest in manifests) {
if (validator.Validate(manifest))
{
if(ci) {
foreach (var manifest in manifests) {
if (validator.Validate(manifest)) {
if (ci) {
Console.WriteLine(" [+] {0} validates against the schema!", Path.GetFileName(manifest));
} else {
Console.WriteLine("- {0} validates against the schema!", Path.GetFileName(manifest));
}
}
else
{
if(ci) {
} else {
if (ci) {
Console.WriteLine(" [-] {0} has {1} Error{2}!", Path.GetFileName(manifest), validator.Errors.Count, validator.Errors.Count > 1 ? "s" : "");
} else {
Console.WriteLine("- {0} has {1} Error{2}!", Path.GetFileName(manifest), validator.Errors.Count, validator.Errors.Count > 1 ? "s" : "");
}
valid = false;
foreach (var error in validator.Errors)
{
foreach (var error in validator.Errors) {
Console.WriteLine(error);
}
}
Expand Down
17 changes: 16 additions & 1 deletion validator.csproj
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="17.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetFramework>net45</TargetFramework>
<PackageId>Scoop.Validator</PackageId>
<Version>1.0.3</Version>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<HintPath>packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json.Schema, Version=3.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<HintPath>packages\Newtonsoft.Json.Schema.3.0.13\lib\net45\Newtonsoft.Json.Schema.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
</Project>