Class: Capucine::Tools

Inherits:
Object
  • Object
show all
Defined in:
lib/tools.rb

Instance Method Summary collapse

Constructor Details

#initialize(capucine) ⇒ Tools

Returns a new instance of Tools.



13
14
15
# File 'lib/tools.rb', line 13

def initialize(capucine)
  @cap = capucine
end

Instance Method Details

#archive_file(path, force = true) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/tools.rb', line 184

def archive_file path, force = true
  return if not File.exist? path

  is_empty = false
  is_dir = File.directory? path
  dir_length = Dir.glob("#{path}/**").length if is_dir
  is_empty = true if dir_length == 0

  return if is_empty

  date = Time.now.strftime("%Y-%m-%d_%Hh%Mm%Ss")
  new_dir_name = path

  if is_dir
    new_dir_name = "#{path}_#{date}"
  end

  if not is_dir
    extension = File.extname path
    base_path = File.dirname path
    file_name = File.basename(path, extension)
    new_dir_name = File.join base_path, "#{file_name}_#{date}#{extension}"
  end

  FileUtils.mkdir_p new_dir_name if is_dir
  FileUtils.mv path, new_dir_name

end

#cleanObject



142
143
144
145
# File 'lib/tools.rb', line 142

def clean
  file = File.join(@cap.settings.working_dir, '.compass_config.rb')
  FileUtils.rm file
end

#compile(scope = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tools.rb', line 80

def compile(scope = nil)

  scope = (scope) ? scope : 'all'
  do_things = self.extract_commands_from_scope(scope)

  do_sass = do_things[0]
  do_coffee = do_things[1]
  do_incloudr = do_things[2]

  @cap.sass.run_once if do_sass
  @cap.coffee.run_once if do_coffee
  @cap.incloudr.run_once if do_incloudr

end

#extract_commands_from_scope(scope) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/tools.rb', line 148

def extract_commands_from_scope(scope)
  all = ['use_compass', 'use_coffeescript', 'use_incloudr']
  todo = [false,false,false]

  scope = (scope != 'all') ? scope.split(',') : all # [] or ['sass', 'coffee']

  all.each_with_index do |command, i|
    if @cap.settings.conf[command] == true
      todo[i] = true
      # if scope.include?(command)
      # end
    end
  end
  return todo

end

#get_config_file_from_scope(scope = nil) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/tools.rb', line 166

def get_config_file_from_scope(scope = nil)
  if not scope
    file = File.join(@cap.settings.content_dir,'shared', 'capucine.yaml')
  elsif scope.match(/^http:\/\//)
    # files = from the web
  else
    # files = from github
  end

  return file
end

#init(scope = nil, config_or_name = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tools.rb', line 36

def init(scope = nil, config_or_name = nil)
  self.archive_file(@cap.settings.user_config_file) if @cap.settings.user_config_file

  # Without Conf
  if not scope and not config_or_name
    file = File.join(@cap.settings.content_dir,'shared', 'capucine.yaml')
    FileUtils.cp file, File.join(@cap.settings.working_dir, 'capucine.yaml')
    return file
  end


  # With Conf
  if config_or_name

    begin
      d = open(config_or_name).read
    rescue
      puts "Error downloading : #{config_or_name}"
      return self
    end

    file = File.new File.join(@cap.settings.working_dir, 'capucine.yaml'), 'w+'
    file.write(d)
    file.close
    # file.unlink

    return self
  end

  if scope
    file = File.join(@cap.settings.root_dir,'templates', "#{scope}.yaml")
    if not File.exists?(file)
      puts "Sorry, this template does not exists : #{scope}"
      return self
    end

    FileUtils.cp file, File.join(@cap.settings.working_dir, 'capucine.yaml')

  end


  return self
end

#js(scope, query = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tools.rb', line 114

def js(scope, query = nil)
  if scope[0] == 'search'
    if scope[1] == 'npm'
    else
      libs = Capucine::CDNJS.search(query)
      libs.each do |lib|
        puts "#{lib['name']} --- #{lib['version']}"
      end
    end
  end

  if scope[0] == 'list'
    if scope[1] == 'npm'
    else
      libs = Capucine::CDNJS.get_all
      libs.each do |lib|
        puts "#{lib['name']} --- #{lib['version']}"
      end
    end
  end

end

#new_project(scope = nil, name = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tools.rb', line 17

def new_project(scope = nil, name = nil)
  name = name || @cap.settings.project_name # Custom name or 'capucine'

  @cap.settings.working_dir = File.join @cap.settings.working_dir, name
  self.archive_file(@cap.settings.working_dir)

  config_file = get_config_file_from_scope(scope)
  files = Dir.glob File.join(@cap.settings.content_dir, 'shared', '**')

  FileUtils.mkdir @cap.settings.working_dir
  FileUtils.cp_r files, @cap.settings.working_dir
  FileUtils.cp config_file, File.join(@cap.settings.working_dir, 'capucine.yaml')

  @cap.settings.set_user_config_file(File.join(@cap.settings.working_dir, 'capucine.yaml'))

  return self

end

#render_template(template_file, content = nil) ⇒ Object



178
179
180
181
182
# File 'lib/tools.rb', line 178

def render_template template_file, content = nil
  config = content
  output = ERB.new(File.new(template_file).read).result(binding)
  return output
end

#updateObject



137
138
139
140
# File 'lib/tools.rb', line 137

def update
  system('gem uninstall -a -x capucine')
  system('gem install capucine')
end

#watch(scope = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/tools.rb', line 95

def watch(scope = nil)
  self.compile(scope)

  scope = (scope) ? scope : 'all'

  do_things = self.extract_commands_from_scope(scope)

  do_sass = do_things[0]
  do_coffee = do_things[1]
  do_incloudr = do_things[2]

  thread_sass = @cap.sass.run_watch if do_sass
  thread_coffee = @cap.coffee.run_watch if do_coffee

  thread_sass.join if thread_sass
  thread_coffee.join if thread_coffee

end