Advertisement
Guest User

Untitled

a guest
May 30th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.47 KB | None | 0 0
  1. module Project =
  2.  
  3.     open System
  4.     open System.IO
  5.     open System.Text
  6.  
  7.     module private Directory =
  8.         let lazyCreate (dir : string) =
  9.             if dir |> Directory.Exists |> not
  10.             then Directory.CreateDirectory dir |> ignore
  11.  
  12.     let private replaceAll (values: Map<string, string>) (text : string) =
  13.         values
  14.         |> Map.toSeq
  15.         |> Seq.fold
  16.             (fun (acc : StringBuilder) (key, value) ->
  17.                 acc.Replace(key, value))
  18.             (new StringBuilder(text))
  19.         |> fun result -> result.ToString()
  20.  
  21.     module private JsFile =
  22.  
  23.         let (|Ext|_|) (path : string) =
  24.             path
  25.             |> Path.GetExtension
  26.             |> fun ext -> ext.ToLower()
  27.             |> function
  28.             | (".js" | ".ts" | ".jsx" | ".tsx") as jsExt -> Some jsExt
  29.             | _ -> None
  30.  
  31.         let containsImageExt (line : string) =
  32.             [".jpg"; ".jpeg"; ".png"; ".svg"; ".gif"]
  33.             |> List.tryFind (fun ext -> line.Contains ext)
  34.             |> function
  35.             | Some _ -> true
  36.             | _ -> false
  37.            
  38.  
  39.         let normalizePaths (targetFilePath : string) (sourceFilePath : string) =
  40.             sourceFilePath
  41.             |> File.ReadAllLines
  42.             |> Seq.ofArray
  43.             |> Seq.map
  44.                 (fun line ->
  45.                     let trimLine = line.Trim()
  46.                     if trimLine.StartsWith("import ") && trimLine |> containsImageExt
  47.                     then trimLine.Replace("import ", "const ").Replace(" from ", " = ")
  48.                     else line)
  49.             |> fun lines -> File.WriteAllLines(targetFilePath, lines)
  50.  
  51.     let toProjectWithNormalizedPaths (targetDirectoryPath : string) (sourceDirectoryPath : string) =
  52.         sourceDirectoryPath
  53.         |> fun path ->
  54.             Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
  55.         |> Seq.iter
  56.             (fun sourceFilePath ->
  57.                 let path = sourceFilePath.[sourceDirectoryPath.Length + 1..]
  58.                 let targetPath = Path.Combine(targetDirectoryPath, path)
  59.                 targetPath |> Path.GetDirectoryName |> Directory.lazyCreate
  60.                 match path with
  61.                 | JsFile.Ext ext ->
  62.                     sourceFilePath |> JsFile.normalizePaths targetPath
  63.                 | _ -> File.Copy(sourceFilePath, targetPath, true))
  64.  
  65. @"c:\Pluton.3D\Marketify\Sources" |> Project.toProjectWithNormalizedPaths @"c:\Pluton.3D\Marketify\Sources2"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement