Class: HighFive::Thor::Tasks::Deploy

Inherits:
HighFive::Thor::Task show all
Includes:
Thor::Actions
Defined in:
lib/high_five/thor/tasks/deploy.rb

Instance Method Summary collapse

Methods inherited from HighFive::Thor::Task

inherited

Instance Method Details

#deploy(target) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/high_five/thor/tasks/deploy.rb', line 18

def deploy(target)
  @environment  = options[:environment]
  @platform     = target
  @weinre_url   = options[:weinre_url]
  @copy_files   = options[:"copy-files"]
  @meta         = {}
  @config       = base_config.build_platform_config(@platform).build_platform_config(@environment)
  @config_root  = File.join("config", "high_five")

  self.source_paths << File.join(base_config.root, @config_root)
  self.source_paths << File.join(base_config.root)
  self.source_paths << File.join(File.dirname(__FILE__), '..', '..', 'generators')
  @config.asset_paths.each do |asset_path|
    self.source_paths << asset_path
  end

  if @config.destination.nil?
    if @config.cordova_path.nil?
      raise "Please set config.destination"
    else
      self.destination_root = "#{@config.cordova_path}/www"
    end
  else
    self.destination_root = @config.destination
  end
  FileUtils.rm_rf(self.destination_root)
  FileUtils.mkdir_p(self.destination_root)

  # TODO Add to config
  say "Deploying app: <#{@platform}> <#{options[:environment]}>"
  say "\t#{self.destination_root}"
  say " -Weinre url: #{@weinre_url}" if @weinre_url


  if @config.compass_dir
    compass_dir = File.join(base_config.root, @config.compass_dir)
    say "Precompiling compass styles from #{compass_dir}}"
    pwd = Dir.pwd
    Dir.chdir compass_dir
    success = false
    if @environment == "production"
      success = system("compass compile --force --no-debug-info -e production")
    else
      success = system("compass compile --force --no-debug-info")
    end
    unless success
      raise "Error compiling CSS, aborting build"
    end
    Dir.chdir pwd
  end

  # Bundle is based on the provided build platformx
  platform_file = File.join(@config_root, "app-#{@platform}.js")
  bundle = builder.find_asset platform_file
  if bundle.nil?
    p source_paths
    error "#{@platform} is not a valid target.  Please create #{platform_file}" and Process.exit
  end


  if (@environment == "production")
    appjs = File.join(self.destination_root, "app.js")
    @javascripts = ["app.js"]

    output_js = nil
    if @config.minify  == :uglifier
      say "      uglify  #{appjs}", :green
      output_js = Uglifier.compile(bundle.to_s)
    elsif @config.minify == :yui
      say "      minify  #{appjs}", :green
      output_js = YUI::JavaScriptCompressor.new.compress(bundle.to_s)
    else
      say "      create  #{appjs}", :green
      output_js = bundle.to_s
    end
    File.open(appjs, "w") do |file|
      file.write output_js
    end
    output_js = nil
  else
    # Add each of the javascript files to the generated folder
    @javascripts = []
    bundle.dependencies.each do |asset|
      next if asset.logical_path =~ /^config\/high_five/ #don't copy manifest files
      copy_file asset.logical_path
      @javascripts << asset.logical_path
    end
  end

  # Adds each of the static javascripts
  @config.static_javascripts.each do |javascript|
    if File.directory? javascript
      directory javascript
      @javascripts.unshift(*Dir[File.join(javascript,'**','*.js')])
    else
      copy_file javascript unless javascript =~ /^https?:\/\//
      @javascripts.unshift javascript
    end
  end

  @stylesheets = []
  @config.sass_files.each do |sass_file|
    asset_name = File.basename(sass_file, File.extname(sass_file))
    css_file = File.join(self.destination_root, "stylesheets", "#{asset_name}.css")
    say "Compiling #{sass_file} -> #{css_file}"
    Sass.compile_file sass_file, css_file
    @stylesheets.push css_file
    copy_file css_file
  end

  @config.static_stylesheets.each do |stylesheet|
    if File.directory? stylesheet
      directory stylesheet
      @stylesheets.unshift(*Dir[File.join(stylesheet,'**','*.css')])
    else
      copy_file stylesheet
      @stylesheets.unshift stylesheet
    end
  end

  # Adds each of the static assets to the generated folder (sylesheets etc)
  @config.static_assets.each do |path, options|
    asset = find_in_source_paths(path)
    destination = path
    if (options[:destination])
      destination = File.join(self.destination_root, options[:destination])
    end
    if File.directory? asset
      directory asset, destination
    else
      copy_file asset, destination
    end
  end

  if (@config.manifest && @environment == 'production')
    @manifest_attr = %Q(manifest="manifest.appcache") #for <html manifest=>
    say "     create manifest", :green
    @manifest_cache = @config.static_assets.collect do |path, options|
      #FIXME: This doesn't respect the options[:destination]
      if (File.directory?(path))
        Dir.glob(path + "/**/*").reject {|x| File.directory?(x) }
      else
        path
      end
    end.flatten

    @manifest_cache += @javascripts
    @manifest_cache += @stylesheets

    template "manifest.erb", File.join(self.destination_root, "manifest.appcache")
  end

  @high_five_javascript = @config.high_five_javascript
  # Build index.html
  say "Generating index.html"
  template File.join(@config_root, "index.html.erb"), File.join(self.destination_root, "index.html")
  if (!@config.dev_index.nil?)
    say "Cloning to #{@config.dev_index}"
    FileUtils.cp(File.join(self.destination_root, "index.html"), File.join(@config.root, @config.dev_index))
  end

  if (@config.cordova_path)
    cordova_path = File.join(base_config.root, @config.cordova_path)
    Dir.chdir cordova_path do
    say "Running cordova prepare in #{Dir.pwd}"
      if system("`npm bin`/cordova prepare")
        say "Cordova prepare complete"
      else
        raise "Error running cordova prepare, aborting build"
      end
    end
  end
end