Sha256: 2f27feaabae3885c28950baea0ddf7c64cbda828a7aea499eb7995e17bc0f6f9

Contents?: true

Size: 1.28 KB

Versions: 11

Compression:

Stored size: 1.28 KB

Contents

require 'papa/helper/path'
require 'papa/command/base'

module Papa
  module Helper
    class Vi
      attr_accessor :path, :branches

      def initialize
        @path = Helper::Path.generate_vi_file_path
        @branches = []
      end

      def run
        initialize_file
        prompt_vi
        read_from_file
        delete_file
        validate_branches
        branches
      end

      private

      def initialize_file
        content = <<-CONTENT


# Add your branches here. One branch name per line.
# Lines starting in pound (#) will be ignored
# Sample:
# feature/1-add-butterfree-gem
# feature/2-add-beedrill-gem
        CONTENT
        File.open(path, 'w') { |file| file.write(content) }
      end

      def prompt_vi
        system('vi', path)
      end

      def read_from_file
        @branches = File.read(path).chomp.split("\n").map do |branch|
          branch.strip!
          if branch.empty? || branch[0] == '#'
            nil
          else
            branch
          end
        end.compact.uniq
      end

      def delete_file
        Command::Base.new("rm #{path}", silent: true).run
      end

      def validate_branches
        if @branches.empty?
          Helper::Output.failure 'No branches specified.'
          exit 1
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
papa-1.2.0 lib/papa/helper/vi.rb
papa-1.1.0 lib/papa/helper/vi.rb
papa-1.0.0 lib/papa/helper/vi.rb
papa-0.7.2 lib/papa/helper/vi.rb
papa-0.7.1 lib/papa/helper/vi.rb
papa-0.7.0 lib/papa/helper/vi.rb
papa-0.6.2 lib/papa/helper/vi.rb
papa-0.6.1 lib/papa/helper/vi.rb
papa-0.6.0 lib/papa/helper/vi.rb
papa-0.5.0 lib/papa/helper/vi.rb
papa-0.4.1 lib/papa/helper/vi.rb