Class: ConfigGitCommit

Inherits:
Object
  • Object
show all
Defined in:
lib/commands/config-git-commit.rb

Instance Method Summary collapse

Constructor Details

#initializeConfigGitCommit

Returns a new instance of ConfigGitCommit.



6
7
8
# File 'lib/commands/config-git-commit.rb', line 6

def initialize ()
    @log = Logger.new(Canzea::config[:logging_root] + '/plans.log')
end

Instance Method Details

#do(gitRoot, gitUri, sourcePath, template, basePath, parameters) ⇒ Object



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
# File 'lib/commands/config-git-commit.rb', line 10

def do(gitRoot, gitUri, sourcePath, template, basePath, parameters)
    begin
        log "Using basePath #{basePath}"

        if (File.exists?(gitRoot) == false)
            log "Cloning git repository #{gitUri}"
            Git.config do |config|
              config.ssh_key = "#{Dir.home}/.ssh/id_rsa"
            end
            g = Git.clone(gitUri, "", :path => gitRoot)
        end

        # if file exists on file system and does not exist in gitRoot, then commit the "oem" file

        if (File.exists?(sourcePath))
            dest = "#{gitRoot}/#{basePath}#{Pathname.new(sourcePath).realpath}"
        else
            dest = "#{gitRoot}/#{basePath}#{sourcePath}"
        end
        log "Writing to #{dest}"

        if (File.exist?(dest) == false and File.exist?(sourcePath))
            FileUtils.mkdir_p Pathname.new(dest).dirname
            FileUtils.cp sourcePath, dest

            log "Recording in repo the default file first: #{sourcePath}"
            if (Canzea::config[:track_changes_in_git])
                g = Git.init(gitRoot)
                g.add(:all=>true)
                g.commit("Original default #{sourcePath}")
            end
        end

        if template
            log "Processing template #{template}"
            FileUtils.mkdir_p Pathname.new(sourcePath).dirname
            t = Template.new
            t.processAndWriteToFile template, sourcePath, parameters
        end
        FileUtils.mkdir_p Pathname.new(dest).dirname
        FileUtils.cp sourcePath, dest, :verbose => true

        if (Canzea::config[:track_changes_in_git])
            g = Git.init(gitRoot)
            g.add(:all=>true)
            log "#{g.status.changed.size() + g.status.added.size()}"
            if (g.status.changed.size() > 0 or g.status.added.size() > 0)
                g.commit("Config update #{sourcePath}")
            end
        end

        if (Canzea::config[:track_changes_in_git])
            g = Git.init(gitRoot)
            g.push
        end
    rescue => exception
        @log.error("ConfigGitCommit Failed")
        @log.error(exception.to_s)
        @log.error(exception.backtrace)
        abort()
    end
end

#log(msg) ⇒ Object



73
74
75
76
# File 'lib/commands/config-git-commit.rb', line 73

def log (msg)
    puts msg
    @log.info(msg)
end