public
Description: Sample applications for the SproutCore JavaScript Framework
Homepage: http://www.sproutcore.com
Clone URL: git://github.com/sproutit/sproutcore-samples.git
sproutit (author)
Tue Oct 20 16:53:37 -0700 2009
commit  8eb6587fb3f13d4ed37c38199f66a607503729e9
tree    bcd988470aa9f61fcf7f663d99e89bc7ea5848e7
parent  97f5d39faec3932605e276b1304016cd189fa2b4
sproutcore-samples / Rakefile
100644 283 lines (219 sloc) 9.121 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Checkout copy of webpage from git and run a build. Then push the build to
# demo.sproutcore.com and symlink pages.
require 'extlib'
 
# Config Settings
LANGUAGES = [:en]
 
# Autodetect some environment variables
HOSTNAME = "demo.sproutcore.com"
USERNAME = "sprout"
HOME_TARGET = "/sproutcore/welcome"
DOCROOT = "/home/sprout/demo.sproutcore.com"
SC_BUILD = 'sc-build'
WORKING = File.dirname(__FILE__)
 
# these targets will not be included in the targets.json feed
# add apps here that are not fully functioning and should not be listed
EXCLUDE_TARGETS = %w(chat video sproutcore/tests sproutcore/docs not_found drag)
 
# store global config options
OPTS = {}
 
# Make it so we can load Abbot as a library. Once Abbot is installed as a
# gem this
$:
desc "builds the pages to prepare for deployment"
task :build do
  Dir.chdir WORKING
  puts `#{SC_BUILD} --all -rv --languages=#{LANGUAGES.join(',')}`
end
 
desc "cleans the build output"
task :clean do
  path = WORKING / 'tmp' / 'build' / 'static'
  puts "removing #{path}"
  puts `rm -r #{path}`
end
 
desc "installs gems needed for this Rakefile to run"
task :install_gems do
  puts "sudo gem install highline net-ssh net-scp sproutit-sproutcore"
  puts `sudo gem install highline net-ssh net-scp sproutit-sproutcore`
end
 
desc "collects the login password from the operator"
task :collect_password do
  
  begin
    require 'highline/import'
  rescue LoadError => e
    puts "\n ~ FATAL: sproutcore gem is required.\n Try: rake install_gems"
    exit(1)
  end
  
  puts "Enter login password for #{HOSTNAME} to complete this task"
  OPTS[:password] = ask("Password: ") { |q| q.echo = '*' }
end
 
desc "finds all targets in the system and computes their build numbers"
task :prepare_targets do
  
  begin
    require 'sproutcore'
  rescue LoadError => e
    puts "\n ~ FATAL: sproutcore gem is required.\n Try: rake install_gems"
    exit(1)
  end
  
  puts "discovering all installed targets"
  SC.build_mode = :production
  project = SC.load_project(WORKING)
  
  # get all targets and prepare them so that build numbers work
  targets = project.targets.values
  
  puts "preparing build numbers"
  targets.each do |t|
    puts " .. #{t.target_name}"
    t.prepare!.compute_build_number if !t.build_number
  end
  
  OPTS[:targets] = targets
  OPTS[:project] = project
end
  
desc "generates json feeds we need to update"
task :json_feeds => :prepare_targets do
  require 'json'
 
  json_hash = []
  OPTS[:targets].each do |target|
    next if EXCLUDE_TARGETS.include?(target.target_name.to_s[1..-1])
    
    target.prepare!
    parent = target.parent_target
    parent = parent.kind_of?(SC::Target) ? parent.target_name : ''
    json_hash << { "name" => target.target_name,
      "kind" => target.target_type,
      "parent" => parent,
      "link_root" => target.url_root }
  end
  
  file_path = WORKING / 'tmp' / 'sc' / 'targets.json'
  FileUtils.mkdir_p(File.dirname(file_path))
  File.open(file_path, 'w') { |f| f.write(json_hash.to_json) }
  
  OPTS[:feeds] = {
    'sc/targets.json' => file_path
  }
end
   
desc "copies the built feeds onto the #{HOSTNAME} server"
task :deploy_feeds => [:collect_password, :json_feeds] do
 
  begin
    require 'net/ssh'
    require 'net/scp'
  rescue LoadError => e
    puts "\n ~ FATAL: net-scp gem is required.\n Try: rake install_gems"
    exit(1)
  end
 
  password = OPTS[:password]
  feeds = OPTS[:feeds] || {}
  
  puts "building directory structure"
  Net::SSH.start(HOSTNAME, USERNAME, :password => password) do |ssh|
    feeds.each do |dst_path, src_path|
      remote_path = "#{DOCROOT}/#{dst_path}"
      puts ssh.exec!(%[mkdir -p "#{File.basename(remote_path)}"]) || "%: mkdir -p #{remote_path}"
      puts ssh.exec!(%[rm -r "#{remote_path}"]) || "%: rm #{remote_path}"
    end
 
  end
        
  puts "copying static resources onto remote server"
  Net::SCP.start(HOSTNAME, USERNAME, :password => password) do |scp|
    feeds.each do |dst_path, local_path|
      remote_path = "#{DOCROOT}/#{dst_path}"
      puts " ~ uploading feed: #{dst_path}"
      scp.upload! local_path, remote_path, :recursive => true
    end
  end # Net::SCP.start
