xcrun Essentials

D36d6bab8f1e0ff4bb3377571e5f7dcd?s=47 Yutaro Muta
September 21, 2020

xcrun Essentials

D36d6bab8f1e0ff4bb3377571e5f7dcd?s=128

Yutaro Muta

September 21, 2020
Tweet

Transcript

  1. xcrun Essentials 2020/09/21 16:30~ @iOSDC Japan 2020 Yutaro Muta @yutailang0119

  2. • Yutaro Muta @yutailang0119 • Hatena Co., Ltd. @Kyoto •

    Conference Staff Who am I ?
  3. https://fortee.jp/iosdc-japan-2020/proposal/5a9f8fa1-8c89-470b-b72c-502f3a4a6f66

  4. Q: ޷͖ͳxcrunίϚϯυ͸ʁ • χίੜ΍Twitterʹ౤ߘ͠·͠ΐ͏ • ར༻͍ͯ͠Δਓ ίϚϯυ໊ • Θ͔Βͳ͍ਓ ޷͖ͳ͓ण࢘ωλ

  5. Goal • xcrunΛ஌Δ • xcrunΛ࢖͏

  6. Agenda • xcrunͱ͸Կ͔? • ར༻͍ͯ͠Δπʔϧ • Documentation Archive / Technical

    Note • man xcrun • xcrunΛ࢖͏ • ར༻ՄೳͳγϛϡϨʔλΛҰཡ • ίϚϯυϥΠϯ͔ΒTest CoverageΛऔಘ • FAQ • ·ͱΊ
  7. xcrunͱ͸Կ͔?

  8. ར༻͍ͯ͠Δπʔϧ

  9. ར༻͍ͯ͠Δπʔϧ • Carthage/Carthage • fastlane/fastlane • apple/swift -> utils •

    apple/swift-tools-support-core -> Utilities • apple/sourcekit-lsp -> xcrun sourcekit-lsp • etc...
  10. Technical Note TN2339 Building from the Command Line with Xcode

    FAQ https://developer.apple.com/library/archive/technotes/tn2339/_index.html
  11. Technical Note TN2339 Building from the Command Line with Xcode

    FAQ • Downloading command-line tools is not available in Xcode for macOS 10.9. How can I install them on my machine? > If Xcode is installed on your machine, then there is no need to install them. Xcode comes bundled with all your command-line tools. macOS 10.9 and later includes shims or wrapper executables. These shims, installed in /usr/bin, can map any tool included in /usr/bin to the corresponding one inside Xcode. xcrun is one of such shims, which allows you to find or run any tool inside Xcode from the command line. Use it to invoke any tool within Xcode from the command line as shown in Listing 1
  12. Technical Note TN2339 Building from the Command Line with Xcode

    FAQ • Downloading command-line tools is not available in Xcode for macOS 10.9. How can I install them on my machine? > If Xcode is installed on your machine, then there is no need to install them. Xcode comes bundled with all your command-line tools. macOS 10.9 and later includes shims or wrapper executables. These shims, installed in /usr/bin, can map any tool included in /usr/bin to the corresponding one inside Xcode. xcrun is one of such shims, which allows you to find or run any tool inside Xcode from the command line. Use it to invoke any tool within Xcode from the command line as shown in Listing 1
  13. man xcrun

  14. man xcrun XCRUN(1) BSD General Commands Manual XCRUN(1) NAME xcrun

    - Run or locate development tools and properties. SYNOPSIS xcrun [--sdk <SDK name>] --find <tool name> xcrun [--sdk <SDK name>] <tool name> ... tool arguments ... <tool name> ... tool arguments ... DESCRIPTION xcrun provides a means to locate or invoke developer tools from the command-line, without requiring users to modify Makefiles or otherwise take inconvenient measures to support multiple Xcode tool chains. The tool xcode-select(1) is used to set a system default for the active developer directory, and may be overridden by the DEVELOPER_DIR environment variable (see ENVIRONMENT). The SDK which will be searched defaults to the most recent available SDK, and can be specified by the SDKROOT environment variable or the --sdk option (which takes precedences over SDKROOT). When used to invoke another tool (as opposed to simply finding it), xcrun will provide the absolute path to the selected SDK in the SDKROOT environment variable. See ENVIRONMENT for more information. ......
  15. man xcrun XCRUN(1) BSD General Commands Manual XCRUN(1) NAME xcrun

    - Run or locate development tools and properties. SYNOPSIS xcrun [--sdk <SDK name>] --find <tool name> xcrun [--sdk <SDK name>] <tool name> ... tool arguments ... <tool name> ... tool arguments ... DESCRIPTION xcrun provides a means to locate or invoke developer tools from the command-line, without requiring users to modify Makefiles or otherwise take inconvenient measures to support multiple Xcode tool chains. The tool xcode-select(1) is used to set a system default for the active developer directory, and may be overridden by the DEVELOPER_DIR environment variable (see ENVIRONMENT). The SDK which will be searched defaults to the most recent available SDK, and can be specified by the SDKROOT environment variable or the --sdk option (which takes precedences over SDKROOT). When used to invoke another tool (as opposed to simply finding it), xcrun will provide the absolute path to the selected SDK in the SDKROOT environment variable. See ENVIRONMENT for more information. ......
  16. xcrunͱ͸Կ͔? • shimͷ1ͭ • ෳ਺ͷXcodeπʔϧνΣʔϯͷαϙʔτΛҙࣝ͢Δ͜ͱͳ͘ɺίϚϯυϥΠ ϯ͔ΒXcode಺ͷ೚ҙͷπʔϧΛݕࡧ·ͨ͸࣮ߦ͢ΔखஈΛఏڙ

  17. [ྫ֎] ಛघͳxcrunίϚϯυͨͪ • shellͷpath͕௨Δlocation ( /usr/bin ) ʹ΋shim͕഑ஔ͞ΕɺxcrunΛܦ༝ͤͣʹ࣮ߦՄೳ • xcodebuid

    • xed • IDEͱͯ͠ͷXcodeҎ֎ͷػೳ΁ϦϯΫ • xcrun sourcekit-lsp (Xcode 11.4+) • xcrun safari-web-extension-converter (Xcode 12+) • https://developer.apple.com/documentation/safariservices/safari_web_extensions/converting_a_web_extension_for_safari
  18. xcrunΛ࢖͏

  19. man xcrun

  20. man xcrun XCRUN(1) BSD General Commands Manual XCRUN(1) DESCRIPTION xcrun

    provides a means to locate or invoke developer tools from the command-line, without requiring users to modify Makefiles or otherwise take inconvenient measures to support multiple Xcode tool chains. The tool xcode-select(1) is used to set a system default for the active developer directory, and may be overridden by the DEVELOPER_DIR environment variable (see ENVIRONMENT). The SDK which will be searched defaults to the most recent available SDK, and can be specified by the SDKROOT environment variable or the --sdk option (which takes precedences over SDKROOT). When used to invoke another tool (as opposed to simply finding it), xcrun will provide the absolute path to the selected SDK in the SDKROOT environment variable. See ENVIRONMENT for more information. Usage xcrun supports several different usages, to both look up the paths to tools as well as execute them. When used with the --find argument, as in xcrun [--sdk <SDK name>] --find <tool name>, the absolute path to the tool (in the provided SDK, if given) will be printed. When used without --find, the name of a tool is required and the tool will be executed with the provided arguments. When used as the target of a symbolic link, it derives the tool name to use from the name it was invoked under, and then executes that tool. ......
  21. 1. ར༻ՄೳͳγϛϡϨʔλΛҰཡ

  22. xcrun simctl

  23. ར༻ՄೳͳγϛϡϨʔλΛҰཡ $ xcrun simctl list devices == Devices == --

    iOS 14.0 -- iPhone 8 (30D89693-D53D-4461-93E0-4BE0877DE2AD) (Shutdown) iPhone 8 Plus (0F2B88B5-F8E0-496E-8603-BBE451D0D2C0) (Shutdown) iPhone 11 (990DABBE-9358-43E7-98DD-8A78A7A30340) (Shutdown) iPhone 11 Pro (9F4628C6-F133-4D3B-8902-9B555F032D22) (Shutdown) ...... -- tvOS 14.0 -- -- watchOS 7.0 -- -- Unavailable: com.apple.CoreSimulator.SimRuntime.iOS-13-6 -- ...... nbv -- Unavailable: com.apple.CoreSimulator.SimRuntime.iOS-13-7 -- ...... -- Unavailable: com.apple.CoreSimulator.SimRuntime.tvOS-13-7 -- ...... -- Unavailable: com.apple.CoreSimulator.SimRuntime.watchOS-6-2 -- ......
  24. [Ԡ༻] CI্ͰͷςετͰ ςετσόΠεΛOSࢦఆ

  25. fastlane.scan Ͱ͸ɺ ςετσόΠεͷॊೈͳࢦఆ͕͠ʹ͍͘

  26. ςετσόΠεͷॊೈͳࢦఆ͕͠ʹ͍͘ • fastlane.scan ͷςετσόΠε͸ɺ ୺຤Ϟσϧ໊ (OSόʔδϣϯ) [iPhone 8 (14.0)] Ͱࢦఆ͢Δ

    • xcodebuild ͷ࢓༷ -destination 'platform=iOS Simulator,name=iPhone 8,OS=14.0' • ࢦఆͨ͠σόΠε͕ଘࡏ͠ͳ͍৔߹ɺద౰ͳσόΠεʹϑΥʔϧόοΫ • ྫ͑͹ɺϝδϟʔOS 2όʔδϣϯͷςετΛCIͰ࣮ߦ͢ΔͨΊʹ͸ɺ XcodeͷϚΠφʔΞοϓσʔτ͝ͱʹࢦఆͷมߋ͕ඞཁ
  27. ςετσόΠεͷॊೈͳࢦఆ͕͠ʹ͍͘ lane :test do |options| scan( ...... devices: [ "iPhone

    8 (13.7)", # ϚγϯʹΠϯετʔϧ͞Ε͍ͯͳ͍σόΠε "iOS 14", # OSόʔδϣϯͷΈͷࢦఆ͸Ͱ͖ͳ͍ "iPhone 8" # OSόʔδϣϯΛࢦఆͤͣʹ࣮ߦɺ͜ͷϞσϧ͕ར༻Մೳ͔͸ෆ໌ ], ...... ) end [HH:mm:ss]: No simulators found that are equal to the version of specifier (13.7) and greater than or equal to the version of deployment target (0) [HH:mm:ss]: Ignoring 'iPhone 8 (13.7)', couldn’t find matching simulator [HH:mm:ss]: Ignoring 'iOS 14', couldn’t find matching simulator [HH:mm:ss]: Couldn't find any matching simulators for '["iPhone 8 (13.7)", "iOS 14"]' - falling back to default simulator [HH:mm:ss]: Found simulator "iPhone 8 (14.0)"
  28. xcrun simctl list devices Ͱར༻ՄೳͳσόΠεΛߜΓࠐΉ

  29. ར༻ՄೳͳσόΠεΛߜΓࠐΉ def self.run(params) require 'open3' require "json" stdout, stderr, status

    = Open3.capture3(*['xcrun', 'simctl', 'list', 'devices', '--json']) # JSONܗࣜͰऔಘ json = JSON.parse(stdout) devices = json['devices'].flat_map { |runtime, metas| metas.map { |meta| Device.new(runtime, meta) # σόΠεܕʹϚοϓ } } devices.filter! { |device| # ৚݅ʹ߹ΘͤͯϑΟϧλ device.os_version.start_with?(*params[:os_versions]) } unless params[:os_versions].empty? devices.filter! { |device| device.name.start_with?(*params[:models]) } unless params[:models].empty? devices.map { |device| device.hash } end https://gist.github.com/yutailang0119/fb9b2df0814575083931f3ed87733d23
  30. iOS 14ͷiPhoneΛࢦఆ͢Δ lane :test do |options| ios_14_device = xcode_simulator_devices( os_versions:

    ['iOS 14'], models: ['iPhone'] ).last devices = [ios_14_device] if devices.include?(nil) then UI.user_error! "The specified devices is not available: #{devices}" end device_identifiers = devices.map { |device| device[:identifier] } scan( ...... devices: device_identifiers, # ["iPhone 8 (14.0)"] ...... ) end
  31. 2. ίϚϯυϥΠϯ͔Β Test CoverageΛऔಘ

  32. Test Coverage in Xcode

  33. Test Coverage in Xcode

  34. Test Coverage in Xcode • Build Scheme > Test >

    Options > Code Coverage: Gather coverage for... Λ Φϯ • ςετΛ࣮ߦ࣌ɺDerivedData/{schemeName}/Logs/Test ҎԼʹɺςετΧ όϨοδ৘ใͳͲΛؚΉ .xcresult ͕ੜ੒͞ΕΔ • Xcode্ͰϏδϡΞϥΠζͯ͠ɺݟΔ͜ͱ͕Ͱ͖Δ • .xcresult ࣗମ͸ɺώϡʔϚϯϦʔμϒϧͰ͸ͳ͍
  35. CI্ͷςετͰ΋ ςετΧόϨοδΛऔಘ͍ͨ͠

  36. CI্ͷςετͰ΋ɺςετΧόϨοδΛऔಘ͍ͨ͠ • CIͰςετΧόϨοδΛऔಘ͠ɺه࿥͢Δ • GitHub Pull RequestʹίϝϯτܗࣜͰՄࢹԽ • MackerelͷService Metricsʹ౤ߘ͢Δ͜ͱͰɺมԽΛه࿥

    • hatena/fastlane-plugin-mackerel_actions > mackerel_post_xcresult
  37. GitHub Pull RequestʹίϝϯτܗࣜͰՄࢹԽ

  38. .xcresult ͔ΒTest CoverageΛऔಘ

  39. xcrun xccov

  40. .xcresult ͔ΒTest CoverageΛऔಘ def self.run(params) require 'open3' require 'json' scan()

    xcresult_file = Dir["#{ENV["PWD"]}/build/**/Logs/Test/*.xcresult"].last stdout, stderr, = Open3.capture3('xcrun', 'xccov', 'view', '--report', xcresult_file, '--json') JSON.parse(stdout) end https://github.com/yutailang0119/fastlane-plugin-xcresult_actions
  41. FAQ

  42. Q: Xcodeͷػೳ͕ xcrunͷͲͷίϚϯυʹରԠ͍ͯ͠Δ͔ Ͳ͏΍ͬͯ୳͢ͷʁ

  43. A: جຊ͸ҎԼʹ͋ΔίϚϯυ • $ ls /Applications/Xcode.app/Contents/Developer/usr/bin • $ ls /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

  44. xcrunͷา͖ํ • ໢ཏతͳυΩϡϝϯτ͸͓ͦΒ͘ͳ͍ • man xcrun ΍ xcrun --help ʹ΋ɺ֤ػೳ΁ͷݴٴ͸ͳ͍

    • લड़ͷpathʹ͋ΔίϚϯυͷhelpΛࢀর • https://developer.apple.com/documentation ͔Βɺ࣮ݱ͍ͨ͠಺༰Λݟͭ ͚ɺxcrunͷهࡌ͕ͳ͍͔୳͢
  45. ·ͱΊ

  46. ·ͱΊ • xcrun͸ɺίϚϯυϥΠϯ͔ΒXcode಺ͷ೚ҙͷπʔϧΛݕࡧɺ࣮ߦͰ͖Δ • IDEͱͯ͠ͷXcodeʹͳ͍πʔϧ΋͋Δ • CIͰXcodeͷػೳΛར༻͍ͨ͠৔߹ʹ༗༻ • xcrun͕ࢀর͢ΔpathͷҰཡΛோΊͯΈΔ •

    ئΘ͘͸ɺπʔϧ͕૿͑ͯཉ͍͠
  47. Q: xcrunΘ͔ͬͨʁ • χίੜ΍Twitterʹ౤ߘ͠·͠ΐ͏ • ࢖͑ͦ͏ͳਓ "xcrunŧŔŕŪũƄŝſ"

  48. Enjoy iOSDC, Thanks!!! • yutailang0119@gmail.com • https://twitter.com/yutailang0119 • https://github.com/yutailang0119

  49. Reference • https://fortee.jp/iosdc-japan-2020/proposal/5a9f8fa1-8c89-470b-b72c-502f3a4a6f66 • https://developer.apple.com/library/archive/technotes/tn2339/_index.html • https://developer.apple.com/documentation/safariservices/safari_web_extensions/ converting_a_web_extension_for_safari • https://gist.github.com/yutailang0119/fb9b2df0814575083931f3ed87733d23

    • https://swet.dena.com/entry/2019/10/23/080000 • https://github.com/yutailang0119/fastlane-plugin-xcresult_actions • https://github.com/hatena/fastlane-plugin-mackerel_actions