class Gitoc::Cli

Public Instance Methods

check() click to toggle source
# File lib/gitoc/cli.rb, line 12
def check
  init_base

  # Get all repositories from the filesystem
  # and tag them with :fs
  repositories = repositories_fs.map do |repository_fs|
    [repository_fs, [:fs]]
  end

  # Add missing repositories from the GiTOC file
  # and tag all repositories from the GiTOC file with :toc
  repositories_gitoc.each do |repository_gitoc|
    _, tags = repositories.find {|repository_fs, _| repository_fs == repository_gitoc }
    if tags
      tags << :toc
    else
      repositories << [repository_gitoc, [:toc]]
    end
  end

  # Sort repositories list
  # and build table rows: [path, url, comment]
  rows = repositories.sort_by {|repository, _| repository.rel_path }.map do |repository, tags|
    path, url = repository.to_hash.values
    url = "-" if url.nil? || url.empty?
    comment = tags.include?(:fs) && tags.include?(:toc) ? "" : {fs: "not in GiTOC", toc: "not on filesystem"}[tags.first]

    [path, url, comment]
  end

  print_table rows  
end
clone() click to toggle source
# File lib/gitoc/cli.rb, line 57
def clone
  init_base

  each_repository do |repo|
    if repo.exist?
      puts "Skip repository, #{repo.path} already exists."
      next
    end

    repo.clone
  end
end
clone_or_pull() click to toggle source
# File lib/gitoc/cli.rb, line 85
def clone_or_pull
  init_base

  each_repository do |repo|
    if repo.exist?
      repo.pull
    else
      repo.clone
    end
  end
end
generate() click to toggle source
# File lib/gitoc/cli.rb, line 46
def generate
  init_base

  toc = repositories_fs.map(&:to_hash)

  # Write git_toc file
  gitoc.write toc.to_yaml
  puts "Write #{gitoc}"
end
pull() click to toggle source
# File lib/gitoc/cli.rb, line 71
def pull
  init_base

  each_repository do |repo|
    unless repo.exist?
      puts "Skip repository, #{repo.path} doesn't exist."
      next
    end

    repo.pull
  end
end

Private Instance Methods

each_repository() { |repo| ... } click to toggle source
# File lib/gitoc/cli.rb, line 126
def each_repository
  repositories_gitoc.each_with_index do |repo, index|
    puts
    say "~/#{repo.path.relative_path_from(home)} (#{index + 1}/#{repositories_gitoc.count})", :cyan

    unless repo.url?
      say "Skip repository with no remote.origin.url", :red
      next
    end

    yield repo
  end
end
gitoc() click to toggle source
# File lib/gitoc/cli.rb, line 103
def gitoc
  @gitoc ||= Pathname.new(options[:toc]).expand_path
end
home() click to toggle source
# File lib/gitoc/cli.rb, line 140
def home
  @home ||= Pathname.new(ENV["HOME"])
end
init_base() click to toggle source
# File lib/gitoc/cli.rb, line 99
def init_base
  Gitoc::Repository.base = Pathname.new(options[:base]).expand_path
end
repositories_fs() click to toggle source
# File lib/gitoc/cli.rb, line 120
def repositories_fs
  @repositories_fs ||= Gitoc::Repository.base.glob("**/.git").map do |git_dir|
    Gitoc::Repository.new(git_dir.parent)
  end.sort_by(&:path)
end
repositories_gitoc() click to toggle source
# File lib/gitoc/cli.rb, line 107
def repositories_gitoc
  @repositories_gitoc ||= begin
    unless gitoc.exist?
      say "#{gitoc} not found", :red
      exit 1
    end

    YAML.load_file(gitoc).map do |attributes|
      Gitoc::Repository.load attributes
    end
  end
end