|
| 1 | +require 'json' |
| 2 | +require 'net/http' |
| 3 | +require 'json' |
| 4 | +require 'tempfile' |
| 5 | + |
| 6 | +namespace :lint do |
| 7 | + desc 'Lints swift files' |
| 8 | + task :swift do |
| 9 | + sh 'swift package --allow-writing-to-package-directory format --lint' |
| 10 | + end |
| 11 | +end |
| 12 | + |
| 13 | +namespace :format do |
| 14 | + desc 'Formats swift files' |
| 15 | + task :swift do |
| 16 | + sh 'swift package --allow-writing-to-package-directory format' |
| 17 | + end |
| 18 | +end |
| 19 | + |
| 20 | +namespace :update do |
| 21 | + desc 'Updates SwiftFormat to the latest version' |
| 22 | + task :swiftformat do |
| 23 | + # Find the most recent release of SwiftFormat in the https://github.com/calda/SwiftFormat repo. |
| 24 | + response = Net::HTTP.get(URI('https://api.github.com/repos/calda/SwiftFormat/releases/latest')) |
| 25 | + latest_release_info = JSON.parse(response) |
| 26 | + |
| 27 | + latest_version_number = latest_release_info['tag_name'] |
| 28 | + |
| 29 | + # Download the artifact bundle for the latest release and compute its checksum. |
| 30 | + temp_dir = Dir.mktmpdir |
| 31 | + artifact_bundle_url = "https://github.com/calda/SwiftFormat/releases/download/#{latest_version_number}/swiftformat.artifactbundle.zip" |
| 32 | + artifact_bundle_zip_path = "#{temp_dir}/swiftformat.artifactbundle.zip" |
| 33 | + |
| 34 | + sh "curl #{artifact_bundle_url} -L --output #{artifact_bundle_zip_path}" |
| 35 | + checksum = `swift package compute-checksum #{artifact_bundle_zip_path}` |
| 36 | + |
| 37 | + # Update the Package.swift file to reference this version |
| 38 | + package_manifest_path = 'Package.swift' |
| 39 | + package_manifest_content = File.read(package_manifest_path) |
| 40 | + |
| 41 | + updated_swift_format_reference = <<-EOS |
| 42 | + .binaryTarget( |
| 43 | + name: "SwiftFormat", |
| 44 | + url: "https://github.com/calda/SwiftFormat/releases/download/#{latest_version_number}/SwiftFormat.artifactbundle.zip", |
| 45 | + checksum: "#{checksum.strip}"), |
| 46 | + EOS |
| 47 | + |
| 48 | + regex = /[ ]*.binaryTarget\([\S\s]*name: "SwiftFormat"[\S\s]*?\),\s/ |
| 49 | + updated_package_manifest = package_manifest_content.gsub(regex, updated_swift_format_reference) |
| 50 | + File.open(package_manifest_path, "w") { |file| file.puts updated_package_manifest } |
| 51 | + |
| 52 | + puts "Updated Package.swift to reference SwiftFormat #{latest_version_number}" |
| 53 | + end |
| 54 | +end |
0 commit comments