module Svgeez class SpriteBuilder def initialize(options) @source = File.expand_path(options.fetch('source', './')) @destination = File.expand_path(options.fetch('destination', './_svgeez/svgeez.svg')) @with_svgo = options['svgo'] end def build unless source_is_destination? if source_file_paths.any? Svgeez.logger.info %{Generating sprite at `#{destination_file_path}` from #{source_file_paths.length} SVG#{'s' if source_file_paths.length > 1}...} # Make destination folder FileUtils.mkdir_p(destination_folder_path) # Notify if SVGO requested but not found if @with_svgo && !svgo_installed? Svgeez.logger.warn %{Unable to find `svgo` in your PATH. Continuing with standard sprite generation...} end # Write the file File.open(destination_file_path, 'w') do |f| f.write build_destination_file_contents end Svgeez.logger.info %{Successfully generated sprite at `#{destination_file_path}`.} else Svgeez.logger.warn %{No SVGs were found in `#{@source}`.} end else Svgeez.logger.error %{Setting `source` and `destination` to the same path isn't allowed!} end end def build_destination_file_contents destination_file_contents = ''.tap do |file_contents| source_file_paths.each do |file_path| IO.read(file_path).match(/^viewBox=".*?").*?>(?.*?)<\/svg>/m) do |matches| file_contents << %{#{matches[:content]}} end end file_contents << '' end if use_svgo? destination_file_contents = `cat <