Created
August 23, 2023 17:45
-
-
Save claybridges/5656ba75e095f0a40743563f8d33507c to your computer and use it in GitHub Desktop.
For SPM Package.resolved file, force all urls to either have/not-have a .git suffix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Forces all SPM urls in `Package.resolved` files to either have, or not have, | |
# a .git suffix, based on `use_suffix`. NOTE: probably WILL rewrite your | |
# `Package.resolved` files, though in predictable and semantically identical ways. | |
# | |
# If you want to do this on only specific files, pass an array of these (full paths) | |
# for `against_files`. | |
# | |
def force_package_resolved(use_suffix:, against_files: nil) | |
# Xcode defines $PROJECT_DIR for build scripts. Otherwise, you might need to | |
# adjust this path. | |
project_dir = ENV["PROJECT_DIR"] || File.realpath("#{__dir__}/../..") | |
against_files ||= Dir["#{project_dir}/**/Package.resolved"] | |
regex, replacement = use_suffix ? | |
[/(^ *\"location\" : .*(?<!\.git))(\",$)/, '\1.git\2']: | |
[/(^ *\"location\" : .*)?\.git(\",$)/, '\1\2'] | |
against_files.each do |file| | |
original = File.read(file) | |
changed = original.gsub(regex, replacement) | |
File.write(file, changed) if changed != original | |
end | |
end | |
if $PROGRAM_NAME == __FILE__ | |
force_package_resolved(use_suffix: false) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment