繁体   English   中英

Swift Package Manager和Xcode:保留Xcode设置?

[英]Swift Package Manager and Xcode: Retaining Xcode Settings?

我正在Swift中开发一个服务器并使用Swift Package Manager。 在我的Mac OS系统上进行开发生成一个Xcode项目以使用Xcode作为我的IDE时,发现它很方便(即,有时我的包依赖关系必须更新。我一直在使用swift package generate-xcodeproj到目前为止我的问题出现了 - 我在Xcode中创建了一些设置。例如,我设置了一个DEBUG标志,我有一个.plist文件,它位于Copy Files阶段。这些文件在丢失时会丢失。我重新生成Xcode项目。似乎我不能简单地使用swift package update因为有时文件会在依赖项中发生变化,而这些文件不会传播到Xcode项目。

我想要的是在Xcode之外的文件中单独建立Xcode设置的方法,当我执行swift package generate-xcodeproj时可以将其导入Xcode。 我还没有看到这样做的方法。

一个相关的问题是:当我进行swift build我希望使用相同的构建设置。

建议?

我无法帮助Copy Files Phase

但是我刚刚玩弄条件编译,像这样:

swift package generate-xcodeproj --xcconfig-overrides Sandbox.xcconfig

Sandbox.xcconfig

FLAG_SANDBOX = -DSANDBOX
OTHER_SWIFT_FLAGS = $(FLAG_SANDBOX)

这将创建一个SANDBOX项目,其中SANDBOX SANDBOX。

这可以在像这样的swift代码中使用

#if SANDBOX
    print("sandbox")
#else
    print("production")
#endif

每次重新生成时,我都会使用脚本或makefile将设置导入到生成的Xcode项目中。 你可以使用xcodeproj ruby​​gem

查看示例脚本

关于在swift build使用您的设置,您能举例说明这样的设置吗? 通常,makefile可以读取您的设置文件并将相应的参数传递给swift build

基于@ vadim上面的回答,这是我问题第一部分的xcodeproj ruby​​gem解决方案:

#!/usr/bin/ruby

# Tweak the .xcodeproj after creating with the swift package manager.

# Resources: 
# https://stackoverflow.com/questions/41527782/swift-package-manager-and-xcode-retaining-xcode-settings/41612477#41612477
# https://stackoverflow.com/questions/20072937/add-run-script-build-phase-to-xcode-project-from-podspec
# https://github.com/IBM-Swift/Kitura-Build/blob/master/build/fix_xcode_project.rb
# http://www.rubydoc.info/github/CocoaPods/Xcodeproj/Xcodeproj%2FProject%2FObject%2FAbstractTarget%3Anew_shell_script_build_phase
# http://www.rubydoc.info/github/CocoaPods/Xcodeproj/Xcodeproj/Project/Object/AbstractTarget
# https://gist.github.com/niklasberglund/129065e2612d00c811d0
# https://github.com/CocoaPods/Xcodeproj
# https://stackoverflow.com/questions/34367048/how-do-you-automate-do-copy-files-in-build-phases-using-a-cocoapods-post-insta?rq=1

require 'xcodeproj'

path_to_project = "Server.xcodeproj"
project = Xcodeproj::Project.open(path_to_project)

# 1) Add Copy Files Phase for Server.plist to the Products directory for Server target
target = project.targets.select { |target| target.name == 'Server' }.first
puts "Add Copy Files Phase to #{target}"
phase = target.new_copy_files_build_phase()

# Contrary to the docs (see http://www.rubydoc.info/github/CocoaPods/Xcodeproj/Xcodeproj/Project/Object/PBXCopyFilesBuildPhase) I believe this is not a path, but rather a code, e.g., 16 indicates to copy the file to the Products Directory.
phase.dst_subfolder_spec = "16"

fileRef = project.new(Xcodeproj::Project::Object::PBXFileReference)
fileRef.path = 'Server.plist'

phase.add_file_reference(fileRef)   

# 2) Add in script phase for testing target-- because I haven't figured out to get access to the Products directory at test-run time.
target = project.targets.select { |target| target.name == 'ServerTests' }.first
puts "Add Script Phase to #{target}"
phase = target.new_shell_script_build_phase()
phase.shell_script = "cp Server.plist /tmp"

# 3) Add in DEBUG flag

# A little overkill, but hopefully appending a DEBUG flag in the Debug configuration for each target doesn't hurt it.
project.targets.each do |target|
    puts "Appending DEBUG flag to #{target}"

    if target.build_settings('Debug')['OTHER_SWIFT_FLAGS'].nil?
        target.build_settings('Debug')['OTHER_SWIFT_FLAGS'] = ""
    end

    target.build_settings('Debug')['OTHER_SWIFT_FLAGS'] << '-DDEBUG'
end

project.save()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM