|
| 1 | +module Fable.Compiler.ProjectParser |
| 2 | + |
| 3 | +open Fable.Compiler.Platform |
| 4 | +open System.Collections.Generic |
| 5 | +open System.Text.RegularExpressions |
| 6 | + |
| 7 | +let (|Regex|_|) (pattern: string) (input: string) = |
| 8 | + let m = Regex.Match(input, pattern) |
| 9 | + if m.Success then |
| 10 | + let mutable groups = [] |
| 11 | + for i = m.Groups.Count - 1 downto 0 do |
| 12 | + groups <- m.Groups.[i].Value::groups |
| 13 | + Some groups |
| 14 | + else None |
| 15 | + |
| 16 | +let parseCompilerOptions projectText = |
| 17 | + |
| 18 | + // get project type |
| 19 | + let m = Regex.Match(projectText, @"<OutputType[^>]*>([^<]*)<\/OutputType[^>]*>") |
| 20 | + let target = if m.Success then m.Groups.[1].Value.Trim().ToLowerInvariant() else "" |
| 21 | + |
| 22 | + // get warning level |
| 23 | + let m = Regex.Match(projectText, @"<WarningLevel[^>]*>([^<]*)<\/WarningLevel[^>]*>") |
| 24 | + let warnLevel = if m.Success then m.Groups.[1].Value.Trim() else "" |
| 25 | + |
| 26 | + // get treat warnings as errors |
| 27 | + let m = Regex.Match(projectText, @"<TreatWarningsAsErrors[^>]*>([^<]*)<\/TreatWarningsAsErrors[^>]*>") |
| 28 | + let treatWarningsAsErrors = m.Success && m.Groups.[1].Value.Trim().ToLowerInvariant() = "true" |
| 29 | + |
| 30 | + // get conditional defines |
| 31 | + let defines = |
| 32 | + Regex.Matches(projectText, @"<DefineConstants[^>]*>([^<]*)<\/DefineConstants[^>]*>") |
| 33 | + |> Seq.collect (fun m -> m.Groups.[1].Value.Split(';')) |
| 34 | + |> Seq.append ["FABLE_COMPILER"] |
| 35 | + |> Seq.map (fun s -> s.Trim()) |
| 36 | + |> Seq.distinct |
| 37 | + |> Seq.except ["$(DefineConstants)"; ""] |
| 38 | + |> Seq.toArray |
| 39 | + |
| 40 | + // get disabled warnings |
| 41 | + let nowarns = |
| 42 | + Regex.Matches(projectText, @"<NoWarn[^>]*>([^<]*)<\/NoWarn[^>]*>") |
| 43 | + |> Seq.collect (fun m -> m.Groups.[1].Value.Split(';')) |
| 44 | + |> Seq.map (fun s -> s.Trim()) |
| 45 | + |> Seq.distinct |
| 46 | + |> Seq.except ["$(NoWarn)"; ""] |
| 47 | + |> Seq.toArray |
| 48 | + |
| 49 | + // get warnings as errors |
| 50 | + let warnAsErrors = |
| 51 | + Regex.Matches(projectText, @"<WarningsAsErrors[^>]*>([^<]*)<\/WarningsAsErrors[^>]*>") |
| 52 | + |> Seq.collect (fun m -> m.Groups.[1].Value.Split(';')) |
| 53 | + |> Seq.map (fun s -> s.Trim()) |
| 54 | + |> Seq.distinct |
| 55 | + |> Seq.except ["$(WarningsAsErrors)"; ""] |
| 56 | + |> Seq.toArray |
| 57 | + |
| 58 | + // get other flags |
| 59 | + let otherFlags = |
| 60 | + Regex.Matches(projectText, @"<OtherFlags[^>]*>([^<]*)<\/OtherFlags[^>]*>") |
| 61 | + |> Seq.collect (fun m -> m.Groups.[1].Value.Split(' ')) |
| 62 | + |> Seq.map (fun s -> s.Trim()) |
| 63 | + |> Seq.distinct |
| 64 | + |> Seq.except ["$(OtherFlags)"; ""] |
| 65 | + |> Seq.toArray |
| 66 | + |
| 67 | + let otherOptions = [| |
| 68 | + if target.Length > 0 then |
| 69 | + yield "--target:" + target |
| 70 | + if warnLevel.Length > 0 then |
| 71 | + yield "--warn:" + warnLevel |
| 72 | + if treatWarningsAsErrors then |
| 73 | + yield "--warnaserror+" |
| 74 | + for d in defines do yield "-d:" + d |
| 75 | + for n in nowarns do yield "--nowarn:" + n |
| 76 | + for e in warnAsErrors do yield "--warnaserror:" + e |
| 77 | + for o in otherFlags do yield o |
| 78 | + |] |
| 79 | + otherOptions |
| 80 | + |
| 81 | +let parseProjectScript projectPath = |
| 82 | + let projectFileName = Path.GetFileName projectPath |
| 83 | + let projectText = readAllText projectPath |
| 84 | + let projectDir = Path.GetDirectoryName projectPath |
| 85 | + let dllRefs, srcFiles = |
| 86 | + (([||], [||]), projectText.Split('\n')) |
| 87 | + ||> Array.fold (fun (dllRefs, srcFiles) line -> |
| 88 | + let line = line.Trim() |
| 89 | + match line.Trim() with |
| 90 | + | Regex @"^#r\s+""(.*?)""$" [_;path] |
| 91 | + when not(path.EndsWith("Fable.Core.dll")) -> |
| 92 | + Array.append [|Path.Combine(projectDir, path)|] dllRefs, srcFiles |
| 93 | + | Regex @"^#load\s+""(.*?)""$" [_;path] -> |
| 94 | + dllRefs, Array.append [|Path.Combine(projectDir, path)|] srcFiles |
| 95 | + | _ -> dllRefs, srcFiles) |
| 96 | + let projectRefs = [||] |
| 97 | + let sourceFiles = Array.append srcFiles [|Path.GetFileName projectPath|] |
| 98 | + let otherOptions = [| "--define:FABLE_COMPILER" |] |
| 99 | + (projectFileName, dllRefs, projectRefs, sourceFiles, otherOptions) |
| 100 | + |
| 101 | +let parseProjectFile projectPath = |
| 102 | + let projectFileName = Path.GetFileName projectPath |
| 103 | + let projectText = readAllText projectPath |
| 104 | + |
| 105 | + // remove all comments |
| 106 | + let projectText = Regex.Replace(projectText, @"<!--[\s\S]*?-->", "") |
| 107 | + |
| 108 | + // get project references |
| 109 | + let projectRefs = |
| 110 | + Regex.Matches(projectText, @"<ProjectReference\s+[^>]*Include\s*=\s*(""[^""]*|'[^']*)") |
| 111 | + |> Seq.map (fun m -> m.Groups.[1].Value.TrimStart('"').TrimStart(''').Trim().Replace("\\", "/")) |
| 112 | + |> Seq.toArray |
| 113 | + |
| 114 | + // replace some variables |
| 115 | + let projectText = projectText.Replace(@"$(MSBuildProjectDirectory)", __dirname) |
| 116 | + let m = Regex.Match(projectText, @"<FSharpSourcesRoot[^>]*>([^<]*)<\/FSharpSourcesRoot[^>]*>") |
| 117 | + let sourcesRoot = if m.Success then m.Groups.[1].Value.Replace("\\", "/") else "" |
| 118 | + let projectText = projectText.Replace(@"$(FSharpSourcesRoot)", sourcesRoot) |
| 119 | + |
| 120 | + // get source files |
| 121 | + let sourceFilesRegex = @"<Compile\s+[^>]*Include\s*=\s*(""[^""]*|'[^']*)" |
| 122 | + let sourceFiles = |
| 123 | + Regex.Matches(projectText, sourceFilesRegex) |
| 124 | + |> Seq.map (fun m -> m.Groups.[1].Value.TrimStart('"').TrimStart(''').Trim().Replace("\\", "/")) |
| 125 | + |> Seq.toArray |
| 126 | + |
| 127 | + let dllRefs = [||] |
| 128 | + let otherOptions = parseCompilerOptions projectText |
| 129 | + (projectFileName, dllRefs, projectRefs, sourceFiles, otherOptions) |
| 130 | + |
| 131 | +let makeHashSetIgnoreCase () = |
| 132 | + let equalityComparerIgnoreCase = |
| 133 | + { new IEqualityComparer<string> with |
| 134 | + member __.Equals(x, y) = x.ToLowerInvariant() = y.ToLowerInvariant() |
| 135 | + member __.GetHashCode(x) = hash (x.ToLowerInvariant()) } |
| 136 | + HashSet<string>(equalityComparerIgnoreCase) |
| 137 | + |
| 138 | +let dedupProjectRefs (projSet: HashSet<string>) projectRefs = |
| 139 | + let newRefs = projectRefs |> Array.filter (fun x -> projSet.Contains(x) |> not) |
| 140 | + projSet.UnionWith(newRefs) |
| 141 | + newRefs |
| 142 | + |
| 143 | +let dedupFileNames (fileSet: HashSet<string>) fileNames = |
| 144 | + let padName (fileName: string) = |
| 145 | + let pos = fileName.LastIndexOf(".") |
| 146 | + let nm = if pos < 0 then fileName else fileName.Substring(0, pos) |
| 147 | + let ext = if pos < 0 then "" else fileName.Substring(pos) |
| 148 | + nm + "_" + ext |
| 149 | + let rec dedup fileName = |
| 150 | + if fileSet.Contains(fileName) then |
| 151 | + dedup (padName fileName) |
| 152 | + else |
| 153 | + fileSet.Add(fileName) |> ignore |
| 154 | + fileName |
| 155 | + fileNames |> Array.map dedup |
| 156 | + |
| 157 | +let rec parseProject (projSet: HashSet<string>) (projectPath: string) = |
| 158 | + let (projectFileName, dllRefs, projectRefs, sourceFiles, otherOptions) = |
| 159 | + if projectPath.EndsWith(".fsx") |
| 160 | + then parseProjectScript projectPath |
| 161 | + else parseProjectFile projectPath |
| 162 | + |
| 163 | + let projectFileDir = Path.GetDirectoryName projectPath |
| 164 | + let isAbsolutePath (path: string) = path.StartsWith("/") || path.IndexOf(":") = 1 |
| 165 | + let makePath path = |
| 166 | + if isAbsolutePath path then path |
| 167 | + else Path.Combine(projectFileDir, path) |
| 168 | + |> normalizeFullPath |
| 169 | + |
| 170 | + let sourcePaths = sourceFiles |> Array.map makePath |
| 171 | + let sourceTexts = sourcePaths |> Array.map readAllText |
| 172 | + |
| 173 | + // parse and combine all referenced projects into one big project |
| 174 | + let parsedProjects = projectRefs |> Array.map makePath |> dedupProjectRefs projSet |> Array.map (parseProject projSet) |
| 175 | + let sourcePaths = sourcePaths |> Array.append (parsedProjects |> Array.collect (fun (_,_,x,_,_) -> x)) |
| 176 | + let sourceTexts = sourceTexts |> Array.append (parsedProjects |> Array.collect (fun (_,_,_,x,_) -> x)) |
| 177 | + let otherOptions = otherOptions |> Array.append (parsedProjects |> Array.collect (fun (_,_,_,_,x) -> x)) |
| 178 | + |
| 179 | + (projectFileName, dllRefs, sourcePaths, sourceTexts, otherOptions |> Array.distinct) |
0 commit comments