Xcode项目构建

Xcodegen构建Xcode Project

2019-07-04  本文已影响1人  SkyMing一C

xcodeprojects存在问题?

Xcode 使用项目文件.xcodeproj文件来捆绑 IDE 的源代码和资源.在大多数情况下是正常工作的,但它有以下几个缺点:

xcodegen 简介

xcodegen是一个工具,它允许我们从名为project.yml的文件中的定义生成xcodeproj文件。

由于xcodeproj文件可以随时生成,我们甚至不必将它保存在我们的git中并且可以忽略它。

以下是xcodegen的两个最重要的功能:

如何安装 GitHub

brew install xcodegen
mint install yonaskolb/xcodegen

生成一个App Project

name: XcodegenApp # The name of the App
options: # Some general settings for the project
  createIntermediateGroups: true # If the folders are nested, also nest the groups in Xcode
  indentWidth: 2 # indent by 2 spaces
  tabWidth: 2 # a tab is 2 spaces
  bundleIdPrefix: "com.SkyMing"
targets: # The List of our targets
  XcodegenApp:
    type: application
    platform: iOS
    deploymentTarget: "10.3"
    sources:
      #Sources
      - path: XcodegenApp
project.yml 文件内容
cd ~/Desktop/XcodegenApp
xcodegen
终端运行

生成 TestTargets

  XcodegenApp-iOS-Tests:
    type: bundle.unit-test
    platform: iOS
    deploymentTarget: "10.3"
    sources:
      - path: XcodegenAppTests
    dependencies:
      - target: XcodegenApp
project.yml添加TestTargets
xcodegen
  XcodegenApp-iOS-UITests:
    type: bundle.ui-testing
    platform: iOS
    sources:
      - path: XcodegenAppUITests
    dependencies:
      - target: XcodegenApp
屏幕快照 2019-07-04 上午11.20.34.png
xcodegen

生成一个 Framework 工程

创建了一个XcodegenAppCore框架,其中包含经典的水果枚举:

public enum Fruit: String {
  case apple
  case banana
  case cherry
}
XcodegenAppCore
<?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>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>FMWK</string>
    <key>CFBundleShortVersionString</key>
    <string>4.0</string>
    <key>CFBundleVersion</key>
    <string>$(CURRENT_PROJECT_VERSION)</string>
    <key>NSPrincipalClass</key>
    <string></string>
</dict>
</plist>
XcodegenAppCore:
  type: framework
  platform: iOS
  deploymentTarget: "10.3"
  sources:
    - path: XcodegenAppCore
project.yml添加XcodegenAppCore
dependencies:
    - target: XcodegenAppCore
project.yml添加dependencies
xcodegen
import UIKit

import XcodegenAppCore

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
      print(Fruit.apple)
    }
}
XcodegenAppCore的使用

使用CocoaPods添加依赖

1. 使用终端cd到工程目录下
2. 创建Podfile文件
pod init
3. 在项目目录里打开Podfile文件
open Podfile
4. 添加SnapKit
pod 'SnapKit', '~> 5.0.0'
5. 保存后退出
6. 开始下载第三方库
pod install
pod install

调整项目文件位置

屏幕快照 2019-07-04 下午2.36.55.png XcodegenApp.xcworkspace

编写genProj脚本忽略xcuserdata与xcworkspace文件并重新生成程序

#!/bin/bash

rm -rf XcodegenApp.xcworkspace/xcuserdata
rm -rf Pods/Pods.xcodeproj/xcuserdata

cd XcodegenApp
rm -rf XcodegenApp.xcodeproj
xcodegen --project .
cd XcodegenApp.xcodeproj
rm -rf project.xcworkspace
rm -rf xcuserdata
# rm -rf xcshareddata
sh genProj

参考

Better iOS projects: Getting (nearly) rid of Xcodeproject - A (not so) short Introduction to Xcodegen

Xcodegen介绍 | 一款可以帮助你更好的构建Xcode Project的工具

上一篇 下一篇

猜你喜欢

热点阅读