@@ -8,80 +8,90 @@ public enum APIClient {
8
8
9
9
public static func bow( moduleName: String , schema: String , output: String ) -> EnvIO < Environment , APIClientError , String > {
10
10
let env = EnvIO < Environment , APIClientError , Environment > . var ( )
11
- let validated = EnvIO < Environment , APIClientError , String > . var ( )
12
- let template = EnvIO < Environment , APIClientError , URL > . var ( )
13
- let schema = schema. expandingTildeInPath
14
- let output = output. expandingTildeInPath
11
+ let templates = EnvIO < Environment , APIClientError , URL > . var ( )
12
+ let generated = EnvIO < Environment , APIClientError , String > . var ( )
15
13
16
14
return binding (
17
- env <- . ask( ) ,
18
- validated <- validate ( schema: schema) ,
19
- template <- env. get. generator. getTemplates ( ) . contramap ( \. fileSystem) ,
20
- |<- bow( moduleName: moduleName, scheme: validated. get, output: output, template: template. get) ,
21
- yield: " RENDER SUCCEEDED " ) ^
15
+ env <- . ask( ) ,
16
+ templates <- env. get. generator. getTemplates ( ) ,
17
+ generated <- bow ( moduleName: moduleName, scheme: schema, output: output, templates: templates. get) ,
18
+ yield: generated. get) ^
22
19
}
23
20
24
- public static func bow( moduleName: String , scheme: String , output: String , template: URL ) -> EnvIO < Environment , APIClientError , String > {
25
- EnvIO { env in
26
- let outputPath = OutputPath ( sources: " \( output) /Sources " ,
27
- tests: " \( output) /XCTest " )
28
-
29
- return binding (
30
- |<- createStructure( outputPath: outputPath) . provide ( env. fileSystem) ,
31
- |<- env. generator. generate ( moduleName: moduleName,
32
- schemePath: scheme,
33
- outputPath: outputPath,
34
- template: template,
35
- logPath: env. logPath) . provide ( env. fileSystem) ,
36
- |<- createSwiftPackage( moduleName: moduleName, outputPath: output, template: template) . provide ( env. fileSystem) ,
37
- yield: " RENDER SUCCEEDED " ) ^
38
- }
21
+ public static func bow( moduleName: String , scheme: String , output: String , templates: URL ) -> EnvIO < Environment , APIClientError , String > {
22
+ let outputURL = URL ( fileURLWithPath: output. expandingTildeInPath)
23
+ let schemeURL = URL ( fileURLWithPath: scheme. expandingTildeInPath)
24
+ let module = OpenAPIModule ( name: moduleName,
25
+ url: outputURL,
26
+ schema: schemeURL,
27
+ templates: templates)
28
+
29
+ return bow ( module: module)
30
+ }
31
+
32
+ public static func bow( module: OpenAPIModule ) -> EnvIO < Environment , APIClientError , String > {
33
+ let env = EnvIO < Environment , APIClientError , Environment > . var ( )
34
+ let validated = EnvIO < Environment , APIClientError , OpenAPIModule > . var ( )
35
+
36
+ return binding (
37
+ env <- . ask( ) ,
38
+ validated <- validate ( module: module) ,
39
+ |<- createStructure( module: validated. get) ,
40
+ |<- env. get. generator. generate ( module: validated. get) ,
41
+ |<- createSwiftPackage( module: validated. get) ,
42
+ yield: " RENDER SUCCEEDED " ) ^
39
43
}
40
44
41
45
// MARK: attributes
42
- private static func validate( schema : String ) -> EnvIO < Environment , APIClientError , String > {
43
- EnvIO . invoke { _ in
44
- guard FileManager . default . fileExists ( atPath : schema) else {
46
+ private static func validate( module : OpenAPIModule ) -> EnvIO < Environment , APIClientError , OpenAPIModule > {
47
+ EnvIO . invoke { env in
48
+ guard env . fileSystem . exist ( item : module . schema) else {
45
49
throw APIClientError ( operation: " validate(schema:output:) " ,
46
50
error: GeneratorError . invalidParameters)
47
51
}
48
52
49
- return schema
53
+ return module
50
54
}
51
55
}
52
56
53
57
// MARK: steps
54
- internal static func createStructure( outputPath: OutputPath ) -> EnvIO < FileSystem , APIClientError , ( ) > {
55
- EnvIO { fileSystem in
56
- let parentPath = outputPath. sources. parentPath
58
+ internal static func createStructure( module: OpenAPIModule ) -> EnvIO < Environment , APIClientError , Void > {
59
+ EnvIO { env in
60
+ guard !env. fileSystem. exist ( item: module. url) else {
61
+ return . raiseError( APIClientError ( operation: " createStructure(atPath:) " , error: GeneratorError . existOutput ( directory: module. url) ) ) ^
62
+ }
57
63
58
- return fileSystem. removeDirectory ( parentPath) . handleError ( { _ in } ) ^
59
- . followedBy( fileSystem. createDirectory ( atPath: parentPath) ) ^
60
- . followedBy( fileSystem. createDirectory ( atPath: outputPath. sources) ) ^
61
- . followedBy( fileSystem. createDirectory ( atPath: outputPath. tests) ) ^
62
- . mapError { _ in APIClientError ( operation: " createStructure(atPath:) " , error: GeneratorError . structure) }
64
+ return env. fileSystem. createDirectory ( at: module. url, withIntermediateDirectories: true ) ^
65
+ . followedBy( env. fileSystem. createDirectory ( at: module. sources) ) ^
66
+ . followedBy( env. fileSystem. createDirectory ( at: module. tests) ) ^
67
+ . mapError { _ in APIClientError ( operation: " createStructure(atPath:) " , error: GeneratorError . structure) }
63
68
}
64
69
}
65
70
66
- internal static func createSwiftPackage( moduleName: String , outputPath: String , template: URL ) -> EnvIO < FileSystem , APIClientError , ( ) > {
67
- EnvIO { fileSystem in
68
- fileSystem. copy ( item: " Package.swift " , from: template. path, to: outputPath) ^
69
- } . followedBy ( package ( moduleName: moduleName, outputPath: outputPath) ) ^
70
- . mapError( FileSystemError . toAPIClientError)
71
- }
72
-
73
- internal static func package ( moduleName: String , outputPath: String ) -> EnvIO < FileSystem , FileSystemError , ( ) > {
74
- EnvIO { fileSystem in
75
- let content = IO < FileSystemError , String > . var ( )
76
- let fixedContent = IO < FileSystemError , String > . var ( )
77
- let path = outputPath + " /Package.swift "
78
-
79
- return binding (
80
- content <- fileSystem. readFile ( atPath: path) ,
81
- fixedContent <- IO . pure ( content. get. replacingOccurrences ( of: " {{ moduleName }} " , with: moduleName) ) ,
82
- |<- fileSystem. write ( content: fixedContent. get, toFile: path) ,
83
- yield: ( )
84
- ) ^
71
+ internal static func createSwiftPackage( module: OpenAPIModule ) -> EnvIO < Environment , APIClientError , Void > {
72
+ func installPackage( module: OpenAPIModule ) -> EnvIO < FileSystem , FileSystemError , Void > {
73
+ EnvIO { fileSystem in
74
+ fileSystem. copy ( item: " Package.swift " , from: module. templates, to: module. url) ^
75
+ }
85
76
}
77
+
78
+ func updatePackageName( module: OpenAPIModule ) -> EnvIO < FileSystem , FileSystemError , Void > {
79
+ EnvIO { fileSystem in
80
+ let content = IO < FileSystemError , String > . var ( )
81
+ let fixedContent = IO < FileSystemError , String > . var ( )
82
+ let output = module. url. appendingPathComponent ( " Package.swift " )
83
+
84
+ return binding (
85
+ content <- fileSystem. readFile ( at: output) ,
86
+ fixedContent <- IO . pure ( content. get. replacingOccurrences ( of: " {{ moduleName }} " , with: module. name) ) ,
87
+ |<- fileSystem. write ( content: fixedContent. get, toFile: output) ,
88
+ yield: ( ) ) ^
89
+ }
90
+ }
91
+
92
+ return installPackage ( module: module)
93
+ . followedBy ( updatePackageName ( module: module) ) ^
94
+ . mapError( FileSystemError . toAPIClientError)
95
+ . contramap ( \. fileSystem) ^
86
96
}
87
97
}
0 commit comments