Skip to content

Instantly share code, notes, and snippets.

@forki
Created February 16, 2014 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forki/9032066 to your computer and use it in GitHub Desktop.
Save forki/9032066 to your computer and use it in GitHub Desktop.
Split ObjectFile
module GitNav.Split
open System
open System.IO
open msu.Navision.NavisionFile
open msu.Navision
open Fake
let normalizedDateTime = new DateTime(2010,1,1,12,0,0)
let splitFileIntoLines (fileInfo:FileInfo) =
fileInfo.FullName
|> StringHelper.ReadFile
|> Seq.toList
|> NavisionFile.split
/// Generates a NavisionObject from seq of lines
let linesToNavisionObjects lines =
lines
|> Seq.map (separated "\r\n")
|> Seq.map NavisionObject.CreateFromObjectString
/// Splits a file and generates NavisionObjects
let splitFileIntoNavisionObjects fileInfo =
fileInfo
|> splitFileIntoLines
|> linesToNavisionObjects
open System.Text.RegularExpressions
let replace word text =
let regex = new Regex(@"\s*[$]" + word + @":\s([^$]*)\s*[$]", RegexOptions.Compiled)
regex.Replace(text,"")
let replaceAll text =
text
|> replace "Modtime"
|> replace "Revision"
|> replace "Author"
|> replace "Archive"
let splitFile delete toFolder cleanToFolder toArchive flat tagsToReplace normalizeDate normalizeVersion fileInfo =
let target = if toFolder = null then new DirectoryInfo(".\\splitted\\") else new DirectoryInfo(toFolder)
if cleanToFolder && target.Exists && (not toArchive) then
target.Delete true
target.Refresh()
if not target.Exists then
target.Create()
fileInfo
|> splitFileIntoLines
|> Seq.map (fun line ->
line
|> Seq.filter ((<>) " Modified=Yes;")
|> Seq.filter ((<>) " Modified=No;")
)
|> linesToNavisionObjects
|> Seq.map (fun (o:NavisionObject) ->
// TODO: Refactor this
if tagsToReplace |> Seq.exists (fun tag -> o.VersionTag.Contains tag) then
o.ObjectString <- replaceAll o.ObjectString
if normalizeDate then
o.UpdateDateTime normalizedDateTime
if normalizeVersion then
tagsToReplace
|> Seq.iter (fun tag ->
let testTag = tag + "TEST"
if o.VersionTag.Contains testTag then
o.ReplaceVersionTag(testTag,"")
else
o.ReplaceVersionTag(tag,""))
o)
|> Seq.iter (fun o ->
let s =
o.ObjectString
.Replace("\r\n}\r\n\r\n", "\r\n}")
.Replace("\r\n}\r\n", "\r\n}")
if toArchive then
let fileName = (if toFolder = "" || toFolder = null then "." else toFolder) + o.GetArchive()
o.SaveToFile fileName
logfn "File created: %s" fileName
else
NavisionFile.WriteFile target.FullName (not flat) s
|> logfn "File created: %s")
if delete then deleteFile fileInfo
let split delete toFolder cleanToFolder toArchive flat tagsToReplace normalizeDate normalizeVersion fileName =
splitFile delete toFolder cleanToFolder toArchive flat tagsToReplace normalizeDate normalizeVersion (new FileInfo(fileName))
@kfuglsang
Copy link

Your implementation seems more elegant than the one I came up with yesterday with the regex'es to detect objects in a file. Could you provide the NavisionFile module as well? Then I'll try to merge it into FAKE.

Btw: Also very curious about your GitNav module. The replaceAll function hints to me that you have Git revision numbers etc. inside your NAV object code/documentation?

@forki
Copy link
Author

forki commented Feb 18, 2014

We store the git revision in CDU1 (and replace it on the fly when we update the db objects)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment