Coda Chang Coda Chang - 26 days ago 5
Ruby Question

Ruby change all css file into scss

I want to change all

css
file into
scss
in my Rails project.

The folder structure is unpredictable, like

stylesheets - A - A_1 - a_1.css
- B - B.css
- C - C_1 - c_1_1 - c_1_1.css
- C_2 - c-2.css
- d.css


I have written some code, but it can only make
css
file which is in
./stylesheets
become
scss


require 'fileutils'

Dir.chdir('app/assets/stylesheets')
Dir.glob('./*.css').each do |f|
puts f
FileUtils.mv f, "#{File.basename(f,'.*')}.scss"
end


If there is a better way like
shell script
, it's also acceptable.

Update



Thanks @maxpleaner, I modified it into this.

require 'fileutils'

Dir.chdir('app/assets/stylesheets')

Dir.glob("./**/*.css").each do |path|
#p path
FileUtils.mv path, "#{File.dirname(path)}/" + "#{File.basename(path,'.*')}.scss"
end


And it works fine.

Answer

To get the paths of all css files in your project, you can use this:

Dir.glob("./**/*.css").each do |path|

However I'm not sure if this will break anything or not (especially application.css). Probably a good idea to commit in git first.