Skip to content

Commit

Permalink
Create protocol ResourceFolderManager and BetaGroupFolderManager
Browse files Browse the repository at this point in the history
  • Loading branch information
DechengMa committed May 20, 2020
1 parent 5173911 commit 99c213e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Sources/AppStoreConnectCLI/Files/BetaGroupFileManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2020 Itty Bitty Apps Pty Ltd

import Foundation
import Files
import Yams

struct BetaGroupFolderManager: ResourceFolderManager {

let path: String

func read() throws -> [BetaGroup] {
var localGroups: [BetaGroup] = []

for file in try Folder(path: path).files {
if file.extension == "yml" {
let group = Readers
.FileReader<BetaGroup>(format: .yaml)
.read(filePath: file.path)

localGroups.append(group)
}
}

return localGroups
}

func save(_ betaGroups: [BetaGroup]) throws {
let folder = try Folder(path: "").createSubfolderIfNeeded(at: path)

try betaGroups.forEach {
let groupFile = try folder.createFile(named: "\($0.formattedFileName).yml")

try groupFile.write(try YAMLEncoder().encode($0))
}
}

func save(groupsWithTesters betaGroupWithTesters: [(BetaGroup, [BetaTester])]) throws {

let folder = try Folder(path: "").createSubfolderIfNeeded(at: path)

try folder.files.forEach { try $0.delete() }

let betaGroups = try betaGroupWithTesters.map { (group: BetaGroup, testers: [BetaTester]) -> BetaGroup in
let testersFile = try folder
.createFile(named: "\(group.formattedFileName)_beta-testers.csv")

try testersFile.write(testers.renderAsCSV())

var group = group

group.testers = testersFile.path

return group
}

try save(betaGroups)
}
}
26 changes: 26 additions & 0 deletions Sources/AppStoreConnectCLI/Files/ResourceFolderManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2020 Itty Bitty Apps Pty Ltd

import Foundation

protocol ResourceFolderManager {
associatedtype T: Codable

var path: String { get }

func read() throws -> [T]

func save(_: [T]) throws
}

protocol FileNameGettable {
var fileName: String { get }
}

extension FileNameGettable {
var formattedFileName: String {
self.fileName
.components(separatedBy: .whitespacesAndNewlines)
.joined()
.snakeCased() ?? "file-name"
}
}

0 comments on commit 99c213e

Please sign in to comment.