Skip to content

Instantly share code, notes, and snippets.

@claybridges
Created August 23, 2023 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claybridges/5656ba75e095f0a40743563f8d33507c to your computer and use it in GitHub Desktop.
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
#!/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