Created
September 22, 2022 03:46
-
-
Save AngelMunoz/b5d191ea54254fe5c595ec3ccf30c4cc to your computer and use it in GitHub Desktop.
A small F# plugin to work in a PoC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#i "nuget: C:/Users/scyth/repos/Perla/src/nupkg" | |
#i "nuget: https://api.nuget.org/v3/index.json" | |
#r "nuget: Newtonsoft.Json, 12.0.3" | |
#r "nuget: CalceTypes, 1.0.2" | |
open System.IO | |
open System.Xml | |
open Newtonsoft.Json | |
open type System.Text.Encoding | |
open CalceTypes | |
let shouldTransform: OnShouldTransform = | |
fun args -> | |
let result = | |
[ ".xml"; ".fsproj"; ".csproj" ] | |
|> List.tryFind (fun exts -> args.extension.AsString = exts) | |
match result with | |
| Some _ -> Ok true | |
| _ -> Ok false | |
let transform: OnTransform = | |
fun args -> | |
let doc = XmlDocument() | |
doc.LoadXml args.content | |
try | |
let content = JsonConvert.SerializeXmlNode(doc) | |
{ content = content | |
targetExtension = FileExtension.Custom ".json" } | |
|> Ok | |
with ex -> | |
PluginError.Transform ex.Message |> Error | |
let Plugin: PluginInfo = | |
{ name = "xml-to-json" | |
pluginApi = PluginApi.Stable | |
load = None | |
shouldTransform = Some shouldTransform | |
transform = Some transform | |
injectImports = None } | |
Plugin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment