My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
namespace Fake
open System.IO
open System.Text

[<AutoOpen>]
module FileHelper =
/// Sets the directory readonly
let setDirectoryReadOnly readOnly (dir:DirectoryInfo) =
if dir.Exists then
let isReadOnly = dir.Attributes &&& FileAttributes.ReadOnly = FileAttributes.ReadOnly
if readOnly && (not isReadOnly) then
dir.Attributes <- dir.Attributes ||| FileAttributes.ReadOnly
if (not readOnly) && not isReadOnly then
dir.Attributes <- dir.Attributes &&& (~~~FileAttributes.ReadOnly)

/// Sets all files in the directory readonly
let rec SetDirReadOnly readOnly (dir:DirectoryInfo) =
dir.GetDirectories() |> Seq.iter (fun dir ->
SetDirReadOnly readOnly dir
setDirectoryReadOnly readOnly dir)
dir.GetFiles() |> Seq.iter (fun file -> file.IsReadOnly <- readOnly)

/// Sets all files in the directory readonly
let SetReadOnly readOnly (files: string seq) =
files |> Seq.iter (fun file ->
let fi = new FileInfo(file)
if fi.Exists then
fi.IsReadOnly <- readOnly
else
let di = new DirectoryInfo(file)
setDirectoryReadOnly readOnly di)


/// Deletes a directory if it exists
let DeleteDir x =
let dir = new DirectoryInfo(x)
if dir.Exists then
// set all files readonly = false
!+ "/**/*.*"
|> SetBaseDir dir.FullName
|> Scan
|> (SetReadOnly false)

log <| sprintf "Deleting %s" dir.FullName
dir.Delete true
else
log <| sprintf "%s does not exist." dir.FullName

/// Creates a directory if it does not exist
let CreateDir x =
let dir = new DirectoryInfo(x)
if not dir.Exists then
log <| sprintf "Creating %s" dir.FullName
dir.Create()
else
log <| sprintf "%s does already exist." dir.FullName

/// Creates a file if it does not exist
let CreateFile x =
let file = new FileInfo(x)
if not file.Exists then
log <| sprintf "Creating %s" file.FullName
let newFile = file.Create()
newFile.Close()
else
log <| sprintf "%s does already exist." file.FullName

/// Deletes a file if it exist
let DeleteFile x =
let file = new FileInfo(x)
if file.Exists then
log <| sprintf "Deleting %s" file.FullName
file.Delete()
else
log <| sprintf "%s does not exist." file.FullName

let (|File|Directory|) (fileSysInfo : FileSystemInfo) =
match fileSysInfo with
| :? FileInfo as file -> File (file.Name)
| :? DirectoryInfo as dir -> Directory (dir.Name, seq { for x in dir.GetFileSystemInfos() -> x })
| _ -> failwith "No file or directory given."

/// Active Pattern for determining file extension
let (|EndsWith|_|) extension (file : string) =
if file.EndsWith extension then Some() else None

/// Active Pattern for determining file name
let (|FileInfoFullName|) (f:FileInfo) = f.FullName

/// Active Pattern for determining FileInfoNameSections
let (|FileInfoNameSections|) (f:FileInfo) = (f.Name,f.Extension,f.FullName)

/// Copies a single file to the target
/// param target: The targetDirectory
/// param file: The fileName
let CopyFile target file =
let fi = new FileInfo(file)
let targetName = target + fi.Name
log <| sprintf "Copy %s to %s" file targetName
fi.CopyTo(targetName,true) |> ignore


/// Copies the files to the target
let Copy target files =
files |> Seq.iter (CopyFile target)

let SilentCopy target files =
files |> Seq.iter (fun file ->
let fi = new FileInfo(file)
let targetName = target + fi.Name
let targetFI = new FileInfo(targetName)
if targetFI.Exists then
if fi.LastWriteTime > targetFI.LastWriteTime then
targetFI.Attributes <- FileAttributes.Normal
fi.CopyTo(targetName,true) |> ignore
else
fi.CopyTo(targetName) |> ignore)


/// Copies the files to the target
let CopyFiles target files = Copy target files

