class Sassmagic::Installers::Base
Attributes
logger[W]
Public Class Methods
new(projectname = '')
click to toggle source
debugger
# File lib/sassmagic/installer.rb, line 5 def initialize(projectname = '') # Dir.mkdir("directory") # txt = File.open("directory/a.txt","w+") # txt.puts("hello world") # txt.close if projectname != '' projectname ="#{projectname}/" end directory "#{projectname}sass" directory "#{projectname}config" directory "#{projectname}stylesheet" directory "#{projectname}image" # write_file 'a.txt','hi' copy File.expand_path("#{File.dirname(__FILE__)}/config/imageuploader.js"),File.expand_path("#{Dir::pwd}/#{projectname}/config/imageuploader.js") copy File.expand_path("#{File.dirname(__FILE__)}/config/sassmagic.json"),File.expand_path("#{Dir::pwd}/#{projectname}/config/sassmagic.json") finalize end
Public Instance Methods
basename(file)
click to toggle source
# File lib/sassmagic/installer.rb, line 102 def basename(file) relativize(file) {|f| File.basename(file)} end
copy(from, to, options = nil, binary = false)
click to toggle source
copy/process a template in the compass template directory to the project directory.
# File lib/sassmagic/installer.rb, line 29 def copy(from, to, options = nil, binary = false) options ||= self.options if self.respond_to?(:options) if binary contents = File.new(from,"rb").read else contents = File.new(from).read end write_file to, contents, options, binary end
directory(dir, options = nil)
click to toggle source
create a directory and all the directories necessary to reach it.
# File lib/sassmagic/installer.rb, line 40 def directory(dir, options = nil) options ||= self.options if self.respond_to?(:options) options ||= {} if File.exists?(dir) && File.directory?(dir) # do nothing elsif File.exists?(dir) msg = "#{basename(dir)} already exists and is not a directory." raise Compass::FilesystemConflict.new(msg) else log_action :directory, separate("#{basename(dir)}/"), options FileUtils.mkdir_p(dir) end end
finalize(options = {})
click to toggle source
# File lib/sassmagic/installer.rb, line 135 def finalize(options = {}) puts <<-NEXTSTEPS ********************************************************************* Congratulations! Your compass project has been created. You may now add sass stylesheets to the sass subdirectory of your project. Sass files beginning with an underscore are called partials and won't be compiled to CSS, but they can be imported into other sass stylesheets. You can configure your project by editing the config.rb configuration file. You must compile your sass stylesheets into CSS when they change. This can be done in one of the following ways: 1. To compile on demand: sassmagic --x [input] [output] More Resources: NEXTSTEPS end
log_action(action, file, options)
click to toggle source
# File lib/sassmagic/installer.rb, line 125 def log_action(action, file, options) quiet = !!options[:quiet] quiet = false if options[:loud] && options[:loud] == true quiet = false if options[:loud] && options[:loud].is_a?(Array) && options[:loud].include?(action) unless quiet logger.record(action, file, options[:extra].to_s) end end
logger()
click to toggle source
# File lib/sassmagic/installer.rb, line 25 def logger @logger ||= Logger.new end
process_erb(contents, ctx = nil)
click to toggle source
# File lib/sassmagic/installer.rb, line 86 def process_erb(contents, ctx = nil) ctx = Object.new.instance_eval("binding") unless ctx.is_a? Binding ERB.new(contents).result(ctx) end
relativize(path) { |path| ... }
click to toggle source
# File lib/sassmagic/installer.rb, line 106 def relativize(path) path = File.expand_path(path) if block_given? yield path else path end end
remove(file_name)
click to toggle source
# File lib/sassmagic/installer.rb, line 91 def remove(file_name) file_name ||= '' if File.directory?(file_name) FileUtils.rm_rf file_name log_action :remove, basename(file_name)+"/", options elsif File.exists?(file_name) File.unlink file_name log_action :remove, basename(file_name), options end end
separate(path)
click to toggle source
Write paths like we’re on unix and then fix it
# File lib/sassmagic/installer.rb, line 116 def separate(path) path.gsub(%r{/}, File::SEPARATOR) end
strip_trailing_separator(path)
click to toggle source
Removes the trailing separator, if any, from a path.
# File lib/sassmagic/installer.rb, line 121 def strip_trailing_separator(path) (path[-1..-1] == File::SEPARATOR) ? path[0..-2] : path end
write_file(file_name, contents, options = nil, binary = false)
click to toggle source
Write a file given the file contents as a string
# File lib/sassmagic/installer.rb, line 55 def write_file(file_name, contents, options = nil, binary = false) options ||= self.options if self.respond_to?(:options) options ||= {} skip_write = false # debugger # contents = process_erb(contents, options[:erb]) if options[:erb] if File.exists?(file_name) existing_contents = IO.read(file_name) if existing_contents == contents log_action :identical, basename(file_name), options skip_write = true elsif options[:force] log_action :overwrite, basename(file_name), options else msg = "File #{basename(file_name)} already exists. Run with --force to force overwrite." raise puts(msg) end else log_action :create, basename(file_name), options end if skip_write FileUtils.touch file_name else mode = "w" mode << "b" if binary open(file_name, mode) do |file| file.write(contents) end end end