func evolve()

in SwiftEvolve/Sources/SwiftEvolve/SwiftEvolveTool.swift [138:170]


  func evolve(withPlanFile planFile: AbsolutePath, options: Step.Options) throws {
    let data = try Data(contentsOf: URL(planFile))
    let plan = try JSONDecoder().decode([PlannedEvolution].self, from: data)

    let evolver = Evolver(plan: plan)

    for file in options.files {
      let parsed = try parsedSource(at: file)
      let evolved = evolver.evolve(in: parsed, at: URL(file))

      if options.replace {
        try withTemporaryFile(dir: file.parentDirectory, prefix: "",
                          suffix: file.basename,
                          deleteOnClose: true) { tempFile in
          tempFile.fileHandle.write(evolved.description)
          
          _ = try withErrorContext(
            url: URL(file),
            debugDescription: "replaceItemAt(file, withItemAt: tempFile, ...)"
          ) {
            try FileManager.default.replaceItemAt(
              URL(file), withItemAt: URL(tempFile.path),
              backupItemName: file.basename + "~",
              options: .withoutDeletingBackupItem
            )
          }
        }
      }
      else {
        print(evolved, terminator: "")
      }
    }
  }