Skip to content

Commit

Permalink
Merge pull request #15 from dawedawe/fix_14
Browse files Browse the repository at this point in the history
Construct fallback xml path
  • Loading branch information
dawedawe authored Aug 20, 2024
2 parents 1133967 + 2ddbd22 commit bac8c19
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [1.0.3] - 2024-08-20

### Fixed

* Fixed support for non-FSharp.Core namespaces, #14.

## [1.0.2] - 2024-05-18

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.300",
"version": "8.0.400",
"rollForward": "latestPatch"
}
}
3 changes: 2 additions & 1 deletion src/Fsih.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ let expressions =
<@ Array2D.blit @> :> Expr, "Microsoft.FSharp.Collections.Array2DModule.blit"
<@ HashIdentity.Reference @> :> Expr, "Microsoft.FSharp.Collections.HashIdentity.Reference"
<@ Array.Parallel.tryFind @> :> Expr, "Microsoft.FSharp.Collections.ArrayModule.Parallel.tryFind"
<@ (|>) @> :> Expr, "Microsoft.FSharp.Core.Operators.op_PipeRight" ]
<@ (|>) @> :> Expr, "Microsoft.FSharp.Core.Operators.op_PipeRight"
<@ System.Console.ReadLine @> :> Expr, "System.Console.ReadLine" ]
)

[<Theory>]
Expand Down
36 changes: 33 additions & 3 deletions src/Fsih/Parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Fsih.Parser

open System
open System.IO
open System.Text.RegularExpressions
open System.Xml
open Spectre.Console

Expand Down Expand Up @@ -68,6 +69,23 @@ let trimDotNet (s: string) =
// WTF, seems like we loose inner xml (example content) if we cache an XmlDocument
let xmlDocCache = Collections.Generic.Dictionary<string, string>()

let toFallbackXmlPath (xmlPath: string) =
let replaceLast (s: string) (oldValue: string) (newValue: string) =
let idx = s.LastIndexOf(oldValue)

if idx >= 0 then
s.Substring(0, idx) + newValue + s.Substring(idx + oldValue.Length)
else
s

let sep = Path.DirectorySeparatorChar
let xmlPath = replaceLast xmlPath "shared" "packs"
let xmlPath = replaceLast xmlPath $".App{sep}" $".App.Ref{sep}"
let version = Regex.Match(xmlPath, @"\d+\.\d+\.\d+").Value
let release = version.Substring(0, version.LastIndexOf('.'))
let xmlPath = replaceLast xmlPath version $"{version}{sep}ref{sep}net{release}"
xmlPath

let tryGetXmlDocument xmlPath =
#if DEBUG
printfn $"trying xml file: %s{xmlPath}"
Expand All @@ -79,14 +97,26 @@ let tryGetXmlDocument xmlPath =
xmlDocument.LoadXml(value)
Some xmlDocument
| _ ->
let rawXml = System.IO.File.ReadAllText(xmlPath)
let fileExists = File.Exists(xmlPath)

let xmlPath =
if fileExists then
xmlPath
else
let fallback = toFallbackXmlPath xmlPath
#if DEBUG
printfn $"falling back to xml file: %s{fallback}"
#endif
fallback

let rawXml = File.ReadAllText(xmlPath)
let xmlDocument = XmlDocument()
xmlDocument.LoadXml(rawXml)
xmlDocCache.Add(xmlPath, rawXml)
xmlDocCache[xmlPath] <- rawXml
Some xmlDocument
with _ ->
#if DEBUG
printfn $"xml file not found: {xmlPath}"
printfn $"xml file not found"
#endif
None

Expand Down

0 comments on commit bac8c19

Please sign in to comment.