-
Notifications
You must be signed in to change notification settings - Fork 520
Support json as config as well as yaml #355
Changes from all commits
377f68b
7464c28
723a0e1
2f53df6
cf8b423
03b9924
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Linq; | ||
using YamlDotNet.Serialization; | ||
using YamlDotNet.Serialization.NamingConventions; | ||
|
||
|
@@ -17,5 +18,23 @@ public static ISerializer CreateSerializer() | |
.WithEmissionPhaseObjectGraphVisitor(args => new OmitDefaultAndEmptyArrayObjectGraphVisitor(args.InnerVisitor)) | ||
.Build(); | ||
} | ||
|
||
public static string ConvertToJson(string yamlContent) | ||
{ | ||
var schema = "\"$schema\" : \"https://raw.githubusercontent.com/dotnet/tye/master/src/schema/tye-schema.json\","; | ||
var deserializer = new DeserializerBuilder().Build(); | ||
var yamlObject = deserializer.Deserialize<string>(yamlContent); | ||
|
||
var serializer = new SerializerBuilder() | ||
.JsonCompatible() | ||
.Build(); | ||
|
||
var json = serializer.Serialize(yamlObject); | ||
|
||
if (string.IsNullOrEmpty(json)) | ||
json = json.Insert(json.IndexOf("{") + 1, schema); | ||
|
||
return json; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be really nice if this could also inject There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added. |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,21 +6,30 @@ namespace Microsoft.Tye | |
{ | ||
public static class InitHost | ||
{ | ||
public static string CreateTyeFile(FileInfo? path, bool force) | ||
public static string CreateTyeFile(FileInfo? path, bool force, bool json = false) | ||
{ | ||
var (content, outputFilePath) = CreateTyeFileContent(path, force); | ||
var (content, outputFilePath) = CreateTyeFileContent(path, force, json); | ||
|
||
File.WriteAllText(outputFilePath, content); | ||
if (json) | ||
{ | ||
var jsonContent = YamlSerializer.ConvertToJson(content); | ||
File.WriteAllText(outputFilePath, jsonContent); | ||
} | ||
else | ||
{ | ||
File.WriteAllText(outputFilePath, content); | ||
} | ||
|
||
return outputFilePath; | ||
} | ||
|
||
public static (string, string) CreateTyeFileContent(FileInfo? path, bool force) | ||
public static (string, string) CreateTyeFileContent(FileInfo? path, bool force, bool json = false) | ||
{ | ||
if (path is FileInfo && path.Exists && !force) | ||
{ | ||
ThrowIfTyeFilePresent(path, "tye.yml"); | ||
ThrowIfTyeFilePresent(path, "tye.yaml"); | ||
ThrowIfTyeFilePresent(path, "tye.json"); | ||
} | ||
|
||
var template = @" | ||
|
@@ -50,7 +59,7 @@ public static (string, string) CreateTyeFileContent(FileInfo? path, bool force) | |
|
||
// Output in the current directory unless an input file was provided, then | ||
// output next to the input file. | ||
var outputFilePath = "tye.yaml"; | ||
var outputFilePath = json ? "tye.json" : "tye.yaml"; | ||
|
||
if (path is FileInfo && path.Exists) | ||
{ | ||
|
@@ -77,7 +86,7 @@ public static (string, string) CreateTyeFileContent(FileInfo? path, bool force) | |
} | ||
|
||
// If the input file is a sln/project then place the config next to it | ||
outputFilePath = Path.Combine(directory.FullName, "tye.yaml"); | ||
outputFilePath = Path.Combine(directory.FullName, outputFilePath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this do with our comments? https://github.com/dotnet/tye/pull/355/files#diff-03e018baedc83b3a7dd33c431001ccfdR48 when you do JSON? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments are not supported in JSON. Also consider the following note from http://www.yaml.org/spec/1.2/spec.html section 3.2.3.3. Comments
|
||
} | ||
else | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this step even needed? (would YamlParser just parse the JSON document since JSON is YAML).
I'm thinking about things like line-numbers matching up if we do a conversion step. We just did a bunch of work in the yaml parser/validator to get better error messages with line numbers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, would have to test. First though is no. Yaml is format (space) specific where JSON can be one line no spaces and still be valid.