Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Project =
- open System
- open System.IO
- open System.Text
- module private Directory =
- let lazyCreate (dir : string) =
- if dir |> Directory.Exists |> not
- then Directory.CreateDirectory dir |> ignore
- let private replaceAll (values: Map<string, string>) (text : string) =
- values
- |> Map.toSeq
- |> Seq.fold
- (fun (acc : StringBuilder) (key, value) ->
- acc.Replace(key, value))
- (new StringBuilder(text))
- |> fun result -> result.ToString()
- module private JsFile =
- let (|Ext|_|) (path : string) =
- path
- |> Path.GetExtension
- |> fun ext -> ext.ToLower()
- |> function
- | (".js" | ".ts" | ".jsx" | ".tsx") as jsExt -> Some jsExt
- | _ -> None
- let containsImageExt (line : string) =
- [".jpg"; ".jpeg"; ".png"; ".svg"; ".gif"]
- |> List.tryFind (fun ext -> line.Contains ext)
- |> function
- | Some _ -> true
- | _ -> false
- let normalizePaths (targetFilePath : string) (sourceFilePath : string) =
- sourceFilePath
- |> File.ReadAllLines
- |> Seq.ofArray
- |> Seq.map
- (fun line ->
- let trimLine = line.Trim()
- if trimLine.StartsWith("import ") && trimLine |> containsImageExt
- then trimLine.Replace("import ", "const ").Replace(" from ", " = ")
- else line)
- |> fun lines -> File.WriteAllLines(targetFilePath, lines)
- let toProjectWithNormalizedPaths (targetDirectoryPath : string) (sourceDirectoryPath : string) =
- sourceDirectoryPath
- |> fun path ->
- Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
- |> Seq.iter
- (fun sourceFilePath ->
- let path = sourceFilePath.[sourceDirectoryPath.Length + 1..]
- let targetPath = Path.Combine(targetDirectoryPath, path)
- targetPath |> Path.GetDirectoryName |> Directory.lazyCreate
- match path with
- | JsFile.Ext ext ->
- sourceFilePath |> JsFile.normalizePaths targetPath
- | _ -> File.Copy(sourceFilePath, targetPath, true))
- @"c:\Pluton.3D\Marketify\Sources" |> Project.toProjectWithNormalizedPaths @"c:\Pluton.3D\Marketify\Sources2"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement