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

时间:2017-01-07 23:35:28

标签: swift xcode swift-package-manager server-side-swift

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

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

相关问题是:当我执行swift package generate-xcodeproj时,我希望使用相同的构建设置。

建议?

3 个答案:

答案 0 :(得分:4)

我无法帮助Copy Files Phase

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

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

Sandbox.xcconfig

FLAG_SANDBOX = -DSANDBOX
OTHER_SWIFT_FLAGS = $(FLAG_SANDBOX)

这会创建一个Xcode项目,其中SANDBOX被定义。

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

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

答案 1 :(得分:3)

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

请参阅an example script

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

答案 2 :(得分:2)

根据上面的@ vadim回答,这是我问题第一部分的xcodeproj rubygem解决方案:

#!/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()