Class: DevSnap::MonitorWorkspace

Inherits:
Object
  • Object
show all
Defined in:
lib/dev_snap/monitor_workspace.rb

Overview

Strong assume it is git workspace

Constant Summary collapse

VCSMarker =
".git"
DevSnapRepoName =
"dev_snap_repos"

Instance Method Summary collapse

Constructor Details

#initialize(root, backupReposPath) ⇒ MonitorWorkspace

Returns a new instance of MonitorWorkspace.

Raises:



15
16
17
18
19
20
21
# File 'lib/dev_snap/monitor_workspace.rb', line 15

def initialize(root, backupReposPath)
  @root = root
  raise Error, "Root does not exist" if not File.exist?(@root)

  @workspace = GitCli::Workspace.new(@root)
  @backupReposPath = backupReposPath
end

Instance Method Details

#is_vcs_workspace?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/dev_snap/monitor_workspace.rb', line 23

def is_vcs_workspace?
  @workspace.is_workspace?
end

#runObject



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
# File 'lib/dev_snap/monitor_workspace.rb', line 27

def run

  if is_vcs_workspace?

    #binding.break

    snapName = ".git_snap"
    gitBackupName = ".git_prod"

    prodGit = File.join(@root, VCSMarker)
    prodGitBack = File.join(@root, gitBackupName)
    snapGit = File.join(@root, snapName)

    # Move .git folder to .git_prod... 
    logger.debug "Move #{prodGit} to #{prodGitBack}" if DevSnap.is_detail_debug?
    FileUtils.mv(prodGit, prodGitBack)
    # no more .git/...

    begin
      if not File.exist?(snapGit)
        # init .git/
        logger.debug "'#{snapGit}' doesn't exist. Init snap git"
        @workspace.vcs.init(@root)
        @workspace.add_remote(DevSnapRepoName, @backupReposPath)
        @workspace.ignore(snapName, gitBackupName)
        @workspace.commit("Adding backup git into ignore list")
      else
        # rename .git.snap/ to .git/
        logger.debug "Rename #{snapGit} to #{prodGit}" if DevSnap.is_detail_debug?
        FileUtils.mv(snapGit, prodGit)
      end

      # here, .git = snap git

      if not @workspace.clean?
        logger.debug "Workspace '#{@workspace.workspace_root}' has changes"
        if DevSnap.is_detail_debug?
          logger.debug "Workspace new files : #{@workspace.newFiles}"
          logger.debug "Workspace modified files : #{@workspace.modFiles}"
          logger.debug "Workspace staged files : #{@workspace.stgFiles}"
          logger.debug "Workspace deleted files : #{@workspace.delFiles}"
          logger.debug "Workspace conflicted files : #{@workspace.cftFiles}"
        end

        @workspace.add(".")
        @workspace.commit("Added by DevSnap version #{DevSnap::VERSION} at #{Time.now}")

        if not @workspace.remote_config.keys.include?("dev_snap_repos")
          @workspace.add_remote(DevSnapRepoName, @backupReposPath)
        end

        @workspace.push_changes(DevSnapRepoName)

      else
        logger.debug "[Skipped] Workspace '#{@workspace.workspace_root}' is clean"
      end

    rescue Exception => ex
      raise

    ensure

      # before begin block, .git/ already renamed to .git_prod
      # If there is still .git/, then it must be the snap
      if File.exist?(prodGit)
        # rename .git to .git.snap.. no more .git
        logger.debug "Rename #{prodGit} to #{snapGit}" if DevSnap.is_detail_debug?
        FileUtils.mv(prodGit, snapGit)
      end

      # rename .git_prod to .git
      logger.debug "Move #{prodGitBack} to #{prodGit}" if DevSnap.is_detail_debug?
      FileUtils.mv(prodGitBack, prodGit)

    end

  else
    logger.debug "Path '#{@root}' is not a VCS workspace"
  end

end