Coda Chang Coda Chang - 20 days ago 8
Ruby Question

How to 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 wrote some code, but it can only make a
css
file which is in
./stylesheets
become
css
:

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 a shell script it is also acceptable.




Thanks to @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.

Comments