Class: Bixbite::Create

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Create

Returns a new instance of Create.



5
6
7
8
9
10
11
12
# File 'lib/bixbite/create.rb', line 5

def initialize options
  @options = options
  @config = @options[:config]
  
  copy @options[:source], @options[:destination]
  download @options[:lib], @options[:destination]
  parse @config["parse"], @options[:destination], @options[:title], @options[:namespace]
end

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



3
4
5
# File 'lib/bixbite/create.rb', line 3

def destination
  @destination
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/bixbite/create.rb', line 3

def options
  @options
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/bixbite/create.rb', line 3

def source
  @source
end

Instance Method Details

#copy(source, destination) ⇒ Object



39
40
41
42
43
# File 'lib/bixbite/create.rb', line 39

def copy source, destination
  puts "Copying to #{destination}..."
  FileUtils.cp_r(source, destination)
  puts "Done!"
end

#download(lib, destination) ⇒ Object



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
# File 'lib/bixbite/create.rb', line 45

def download lib, destination
  require 'net/http'
  
  def get domain, source, destination
    Net::HTTP.start(domain) do |http|
      resp = http.get(source)
      open(destination, "wb") do |file|
        file.write(resp.body)
        puts "Done!"
      end
    end
  end
  
  google = 'ajax.googleapis.com'
  file = '__JS_LIB_LOWERCASE__-latest.js'
  dest = File.join(destination, "template", @config["directories"]["src"]["html"][0], "js/cmn/lib", file)
  
  puts "Downloading #{lib} from #{google}"
  
  case lib
    when 'jQuery'
      get google, '/ajax/libs/jquery/1/jquery.js', dest
    when 'YUI'
      get google, '/ajax/libs/yui/2/build/yuiloader/yuiloader.js', dest
    when 'MooTools'
      get google, '/ajax/libs/mootools/1.2/mootools.js', dest
    else
  end
  
end

#get(domain, source, destination) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/bixbite/create.rb', line 48

def get domain, source, destination
  Net::HTTP.start(domain) do |http|
    resp = http.get(source)
    open(destination, "wb") do |file|
      file.write(resp.body)
      puts "Done!"
    end
  end
end

#parse(list, source, name, namespace) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bixbite/create.rb', line 14

def parse list, source, name, namespace
  puts "Building project..."
  list.each do |glob|
    files = Dir[File.join(source, "template", glob)]
    files.each do |file|
      File.open(file, "r+") do |f|
          lines = f.readlines
          lines.each do |line|
            line.gsub!(/__PROJECT_NAME__/, name);
            line.gsub!(/__PROJECT_NAMESPACE__/, namespace);
            line.gsub!(/__JS_LIB__/, @options[:lib]);
            line.gsub!(/__JS_LIB_LOWERCASE__/, @options[:lib].downcase);
          end
          f.pos = 0
          f.print lines
          f.truncate(f.pos)
      end unless File.directory?(file)
      
      File.rename(file, file.gsub(/__PROJECT_NAME__/, name)) if file.match(/__PROJECT_NAME__/)
      File.rename(file, file.gsub(/__JS_LIB_LOWERCASE__/, @options[:lib].downcase)) if file.match(/__JS_LIB_LOWERCASE__/)
    end
  end
  puts "All done!"
end