/// Exclude SVN files (path with .svn)
let excludeSVNFiles (path:string) = not <| path.Contains ".svn"

/// Includes all files
let allFiles (path:string) = true

/// Copies a directory recursivly
/// If the target directory does not exist, it will be created
let CopyDir target source filterFile =
CreateDir target
Directory.GetFiles(source, "*.*", SearchOption.AllDirectories)
|> Seq.filter filterFile
|> Seq.iter (fun file ->
let newFile = target + file.Remove(0, source.Length)
log <| sprintf "%s => %s" file newFile
Directory.CreateDirectory(Path.GetDirectoryName(newFile)) |> ignore
File.Copy(file, newFile, true))

/// Cleans a directory
let CleanDir dir =
let di = new DirectoryInfo(dir)
if di.Exists then
log <| sprintf "Deleting content of %s" dir
// delete all files
Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories)
|> Seq.iter (fun file ->
let fi = new FileInfo(file)
fi.IsReadOnly <- false
fi.Delete())

// deletes all subdirectories
let rec deleteDirs actDir =
Directory.GetDirectories(actDir) |> Seq.iter deleteDirs
Directory.Delete(actDir,true)

Directory.GetDirectories(dir) |> Seq.iter deleteDirs
else
CreateDir dir

// set writeable
File.SetAttributes(dir,FileAttributes.Normal)

/// Reads a file line by line
let ReadFile file = StringHelper.ReadFile file

/// Reads a csv file line by line
/// delimiter is a ,
let ReadCSVFile(file:string) =
let csvRegEx = new System.Text.RegularExpressions.Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))")

seq {for line in ReadFile file ->
csvRegEx.Split(line)
|> Array.map (fun s -> s.Trim([| '"' |]))}


/// Appends all given files to one file
let AppendTextFiles newFileName files =
let fi = new FileInfo(newFileName)
if fi.Exists then failwith "File %s already exists."
use writer = new StreamWriter(fi.FullName, false, Encoding.Default);

files
|> Seq.iter (fun file ->
log <| sprintf "Appending %s to %s" file fi.FullName
ReadFile file |> Seq.iter(fun line -> writer.WriteLine(line)))

writer.Close()

/// Converts a file to it's full file system name
let FullName fileName = (new FileInfo(fileName)).FullName

/// Compares the given files for changes
/// If delete = true then equal files will be removed
let CompareFiles delete originalFileName compareFileName =
let ori = new FileInfo(originalFileName)
let comp = new FileInfo(compareFileName)
if ori.Exists && comp.Exists && ori.LastWriteTime = comp.LastWriteTime && ori.Length = comp.Length then
if delete then
comp.Attributes <- FileAttributes.Normal
comp.Delete()
log <| sprintf "Deleting File: %s" comp.FullName
else
log <| sprintf "Files equal: %s" comp.FullName
true
else false

/// Checks if the directory exists
let TestDir dir =
let di = new DirectoryInfo(dir)

if di.Exists then true else
log <| sprintf "%s not found" di.FullName
false

/// Checks the srcFiles for changes to the last release
/// param lastReleaseDir: The directory of the last release
/// param patchDir: The target director
/// param srcFiles: The source files
let GeneratePatch lastReleaseDir patchDir srcFiles =
srcFiles
|> Seq.map (fun n ->
let newFile = toRelativePath n
let oldFile = lastReleaseDir + newFile.TrimStart('.')
newFile,oldFile)
|> Seq.filter (fun (newF,oldF) -> CompareFiles false oldF newF |> not)
|> Seq.iter (fun (newF,oldF) -> CopyFile patchDir newF)
Show details Hide details

Change log

r349 by steffen....@msu-solutions.de on Sep 30, 2009   Diff

 
Go to: 
Project members, sign in to write a code review

Older revisions

r348 by steffen....@msu-solutions.de on Sep 30, 2009   Diff

 
r341 by steffen....@msu-solutions.de on Sep 29, 2009   Diff
Patch generator
r324 by steffen....@msu-solutions.de on Sep 26, 2009   Diff

 
All revisions of this file

File info

Size: 8252 bytes, 228 lines
Hosted by Google Code