From 7e50fc5449fdb15124f4f1356f70ed6b25b0e046 Mon Sep 17 00:00:00 2001 From: Simon Pilkington Date: Thu, 4 Aug 2022 16:12:35 +1000 Subject: [PATCH] Don't guess baseName. --- .../SmokeFrameworkGenerateClient/plugin.swift | 26 +++++++++---------- .../SmokeFrameworkGenerateHttp1/plugin.swift | 26 +++++++++---------- .../SmokeFrameworkGenerateModel/plugin.swift | 26 +++++++++---------- 3 files changed, 36 insertions(+), 42 deletions(-) diff --git a/Plugins/SmokeFrameworkGenerateClient/plugin.swift b/Plugins/SmokeFrameworkGenerateClient/plugin.swift index bb92147..19fc34b 100644 --- a/Plugins/SmokeFrameworkGenerateClient/plugin.swift +++ b/Plugins/SmokeFrameworkGenerateClient/plugin.swift @@ -36,6 +36,7 @@ struct SmokeFrameworkGenerateClientPlugin: BuildToolPlugin { } struct SmokeFrameworkCodeGen: Decodable { + let baseName: String let modelLocations: ModelLocations? let modelFilePath: String? // legacy location } @@ -47,14 +48,19 @@ struct SmokeFrameworkGenerateClientPlugin: BuildToolPlugin { let smokeFrameworkApplicationGenerateTool = try context.tool(named: "SmokeFrameworkApplicationGenerate") let sourcesDirectory = context.pluginWorkDirectory.appending("Sources") - var baseName = target.name - if baseName.hasSuffix(targetSuffix) { - baseName = String(baseName.dropLast(targetSuffix.count)) + let inputFile = context.package.directory.appending("smoke-framework-codegen.json") + let configFilePath = inputFile.string + let configFile = FileHandle(forReadingAtPath: configFilePath) + + guard let configData = configFile?.readDataToEndOfFile() else { + throw PluginError.missingConfigFile(expectedPath: configFilePath) } - let inputFile = context.package.directory.appending("smoke-framework-codegen.json") + let config = try JSONDecoder().decode(SmokeFrameworkCodeGen.self, from: configData) - let modelFilePathOverride = try getModelFilePathOverride(target: target, configFilePath: inputFile.string, + let baseName = config.baseName + + let modelFilePathOverride = try getModelFilePathOverride(target: target, config: config, baseFilePath: context.package.directory) let clientDirectory = sourcesDirectory.appending("\(baseName)\(targetSuffix)") @@ -94,16 +100,8 @@ struct SmokeFrameworkGenerateClientPlugin: BuildToolPlugin { return [command] } - private func getModelFilePathOverride(target: Target, configFilePath: String, + private func getModelFilePathOverride(target: Target, config: SmokeFrameworkCodeGen, baseFilePath: PackagePlugin.Path) throws -> String { - let configFile = FileHandle(forReadingAtPath: configFilePath) - - guard let configData = configFile?.readDataToEndOfFile() else { - throw PluginError.missingConfigFile(expectedPath: configFilePath) - } - - let config = try JSONDecoder().decode(SmokeFrameworkCodeGen.self, from: configData) - // find the model for the current target let filteredModelLocations = config.modelLocations?.targetMap.compactMap { (targetName, modelLocation) -> ModelLocation? in if targetName == target.name { diff --git a/Plugins/SmokeFrameworkGenerateHttp1/plugin.swift b/Plugins/SmokeFrameworkGenerateHttp1/plugin.swift index 6ae03ab..ed53fe0 100644 --- a/Plugins/SmokeFrameworkGenerateHttp1/plugin.swift +++ b/Plugins/SmokeFrameworkGenerateHttp1/plugin.swift @@ -36,6 +36,7 @@ struct SmokeFrameworkGenerateHttp1Plugin: BuildToolPlugin { } struct SmokeFrameworkCodeGen: Decodable { + let baseName: String let modelLocations: ModelLocations? let modelFilePath: String? // legacy location } @@ -47,14 +48,19 @@ struct SmokeFrameworkGenerateHttp1Plugin: BuildToolPlugin { let smokeFrameworkApplicationGenerateTool = try context.tool(named: "SmokeFrameworkApplicationGenerate") let sourcesDirectory = context.pluginWorkDirectory.appending("Sources") - var baseName = target.name - if baseName.hasSuffix(targetSuffix) { - baseName = String(baseName.dropLast(targetSuffix.count)) + let inputFile = context.package.directory.appending("smoke-framework-codegen.json") + let configFilePath = inputFile.string + let configFile = FileHandle(forReadingAtPath: configFilePath) + + guard let configData = configFile?.readDataToEndOfFile() else { + throw PluginError.missingConfigFile(expectedPath: configFilePath) } - let inputFile = context.package.directory.appending("smoke-framework-codegen.json") + let config = try JSONDecoder().decode(SmokeFrameworkCodeGen.self, from: configData) - let modelFilePathOverride = try getModelFilePathOverride(target: target, configFilePath: inputFile.string, + let baseName = config.baseName + + let modelFilePathOverride = try getModelFilePathOverride(target: target, config: config, baseFilePath: context.package.directory) let http1Directory = sourcesDirectory.appending("\(baseName)\(targetSuffix)") @@ -89,16 +95,8 @@ struct SmokeFrameworkGenerateHttp1Plugin: BuildToolPlugin { return [command] } - private func getModelFilePathOverride(target: Target, configFilePath: String, + private func getModelFilePathOverride(target: Target, config: SmokeFrameworkCodeGen, baseFilePath: PackagePlugin.Path) throws -> String { - let configFile = FileHandle(forReadingAtPath: configFilePath) - - guard let configData = configFile?.readDataToEndOfFile() else { - throw PluginError.missingConfigFile(expectedPath: configFilePath) - } - - let config = try JSONDecoder().decode(SmokeFrameworkCodeGen.self, from: configData) - // find the model for the current target let filteredModelLocations = config.modelLocations?.targetMap.compactMap { (targetName, modelLocation) -> ModelLocation? in if targetName == target.name { diff --git a/Plugins/SmokeFrameworkGenerateModel/plugin.swift b/Plugins/SmokeFrameworkGenerateModel/plugin.swift index 528a439..6a46487 100644 --- a/Plugins/SmokeFrameworkGenerateModel/plugin.swift +++ b/Plugins/SmokeFrameworkGenerateModel/plugin.swift @@ -36,6 +36,7 @@ struct SmokeFrameworkGenerateModelPlugin: BuildToolPlugin { } struct SmokeFrameworkCodeGen: Decodable { + let baseName: String let modelLocations: ModelLocations? let modelFilePath: String? // legacy location } @@ -47,14 +48,19 @@ struct SmokeFrameworkGenerateModelPlugin: BuildToolPlugin { let smokeFrameworkApplicationGenerateTool = try context.tool(named: "SmokeFrameworkApplicationGenerate") let sourcesDirectory = context.pluginWorkDirectory.appending("Sources") - var baseName = target.name - if baseName.hasSuffix(targetSuffix) { - baseName = String(baseName.dropLast(targetSuffix.count)) + let inputFile = context.package.directory.appending("smoke-framework-codegen.json") + let configFilePath = inputFile.string + let configFile = FileHandle(forReadingAtPath: configFilePath) + + guard let configData = configFile?.readDataToEndOfFile() else { + throw PluginError.missingConfigFile(expectedPath: configFilePath) } - let inputFile = context.package.directory.appending("smoke-framework-codegen.json") + let config = try JSONDecoder().decode(SmokeFrameworkCodeGen.self, from: configData) - let modelFilePathOverride = try getModelFilePathOverride(target: target, configFilePath: inputFile.string, + let baseName = config.baseName + + let modelFilePathOverride = try getModelFilePathOverride(target: target, config: config, baseFilePath: context.package.directory) let modelDirectory = sourcesDirectory.appending("\(baseName)\(targetSuffix)") @@ -90,16 +96,8 @@ struct SmokeFrameworkGenerateModelPlugin: BuildToolPlugin { return [command] } - private func getModelFilePathOverride(target: Target, configFilePath: String, + private func getModelFilePathOverride(target: Target, config: SmokeFrameworkCodeGen, baseFilePath: PackagePlugin.Path) throws -> String { - let configFile = FileHandle(forReadingAtPath: configFilePath) - - guard let configData = configFile?.readDataToEndOfFile() else { - throw PluginError.missingConfigFile(expectedPath: configFilePath) - } - - let config = try JSONDecoder().decode(SmokeFrameworkCodeGen.self, from: configData) - // find the model for the current target let filteredModelLocations = config.modelLocations?.targetMap.compactMap { (targetName, modelLocation) -> ModelLocation? in if targetName == target.name {