Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate optional info plist and entitlements #415

Merged
merged 8 commits into from
Oct 30, 2018
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#### Added
- Added `weak` linking setting for dependencies [#411](https://github.com/yonaskolb/XcodeGen/pull/411) @alvarhansen
- Added `info` to targets for generating an `Info.plist` [#415](https://github.com/yonaskolb/XcodeGen/pull/415) @yonaskolb
- Added `entitlements` to targets for generating an `.entitlement` file [#415](https://github.com/yonaskolb/XcodeGen/pull/415) @yonaskolb

#### Changed
- Performance improvements for large projects [#388](https://github.com/yonaskolb/XcodeGen/pull/388) [#417](https://github.com/yonaskolb/XcodeGen/pull/417) [#416](https://github.com/yonaskolb/XcodeGen/pull/416) @yonaskolb @kastiglione
Expand Down
29 changes: 29 additions & 0 deletions Docs/ProjectSpec.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ Settings are merged in the following order: groups, base, configs.
- `FRAMEWORK_SEARCH_PATHS`: If carthage dependencies are used, the platform build path will be added to this setting
- `OTHER_LDFLAGS`: See `requiresObjCLinking` below
- [ ] **dependencies**: **[[Dependency](#dependency)]** - Dependencies for the target
- [ ] **info**: **[Plist](#plist)** - If defined, this will generate and write an `Info.plist` to the specified path and use it by setting the `INFOPLIST_FILE` build setting for every configuration. The following properties are generated automatically, the rest will have to be provided.
- `CFBundleIdentifier`
- `CFBundleInfoDictionaryVersion`
- `CFBundleExecutable`
- `CFBundleName`
- `CFBundleDevelopmentRegion`
- `CFBundleShortVersionString`
- `CFBundleVersion`
- `CFBundlePackageType`
- [ ] **entitlements**: **[Plist](#plist)** - If defined this will generate and write a `.entitlements` file, and use it by setting `CODE_SIGN_ENTITLEMENTS` build setting for every configuration. All properties must be provided
- [ ] **templates**: **[String]** - A list of target templates that will be merged in order
- [ ] **transitivelyLinkDependencies**: **Bool** - If this is not specified the value from the project set in [Options](#options)`.transitivelyLinkDependencies` will be used.
- [ ] **directlyEmbedCarthageDependencies**: **Bool** - If this is `true` Carthage dependencies will be embedded using an `Embed Frameworks` build phase instead of the `copy-frameworks` script. Defaults to `true` for all targets except iOS/tvOS/watchOS Applications.
Expand Down Expand Up @@ -371,6 +381,25 @@ targets:
Debug: App/debug.xcconfig
Release: App/release.xcconfig
```
### Plist
Plists are created on disk on every generation of the project. They can be used as a way to define `Info.plist` or `.entitlement` files. Some `Info.plist` properties are generated automatically.

- [x] **path**: **String** - This is the path where the plist will be written to
- [x] **properties**: **[String: Any]** - This is a map of all the plist keys and values

```yml
targets:
App:
info:
path: App/Info.plist
properties:
UISupportedInterfaceOrientations: [UIInterfaceOrientationPortrait]
UILaunchStoryboardName: LaunchScreen
entitlements:
path: App/App.entitlements
properties:
com.apple.security.application-groups: group.com.app
```

### Build Script

Expand Down
26 changes: 26 additions & 0 deletions Sources/ProjectSpec/Plist.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation
import JSONUtilities

public struct Plist: Equatable {

public let path: String
public let properties: [String: Any]

public init(path: String, attributes: [String: Any] = [:]) {
self.path = path
self.properties = attributes
}

public static func == (lhs: Plist, rhs: Plist) -> Bool {
return lhs.path == rhs.path &&
NSDictionary(dictionary: lhs.properties).isEqual(to: rhs.properties)
}
}

extension Plist: JSONObjectConvertible {

public init(jsonDictionary: JSONDictionary) throws {
path = try jsonDictionary.json(atKeyPath: "path")
properties = jsonDictionary.json(atKeyPath: "properties") ?? [:]
}
}
16 changes: 16 additions & 0 deletions Sources/ProjectSpec/Target.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public struct Target: ProjectTarget {
public var settings: Settings
public var sources: [TargetSource]
public var dependencies: [Dependency]
public var info: Plist?
public var entitlements: Plist?
public var transitivelyLinkDependencies: Bool?
public var directlyEmbedCarthageDependencies: Bool?
public var requiresObjCLinking: Bool?
Expand Down Expand Up @@ -63,6 +65,8 @@ public struct Target: ProjectTarget {
configFiles: [String: String] = [:],
sources: [TargetSource] = [],
dependencies: [Dependency] = [],
info: Plist? = nil,
entitlements: Plist? = nil,
transitivelyLinkDependencies: Bool? = nil,
directlyEmbedCarthageDependencies: Bool? = nil,
requiresObjCLinking: Bool? = nil,
Expand All @@ -82,6 +86,8 @@ public struct Target: ProjectTarget {
self.configFiles = configFiles
self.sources = sources
self.dependencies = dependencies
self.info = info
self.entitlements = entitlements
self.transitivelyLinkDependencies = transitivelyLinkDependencies
self.directlyEmbedCarthageDependencies = directlyEmbedCarthageDependencies
self.requiresObjCLinking = requiresObjCLinking
Expand Down Expand Up @@ -209,6 +215,8 @@ extension Target: Equatable {
lhs.settings == rhs.settings &&
lhs.configFiles == rhs.configFiles &&
lhs.sources == rhs.sources &&
lhs.info == rhs.info &&
lhs.entitlements == rhs.entitlements &&
lhs.dependencies == rhs.dependencies &&
lhs.prebuildScripts == rhs.prebuildScripts &&
lhs.postbuildScripts == rhs.postbuildScripts &&
Expand Down Expand Up @@ -278,6 +286,14 @@ extension Target: NamedJSONDictionaryConvertible {
} else {
dependencies = try jsonDictionary.json(atKeyPath: "dependencies", invalidItemBehaviour: .fail)
}

if jsonDictionary["info"] != nil {
info = try jsonDictionary.json(atKeyPath: "info") as Plist
}
if jsonDictionary["entitlements"] != nil {
entitlements = try jsonDictionary.json(atKeyPath: "entitlements") as Plist
}

transitivelyLinkDependencies = jsonDictionary.json(atKeyPath: "transitivelyLinkDependencies")
directlyEmbedCarthageDependencies = jsonDictionary.json(atKeyPath: "directlyEmbedCarthageDependencies")
requiresObjCLinking = jsonDictionary.json(atKeyPath: "requiresObjCLinking")
Expand Down
16 changes: 4 additions & 12 deletions Sources/XcodeGen/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,11 @@ func generate(spec: String, project: String, isQuiet: Bool, justVersion: Bool) {
let xcodeProject = try projectGenerator.generateXcodeProject()

logger.info("⚙️ Writing project...")
let projectWriter = ProjectWriter(project: project)
try projectWriter.writeXcodeProject(xcodeProject)
try projectWriter.writePlists()

let projectFile = projectPath + "\(project.name).xcodeproj"
let tempPath = Path.temporary + "XcodeGen_\(Int(NSTimeIntervalSince1970))"
try? tempPath.delete()
if projectFile.exists {
try projectFile.copy(tempPath)
}
try xcodeProject.write(path: tempPath, override: true)
try? projectFile.delete()
try tempPath.copy(projectFile)
try? tempPath.delete()

logger.success("💾 Saved project to \(projectFile.string)")
logger.success("💾 Saved project to \(project.projectPath.string)")
} catch let error as SpecValidationError {
fatalError(error.description)
} catch {
Expand Down
42 changes: 42 additions & 0 deletions Sources/XcodeGenKit/InfoPlistGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Foundation
import ProjectSpec
import PathKit

public class InfoPlistGenerator {

/**
Default info plist attributes taken from:
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project Templates/Base/Base_DefinitionsInfoPlist.xctemplate/TemplateInfo.plist
*/
var defaultInfoPlist: [String: Any] = {
var dictionary: [String: Any] = [:]
dictionary["CFBundleIdentifier"] = "$(PRODUCT_BUNDLE_IDENTIFIER)"
dictionary["CFBundleInfoDictionaryVersion"] = "6.0"
dictionary["CFBundleExecutable"] = "$(EXECUTABLE_NAME)"
dictionary["CFBundleName"] = "$(PRODUCT_NAME)"
dictionary["CFBundleDevelopmentRegion"] = "$(DEVELOPMENT_LANGUAGE)"
dictionary["CFBundleShortVersionString"] = "1.0"
dictionary["CFBundleVersion"] = "1"
return dictionary
}()

public func generateProperties(target: Target) -> [String: Any] {
var targetInfoPlist = defaultInfoPlist
switch target.type {
case .uiTestBundle,
.unitTestBundle:
targetInfoPlist["CFBundlePackageType"] = "BNDL"
case .application,
.watch2App:
targetInfoPlist["CFBundlePackageType"] = "APPL"
case .framework:
targetInfoPlist["CFBundlePackageType"] = "FMWK"
case .bundle:
targetInfoPlist["CFBundlePackageType"] = "BNDL"
case .xpcService:
targetInfoPlist["CFBundlePackageType"] = "XPC"
default: break
}
return targetInfoPlist
}
}
11 changes: 9 additions & 2 deletions Sources/XcodeGenKit/PBXProjGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,15 @@ public class PBXProjGenerator {
let configs: [XCBuildConfiguration] = project.configs.map { config in
var buildSettings = project.getTargetBuildSettings(target: target, config: config)

// automatically set INFOPLIST_FILE path
if !project.targetHasBuildSetting("INFOPLIST_FILE", target: target, config: config) {
// Set CODE_SIGN_ENTITLEMENTS
if let entitlements = target.entitlements {
buildSettings["CODE_SIGN_ENTITLEMENTS"] = entitlements.path
}

// Set INFOPLIST_FILE
if let info = target.info {
buildSettings["INFOPLIST_FILE"] = info.path
} else if !project.targetHasBuildSetting("INFOPLIST_FILE", target: target, config: config) {
if searchForPlist {
plistPath = getInfoPlist(target.sources)
searchForPlist = false
Expand Down
56 changes: 56 additions & 0 deletions Sources/XcodeGenKit/ProjectWriter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Foundation
import ProjectSpec
import xcodeproj
import PathKit

public class ProjectWriter {

let project: Project

public init(project: Project) {
self.project = project
}

public func writeXcodeProject(_ xcodeProject: XcodeProj) throws {
let projectPath = project.projectPath
let tempPath = Path.temporary + "XcodeGen_\(Int(NSTimeIntervalSince1970))"
try? tempPath.delete()
if projectPath.exists {
try projectPath.copy(tempPath)
}
try xcodeProject.write(path: tempPath, override: true)
try? projectPath.delete()
try tempPath.copy(projectPath)
try? tempPath.delete()
}

public func writePlists() throws {

let infoPlistGenerator = InfoPlistGenerator()
for target in project.targets {
// write Info.plist
if let plist = target.info {
let properties = infoPlistGenerator.generateProperties(target: target).merged(plist.properties)
try writePlist(properties, path: plist.path)
}

// write entitlements
if let plist = target.entitlements {
try writePlist(plist.properties, path: plist.path)
}
}
}

private func writePlist(_ plist: [String: Any], path: String) throws {
let path = project.basePath + path
if path.exists, let data: Data = try? path.read(),
let existingPlist = (try? PropertyListSerialization.propertyList(from: data, format: nil)) as? [String: Any], NSDictionary(dictionary: plist).isEqual(to: existingPlist) {
// file is the same
return
}
let data = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)
try? path.delete()
try path.parent().mkpath()
try path.write(data)
}
}
8 changes: 8 additions & 0 deletions Tests/Fixtures/TestProject/App_iOS/App.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?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>com.apple.security.application-groups</key>
<string>group.com.app</string>
</dict>
</plist>
2 changes: 2 additions & 0 deletions Tests/Fixtures/TestProject/App_macOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>CustomSetting</key>
<string>$CUSTOM_SETTING</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSMainStoryboardFile</key>
Expand Down
8 changes: 8 additions & 0 deletions Tests/Fixtures/TestProject/Project.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@
FR_DBD29CA78CDBBEF69B2C39C6D23BBDA1 /* Empty.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Empty.h; sourceTree = "<group>"; };
FR_DC1C8DE46218F90A3F4FD5F8DE4F0ABB /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Result.framework; sourceTree = "<group>"; };
FR_E3314150DA4CEFF65D69CF7DB678E845 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
FR_E7BC65B06A0819B53634C5CD51946D98 /* App.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = App.entitlements; sourceTree = "<group>"; };
FR_EBD99C110BD7AE780C87A31CF2E4E7DB /* App_iOS_Tests.xctest */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.cfbundle; path = App_iOS_Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
FR_ED407ECED0DF010B1E787D238EA91A5D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
FR_EFD283107EDF836BF0D9F4EB3F9A0016 /* Framework.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Framework.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -722,6 +723,7 @@
G_8F5765334752A7E52B1E63ACAC466729 /* App */ = {
isa = PBXGroup;
children = (
FR_E7BC65B06A0819B53634C5CD51946D98 /* App.entitlements */,
FR_B993F75B001AB1C272CE83CACC06F0E5 /* AppDelegate.swift */,
FR_A0867B127ACF3ED382EB2FD5133D3EA8 /* Assets.xcassets */,
FR_05405007CB77B2E003B19B89401C12AB /* Info.plist */,
Expand Down Expand Up @@ -2258,6 +2260,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = App_iOS/App.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -2374,6 +2377,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = App_iOS/App.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -3920,6 +3924,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = App_iOS/App.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -4248,6 +4253,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = App_iOS/App.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -4293,6 +4299,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = App_iOS/App.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -4384,6 +4391,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = App_iOS/App.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down
12 changes: 12 additions & 0 deletions Tests/Fixtures/TestProject/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ targets:
App_macOS:
type: application
platform: macOS
info:
path: App_macOS/Info.plist
properties:
LSMinimumSystemVersion: $(MACOSX_DEPLOYMENT_TARGET)
NSMainStoryboardFile: Main
NSPrincipalClass: NSApplication
CFBundleIconFile: ""
CustomSetting: $CUSTOM_SETTING
attributes:
ProvisioningStyle: Automatic
sources:
Expand All @@ -39,6 +47,10 @@ targets:
App_iOS:
type: application
platform: iOS
entitlements:
path: App_iOS/App.entitlements
properties:
com.apple.security.application-groups: group.com.app
attributes:
ProvisioningStyle: Automatic
sources:
Expand Down
4 changes: 3 additions & 1 deletion Tests/XcodeGenKitTests/ProjectFixtureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ fileprivate func generateXcodeProject(specPath: Path, file: String = #file, line
let project = try Project(path: specPath)
let generator = ProjectGenerator(project: project)
let xcodeProject = try generator.generateXcodeProject()
try xcodeProject.write(path: project.projectPath, override: true)
let writer = ProjectWriter(project: project)
try writer.writePlists()
try writer.writeXcodeProject(xcodeProject)

return xcodeProject
}
Loading