Class: Sassmagic::Installers::Base
- Inherits:
-
Object
- Object
- Sassmagic::Installers::Base
- Defined in:
- lib/sassmagic/installer.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #basename(file) ⇒ Object
-
#copy(from, to, options = nil, binary = false) ⇒ Object
copy/process a template in the compass template directory to the project directory.
-
#directory(dir, options = nil) ⇒ Object
create a directory and all the directories necessary to reach it.
- #finalize(options = {}) ⇒ Object
-
#initialize(projectname = '') ⇒ Base
constructor
debugger.
- #log_action(action, file, options) ⇒ Object
- #process_erb(contents, ctx = nil) ⇒ Object
- #relativize(path) ⇒ Object
- #remove(file_name) ⇒ Object
-
#separate(path) ⇒ Object
Write paths like we’re on unix and then fix it.
-
#strip_trailing_separator(path) ⇒ Object
Removes the trailing separator, if any, from a path.
-
#write_file(file_name, contents, options = nil, binary = false) ⇒ Object
Write a file given the file contents as a string.
Constructor Details
#initialize(projectname = '') ⇒ Base
debugger
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 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.("#{File.dirname(__FILE__)}/config/imageuploader.js"),File.("#{Dir::pwd}/#{projectname}/config/imageuploader.js") copy File.("#{File.dirname(__FILE__)}/config/sassmagic.json"),File.("#{Dir::pwd}/#{projectname}/config/sassmagic.json") finalize end |
Instance Attribute Details
Instance Method Details
#basename(file) ⇒ Object
102 103 104 |
# File 'lib/sassmagic/installer.rb', line 102 def basename(file) relativize(file) {|f| File.basename(file)} end |
#copy(from, to, options = nil, binary = false) ⇒ Object
copy/process a template in the compass template directory to the project directory.
29 30 31 32 33 34 35 36 37 |
# File 'lib/sassmagic/installer.rb', line 29 def copy(from, to, = nil, binary = false) ||= self. if self.respond_to?(:options) if binary contents = File.new(from,"rb").read else contents = File.new(from).read end write_file to, contents, , binary end |
#directory(dir, options = nil) ⇒ Object
create a directory and all the directories necessary to reach it.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sassmagic/installer.rb', line 40 def directory(dir, = nil) ||= self. if self.respond_to?(: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)}/"), FileUtils.mkdir_p(dir) end end |
#finalize(options = {}) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sassmagic/installer.rb', line 135 def finalize( = {}) 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) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/sassmagic/installer.rb', line 125 def log_action(action, file, ) quiet = !![:quiet] quiet = false if [:loud] && [:loud] == true quiet = false if [:loud] && [:loud].is_a?(Array) && [:loud].include?(action) unless quiet logger.record(action, file, [:extra].to_s) end end |
#process_erb(contents, ctx = nil) ⇒ Object
86 87 88 89 |
# 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) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/sassmagic/installer.rb', line 106 def relativize(path) path = File.(path) if block_given? yield path else path end end |
#remove(file_name) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# 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)+"/", elsif File.exists?(file_name) File.unlink file_name log_action :remove, basename(file_name), end end |
#separate(path) ⇒ Object
Write paths like we’re on unix and then fix it
116 117 118 |
# File 'lib/sassmagic/installer.rb', line 116 def separate(path) path.gsub(%r{/}, File::SEPARATOR) end |
#strip_trailing_separator(path) ⇒ Object
Removes the trailing separator, if any, from a path.
121 122 123 |
# 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) ⇒ Object
Write a file given the file contents as a string
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/sassmagic/installer.rb', line 55 def write_file(file_name, contents, = nil, binary = false) ||= self. if self.respond_to?(: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), skip_write = true elsif [:force] log_action :overwrite, basename(file_name), 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), 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 |