class RokuBuilder::Stager

Change stage of roku application

Public Class Methods

new(config:, options:) click to toggle source
# File lib/roku_builder/stager.rb, line 8
def initialize(config:, options:)
  @config = config
  @options = options
  @method = get_method
  @ref = get_git_ref
  @scripts = get_scripts
  @plugin = get_plugin
  @root_dir = config.root_dir
  @logger = Logger.instance
  @stage_success = true
  @stash_key = "roku-builder-temp-stash"
end

Public Instance Methods

method() click to toggle source

Helper method to get the staging method being used @return [Symbol] staging method being used

# File lib/roku_builder/stager.rb, line 23
def method
  @method
end
stage() click to toggle source

Change the stage of the app depending on the method @return [Boolean] whether the staging was successful or not

# File lib/roku_builder/stager.rb, line 30
def stage
  @orginal_directory = Dir.pwd
  case @method
  when :current, :in
    # Do Nothing
  when :working
    switch_directory
  when :git
    switch_directory
    begin
      git_switch_to(branch: @ref)
    rescue Git::GitExecuteError
      git_rescue
      @stage_success = false
    end
  when :script
    switch_directory
    staging_logs = RokuBuilder.system(command: @scripts[:stage])
    if !staging_logs.empty?
      @logger.warn "===== Staging Logs Start ====="
      puts staging_logs
      @logger.warn "===== Staging Logs End ======="
    end
  when :plugin
    @plugin.stage(options: @options)
  end
  RokuBuilder.process_hook(hook: "post_stage", params: {config: @config, options: @options})
  @stage_success
end
unstage() click to toggle source

Revert the change that the stage method made @return [Boolean] whether the revert was successful or not

# File lib/roku_builder/stager.rb, line 62
def unstage
  @orginal_directory ||= Dir.pwd
  unstage_success = true
  case @method
  when :current, :in, :working
    # Do Nothing
  when :git
    switch_directory
    begin
      git_switch_from(branch: @ref, checkout: @stage_success)
    rescue Git::GitExecuteError
      git_rescue
      unstage_success = false
    end
    switch_directory_back
  when :script
    switch_directory
    if @scripts[:unstage]
      unstaging_logs = RokuBuilder.system(command: @scripts[:unstage])
      if !unstaging_logs.empty?
        @logger.warn "===== Unstaging Logs Start ====="
        puts unstaging_logs
        @logger.warn "===== Unstaging Logs End ======="
      end
    end
    switch_directory_back
  when :plugin
    @plugin.unstage(options: @options)
  end
  RokuBuilder.process_hook(hook: "post_unstage", params: {config: @config, options: @options})
  unstage_success
end

Private Instance Methods

get_git_ref() click to toggle source
# File lib/roku_builder/stager.rb, line 105
def get_git_ref
  if @options[:ref]
    @options[:ref]
  elsif @config.stage
    @config.stage[:branch]
  end
end
get_method() click to toggle source
# File lib/roku_builder/stager.rb, line 97
def get_method
  method = ([:in, :current, :working] & @options.keys).first
  if @config.project
    method = @config.project[:stage_method]
  end
  method
end
get_plugin() click to toggle source
# File lib/roku_builder/stager.rb, line 117
def get_plugin
  if @config.project and @config.project[:staging_plugin]
    RokuBuilder.plugins[RokuBuilder.plugins.index{|p| p.to_s == @config.project[:staging_plugin]}].new(config: @config)
  end
end
get_scripts() click to toggle source
# File lib/roku_builder/stager.rb, line 113
def get_scripts
  @config.stage[:script] if @config.stage
end
git_rescue() click to toggle source

Called if resuce from git exception

# File lib/roku_builder/stager.rb, line 193
def git_rescue
  @logger.error "Branch or ref does not exist"
end
git_switch_from(branch:, checkout: true) click to toggle source

Switch back to the previous branch @param branch [String] teh branch to switch from @param checkout [Boolean] whether to actually run the checkout command

# File lib/roku_builder/stager.rb, line 148
def git_switch_from(branch:, checkout: true)
  @current_branch ||= nil
  if branch
    @git ||= Git.open(@root_dir)
    if @git and (@current_branch or load_state)
      @git.checkout(@current_branch) if checkout
    end
    if @git
      index = 0
      @git.branch.stashes.each do |stash|
        if stash.message == @stash_key
          @git.branch.stashes.pop("stash@{#{index}}")
          break
        end
        index += 1
      end
    end
  end
end
git_switch_to(branch:) click to toggle source

Switch to the correct branch @param branch [String] the branch to switch to

# File lib/roku_builder/stager.rb, line 133
def git_switch_to(branch:)
  if branch
    @git ||= Git.open(@root_dir)
    @git.branch.stashes.save(@stash_key)
    if @git and branch != @git.current_branch
      @current_branch = @git.current_branch
      @git.checkout(branch)
      save_state
    end
  end
end
load_state() click to toggle source

Load staging state from file

# File lib/roku_builder/stager.rb, line 177
def load_state
  store = PStore.new(File.expand_path("~/.roku_pstore"))
  store.transaction do
    @git.branches.each do |branch|
      if branch.to_s == store[:current_branch]
        @current_branch = branch
        store[:current_branch] = nil
        break
      end
    end
    !!@current_branch
  end
  !!@current_branch
end
save_state() click to toggle source

Save staging state to file

# File lib/roku_builder/stager.rb, line 169
def save_state
  store = PStore.new(File.expand_path("~/.roku_pstore"))
  store.transaction do
    store[:current_branch] = @current_branch.to_s
  end
end
switch_directory() click to toggle source
# File lib/roku_builder/stager.rb, line 123
def switch_directory
  Dir.chdir(@root_dir) unless @root_dir.nil? or @root_dir == @orginal_directory
end
switch_directory_back() click to toggle source
# File lib/roku_builder/stager.rb, line 127
def switch_directory_back
  Dir.chdir(@orginal_directory) unless @root_dir == @orginal_directory
end