Sha256: a786097b9492a7e19514c36659ce44f26814d398863052ca285e0d5b3837ca91

Contents?: true

Size: 1.78 KB

Versions: 9

Compression:

Stored size: 1.78 KB

Contents

class EcoRake
  module Shell
    module Command
      include Rake::DSL

      # It double quotes a string (escapes the double quotes)
      # @param str [String]
      # @return [String]
      def double_quote(str)
        "\"#{str}\"" if str
      end

      # Helper to build command line
      # @note it excludes `nil` values.
      # @return [String, Array<String>]
      def array_cmd(base, *opts)
        base = [base] unless base.is_a?(Array)
        base.tap do |out|
          opts.each {|opt| out << opt unless opt.nil?}
          yield(out) if block_given?
        end
      end

      # @see #array_cmd it the same but it returns a `String`
      # @return [String]
      def string_cmd(base, *options, join: ' ')
        array_cmd(base, *options).join(join)
      end

      # @param continue [Boolean] whether it should continue when one failed.
      # @param arr [Array<String>] array of commands to be run
      def sh_chain(cmds, continue: false, &block)
        cmds.each do |cmd|
          next sh(cmd, &block) unless continue
          ch_continue(cmd, &block)
        end
      end

      # It **continues** even if there was an error or `exit(1)` during the execution
      # @param comm [String] the command line
      def sh_continue(comm, &block)
        sh(comm, &sh_default_block(comm, &block))
      end

      # Returns the default block for `sh` native method.
      # @note it wraps `block` if given
      # @return [Proc]
      def sh_default_block(comm)
        proc do |ok, res|
          yield(ok, res) if block_given?
          unless ok
            msg = "Command failed (status = #{res.exitstatus})"
            puts "#{msg}\n  • #{comm}"
          end
          res.exitstatus
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
eco-rake-0.2.3 lib/eco-rake/shell/command.rb
eco-rake-0.2.2 lib/eco-rake/shell/command.rb
eco-rake-0.2.1 lib/eco-rake/shell/command.rb
eco-rake-0.2.0 lib/eco-rake/shell/command.rb
eco-rake-0.1.6 lib/eco-rake/shell/command.rb
eco-rake-0.1.5 lib/eco-rake/shell/command.rb
eco-rake-0.1.4 lib/eco-rake/shell/command.rb
eco-rake-0.1.3 lib/eco-rake/shell/command.rb
eco-rake-0.1.2 lib/eco-rake/shell/command.rb