Sha256: 2f3ca83864dfe6c9f5f93def933a1f0c4a30b01e954d115b4603cf65ea344a34

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require "thor"
require "jive"
require "pathname"

module Jive
  module Cli
    class App < Thor
      def self.exit_on_failure?
        true
      end

      desc "clone <org>/<project>", "git clone to ~/src/github.com/<org>/<project>"
      def clone(slug)
        target_dir = Pathname.new(Dir.home).join("src/github.com/#{slug}")
        run_each([
          [:mkdir, "-p", target_dir.parent.to_s],
          [:git, "clone", "git@github.com:#{slug}.git", target_dir]
        ])
        after_run([
          ["cd", target_dir],
          ["setenv", "JIVE_LAST_RUN=#{Time.now.to_i}"]
        ])
      end

      desc "setup", "provide instructions to integrate into shell"
      def setup
        say <<~MESSAGE
          Include the following in your ~/.bash_profile

            source #{::Jive.root.join("jive.sh")}
        MESSAGE
      end

      private

      COMMAND_MAP = {
        cd: "/usr/bin/cd",
        echo: "/usr/bin/echo",
        git: "/usr/bin/git",
        mkdir: "/usr/bin/mkdir"
      }.freeze

      def run_each(tasks)
        tasks.each do |task|
          break unless execute(task)
        end
      end

      def execute(command, env: {})
        system(env, expand(command))
      end

      def after_run(tasks)
        finalizer_fd = 9
        pipe = IO.new(finalizer_fd)
        pipe.puts(tasks.map { |x| x.join(":") }.join("\n"))
      end

      def expand(command)
        Array(command)
          .flatten
          .map { |x| COMMAND_MAP.fetch(x, x).to_s }
          .join(" ")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jive-0.2.1 lib/jive/cli.rb