Module: Rake::DSL

Included in:
HerokuApp
Defined in:
lib/ggake/haml.rb,
lib/ggake/coffee.rb,
lib/ggake/heroku.rb,
lib/ggake/cloud_cp.rb,
lib/ggake/dbdeploy.rb,
lib/ggake/transient.rb

Instance Method Summary collapse

Instance Method Details

#cloud_cp(dir, options) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/ggake/cloud_cp.rb', line 4

def cloud_cp(dir, options)
  # Done here to allow fog to be an optional dependency
  require 'fog'

  connection = Fog::Storage.new(options[:credentials])
  cd dir do
    upload_sub_dir(connection, '.', options[:bucket], options[:public] || false)
  end
end

#coffee(input, options) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/ggake/coffee.rb', line 4

def coffee(input, options)
  File.join(options[:output], File.basename(input)).ext('js').tap do |js_file|
    file js_file => input do
      sh "coffee --compile --output #{options[:output]} #{input}"
    end
  end
end

#dbdeploy(opts) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ggake/dbdeploy.rb', line 6

def dbdeploy(opts)
  opts[:driver] ||= 'org.postgresql.Driver'
  opts[:migrations] ||= 'src/db/migrations'
  opts[:password] = "\n" if !opts[:password] || opts[:password].empty?

  java_args = ["-cp", opts[:classpath],
               "com.dbdeploy.CommandLineTarget",
               "--driver", opts[:driver],
               "--url", opts[:db_url],
               '--scriptdirectory', opts[:migrations],
               '--userid', opts[:user],
               '--password']
  Open3.popen2("java", *java_args) do |i, o, w|
    i.print opts[:password]
    i.close
    
    $stdout.print(o.gets)
  end
end

#foreman(env_vars, cmd) ⇒ Object



6
7
8
9
10
# File 'lib/ggake/heroku.rb', line 6

def foreman(env_vars, cmd)
  Bundler.with_clean_env do
    sh "#{env_vars.collect { |var| var.join('=') }.join(' ')} foreman #{cmd}"
  end
end

#haml_template(tmpl, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ggake/haml.rb', line 4

def haml_template(tmpl, options)
  extras = ''
  lib_dir = options[:lib]
  if lib_dir
    libraries = Dir["#{lib_dir}/*.rb"].collect { |l| File.basename(l, '*.rb') }
    extras << "-I#{lib_dir} "
    extras << libraries.collect { |l| "-r#{l}" }.join(' ')
  end
  File.join(options[:output], File.basename(tmpl)).ext('html').tap do |html_file|
    directory options[:output]
    file html_file => [options[:output], tmpl] do
      sh "haml -t ugly #{extras} #{tmpl} #{html_file}"
    end
  end
end

#heroku(*cmd_args) ⇒ Object



12
13
14
15
16
# File 'lib/ggake/heroku.rb', line 12

def heroku(*cmd_args)
  Bundler.with_clean_env do
    sh "heroku #{ cmd_args.join(' ') }"
  end
end

#heroku_config_get(var, app) ⇒ Object



18
19
20
21
22
# File 'lib/ggake/heroku.rb', line 18

def heroku_config_get(var, app)
  Bundler.with_clean_env do
    `heroku config:get #{var} --app #{app}`.chomp
  end
end

#transient(name, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ggake/transient.rb', line 6

def transient(name, value)
  transients = File.join('.rake', 'transients')
  FileUtils.mkdir_p transients

  value_file = File.join(transients, name.to_s)
  value_hash = Digest::SHA1.hexdigest(value.to_s)
  if File.exists? value_file
    previous_hash = IO.read(value_file)
    FileUtils.rm value_file if previous_hash != value_hash
  end

  file value_file do
    File.open(value_file, "w+") { |f| f.print value_hash }
  end

  task name => value_file
end

#upload_sub_dir(connection, dir, bucket, is_public) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ggake/cloud_cp.rb', line 14

def upload_sub_dir(connection, dir, bucket, is_public)
  is_public ||= false
  Dir["#{dir}/*"].each do |file|
    if File.directory? file
      upload_sub_dir(connection, file, bucket, is_public)
    else
      file_name = file.gsub("./", '')
      puts "Uploading: '#{file_name}'"
      directory = connection.directories.get(bucket)
      file = directory.files.create(
        :key    => file_name,
        :body   => IO.read(file),
        :public => is_public
      )
    end
  end
end