yonaskolb / XcodeGen

A Swift command line tool for generating your Xcode project
MIT License
7.06k stars 818 forks source link

Inherit from other target #285

Closed Lutzifer closed 6 years ago

Lutzifer commented 6 years ago

Is it currently possible to inherit from another target?

Use case:

As Additional Source Folders cannot be solved using configurations (or at least not in a good way that I know of) this would be one additional benefit of using xcodegen.

yonaskolb commented 6 years ago

You can't directly inherit from a target, but you can achieve something similar in 2 different ways:

Yaml references You can use native yaml anchors and references to refer to the inherited target and then just add some overrides. These page can help with yaml documentation if you need https://learnxinyminutes.com/docs/yaml/

File includes XcodeGen has a feature for including other spec files, which you can then override with your own settings. It doesn't really solve your problem as this wouldn't let you have 2 different targets, but you can at least define common target properties in one place and then use and override elsewhere. https://github.com/yonaskolb/XcodeGen/blob/master/Docs/ProjectSpec.md#include

If we were just talking about build settings, then settingGroups would have you covered.

Lutzifer commented 6 years ago

thx, will have a look into this

Lutzifer commented 6 years ago

For the record, referencing it with $main and *main works:

name: TemplateApp
options:
  bundleIdPrefix: de.number42
  createIntermediateGroups: true
targets:
  TemplateApp: &main
    type: application
    platform: iOS
    deploymentTarget: "10.3"
    indentWidth: 2
    tabWidth: 2
    sources:
      - path: CodeGeneration/SourceryProtocols
      - path: GeneratedCode
      - path: CodeGeneration/SourceryTemplates
        type: folder
      - path: TemplateApp
    prebuildScripts:
      - script: sh BuildScripts/Phases/iOSPreCompile.sh
        name: iOSPrescripts
  TemplateAppDebug:
    <<: *main
    sources:
      - path: CodeGeneration/SourceryProtocols
      - path: GeneratedCode
      - path: CodeGeneration/SourceryTemplates
        type: folder
      - path: TemplateApp
      - path: TemplateAppDebugModule

Haven't yet figured out how to do the same for the source folder to avoid the duplication

yonaskolb commented 6 years ago

Cool. Would having something like targetTemplates that you can inherit from be useful?

Lutzifer commented 6 years ago

@yonaskolb definitely useful and easier to read

albyok commented 6 years ago

I did something like this:

commonSources: &commonSources 
    - path: target_files/template
      name: Target_Template

And then in targets:

sources: 
      - path: target_files/moosaico
        name: Target_Moosaico
      - << : *commonSources 

The problem is that I can't specify more than one path in commonSources. I had to create a "commonSources2". I think it's kind of a bug, because I used the same approach for dependencies and there it worked like a charm with multiple frameworks.

yonaskolb commented 6 years ago

Implemented in #355 Does that meet your needs?

Lutzifer commented 6 years ago

@yonaskolb yes, it does. Thx and kudos!

Example usage:

targetTemplates:
  BaseApp:
    type: application
    platform: iOS
    sources:
      #Sources
      - path: Sources/N42Utils
      - path: Sources/AppTemplate/SourceryProtocols
      - path: Sources/AppTemplate/Base
      #Folder And File References
      - path: Carthage/Checkouts
        buildPhase: none
        type: folder
    prebuildScripts:
      - script: sh Resources/BuildScripts/Local/Timestamp.sh > $SRCROOT/.timestamp
                echo "Marked timestamp of run"
        name: Mark Timestamp
      - script: sh Resources/BuildScripts/Phases/iOSPrescripts.sh
        name: iOSPrescripts
      - script: sh $SRCROOT/Resources/Buildscripts/Local/MarkTimestamp.sh "Prescripts"
        name: Timestamp Prescripts
    postbuildScripts:
      - script: sh $SRCROOT/Resources/Buildscripts/Local/MarkTimestamp.sh "Timestamp Build Copy Link Carthage"
        name: Timestamp Build Copy Link Carthage
      - script: sh Resources/BuildScripts/Phases/iOSPostscripts.sh
        name: iOSPostscripts
      - script: sh $SRCROOT/Resources/Buildscripts/Local/MarkTimestamp.sh "Postscripts"
        name: Timestamp Postscripts
    attributes:
      ProvisioningStyle: Manual
      DevelopmentTeam: "$(N42_RELEASE_PROVISIONING_DEVELOPMENT_TEAM)"
    settings:
      base:
        PRODUCT_BUNDLE_IDENTIFIER: "$(N42_BUNDLE_IDENTIFIER)"
        PRODUCT_NAME: $(N42_PRODUCT_NAME)
        INFOPLIST_FILE: "Sources/AppTemplate/Base/Supporting Files/Info.plist"
        # SWIFT_OBJC_BRIDGING_HEADER: "Sources/Base/Supporting Files/Bridging-Header.h"
        CODE_SIGN_STYLE: Manual
        DEVELOPMENT_TEAM: "$(N42_RELEASE_PROVISIONING_DEVELOPMENT_TEAM)"
      configs:
        Release:
          CODE_SIGN_IDENTITY: $(N42_RELEASE_PROVISIONING_PROFILE_SIGNING_IDENTITY)
          CODE_SIGN_IDENTITY[sdk=iphoneos*]: $(N42_RELEASE_PROVISIONING_PROFILE_SIGNING_IDENTITY)
          PROVISIONING_PROFILE_SPECIFIER: $(N42_RELEASE_PROVISIONING_PROFILE)
        Debug:
          CODE_SIGN_IDENTITY: iOS Developer
          PROVISIONING_PROFILE_SPECIFIER: $(N42_INTERNAL_PROVISIONING_PROFILE)
    scheme:
      testTargets:
        - AppTemplateTests
        - AppTemplateUITests
    dependencies:
      - carthage: GRDB
      - carthage: RxGRDB
      - carthage: Alamofire
      - carthage: Differentiator
      - carthage: Dip
      - carthage: Moya
      - carthage: ObjcExceptionBridging
      - carthage: Result
      - carthage: RxBlocking
      - carthage: RxCocoa
      - carthage: RxDataSources
      - carthage: RxGesture
      - carthage: RxMoya
      - carthage: RxOptional
      - carthage: RxSwift
      - carthage: RxUserDefaults
      - carthage: TinyConstraints
      - carthage: XCGLogger
      #Crashlytics
      - framework: Carthage/Build/Crashlytics.framework
        embed: false
      - framework: Carthage/Build/Fabric.framework
        embed: false
      - target: KitTemplate
targets:
  AppTemplate:
    templates: [BaseApp]
    sources:
      - path: Sources/AppTemplate/Release
  AppTemplateDebug:
    templates: [BaseApp]
    settings:
      base:
        SWIFT_ACTIVE_COMPILATION_CONDITIONS: DEBUG_MODULES
    sources:
      - path: Sources/AppTemplate/Debug