Skip to content

Commit 55818c6

Browse files
authored
Merge pull request #96 from noppoMan/refactor
Refactor
2 parents ed68dda + 95b2f1f commit 55818c6

22 files changed

Lines changed: 189 additions & 175 deletions

Sources/Hexaville/main.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ class GenerateProject: Command {
4545
let swiftToolVersion = Key<String>("--swift-tools-version", description: "Major Swift Tool Version for this project. default is 4.0")
4646
let dest = Key<String>("-o", "--dest", description: "Destination for the project")
4747

48-
private func resolveSwiftVersion() throws -> SwiftVersionContainer {
48+
private func resolveSwiftVersion() throws -> SwiftVersion {
4949
guard let version = swiftToolVersion.value else {
5050
// default is 4.0
5151
return Configuration.SwiftConfiguration.defaultVersion
5252
}
5353

54-
let swiftVersion = try SwiftVersionContainer(string: version)
54+
let swiftVersion = try SwiftVersion(string: version)
5555

5656
if (Configuration.SwiftConfiguration.supportedVersionsRange ~= swiftVersion.asCompareableVersion().major) == false {
5757
throw HexavilleError.unsupportedSwiftToolsVersion(version)
@@ -65,12 +65,11 @@ class GenerateProject: Command {
6565
let suffix = "-\(hashId)-bucket"
6666

6767
let bucketNameMaxLength = 63
68-
let maxLength = bucketNameMaxLength - (prefix + suffix).characters.count
68+
let maxLength = bucketNameMaxLength - (prefix + suffix).count
6969

70-
let allowedCharacters = Set("abcdefghijklmnopqrstuvwxyz1234567890-".characters)
70+
let allowedCharacters = Set("abcdefghijklmnopqrstuvwxyz1234567890-")
7171
let sanitizedCharacters = projectName
7272
.lowercased()
73-
.characters
7473
.filter { allowedCharacters.contains($0) }
7574
.prefix(maxLength)
7675

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// AWSConfiguration.swift
3+
// HexavillePackageDescription
4+
//
5+
// Created by Yuki Takei on 2017/12/18.
6+
//
7+
8+
import Foundation
9+
import SwiftAWSS3
10+
import SwiftAWSLambda
11+
import SwiftAWSApigateway
12+
import SwiftAWSIam
13+
import AWSSDKSwiftCore
14+
15+
public struct AWSConfiguration {
16+
public struct Endpoints {
17+
let s3Endpoint: String?
18+
let lambdaEndpoint: String?
19+
let apiGatewayEndpoint: String?
20+
21+
init(s3Endpoint: String? = nil, lambdaEndpoint: String? = nil, apiGatewayEndpoint: String) {
22+
self.s3Endpoint = s3Endpoint
23+
self.lambdaEndpoint = lambdaEndpoint
24+
self.apiGatewayEndpoint = apiGatewayEndpoint
25+
}
26+
}
27+
28+
public struct LambdaCodeConfig {
29+
public let role: String?
30+
public let bucket: String
31+
public let timeout: Int
32+
public let memory: Int32?
33+
public let vpcConfig: Lambda.VpcConfig?
34+
public let environment: [String : String]
35+
36+
public init(role: String?, bucket: String, timeout: Int = 10, memory: Int32? = nil, vpcConfig: Lambda.VpcConfig? = nil, environment: [String : String] = [:]) {
37+
self.role = role
38+
self.bucket = bucket
39+
self.timeout = timeout
40+
self.memory = memory
41+
self.vpcConfig = vpcConfig
42+
self.environment = environment
43+
}
44+
}
45+
46+
public let credential: AWSSDKSwiftCore.Credential?
47+
public let region: AWSSDKSwiftCore.Region?
48+
public let endpoints: Endpoints?
49+
public let lambdaCodeConfig: LambdaCodeConfig
50+
51+
public init(credential: AWSSDKSwiftCore.Credential? = nil, region: AWSSDKSwiftCore.Region? = nil, endpoints: Endpoints? = nil, lambdaCodeConfig: LambdaCodeConfig) {
52+
self.credential = credential
53+
self.region = region
54+
self.endpoints = endpoints
55+
self.lambdaCodeConfig = lambdaCodeConfig
56+
}
57+
}

Sources/HexavilleCore/HexavillefileLoader/AWSLoader.swift renamed to Sources/HexavilleCore/Cloud/AWS/AWSConfigurationLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Yaml
1111
import AWSSDKSwiftCore
1212
import SwiftAWSLambda
1313

14-
struct AWSLoader: PlatformConfigurationLoadable {
14+
struct AWSConfigurationLoader: PlatformConfigurationLoadable {
1515

1616
let appName: String
1717

Sources/HexavilleCore/Launcher/launcherProvider/AWSLauncherProvider.swift renamed to Sources/HexavilleCore/Cloud/AWS/AWSLauncherProvider.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ import AWSSDKSwiftCore
2020
import Foundation
2121
import SwiftyJSON
2222

23+
struct Resource {
24+
let pathPart: String
25+
let method: String?
26+
var apiGatewayResource: Apigateway.Resource?
27+
var apiGatewayParentResource: Apigateway.Resource?
28+
}
29+
2330
extension AWSSDKSwiftCore.AWSShape {
2431
public func toJSONString() -> String {
2532
do {
@@ -830,6 +837,6 @@ extension AWSLauncherProvider {
830837

831838
func pathForARN(_ path: String) throws -> String {
832839
let regex = try NSRegularExpression(pattern: "\\{[a-zA-Z_-]*\\}", options: [])
833-
return regex.stringByReplacingMatches(in: path, options: [], range: NSRange(location: 0, length: path.characters.count), withTemplate: "*")
840+
return regex.stringByReplacingMatches(in: path, options: [], range: NSRange(location: 0, length: path.count), withTemplate: "*")
834841
}
835842
}

Sources/HexavilleCore/Launcher/launcherProvider/CloudLauncherProvider.swift renamed to Sources/HexavilleCore/Cloud/Launcher/CloudLauncherProvider.swift

File renamed without changes.

Sources/HexavilleCore/Launcher/Launcher.swift renamed to Sources/HexavilleCore/Cloud/Launcher/Launcher.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
//
88

99
import Foundation
10-
import AWSSDKSwiftCore
11-
import SwiftAWSApigateway
1210
import SwiftyJSON
1311

1412
public enum DeploymentStage {
@@ -56,13 +54,6 @@ public struct Route {
5654
let resource: String
5755
}
5856

59-
struct Resource {
60-
let pathPart: String
61-
let method: String?
62-
var apiGatewayResource: Apigateway.Resource?
63-
var apiGatewayParentResource: Apigateway.Resource?
64-
}
65-
6657
struct DeployResult {
6758
let endpoint: String
6859
}

Sources/HexavilleCore/Configuration.swift renamed to Sources/HexavilleCore/Configuration/Configuration.swift

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,20 @@
88

99
import Foundation
1010
import Yaml
11-
import SwiftAWSS3
12-
import SwiftAWSLambda
13-
import SwiftAWSApigateway
14-
import SwiftAWSIam
15-
import AWSSDKSwiftCore
1611

1712
public enum ConfigurationError: Error {
1813
case invalidSwiftBuildConfiguration(String)
1914
}
2015

21-
public struct AWSConfiguration {
22-
public struct Endpoints {
23-
let s3Endpoint: String?
24-
let lambdaEndpoint: String?
25-
let apiGatewayEndpoint: String?
26-
27-
init(s3Endpoint: String? = nil, lambdaEndpoint: String? = nil, apiGatewayEndpoint: String) {
28-
self.s3Endpoint = s3Endpoint
29-
self.lambdaEndpoint = lambdaEndpoint
30-
self.apiGatewayEndpoint = apiGatewayEndpoint
31-
}
32-
}
33-
34-
public struct LambdaCodeConfig {
35-
public let role: String?
36-
public let bucket: String
37-
public let timeout: Int
38-
public let memory: Int32?
39-
public let vpcConfig: Lambda.VpcConfig?
40-
public let environment: [String : String]
41-
42-
public init(role: String?, bucket: String, timeout: Int = 10, memory: Int32? = nil, vpcConfig: Lambda.VpcConfig? = nil, environment: [String : String] = [:]) {
43-
self.role = role
44-
self.bucket = bucket
45-
self.timeout = timeout
46-
self.memory = memory
47-
self.vpcConfig = vpcConfig
48-
self.environment = environment
49-
}
50-
}
51-
52-
public let credential: AWSSDKSwiftCore.Credential?
53-
public let region: AWSSDKSwiftCore.Region?
54-
public let endpoints: Endpoints?
55-
public let lambdaCodeConfig: LambdaCodeConfig
56-
57-
public init(credential: AWSSDKSwiftCore.Credential? = nil, region: AWSSDKSwiftCore.Region? = nil, endpoints: Endpoints? = nil, lambdaCodeConfig: LambdaCodeConfig) {
58-
self.credential = credential
59-
self.region = region
60-
self.endpoints = endpoints
61-
self.lambdaCodeConfig = lambdaCodeConfig
62-
}
63-
}
64-
6516
public struct Configuration {
6617
public struct SwiftConfiguration {
6718

6819
public static var supportedVersionsRange: CountableClosedRange<Int> {
6920
return 3...4
7021
}
7122

72-
public static var defaultVersion: SwiftVersionContainer {
73-
return .release(SwiftVersion(major: 4, minor: 0))
23+
public static var defaultVersion: SwiftVersion {
24+
return .release(Version(major: 4, minor: 0))
7425
}
7526

7627
public struct Build {
@@ -83,7 +34,7 @@ public struct Configuration {
8334

8435
public let build: Build
8536

86-
public let version: SwiftVersionContainer
37+
public let version: SwiftVersion
8738

8839
init(yml: Yaml) throws {
8940
let swiftBuildConfiguration = yml["build"]["configuration"].string ?? "debug"
@@ -95,13 +46,13 @@ public struct Configuration {
9546
let versionYml = yml["version"]
9647

9748
if let version = versionYml.string {
98-
self.version = try SwiftVersionContainer(string: version)
49+
self.version = try SwiftVersion(string: version)
9950

10051
} else if let version = versionYml.double {
101-
self.version = .release(try SwiftVersion(string: version.description))
52+
self.version = .release(try Version(string: version.description))
10253

10354
} else if let version = versionYml.int {
104-
self.version = .release(try SwiftVersion(string: version.description))
55+
self.version = .release(try Version(string: version.description))
10556

10657
} else {
10758
self.version = SwiftConfiguration.defaultVersion
File renamed without changes.

Sources/HexavilleCore/HexavillefileLoader/HexavillefileLoader.swift renamed to Sources/HexavilleCore/Configuration/HexavillefileLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public struct HexavillefileLoader {
5050
case "aws":
5151
return Configuration(
5252
name: appName,
53-
platformConfiguration: try AWSLoader(appName: appName, yaml: config["aws"], environment: environment).load(),
53+
platformConfiguration: try AWSConfigurationLoader(appName: appName, yaml: config["aws"], environment: environment).load(),
5454
buildConfiguration: buildConfiguration,
5555
swiftConfiguration: swiftConfiguration
5656
)

Sources/HexavilleCore/SwiftVersion.swift renamed to Sources/HexavilleCore/Configuration/SwiftVersion.swift

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22
// SwiftVersion.swift
33
// HexavillePackageDescription
44
//
5-
// Created by Yuki Takei on 2017/08/13.
5+
// Created by Yuki Takei on 2017/12/18.
66
//
77

88
import Foundation
99

10-
public enum SwiftVersionContainer {
11-
case release(SwiftVersion)
10+
public enum SwiftVersion {
11+
case release(Version)
1212
case developmentSnapshot(SwiftDevelopmentSnapshot)
1313
}
1414

15-
extension SwiftVersionContainer {
15+
extension SwiftVersion {
1616
public init(string versionString: String) throws {
1717
if versionString.contains(substring: SwiftDevelopmentSnapshot.snapshotIdentifer) {
1818
self = .developmentSnapshot(try SwiftDevelopmentSnapshot(string: versionString))
1919
} else {
20-
self = .release(try SwiftVersion(string: versionString))
20+
self = .release(try Version(string: versionString))
2121
}
2222
}
2323
}
2424

25-
extension SwiftVersionContainer {
25+
extension SwiftVersion {
2626
public var versionString: String {
2727
switch self {
2828
case .developmentSnapshot(let snapshot):
@@ -65,101 +65,17 @@ extension SwiftVersionContainer {
6565
return "\(downloadBaseURLString)/\(path)/\(fileName).tar.gz"
6666
}
6767

68-
public func asCompareableVersion() -> SwiftVersion {
68+
public func asCompareableVersion() -> Version {
6969
switch self {
7070
case .developmentSnapshot(let snapshot):
71-
return SwiftVersion(major: snapshot.major, minor: snapshot.minor, patch: snapshot.patch)
71+
return Version(major: snapshot.major, minor: snapshot.minor, patch: snapshot.patch)
7272

7373
case .release(let version):
7474
return version
7575
}
7676
}
7777
}
7878

79-
public protocol SwiftVersionRepresentable: Hashable, Comparable {
80-
var major: Int { get }
81-
var minor: Int { get }
82-
var patch: Int { get }
83-
}
84-
85-
extension SwiftVersionRepresentable {
86-
public static func < <Other: SwiftVersionRepresentable>(lhs: Self, rhs: Other) -> Bool {
87-
if lhs.major != rhs.major {
88-
return lhs.major < rhs.major
89-
} else {
90-
if lhs.minor < rhs.minor {
91-
return true
92-
}
93-
return lhs.patch < rhs.patch
94-
}
95-
}
96-
}
97-
98-
extension SwiftVersionRepresentable {
99-
public var hashValue: Int {
100-
return (major << 8) | minor | patch
101-
}
102-
103-
public static func == <Other: SwiftVersionRepresentable>(lhs: Self, rhs: Other) -> Bool {
104-
return lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch == rhs.patch
105-
}
106-
107-
public static func ~= <Other: SwiftVersionRepresentable>(match: Self, version: Other) -> Bool {
108-
return match == version
109-
}
110-
}
111-
112-
enum SwiftVersionError: Error {
113-
case invalidVersion(String)
114-
case notEmpty
115-
}
116-
117-
public struct SwiftVersion: SwiftVersionRepresentable {
118-
public let major: Int
119-
public let minor: Int
120-
public let patch: Int
121-
122-
public var versionString: String {
123-
var version = "\(major).\(minor)"
124-
if patch > 0 {
125-
version += ".\(patch)"
126-
}
127-
return version
128-
}
129-
130-
public init(major: Int, minor: Int, patch: Int = 0) {
131-
self.major = major
132-
self.minor = minor
133-
self.patch = patch
134-
}
135-
}
136-
137-
extension SwiftVersion {
138-
public init(string: String) throws {
139-
let components = string.components(separatedBy: ".")
140-
if components.count == 0 {
141-
throw SwiftVersionError.notEmpty
142-
}
143-
144-
var intCastedComponents: [Int] = try components.map({
145-
guard let int = Int($0) else {
146-
throw SwiftVersionError.invalidVersion(string)
147-
}
148-
return int
149-
})
150-
151-
switch intCastedComponents.count {
152-
case 1:
153-
self = SwiftVersion(major: intCastedComponents[0], minor: 0)
154-
case 2:
155-
self = SwiftVersion(major: intCastedComponents[0], minor: intCastedComponents[1])
156-
case 3:
157-
self = SwiftVersion(major: intCastedComponents[0], minor: intCastedComponents[1], patch: intCastedComponents[2])
158-
default:
159-
throw SwiftVersionError.invalidVersion(string)
160-
}
161-
}
162-
}
16379

16480
public enum SwiftDevelopmentSnapshotError: Error {
16581
case invalidDevelopmentSnapshotName(String)

0 commit comments

Comments
 (0)