-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create protocol ResourceFolderManager and BetaGroupFolderManager
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
Sources/AppStoreConnectCLI/Files/BetaGroupFileManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
Sources/AppStoreConnectCLI/Files/ResourceFolderManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |