Skip to content

Commit 3d24eb4

Browse files
committed
Add command to update SwiftFormat reference to latest version
1 parent cdf293c commit 3d24eb4

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

.github/workflows/main.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
steps:
1919
- uses: actions/checkout@v2
2020
- name: Test Package Plugin
21-
run: swift package --allow-writing-to-package-directory format --lint
21+
run: bundle exec rake lint:swift
2222

2323
test-package-plugin-macos-13:
2424
name: Test Package Plugin
@@ -33,7 +33,7 @@ jobs:
3333
steps:
3434
- uses: actions/checkout@v2
3535
- name: Test Package Plugin
36-
run: swift package --allow-writing-to-package-directory format --lint
36+
run: bundle exec rake lint:swift
3737

3838
unit-tests:
3939
name: Unit Tests

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org' do
2+
gem "rake", "~> 13.0.0"
3+
end

Gemfile.lock

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
GEM
2+
specs:
3+
4+
GEM
5+
remote: https://rubygems.org/
6+
specs:
7+
rake (13.0.6)
8+
9+
PLATFORMS
10+
arm64-darwin-23
11+
ruby
12+
13+
DEPENDENCIES
14+
rake (~> 13.0.0)!
15+
16+
BUNDLED WITH
17+
2.5.4

Rakefile

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)