From 4b821d9da5a6dde47dd17779babe5498d048ba25 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Sat, 24 Mar 2018 19:51:26 -0400 Subject: [PATCH 1/2] store cli arguments on environment --- Sources/Service/Container.swift | 2 +- Sources/Service/Environment.swift | 9 ++++++--- Sources/Service/Provider.swift | 12 +++++++++++- Sources/Service/SubContainer.swift | 3 ++- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Sources/Service/Container.swift b/Sources/Service/Container.swift index 90da7aa..adcc17b 100644 --- a/Sources/Service/Container.swift +++ b/Sources/Service/Container.swift @@ -6,7 +6,7 @@ import Dispatch /// to determine which service instances are most appropriate to create. public protocol Container: ServiceCacheable, BasicWorker { var config: Config { get } - var environment: Environment { get } + var environment: Environment { get set } var services: Services { get } } diff --git a/Sources/Service/Environment.swift b/Sources/Service/Environment.swift index c9a50b3..96b36f5 100644 --- a/Sources/Service/Environment.swift +++ b/Sources/Service/Environment.swift @@ -8,11 +8,14 @@ public struct Environment { /// `true` if this environment is production. public let isRelease: Bool - /// Create a new environment. - /// Use the static helper methods. - internal init(name: String, isRelease: Bool) { + /// The command-line arguments for this `Environment`. + public var arguments: [String] + + /// Create a new environment. Use the static helper methods. + public init(name: String, isRelease: Bool, arguments: [String] = CommandLine.arguments) { self.name = name self.isRelease = isRelease + self.arguments = arguments } } diff --git a/Sources/Service/Provider.swift b/Sources/Service/Provider.swift index 46673ca..e88b1d4 100644 --- a/Sources/Service/Provider.swift +++ b/Sources/Service/Provider.swift @@ -28,6 +28,11 @@ public protocol Provider { /// The location of the views directory /// _relative_ to the root of the provider package. static var viewsDir: String { get } + + /// Gives the provider a chance to detect information from the `Environment`. + /// - parameters: + /// - env: Mutable `Environment`. This can be used to parse command line flags, fetch env variables, and more. + func detect(_ env: inout Environment) throws /// Register all services provided by the provider here. func register(_ services: inout Services) throws @@ -49,11 +54,16 @@ extension Provider { // MARK: Optional extension Provider { + /// By default, the `Public` folder will be used as all `Provider`'s public dir. public static var publicDir: String { return "Public" } - + + /// By default, the `Resources/Views` folder will be used for all `Provider`'s views dir. public static var viewsDir: String { return "Resources/Views" } + + /// By default, all providers will ignore the `Environment` during `detect(_:)`. + public func detect(_ env: inout Environment) throws { } } diff --git a/Sources/Service/SubContainer.swift b/Sources/Service/SubContainer.swift index 3ee7899..c8b1aef 100644 --- a/Sources/Service/SubContainer.swift +++ b/Sources/Service/SubContainer.swift @@ -30,7 +30,8 @@ extension SubContainer { /// See Container.environment public var environment: Environment { - return superContainer.environment + get { return superContainer.environment } + set { superContainer.environment = newValue } } } From df54f3d6f15b214e52e9ee64b3f9209f7dd7bd24 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Sat, 24 Mar 2018 22:54:29 -0400 Subject: [PATCH 2/2] container updates --- Sources/Service/Container.swift | 2 +- Sources/Service/SubContainer.swift | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Sources/Service/Container.swift b/Sources/Service/Container.swift index adcc17b..90da7aa 100644 --- a/Sources/Service/Container.swift +++ b/Sources/Service/Container.swift @@ -6,7 +6,7 @@ import Dispatch /// to determine which service instances are most appropriate to create. public protocol Container: ServiceCacheable, BasicWorker { var config: Config { get } - var environment: Environment { get set } + var environment: Environment { get } var services: Services { get } } diff --git a/Sources/Service/SubContainer.swift b/Sources/Service/SubContainer.swift index c8b1aef..3ee7899 100644 --- a/Sources/Service/SubContainer.swift +++ b/Sources/Service/SubContainer.swift @@ -30,8 +30,7 @@ extension SubContainer { /// See Container.environment public var environment: Environment { - get { return superContainer.environment } - set { superContainer.environment = newValue } + return superContainer.environment } }