end
 
desc "copies the built files onto the #{HOSTNAME} server"
task :deploy_assets => [:collect_password, :build, :prepare_targets] do
 
  begin
    require 'net/ssh'
    require 'net/scp'
  rescue LoadError => e
    puts "\n ~ FATAL: net-scp gem is required.\n Try: rake install_gems"
    exit(1)
  end
 
  password = OPTS[:password]
  targets = OPTS[:targets]
  
  installed = {}
  
  puts "building directory structure"
  Net::SSH.start(HOSTNAME, USERNAME, :password => password) do |ssh|
    targets.each do |target|
      LANGUAGES.each do |lang|
        remote_path = "#{DOCROOT}/static#{target.index_root}/#{lang}"
        puts ssh.exec!(%[mkdir -p "#{remote_path}"]) || "%: mkdir -p #{remote_path}"
        
        # see if this guy is already installed
        remote_path = "#{remote_path}/#{target.build_number}"
        installed[remote_path] = !(ssh.exec!("ls #{remote_path}") =~ /No such file or directory/)
        
      end
    end
  end
        
  puts "copying static resources onto remote server"
  Net::SCP.start(HOSTNAME, USERNAME, :password => password) do |scp|
 
    targets.each do |target|
      LANGUAGES.each do |lang|
        local_path = target.build_root / lang / target.build_number
        remote_path = "#{DOCROOT}/static#{target.index_root}/#{lang}"
 
        short_path = local_path.gsub /^#{Regexp.escape(target.build_root)}/,''
 
        if installed["#{remote_path}/#{target.build_number}"]
          puts " ~ #{target.target_name}#{short_path} already installed"
          
        elsif File.directory?(local_path)
          puts " ~ uploading #{target.target_name}#{short_path}"
          scp.upload! local_path, remote_path, :recursive => true
 
        else
          puts "\n\n ~ WARN: cannot install #{target.target_name}:#{lang} - local path #{local_path} does not exist\n\n"
        end
        
      end # LANGUAGES.each
    end # targets.each
    
  end # Net::SCP.start
  
end
 
desc "creates symlinks to the latest versions of all pages and apps. Make sure you deploy your assets also!"
task :link_current => [:collect_password, :prepare_targets] do
 
  # don't require unless this task runs to avoid dependency problems
  begin
    require 'net/ssh'
  rescue LoadError => e
    puts "\n ~ FATAL: net-ssh gem is required.\n Try: rake install_gems"
    exit(1)
  end
    
 
  
  # now filter out only app targets living in the current project
  targets = OPTS[:targets]
  project = OPTS[:project]
  targets = targets.select { |t| t.target_type == :app }
  targets = targets.select do |t|
    t.source_root =~ /^#{Regexp.escape(project.project_root)}/
  end
 
  puts "linking targets:\n #{targets.map {|t| t.target_name} * "\n " }"
  
  # SSH in and do the symlink
  password = OPTS[:password]
  Net::SSH.start(HOSTNAME, USERNAME, :password => password) do |ssh|
    targets.each do |target|
      # find the local build number
      build_number = target.prepare!.compute_build_number
 
      puts "Installing #{target.target_name}..."
      
      # first, link index.html
      from_path = "#{DOCROOT}/static#{target.index_root}/en/#{build_number}/index.html"
      to_path = "#{DOCROOT}#{target.target_name}"
      
      puts ssh.exec!("mkdir -p #{to_path}") || "% mkdir -p #{to_path}"
      to_path = "#{to_path}/index.html"
      unless ssh.exec!("ls #{to_path}").empty? # check for existance
        puts ssh.exec!("rm #{to_path}") || " ~ Removed link at #{to_path}"
      end
      puts ssh.exec!("ln -s #{from_path} #{to_path}") || " ~ Linked #{from_path} => #{to_path}"
 
      # link each language
      LANGUAGES.each do |lang|
        from_path = "#{DOCROOT}/static#{target.index_root}/#{lang}/#{build_number}"
        to_path = "#{DOCROOT}#{target.target_name}/#{lang}"
 
        puts " ~ installing language: #{lang}"
        unless ssh.exec!("ls #{to_path}").empty? # check for existance
          puts ssh.exec!("rm #{to_path}") || " ~ Removed link at #{to_path}"
        end
        puts ssh.exec!("ln -s #{from_path} #{to_path}") || " ~ Linked #{from_path} => #{to_path}"
      end
      
      # Also - this is for home only
      if target.target_name.to_s == HOME_TARGET
        to_path = "#{DOCROOT}/index.html"
        from_path = "#{DOCROOT}/static#{target.index_root}/en/#{build_number}/index.html"
        unless ssh.exec!("ls #{to_path}").empty? # check for existance
          puts ssh.exec!("rm #{to_path}") || " ~ Removed link at #{to_path}"
        end
        puts ssh.exec!("ln -s #{from_path} #{to_path}") || " ~ Linked #{from_path} => #{to_path}"
      end
        
    end
      
  end
  
end
 
desc "builds and then deploys the files to the server. This will not clean the build first, which will be faster. If you have trouble try deploy_clean"
task :deploy => [:collect_password, :build, :deploy_assets, :deploy_feeds, :link_current]
 
desc "first cleans, then deploys the files"
task :deploy_clean => [:clean, :deploy]