Skip to content

Commit

Permalink
Fsdk,Tools: move Replace funcs to Fsdk
Browse files Browse the repository at this point in the history
So that they can be used from the nuget pkg.
  • Loading branch information
knocte committed May 2, 2023
1 parent 5c4f55b commit b128a51
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
39 changes: 39 additions & 0 deletions Fsdk/Misc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Fsdk

open System
open System.IO
open System.Text
open System.Reflection
open System.Linq
#if LEGACY_FRAMEWORK
Expand Down Expand Up @@ -753,3 +754,41 @@ module Misc =
)
else
failwithf "argument not recognized: %s" head


let ReplaceTextInFile
(file: FileInfo)
(oldString: string)
(newString: string)
=
let hasUtf8Bom(file: FileInfo) =
let fileContentInBytes = File.ReadAllBytes file.FullName

fileContentInBytes.Length > 2
&& fileContentInBytes.[..2] = [| 0xEFuy; 0xBBuy; 0xBFuy |]

let oldText = File.ReadAllText file.FullName
let newText = oldText.Replace(oldString, newString)

if newText <> oldText then
File.WriteAllText(
file.FullName,
newText,
UTF8Encoding(hasUtf8Bom file)
)

let rec ReplaceTextInDir
(dir: DirectoryInfo)
(oldString: string)
(newString: string)
=
for file in dir.GetFiles() do
// TODO: use @realmarv's method to find if it's binary file here
if file.Extension.ToLower() <> ".dll"
&& file.Extension.ToLower() <> ".exe"
&& file.Extension.ToLower() <> ".png" then
ReplaceTextInFile file oldString newString

for subFolder in dir.GetDirectories() do
if subFolder.Name <> ".git" then
ReplaceTextInDir subFolder oldString newString
33 changes: 2 additions & 31 deletions Tools/replace.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,13 @@

open System
open System.IO
open System.Text

#r "System.Configuration"
open System.Configuration

#load "../Fsdk/Misc.fs"
open Fsdk

let ReplaceInFile (file: FileInfo) (oldString: string) (newString: string) =
let hasUtf8Bom(file: FileInfo) =
let fileContentInBytes = File.ReadAllBytes file.FullName

fileContentInBytes.Length > 2
&& fileContentInBytes.[..2] = [| 0xEFuy; 0xBBuy; 0xBFuy |]

let oldText = File.ReadAllText file.FullName
let newText = oldText.Replace(oldString, newString)

if newText <> oldText then
File.WriteAllText(file.FullName, newText, UTF8Encoding(hasUtf8Bom file))

let rec ReplaceInDir
(dir: DirectoryInfo)
(oldString: string)
(newString: string)
=
for file in dir.GetFiles() do
if file.Extension.ToLower() <> ".dll"
&& file.Extension.ToLower() <> ".exe"
&& file.Extension.ToLower() <> ".png" then
ReplaceInFile file oldString newString

for subFolder in dir.GetDirectories() do
if subFolder.Name <> ".git" then
ReplaceInDir subFolder oldString newString

let args = Misc.FsxOnlyArguments()

let errTooManyArgs =
Expand Down Expand Up @@ -80,7 +51,7 @@ match particularFile with
| None ->
let startDir = DirectoryInfo(Directory.GetCurrentDirectory())
let oldString, newString = args.[0], args.[1]
ReplaceInDir startDir oldString newString
Misc.ReplaceTextInDir startDir oldString newString
| Some file ->
let oldString, newString = args.[1], args.[2]
ReplaceInFile file oldString newString
Misc.ReplaceTextInFile file oldString newString

0 comments on commit b128a51

Please sign in to comment.