Class: Precompile2git
- Inherits:
-
Object
- Object
- Precompile2git
- Defined in:
- lib/precompile2git.rb
Instance Method Summary collapse
-
#commit_and_push ⇒ Object
Commit and push to compiled branch.
-
#initialize ⇒ Precompile2git
constructor
A new instance of Precompile2git.
-
#precompile ⇒ Object
Creates a new process and start the “rake assets:precompile” task.
-
#start ⇒ Object
Run the first precompilation task and starts watching a git repo for any update.
-
#sync_and_merge ⇒ Object
Resets both compiled and uncompiled branch to have a mirror of origin Then merges uncompiled_branch to compiled one.
-
#sync_with_origin(branch_name) ⇒ Object
Makes sure that each branch is the mirror of origin, to prevent any merging issue.
-
#up_to_date? ⇒ Boolean
Checkout the repo and check if there is any new commit in uncompiled branch.
-
#watch_repo(interval) ⇒ Object
Watch at a given interval if the repo has been updated.
Constructor Details
#initialize ⇒ Precompile2git
Returns a new instance of Precompile2git.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/precompile2git.rb', line 10 def initialize @project_dir = Dir.getwd @logger = Logger.new(STDOUT) config = YAML.load_file(@project_dir + "/config/precompile2git.yml") config["precompile2git"].each { |key, value| instance_variable_set("@#{key}", value) } git_opts = @log_git ? { :log => @logger } : {} @g = Git.open(@project_dir , git_opts) @g.config('user.name', @user_name) if @user_name @g.config('user.email', @user_email) if @user_email end |
Instance Method Details
#commit_and_push ⇒ Object
Commit and push to compiled branch
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/precompile2git.rb', line 28 def commit_and_push begin @g.add('.') @g.commit_all("Assets precompilation") rescue Git::GitExecuteError => e @logger.error("Could not commit. This can occur if there was nothing to commit. Git error: " + e.inspect) end begin @g.push("origin", @compiled_branch) rescue Git::GitExecuteError => e @logger.error("Could not push changes. Git error: " + e.inspect) end end |
#precompile ⇒ Object
Creates a new process and start the “rake assets:precompile” task
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 |
# File 'lib/precompile2git.rb', line 64 def precompile begin if @precompilation_process_pid @logger.info("A precompilation has been launched before. Killing any rake task that may be still running...") begin pids = Process.descendant_processes(@precompilation_process_pid) pids.each do |pid| @logger.info("Killing pid:" + pid.to_s) Process.kill(9, pid) end rescue Exception => e @logger.info("Something went wrong when killing running processes: " + e.to_s) end end @precompilation_process_pid = fork do @logger.info("Precompiler: Starting assets precompilation...") system('bundle install > ' + @commands_log_file + '; RAILS_ENV=' + @rails_env + ' rake assets:precompile > ' + @commands_log_file + ';') @logger.info("Precompiler: Precompilation done. Committing and pushing") commit_and_push @logger.info("Precompiler: Pushed to main branch. Ready to deploy!") @precompilation_process_pid = nil end rescue Exception => e @logger.info "Something went wrong in precompilation process: " + e.to_s end end |
#start ⇒ Object
Run the first precompilation task and starts watching a git repo for any update
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/precompile2git.rb', line 47 def start @logger.info("Syncing repo.") sync_and_merge @logger.info("Syncing done, running first precompilation.") precompile @logger.info("Precompilation process started, start watching.") @watch_thread = watch_repo(5) @watch_thread.join end |
#sync_and_merge ⇒ Object
Resets both compiled and uncompiled branch to have a mirror of origin Then merges uncompiled_branch to compiled one
122 123 124 125 126 127 |
# File 'lib/precompile2git.rb', line 122 def sync_and_merge sync_with_origin(@uncompiled_branch) sync_with_origin(@compiled_branch) @g.merge(@uncompiled_branch, nil) end |
#sync_with_origin(branch_name) ⇒ Object
Makes sure that each branch is the mirror of origin, to prevent any merging issue
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/precompile2git.rb', line 104 def sync_with_origin( branch_name ) locals = @g.branches.local.map{ |b| b.to_s } @g.reset_hard if locals.include?(branch_name) @g.checkout(branch_name) else @g.checkout("origin/" + branch_name, { :new_branch => branch_name } ) end @g.reset_hard("origin/" + branch_name) @g.pull("origin", branch_name) end |
#up_to_date? ⇒ Boolean
Checkout the repo and check if there is any new commit in uncompiled branch
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/precompile2git.rb', line 131 def up_to_date? begin @g.fetch # log should be empty if no updates log = @g.log.between(@uncompiled_branch, "origin/" + @uncompiled_branch) return log.size == 0 rescue Exception => e @logger.error("Could not check repository state. ") return true end end |
#watch_repo(interval) ⇒ Object
Watch at a given interval if the repo has been updated. If so, any running rake task should be killed and a new one should be launched
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 |
# File 'lib/precompile2git.rb', line 149 def watch_repo(interval) Thread.new do loop do start_time = Time.now begin up_to_date = up_to_date? unless up_to_date @logger.info("New commits found, precompiling.") sync_and_merge precompile end rescue Git::GitExecuteError => e @logger.error("Something went wrong with Git : " + e.to_s) end elapsed = Time.now - start_time sleep( [ interval - elapsed, 0].max ) end end end |