From b6774f1f05b343b08f29ce2c47095c408e6db6ce Mon Sep 17 00:00:00 2001 From: Mark Murray <2034704+markmur@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:13:04 +0000 Subject: [PATCH] Configure swiftformat + modify license CI check (#247) * Implement swiftformat to auto-format sample code * Add format build phase to MobileBuyIntegration --- .github/workflows/lint.yml | 2 +- .swiftformat | 9 ++++++ .../project.pbxproj | 19 ++++++++++++ Scripts/copy_license | 20 ------------- Scripts/ensure_license | 29 +++++++++++++++++++ 5 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 .swiftformat delete mode 100755 Scripts/copy_license create mode 100755 Scripts/ensure_license diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 5c3a275c..87baa098 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -19,7 +19,7 @@ jobs: runs-on: macos-latest steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - - run: ./Scripts/copy_license && git diff --name-only --exit-code + - run: ./Scripts/ensure_license lint-podspec: name: CocoaPods diff --git a/.swiftformat b/.swiftformat new file mode 100644 index 00000000..966a7da1 --- /dev/null +++ b/.swiftformat @@ -0,0 +1,9 @@ +--indent 4 +--linebreaks lf +--wraparguments before-first +--wrapcollections before-first +--commas inline +--allman false +--semicolons inline +--trimwhitespace always +--disable redundantReturn diff --git a/Samples/MobileBuyIntegration/MobileBuyIntegration.xcodeproj/project.pbxproj b/Samples/MobileBuyIntegration/MobileBuyIntegration.xcodeproj/project.pbxproj index 83cec568..6eaac218 100644 --- a/Samples/MobileBuyIntegration/MobileBuyIntegration.xcodeproj/project.pbxproj +++ b/Samples/MobileBuyIntegration/MobileBuyIntegration.xcodeproj/project.pbxproj @@ -174,6 +174,7 @@ isa = PBXNativeTarget; buildConfigurationList = 4EBBA77B2A5F0CE200193E19 /* Build configuration list for PBXNativeTarget "MobileBuyIntegration" */; buildPhases = ( + 6A9124C62CF6114E0076C21C /* Run Swiftformat over sample code */, 4EBBA7632A5F0CE200193E19 /* Sources */, 4EBBA7642A5F0CE200193E19 /* Frameworks */, 4EBBA7652A5F0CE200193E19 /* Resources */, @@ -264,6 +265,24 @@ shellPath = /bin/sh; shellScript = "# Remove .pcm binaries from build\nfind \"${TARGET_BUILD_DIR}/${FULL_PRODUCT_NAME}\" -name \"*.pcm\" -type f -delete\n\n# Remove Swiftlint binaries from build\nfind \"${TARGET_BUILD_DIR}/${FULL_PRODUCT_NAME}\" -iname \"swiftlint*\" -type f -delete\n\n\n\n"; }; + 6A9124C62CF6114E0076C21C /* Run Swiftformat over sample code */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + ); + name = "Run Swiftformat over sample code"; + outputFileListPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "export PATH=\"/opt/homebrew/bin:$PATH\"\n\nif which swiftformat >/dev/null; then\n\tswiftformat \"${SRCROOT}\" \nelse \n\techo \"error: SwiftFormat not installed, download using \\\"brew install swiftformat\\\"\"\nfi\n"; + }; /* End PBXShellScriptBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ diff --git a/Scripts/copy_license b/Scripts/copy_license deleted file mode 100755 index 2017e747..00000000 --- a/Scripts/copy_license +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env ruby - -require 'find' - -license_text = "/*\n" + File.read('LICENSE') + "*/\n\n" - -Find.find('.') do |path| - next unless File.file?(path) && path.end_with?('.swift') && !path.end_with?('Package.swift') - - content = File.read(path) - - # Remove existing license - content.gsub!(/\/\*.*?\*\//m, '') - # remove existing newlines from start of file - content.gsub!(/\A\n*/, '') - # Add new license - content.prepend(license_text) - - File.write(path, content) -end diff --git a/Scripts/ensure_license b/Scripts/ensure_license new file mode 100755 index 00000000..99a2881c --- /dev/null +++ b/Scripts/ensure_license @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby + +require 'find' + +# Read and normalize the license text by removing extra whitespace +license_text = "/*\n" + File.read('LICENSE') + "*/\n" +normalized_license = license_text.gsub(/\s+/, ' ').strip + +Find.find('.') do |path| + next unless File.file?(path) && path.end_with?('.swift') && !path.end_with?('Package.swift') + + # Read the current content of the Swift file + content = File.read(path) + + # Extract the existing license part from the file, if present + if content =~ /\A\s*\/\*.*?\*\//m + existing_license = $&.gsub(/\s+/, ' ').strip + else + existing_license = "" + end + + # Check if the existing license matches the normalized expected license + if existing_license != normalized_license + puts "License missing or incorrect in file: #{path}" + exit 1 + else + puts "✔️ #{path}" + end +end