Sha256: 23e5d1758a9f04256c7b0dc3ca1176f400cf468158fdd18a5175c7ff565a7ab0

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# typed: true
# frozen_string_literal: true

module Bhook
  class Directory
    GIT_DIR = '.git'
    MD_EXT = '.md'
    
    attr_reader :sub_dirs, :md_files

    def initialize(src_path, git = nil)
      @src_path = src_path
      @git = git
      @sub_dirs = []
      @md_files = []
      build_next_level_nodes
    end

    def root_dir_for_a_commit?
      !@git
    end

    def write!(out_path)
      dir_path = if root_dir_for_a_commit?
                   out_path
                 else
                   File.join(out_path, @src_path.basename)
                 end

      FileUtils.mkdir_p(dir_path, verbose: true)
      @sub_dirs.each { |dir| dir.write!(dir_path) }
      @md_files.each { |file| file.write!(dir_path) }
    end

    def all_md_files
      @md_files + @sub_dirs.map(&:all_md_files).flatten
    end

    def to_s
      @src_path.to_s
    end

    private

    def build_next_level_nodes
      if root_dir_for_a_commit?
        @git = Git.open(@src_path)
      end

      @sub_dirs = @src_path.children.map do |child|
        if child.directory? && child.basename.to_s != GIT_DIR
          Directory.new(child, @git)
        end
      end.compact
      @md_files = @src_path.children.select do |child|
        !child.directory? && child.extname == MD_EXT
      end.map do |child|
        MdFile.new(child, @git)
      end.compact
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bhook-0.1.0 lib/bhook/directory.rb