generate-podspec.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/bin/bash
  2. set -euo pipefail
  3. SCRIPT_NAME="$(basename "$0")"
  4. # Functions
  5. # #########
  6. # Arg1: Mode (full, usage_only). Defaults to 'full'.
  7. print_usage() {
  8. echo "Usage: ${SCRIPT_NAME} [VERSION]"
  9. if [[ "${1:-full}" == 'full' ]]; then
  10. echo ''
  11. echo 'Generates the podspec file for the current project.'
  12. echo 'Platform requirements (min SDKs), version are read from the xcconfig files.'
  13. echo 'If called with VERSION, the script verifies that the current MARKETING_VERSION in the xcconfig matches VERSION.'
  14. echo ''
  15. echo 'Examples:'
  16. echo "$ ${SCRIPT_NAME} # Gemerates podspec without verifying the MARKETING_VERSION against a given constant."
  17. echo "$ ${SCRIPT_NAME} 1.2.3 # Generates podspec and verifies that MARKETING_VERSION is 1.2.3."
  18. fi
  19. }
  20. # Arg1: Config variable name
  21. # Arg2: Config value pattern (regex) - must escape forward slashes (used as sed separator)
  22. # Arg3: Config file path
  23. read_config_var() {
  24. sed -nE "s/^ *${1} *= *(${2}) *$/\1/p" "${3}"
  25. return $?
  26. }
  27. # Parse arguments
  28. # ###############
  29. VERSION_TO_VERIFY=''
  30. if [[ $# -gt 0 ]]; then
  31. if [[ $# -eq 1 ]]; then
  32. if [[ $1 == '--help' ]] || [[ $1 == '-h' ]]; then
  33. print_usage 'full'
  34. exit 0
  35. fi
  36. VERSION_TO_VERIFY="$1"
  37. else
  38. echo 'Invalid number of arguments!'
  39. echo 'For more information use --help.'
  40. echo ''
  41. print_usage 'usage_only'
  42. exit 1
  43. fi
  44. fi
  45. # Define variables
  46. # ################
  47. SHARED_XCCONFIG_FILE='./Configs/Module-Shared.xcconfig'
  48. # We define separate vars here in case we ever split it up.
  49. VERSION_XCCONFIG_FILE="${SHARED_XCCONFIG_FILE}"
  50. SDKS_XCCONFIG_FILE="${SHARED_XCCONFIG_FILE}"
  51. VERSION_CONFIG_VAR='MARKETING_VERSION'
  52. MACOS_SDK_CONFIG_VAR='MACOSX_DEPLOYMENT_TARGET'
  53. IOS_SDK_CONFIG_VAR='IPHONEOS_DEPLOYMENT_TARGET'
  54. TVOS_SDK_CONFIG_VAR='TVOS_DEPLOYMENT_TARGET'
  55. VISIONOS_SDK_CONFIG_VAR='XROS_DEPLOYMENT_TARGET'
  56. WATCHOS_SDK_CONFIG_VAR='WATCHOS_DEPLOYMENT_TARGET'
  57. # Read files
  58. # ##########
  59. echo 'Reading config...'
  60. pushd "$(dirname "$0")/../" > /dev/null
  61. CURRENT_VERSION="$(read_config_var "${VERSION_CONFIG_VAR}" '[0-9]+\.[0-9]+\.[0-9]+' "${VERSION_XCCONFIG_FILE}")"
  62. MACOS_SDK="$(read_config_var "${MACOS_SDK_CONFIG_VAR}" '[0-9]+\.[0-9]+' "${SDKS_XCCONFIG_FILE}")"
  63. IOS_SDK="$(read_config_var "${IOS_SDK_CONFIG_VAR}" '[0-9]+\.[0-9]+' "${SDKS_XCCONFIG_FILE}")"
  64. TVOS_SDK="$(read_config_var "${TVOS_SDK_CONFIG_VAR}" '[0-9]+\.[0-9]+' "${SDKS_XCCONFIG_FILE}")"
  65. VISIONOS_SDK="$(read_config_var "${VISIONOS_SDK_CONFIG_VAR}" '[0-9]+\.[0-9]+' "${SDKS_XCCONFIG_FILE}")"
  66. WATCHOS_SDK="$(read_config_var "${WATCHOS_SDK_CONFIG_VAR}" '[0-9]+\.[0-9]+' "${SDKS_XCCONFIG_FILE}")"
  67. SUPPORTED_SWIFT_VERSIONS=''
  68. for SPM_PKG_DEF in Package@swift-*.swift; do
  69. SWIFT_VERSION="$(echo "${SPM_PKG_DEF}" | sed -E 's/^Package@swift-([0-9]+\.[0-9]+)\.swift$/\1/g')"
  70. if [[ -n "${SWIFT_VERSION}" ]] && [[ "${SWIFT_VERSION}" != "${SPM_PKG_DEF}" ]]; then
  71. # We add the comma to the end here, since we will add the last version at the end.
  72. SUPPORTED_SWIFT_VERSIONS="${SUPPORTED_SWIFT_VERSIONS}'${SWIFT_VERSION}', "
  73. fi
  74. done
  75. SUPPORTED_SWIFT_VERSIONS="${SUPPORTED_SWIFT_VERSIONS}'$(swift package tools-version | awk -F'.' '{print $1"."$2}')'"
  76. popd > /dev/null
  77. # Verify variables
  78. # ################
  79. echo 'Verifying config...'
  80. if [[ -z "${CURRENT_VERSION}" ]]; then
  81. echo "Could not find MARKETING_VERSION in ${VERSION_XCCONFIG_FILE}!"
  82. exit 1
  83. elif [[ -n "${VERSION_TO_VERIFY}" ]] && [[ "${VERSION_TO_VERIFY}" != "${CURRENT_VERSION}" ]]; then
  84. echo "MARKETING_VERSION in ${VERSION_XCCONFIG_FILE} is ${CURRENT_VERSION}, but ${VERSION_TO_VERIFY} was expected!"
  85. exit 1
  86. fi
  87. if [[ -z "${MACOS_SDK}" ]]; then
  88. echo "Could not find ${MACOS_SDK_CONFIG_VAR} in ${SDKS_XCCONFIG_FILE}!"
  89. exit 1
  90. fi
  91. if [[ -z "${IOS_SDK}" ]]; then
  92. echo "Could not find ${IOS_SDK_CONFIG_VAR} in ${SDKS_XCCONFIG_FILE}!"
  93. exit 1
  94. fi
  95. if [[ -z "${TVOS_SDK}" ]]; then
  96. echo "Could not find ${TVOS_SDK_CONFIG_VAR} in ${SDKS_XCCONFIG_FILE}!"
  97. exit 1
  98. fi
  99. if [[ -z "${VISIONOS_SDK}" ]]; then
  100. echo "Could not find ${VISIONOS_SDK_CONFIG_VAR} in ${SDKS_XCCONFIG_FILE}!"
  101. exit 1
  102. fi
  103. if [[ -z "${WATCHOS_SDK}" ]]; then
  104. echo "Could not find ${WATCHOS_SDK_CONFIG_VAR} in ${SDKS_XCCONFIG_FILE}!"
  105. exit 1
  106. fi
  107. # Generate podspec
  108. # ################
  109. echo "Generating podspec..."
  110. pushd "$(dirname "$0")/../" > /dev/null
  111. cat << EOF > ./CocoaLumberjack.podspec
  112. Pod::Spec.new do |s|
  113. s.name = 'CocoaLumberjack'
  114. s.version = '${CURRENT_VERSION}'
  115. s.license = 'BSD'
  116. s.summary = 'A fast & simple, yet powerful & flexible logging framework for macOS, iOS, tvOS, watchOS and visionOS.'
  117. s.authors = { 'Robbie Hanson' => 'robbiehanson@deusty.com' }
  118. s.homepage = 'https://github.com/CocoaLumberjack/CocoaLumberjack'
  119. s.source = { :git => 'https://github.com/CocoaLumberjack/CocoaLumberjack.git',
  120. :tag => "#{s.version}" }
  121. s.description = 'It is similar in concept to other popular logging frameworks such as log4j, ' \\
  122. 'yet is designed specifically for objective-c, and takes advantage of features ' \\
  123. 'such as multi-threading, grand central dispatch (if available), lockless ' \\
  124. 'atomic operations, and the dynamic nature of the objective-c runtime.'
  125. s.cocoapods_version = '>= 1.13.0'
  126. s.swift_versions = [${SUPPORTED_SWIFT_VERSIONS}]
  127. s.osx.deployment_target = '${MACOS_SDK}'
  128. s.ios.deployment_target = '${IOS_SDK}'
  129. s.tvos.deployment_target = '${TVOS_SDK}'
  130. s.visionos.deployment_target = '${VISIONOS_SDK}'
  131. s.watchos.deployment_target = '${WATCHOS_SDK}'
  132. s.pod_target_xcconfig = {
  133. 'OTHER_SWIFT_FLAGS' => '\$(inherited) -DCOCOAPODS'
  134. }
  135. s.preserve_paths = 'README.md', 'LICENSE'
  136. s.default_subspecs = 'Core'
  137. s.subspec 'Core' do |ss|
  138. ss.source_files = 'Sources/CocoaLumberjack/**/*.{h,m}'
  139. ss.private_header_files = 'Sources/CocoaLumberjack/DD*Internal.{h}'
  140. ss.resource_bundles = {
  141. 'CocoaLumberjackPrivacy' => ['Sources/CocoaLumberjack/PrivacyInfo.xcprivacy']
  142. }
  143. end
  144. s.subspec 'Swift' do |ss|
  145. ss.dependency 'CocoaLumberjack/Core'
  146. ss.source_files = 'Sources/CocoaLumberjackSwift/**/*.swift', 'Sources/CocoaLumberjackSwiftSupport/include/**/*.{h}'
  147. end
  148. end
  149. EOF
  150. popd > /dev/null