Class: Yad::Scm::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/yad/scm/git.rb

Class Method Summary collapse

Class Method Details

.build_checkout_command(repository, destination_directory, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/yad/scm/git.rb', line 4

def self.build_checkout_command(repository, destination_directory, options = {})
  default_options = { :revision => 'origin/master', :enable_submodules => true }
  options = default_options.merge(options)

  commands = ["([ -d #{destination_directory}/cached-copy/.git ] && echo 'Existing repository found' || git clone #{repository} #{destination_directory}/cached-copy)",
              "cd #{destination_directory}/cached-copy",
              "git fetch",
              "git reset --hard #{options[:revision]}"
             ]

  if options[:enable_submodules]
    commands << [ "git submodule -q init",
                  "git submodule -q update"
                ]
  end
  commands.join(" && ")
end

.build_export_command(source_directory, destination_directory) ⇒ Object



22
23
24
# File 'lib/yad/scm/git.rb', line 22

def self.build_export_command(source_directory, destination_directory)
  "mkdir -p #{destination_directory} && rsync -a -f '- .git' #{source_directory}/cached-copy/ #{destination_directory}"
end

.build_inline_revision_identifier_command(scm_directory, options = {}) ⇒ Object



26
27
28
29
30
# File 'lib/yad/scm/git.rb', line 26

def self.build_inline_revision_identifier_command(scm_directory, options = {})
  default_options = { :revision => 'origin/master' }
  options = default_options.merge(options)
  "`cd #{scm_directory}/cached-copy && git rev-parse #{options[:revision]}`"
end

.define_tasksObject



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
# File 'lib/yad/scm/git.rb', line 32

def self.define_tasks
  return if @tasks_already_defined
  @tasks_already_defined = true
  namespace :yad do
    namespace :scm do
      
      desc "Updates the source code on the application servers and exports it as the latest release"
      remote_task :update, :roles => :app do
        begin
          options = Rake::RemoteTask.get_options_hash(:revision, :enable_submodules)
          checkout_command = Yad::Scm::Git.build_checkout_command(repository, scm_path, options)
          export_command = Yad::Scm::Git.build_export_command(scm_path, release_path)
          cmd = Yad::Core.build_update_source_code_command(checkout_command, export_command, release_path)
          run(cmd)
          puts("source code updated on #{target_host}")
        rescue => e
          cmd = Yad::Core.build_remove_directory_command(release_path)
          run(cmd)
          raise e
        end
      end
      
    end
  end

end