@@ -14,16 +14,18 @@ public extension BrewfileManager
1414{
1515 enum BrewfileDumpingError : LocalizedError
1616 {
17- case couldNotDetermineWorkingDirectory, errorWhileDumpingBrewfile( error: String ) , couldNotReadBrewfile( error: String )
17+ case couldNotDetermineWorkingDirectory
18+ case errorWhileDumpingBrewfile( errors: [ String ] )
19+ case couldNotReadBrewfile( error: String )
1820
1921 public var errorDescription : String ?
2022 {
2123 switch self
2224 {
2325 case . couldNotDetermineWorkingDirectory:
2426 return String ( localized: " error.brewfile.export.could-not-determine-working-directory " )
25- case . errorWhileDumpingBrewfile( let error ) :
26- return String ( localized: " error.brewfile.export.could-not-dump-with-error. \( error ) " )
27+ case . errorWhileDumpingBrewfile:
28+ return String ( localized: " error.brewfile.export.could-not-dump-with-error " )
2729 case . couldNotReadBrewfile( let error) :
2830 return error
2931 }
@@ -34,46 +36,38 @@ public extension BrewfileManager
3436 @MainActor
3537 func exportBrewfile( ) async throws ( BrewfileDumpingError) -> BrewbakFile
3638 {
37- let brewfileParentLocation : URL = URL . temporaryDirectory
38-
39- let pathRawOutput : [ TerminalOutput ] = await shell ( URL ( string: " /bin/pwd " ) !, [ " -L " ] )
40-
41- let brewfileDumpingResult : [ TerminalOutput ] = await shell ( AppConstants . shared. brewExecutablePath, [ " bundle " , " -f " , " dump " ] , workingDirectory: brewfileParentLocation)
42-
43- /// Throw an error if the working directory could not be determined
44- if pathRawOutput. containsErrors
45- {
46- throw BrewfileDumpingError . couldNotDetermineWorkingDirectory
47- }
39+ let brewfileParentLocation : URL = URL . temporaryDirectory. resolvingSymlinksInPath ( )
4840
49- guard let finalPathOutput: String = pathRawOutput. standardOutputs. first else
50- {
51- throw BrewfileDumpingError . couldNotDetermineWorkingDirectory
52- }
53-
54- /// Throw an error if the working directory is so fucked up it's unusable
55- guard let workingDirectory: URL = URL ( string: finalPathOutput. trimmingCharacters ( in: . whitespacesAndNewlines) ) else
56- {
57- throw BrewfileDumpingError . couldNotDetermineWorkingDirectory
58- }
41+ let finalBrewfileLocation : URL = brewfileParentLocation. appendingPathComponent ( " Brewfile " , conformingTo: . fileURL)
42+
43+ AppConstants . shared. logger. info ( " Brewfile parent location: \( brewfileParentLocation) " )
44+
45+ let brewfileDumpingResult : [ TerminalOutput ] = await shell ( AppConstants . shared. brewExecutablePath, [ " bundle " , " dump " , " --file " , finalBrewfileLocation. path] )
5946
60- if brewfileDumpingResult. standardErrors . contains ( " (E|e)rror " )
47+ guard ! brewfileDumpingResult. contains ( " Error " , in : . standardErrors , . standardOutputs ) else
6148 {
62- throw BrewfileDumpingError . errorWhileDumpingBrewfile ( error: brewfileDumpingResult. standardErrors. formatted ( . list( type: . and) ) )
49+ AppConstants . shared. logger. error ( " There was an error in the dumping result " )
50+
51+ throw BrewfileDumpingError . errorWhileDumpingBrewfile ( errors: brewfileDumpingResult. standardErrors)
6352 }
6453
65- AppConstants . shared. logger. info ( " Path: \( workingDirectory. path ( ) , privacy: . auto) " )
66-
6754 print ( " Brewfile dumping result: \( brewfileDumpingResult) " )
6855
69- let brewfileLocation : URL = brewfileParentLocation. appendingPathComponent ( " Brewfile " , conformingTo: . fileURL)
70-
56+ let doesBrewfileExist : Bool = FileManager . default. fileExists ( atPath: finalBrewfileLocation. path ( ) )
57+
58+ AppConstants . shared. logger. info ( " Does brewfile exist an expected location? \( doesBrewfileExist) " )
59+
7160 do
7261 {
73- let brewfileContents : String = try String ( contentsOf: brewfileLocation )
62+ let brewfileContents : String = try String ( contentsOf: finalBrewfileLocation )
7463
7564 /// Delete the brewfile
76- try ? FileManager . default. removeItem ( at: brewfileLocation)
65+ do
66+ {
67+ try FileManager . default. removeItem ( at: finalBrewfileLocation)
68+ } catch let tempBrewfileDeletionError {
69+ AppConstants . shared. logger. error ( " Fialed while deleting old brewfile: \( tempBrewfileDeletionError) " )
70+ }
7771
7872 return . init( text: brewfileContents)
7973 }
0 commit comments