Skip to content

Commit

Permalink
Don't guess baseName.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Pilkington committed Aug 4, 2022
1 parent eff332d commit 7e50fc5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 42 deletions.
26 changes: 12 additions & 14 deletions Plugins/SmokeFrameworkGenerateClient/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct SmokeFrameworkGenerateClientPlugin: BuildToolPlugin {
}

struct SmokeFrameworkCodeGen: Decodable {
let baseName: String
let modelLocations: ModelLocations?
let modelFilePath: String? // legacy location
}
Expand All @@ -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)")
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 12 additions & 14 deletions Plugins/SmokeFrameworkGenerateHttp1/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct SmokeFrameworkGenerateHttp1Plugin: BuildToolPlugin {
}

struct SmokeFrameworkCodeGen: Decodable {
let baseName: String
let modelLocations: ModelLocations?
let modelFilePath: String? // legacy location
}
Expand All @@ -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)")
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 12 additions & 14 deletions Plugins/SmokeFrameworkGenerateModel/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct SmokeFrameworkGenerateModelPlugin: BuildToolPlugin {
}

struct SmokeFrameworkCodeGen: Decodable {
let baseName: String
let modelLocations: ModelLocations?
let modelFilePath: String? // legacy location
}
Expand All @@ -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)")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7e50fc5

Please sign in to comment.