@@ -40,52 +40,59 @@ public func generate(dstdir: String, projectName: String, srcroot: String, modul
40
40
let schemeName = " \( projectName) .xcscheme "
41
41
42
42
////// the pbxproj file describes the project and its targets
43
- try open ( xcodeprojPath, " project.pbxproj " ) { fwrite in
44
- try pbxproj ( srcroot: srcroot, projectRoot: dstdir, xcodeprojPath: xcodeprojPath, modules: modules, externalModules: externalModules, products: products, options: options, printer: fwrite )
43
+ try open ( xcodeprojPath, " project.pbxproj " ) { stream in
44
+ try pbxproj ( srcroot: srcroot, projectRoot: dstdir, xcodeprojPath: xcodeprojPath, modules: modules, externalModules: externalModules, products: products, options: options, printer: stream )
45
45
}
46
46
47
47
////// the scheme acts like an aggregate target for all our targets
48
48
/// it has all tests associated so CMD+U works
49
- try open ( schemesDirectory, schemeName) { fwrite in
50
- xcscheme ( container: xcodeprojName, modules: modules, printer: fwrite )
49
+ try open ( schemesDirectory, schemeName) { stream in
50
+ xcscheme ( container: xcodeprojName, modules: modules, printer: stream )
51
51
}
52
52
53
53
////// we generate this file to ensure our main scheme is listed
54
54
/// before any inferred schemes Xcode may autocreate
55
- try open ( schemesDirectory, " xcschememanagement.plist " ) { fwrite in
56
- fwrite ( " <?xml version= \" 1.0 \" encoding= \" UTF-8 \" ?> " )
57
- fwrite ( " <plist version= \" 1.0 \" > " )
58
- fwrite ( " <dict> " )
59
- fwrite ( " <key>SchemeUserState</key> " )
60
- fwrite ( " <dict> " )
61
- fwrite ( " <key> \( schemeName) </key> " )
62
- fwrite ( " <dict></dict> " )
63
- fwrite ( " </dict> " )
64
- fwrite ( " <key>SuppressBuildableAutocreation</key> " )
65
- fwrite ( " <dict></dict> " )
66
- fwrite ( " </dict> " )
67
- fwrite ( " </plist> " )
55
+ try open ( schemesDirectory, " xcschememanagement.plist " ) { print in
56
+ print ( " <?xml version= \" 1.0 \" encoding= \" UTF-8 \" ?> " )
57
+ print ( " <plist version= \" 1.0 \" > " )
58
+ print ( " <dict> " )
59
+ print ( " <key>SchemeUserState</key> " )
60
+ print ( " <dict> " )
61
+ print ( " <key> \( schemeName) </key> " )
62
+ print ( " <dict></dict> " )
63
+ print ( " </dict> " )
64
+ print ( " <key>SuppressBuildableAutocreation</key> " )
65
+ print ( " <dict></dict> " )
66
+ print ( " </dict> " )
67
+ print ( " </plist> " )
68
68
}
69
69
70
70
return xcodeprojPath
71
71
}
72
72
73
+ import class Foundation. NSData
73
74
74
- private func open( _ path: String ... , body: ( ( String ) -> Void ) throws -> Void ) throws {
75
- var error : ErrorProtocol ? = nil
76
-
77
- try Utility . fopen ( Path . join ( path) , mode: . Write) { fp in
78
- try body { line in
79
- if error == nil {
80
- do {
81
- try fputs ( line, fp)
82
- try fputs ( " \n " , fp)
83
- } catch let caught {
84
- error = caught
85
- }
86
- }
75
+ /// Writes the contents to the file specified.
76
+ /// Doesn't re-writes the file in case the new and old contents of file are same.
77
+ func open( _ path: String ... , body: ( ( String ) -> Void ) throws -> Void ) throws {
78
+ let path = Path . join ( path)
79
+ let stream = OutputByteStream ( )
80
+ try body { line in
81
+ stream <<< line
82
+ stream <<< " \n "
83
+ }
84
+ // If file is already present compare its content with our stream
85
+ // and re-write only if its new.
86
+ if path. isFile, let data = NSData ( contentsOfFile: path) {
87
+ var contents = [ UInt8] ( repeating: 0 , count: data. length / sizeof( UInt8) )
88
+ data. getBytes ( & contents, length: data. length)
89
+ // If contents are same then no need to re-write.
90
+ if contents == stream. bytes. bytes {
91
+ return
87
92
}
88
93
}
89
-
90
- guard error == nil else { throw error! }
94
+ // Write the real file.
95
+ try fopen ( path, mode: . Write) { fp in
96
+ try fputs ( stream. bytes. bytes, fp)
97
+ }
91
98
}
0 commit comments