Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AnasAlhasani committed Jan 1, 2022
1 parent 0dce4a7 commit db55534
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 91 deletions.
96 changes: 6 additions & 90 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,90 +1,6 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm

.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build/

# Accio dependency management
Dependencies/
.accio/

# fastlane
#
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/
.DS_Store
/.build
.swiftpm
/Packages
/*.xcodeproj
xcuserdata/
17 changes: 17 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// swift-tools-version:5.5

import PackageDescription

let package = Package(
name: "XcodeConfig",
products: [
.library(name: "XcodeConfig", targets: ["XcodeConfig"])
],
targets: [
.target(name: "XcodeConfig"),
.testTarget(
name: "XcodeConfigTests",
dependencies: ["XcodeConfig"]
)
]
)
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
# XcodeConfig
# @XcodeConfig

Property wrapper that allows you to access build settings values through the `infoDictionary` property of Foundation’s `Bundle` API.

Create a build configuration file i.e `Development.xcconfig`:
```xcconfig
HOST = localhost
TIMEOUT = 15.0
```

In your `Info.plist` you can reference build settings values using the following syntax `$(BUILD_SETTING_NAME)`:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>HOST</key>
<string>$(HOST)</string>
<key>TIMEOUT</key>
<string>$(TIMEOUT)</string>
</dict>
</plist>
```

For example, adding `@XcodeConfig` like this:
```swift
enum API {
@XcodeConfig(key: "HOST")
static var host: String

@XcodeConfig(key: "TIMEOUT")
static var timeout: TimeInterval
}
```

will access build settings values: `"localhost"` and `15.0` which are defined in `Development.xcconfig`.
83 changes: 83 additions & 0 deletions Sources/XcodeConfig/XcodeConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// XcodeConfig.swift
//
// Created by Anas Alhasani on 01/01/2022.
//

import Foundation

/// Property wrapper that allows you to access build settings values
/// through the `infoDictionary` property of Foundation’s `Bundle` API.
///
/// Create a build configuration file i.e `Development.xcconfig`:
/// ```xcconfig
/// HOST = localhost
/// TIMEOUT = 15.0
/// ```
///
/// In your `Info.plist` you can reference build settings values using the following syntax `$(BUILD_SETTING_NAME)`:
/// ```xml
/// <?xml version="1.0" encoding="UTF-8"?>
/// <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
/// <plist version="1.0">
/// <dict>
/// <key>HOST</key>
/// <string>$(HOST)</string>
/// <key>TIMEOUT</key>
/// <string>$(TIMEOUT)</string>
/// </dict>
/// </plist>
/// ```
///
/// For example, adding `@XcodeConfig` like this:
/// ```swift
/// enum API {
/// @XcodeConfig(key: "HOST")
/// static var host: String
///
/// @XcodeConfig(key: "TIMEOUT")
/// static var timeout: TimeInterval
/// }
/// ```
/// will access build settings values: `"localhost"` and `15.0`
/// which are defined in `Development.xcconfig`.
///
@propertyWrapper
public struct XcodeConfig<Value: LosslessStringConvertible> {

// MARK: Private Properties

private let key: String
private let bundle: Bundle

// MARK: Public Properties

public var wrappedValue: Value { value(for: key) }

// MARK: Init

public init(key: String, bundle: Bundle = .main) {
self.key = key
self.bundle = bundle
}

// MARK: Implementation

private func value(for key: String) -> Value {
guard let object = bundle.object(forInfoDictionaryKey: key) else {
fatalError("Missing key: \(key)")
}

switch object {
case let value as Value:
return value

case let string as String:
guard let value = Value(string) else { fallthrough }
return value

default:
fatalError("Invalid Value")
}
}
}
3 changes: 3 additions & 0 deletions Tests/XcodeConfigTests/XcodeConfigTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import XCTest

final class XcodeConfigTests: XCTestCase {}

0 comments on commit db55534

Please sign in to comment.