From 7759726fc58baf63eef507f613b8adfac3922da2 Mon Sep 17 00:00:00 2001 From: Cal Stephens Date: Tue, 11 Jun 2024 10:15:07 -0700 Subject: [PATCH] Add command to update SwiftFormat reference to latest version --- .github/actions/setup/action.yml | 3 ++ .github/workflows/main.yml | 13 ++++++-- Gemfile | 3 ++ Gemfile.lock | 17 ++++++++++ Rakefile | 54 ++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Rakefile diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml index 9bacea6..60a29c0 100644 --- a/.github/actions/setup/action.yml +++ b/.github/actions/setup/action.yml @@ -10,3 +10,6 @@ runs: run: sudo xcode-select --switch /Applications/Xcode_${{ inputs.xcode }}.app if: ${{ inputs.xcode }} shell: bash + - name: Install Ruby Gems + run: bundle install + shell: bash diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5f22654..338ece1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,8 +17,11 @@ jobs: - '13.4.1' # Swift 5.6 steps: - uses: actions/checkout@v2 + - uses: ./.github/actions/setup + with: + xcode: ${{ matrix.xcode }} - name: Test Package Plugin - run: swift package --allow-writing-to-package-directory format --lint + run: bundle exec rake lint:swift test-package-plugin-macos-13: name: Test Package Plugin @@ -32,8 +35,11 @@ jobs: - '15.0' # Swift 5.9 steps: - uses: actions/checkout@v2 + - uses: ./.github/actions/setup + with: + xcode: ${{ matrix.xcode }} - name: Test Package Plugin - run: swift package --allow-writing-to-package-directory format --lint + run: bundle exec rake lint:swift unit-tests: name: Unit Tests @@ -45,5 +51,8 @@ jobs: - '15.0' # Swift 5.9 steps: - uses: actions/checkout@v2 + - uses: ./.github/actions/setup + with: + xcode: ${{ matrix.xcode }} - name: Run Unit Tests run: swift test diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..9c085a2 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' do + gem "rake", "~> 13.0.0" +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..2ffc012 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,17 @@ +GEM + specs: + +GEM + remote: https://rubygems.org/ + specs: + rake (13.0.6) + +PLATFORMS + arm64-darwin-23 + ruby + +DEPENDENCIES + rake (~> 13.0.0)! + +BUNDLED WITH + 2.5.4 diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..72c096f --- /dev/null +++ b/Rakefile @@ -0,0 +1,54 @@ +require 'json' +require 'net/http' +require 'json' +require 'tempfile' + +namespace :lint do + desc 'Lints swift files' + task :swift do + sh 'swift package --allow-writing-to-package-directory format --lint' + end +end + +namespace :format do + desc 'Formats swift files' + task :swift do + sh 'swift package --allow-writing-to-package-directory format' + end +end + +namespace :update do + desc 'Updates SwiftFormat to the latest version' + task :swiftformat do + # Find the most recent release of SwiftFormat in the https://github.com/calda/SwiftFormat repo. + response = Net::HTTP.get(URI('https://api.github.com/repos/calda/SwiftFormat/releases/latest')) + latest_release_info = JSON.parse(response) + + latest_version_number = latest_release_info['tag_name'] + + # Download the artifact bundle for the latest release and compute its checksum. + temp_dir = Dir.mktmpdir + artifact_bundle_url = "https://github.com/calda/SwiftFormat/releases/download/#{latest_version_number}/swiftformat.artifactbundle.zip" + artifact_bundle_zip_path = "#{temp_dir}/swiftformat.artifactbundle.zip" + + sh "curl #{artifact_bundle_url} -L --output #{artifact_bundle_zip_path}" + checksum = `swift package compute-checksum #{artifact_bundle_zip_path}` + + # Update the Package.swift file to reference this version + package_manifest_path = 'Package.swift' + package_manifest_content = File.read(package_manifest_path) + + updated_swift_format_reference = <<-EOS + .binaryTarget( + name: "SwiftFormat", + url: "https://github.com/calda/SwiftFormat/releases/download/#{latest_version_number}/SwiftFormat.artifactbundle.zip", + checksum: "#{checksum.strip}"), + EOS + + regex = /[ ]*.binaryTarget\([\S\s]*name: "SwiftFormat"[\S\s]*?\),\s/ + updated_package_manifest = package_manifest_content.gsub(regex, updated_swift_format_reference) + File.open(package_manifest_path, "w") { |file| file.puts updated_package_manifest } + + puts "Updated Package.swift to reference SwiftFormat #{latest_version_number}" + end +end