Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

14 changes: 5 additions & 9 deletions Sources/VariantsCore/Factory/Android/GradleScriptFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,16 @@ class GradleScriptFactory: GradleFactory {
fileprivate extension Sequence where Iterator.Element == CustomProperty {
func envVars() -> [CustomProperty] {
return self
.filter({ $0.destination == .project && $0.processForEnvironment().isEnvVar })
.filter({ $0.destination == .project && $0.isEnvironmentVariable })
.map { (property) -> CustomProperty in
let processed = property.processForEnvironment()
if processed.isEnvVar {
return CustomProperty(name: property.name,
value: "System.getenv('"+processed.string+"')",
destination: property.destination)
}
return property
return CustomProperty(name: property.name,
value: "System.getenv('"+property.environmentValue+"')",
destination: property.destination)
}
}

func literal() -> [CustomProperty] {
return self
.filter({ $0.destination == .project && !$0.processForEnvironment().isEnvVar })
.filter({ $0.destination == .project && !$0.isEnvironmentVariable })
}
}
14 changes: 5 additions & 9 deletions Sources/VariantsCore/Factory/FastlaneParametersFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,16 @@ class FastlaneParametersFactory: ParametersFactory {
fileprivate extension Sequence where Iterator.Element == CustomProperty {
func envVars() -> [CustomProperty] {
return self
.filter({ $0.destination == .fastlane && $0.processForEnvironment().isEnvVar })
.filter({ $0.destination == .fastlane && $0.isEnvironmentVariable })
.map { (property) -> CustomProperty in
let processed = property.processForEnvironment()
if processed.isEnvVar {
return CustomProperty(name: property.name,
value: processed.string,
destination: property.destination)
}
return property
return CustomProperty(name: property.name,
value: property.environmentValue,
destination: property.destination)
}
}

func literal() -> [CustomProperty] {
return self
.filter({ $0.destination == .fastlane && !$0.processForEnvironment().isEnvVar })
.filter({ $0.destination == .fastlane && !$0.isEnvironmentVariable })
}
}
14 changes: 5 additions & 9 deletions Sources/VariantsCore/Factory/iOS/SecretsFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,16 @@ class SecretsFactory {
fileprivate extension Sequence where Iterator.Element == CustomProperty {
func envVars() -> [CustomProperty] {
return self
.filter({ $0.destination == .project && $0.processForEnvironment().isEnvVar })
.filter({ $0.destination == .project && $0.isEnvironmentVariable })
.map { (property) -> CustomProperty in
let processed = property.processForEnvironment()
if processed.isEnvVar {
return CustomProperty(name: property.name,
value: "os.environ.get('"+processed.string+"')",
destination: property.destination)
}
return property
return CustomProperty(name: property.name,
value: "os.environ.get('"+property.environmentValue+"')",
destination: property.destination)
}
}

func literal() -> [CustomProperty] {
return self
.filter({ $0.destination == .project && !$0.processForEnvironment().isEnvVar })
.filter({ $0.destination == .project && !$0.isEnvironmentVariable })
}
}
53 changes: 50 additions & 3 deletions Sources/VariantsCore/Schemas/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,44 @@ public struct Configuration: Codable {
}

public struct CustomProperty: Codable {
public var name: String
public var value: String
public var destination: Destination
var name: String
var value: String
private var env: Bool? = false
private(set) var isEnvironmentVariable: Bool
var destination: Destination

enum CodingKeys: String, CodingKey {
case name
case value
case env
case destination
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

var isEnvVar: Bool?
if container.contains(.env) {
isEnvVar = try container.decode(Bool?.self, forKey: .env)
}

name = try container.decode(String.self, forKey: .name)
value = try container.decode(String.self, forKey: .value)
isEnvironmentVariable = isEnvVar ?? false
destination = try container.decode(Destination.self, forKey: .destination)
}

public init(
name: String,
value: String,
env: Bool = false,
destination: Destination
) {
self.name = name
self.value = value
self.isEnvironmentVariable = env
self.destination = destination
}

public enum Destination: String, Codable {
case project
Expand All @@ -28,3 +63,15 @@ extension CustomProperty: Equatable {
return lhs.name == rhs.name
}
}

extension CustomProperty {
var environmentValue: String {
guard isEnvironmentVariable == true else { return value }
switch destination {
case .project:
return value
case .fastlane:
return "ENV[\""+value+"\"]"
}
}
}
2 changes: 1 addition & 1 deletion Sources/VariantsCore/Schemas/iOS/iOSVariant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct iOSVariant: Codable {
}

custom?
.filter { $0.destination == .project && !$0.processForEnvironment().isEnvVar }
.filter { $0.destination == .project && !$0.isEnvironmentVariable }
.forEach({ config in
customDictionary[config.name] = config.value
})
Expand Down
20 changes: 14 additions & 6 deletions Templates/android/variants-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ android:
#
custom:
- name: SAMPLE_PROPERTY
value: "{{ envVars.SAMPLE_ENVIRONMENT_VARIABLE }}"
value: SAMPLE_VALUE
destination: fastlane

- name: SAMPLE_PROPERTY_FROM_ENVIRONMENT
value: SAMPLE_ENVIRONMENT_VARIABLE
env: true
destination: fastlane

# Sample variant "BETA"
Expand All @@ -44,7 +49,8 @@ android:
store_destination: AppCenter
custom:
- name: SAMPLE_PROPERTY
value: "{{ envVars.SAMPLE_ENVIRONMENT_VARIABLE }}"
value: SAMPLE_ENVIRONMENT_VARIABLE
env: true
destination: fastlane

# ----------------------------------------------------------------------
Expand All @@ -58,9 +64,9 @@ android:

#signing:
# key_alias: android
# key_password: "{{ envVars.APP_SIGN_KEY_PASSWORD }}"
# key_password: APP_SIGN_KEY_PASSWORD
# store_file: /usr/local/android/production.keystore
# store_password: "{{ envVars.APP_SIGN_STORE_PASSWORD }}"
# store_password: APP_SIGN_STORE_PASSWORD

# ----------------------------------------------------------------------
# custom: - Not required.
Expand All @@ -73,8 +79,10 @@ android:

#custom:
# - name: mvnUser
# value: "{{ envVars.MAVEN_USERNAME }}"
# value: MAVEN_USERNAME
# env: true
# destination: project
# - name: mvnPass
# value: "{{ envVars.MAVEN_PASSWORD }}"
# value: MAVEN_PASSWORD
# env: true
# destination: project
4 changes: 4 additions & 0 deletions Templates/ios/variants-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ ios:
custom:
- name: OTHER_SWIFT_FLAGS
value: $(inherited)
env: false
destination: project
- name: SAMPLE_FASTLANE_PROPERTY
value: This will be available to fastlane
env: false
destination: fastlane
#
# Sample variant, "beta".
Expand Down Expand Up @@ -76,9 +78,11 @@ ios:
custom:
- name: OTHER_SWIFT_FLAGS
value: $(inherited)
env: false
destination: project
- name: SAMPLE_FASTLANE_PROPERTY
value: This will be available to fastlane on Beta variant
env: false
destination: fastlane

signing:
Expand Down
46 changes: 24 additions & 22 deletions Tests/VariantsCoreTests/CustomProperty+EnvironmentVarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ class CustomPropertyEnvironmentVarTests: XCTestCase {
func testProcessForEnvironment_forProject_true() {
let environmentVarProperty = CustomProperty(
name: "AN_ENV_VAR",
value: "{{ envVars.A_SECRET }}",
value: "A_SECRET",
env: true,
destination: .project
)

XCTAssertTrue(
environmentVarProperty.processForEnvironment().isEnvVar,
"After processing `isEnvVar` should be true as it matches the pattern"
environmentVarProperty.isEnvironmentVariable,
"`isEnvironmentVariable` should be true as `env` is set to true"
)

XCTAssertEqual(
environmentVarProperty.processForEnvironment().string,
environmentVarProperty.environmentValue,
"A_SECRET",
"After processing `string` should be processed to extract env var name"
"`environmentValue` should be equal to `value` as `destination` is project"
)
}

Expand All @@ -37,33 +38,34 @@ class CustomPropertyEnvironmentVarTests: XCTestCase {
)

XCTAssertFalse(
environmentVarProperty.processForEnvironment().isEnvVar,
"After processing `isEnvVar` should be false as it ddoesn't match the pattern"
environmentVarProperty.isEnvironmentVariable,
"`isEnvironmentVariable` should be false as `env` is not set and defaults to false"
)

XCTAssertEqual(
environmentVarProperty.processForEnvironment().string,
environmentVarProperty.environmentValue,
environmentVarProperty.value,
"After processing `string` should be exactly the same"
"`environmentValue` should be equal to `value` as `destination` is project and/or `env` isn't set and defaults to false"
)
}

func testProcessForEnvironment_forFastlane_true() {
let environmentVarProperty = CustomProperty(
name: "AN_ENV_VAR",
value: "{{ envVars.A_SECRET }}",
value: "A_SECRET",
env: true,
destination: .fastlane
)

XCTAssertTrue(
environmentVarProperty.processForEnvironment().isEnvVar,
"After processing `isEnvVar` should be true as it matches the pattern"
environmentVarProperty.isEnvironmentVariable,
"`isEnvironmentVariable` should be true as `env` is set to true"
)

XCTAssertEqual(
environmentVarProperty.processForEnvironment().string,
environmentVarProperty.environmentValue,
"ENV[\"A_SECRET\"]",
"After processing `string` should be processed to extract env var name"
"`environmentValue` should be contained within 'ENV[\"\"]' as `destination` is fastlane and `env` is set to true"
)
}

Expand All @@ -75,14 +77,14 @@ class CustomPropertyEnvironmentVarTests: XCTestCase {
)

XCTAssertFalse(
environmentVarProperty.processForEnvironment().isEnvVar,
"After processing `isEnvVar` should be false as it ddoesn't match the pattern"
environmentVarProperty.isEnvironmentVariable,
"`isEnvironmentVariable` should be false as `env` is not set and defaults to false"
)

XCTAssertEqual(
environmentVarProperty.processForEnvironment().string,
environmentVarProperty.environmentValue,
environmentVarProperty.value,
"After processing `string` should be exactly the same"
"`environmentValue` should be equal to `value` as `destination`, as `env` isn't set and defaults to false"
)
}

Expand All @@ -95,18 +97,18 @@ class CustomPropertyEnvironmentVarTests: XCTestCase {
),
CustomProperty(
name: "AN_ENV_VAR",
value: "{{ envVars.A_SECRET }}",
value: "A_SECRET",
env: true,
destination: .fastlane
)
]

let fastlaneParameters = propertiesArray
.filter { $0.destination == .fastlane }
.map { (property) -> CustomProperty in
let processed = property.processForEnvironment()
if processed.isEnvVar {
if property.isEnvironmentVariable {
return CustomProperty(name: property.name,
value: processed.string,
value: property.environmentValue,
destination: property.destination)
}
return property
Expand Down
Loading