Last active
January 20, 2021 09:15
-
-
Save mrhead/a30a8c6154287f07e9c51cbf336eccd3 to your computer and use it in GitHub Desktop.
Convert Stimulus 1.x target syntax to Stimulus 2.0 target syntax
This file contains hidden or 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
# 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