Skip to content

Instantly share code, notes, and snippets.

@mrhead
Last active January 20, 2021 09:15
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 mrhead/a30a8c6154287f07e9c51cbf336eccd3 to your computer and use it in GitHub Desktop.
Save mrhead/a30a8c6154287f07e9c51cbf336eccd3 to your computer and use it in GitHub Desktop.
Convert Stimulus 1.x target syntax to Stimulus 2.0 target syntax
# Convert Stimulus 1.x target syntax to Stimulus 2.0 target syntax
#
# ack data-target app/views | cut -f 1 -d : | sort -u | while read FILE; do ruby convert_targets.rb $FILE; done
# ack data app/views | grep target: | cut -f 1 -d : | sort -u | while read FILE; do ruby convert_targets.rb $FILE; done
unless filename = ARGV[0]
puts "Filename is required"
exit(-1)
end
new_content = ""
File.readlines(filename).each do |line|
if captures = line.match(/data-target="([^\.]+)\.([a-zA-Z-]+)"/)
new = %Q{data-#{captures[1]}-target="#{captures[2]}"}
new_content << line.gsub(captures[0], new)
elsif captures = line.match(/target: "([a-z-]+)\.([a-zA-Z-]+)"/)
new = %Q{#{captures[1].gsub("-", "_")}_target: "#{captures[2]}"}
new_content << line.gsub(captures[0], new)
else
new_content << line
end
end
File.write(filename, new_content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment