Sha256: f216301a8b4ece9c394e110bf0e3c55dcf0fc28088b35ec37ac8e80a1c52bed5

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# typed: strict
# frozen_string_literal: true

module Bhook
  class Directory
    extend T::Sig
    
    GIT_DIR = '.git'
    MD_EXT = '.md'
    
    sig { params(src_path: Pathname, out_path: Pathname, git: Git::Base).void }
    def initialize(src_path, out_path, git)
      @src_path = src_path
      @out_path = T.let(out_path.join(src_path.basename), Pathname)
      @git = git
      @sub_dirs = T.let([], T::Array[Directory])
      @md_files = T.let([], T::Array[MdFile])
      build_next_level_nodes
    end

    sig { params(theme: Theme).void }
    def write!(theme)
      FileUtils.mkdir_p(@out_path, verbose: true)
      @sub_dirs.each { |dir| dir.write!(theme) }
      @md_files.map do |file| 
        Thread.new { file.write!(theme) }
      end.each do |thread|
        thread.join
      end
    end
    
    sig { returns(T::Array[MdFile]) }
    def all_md_files
      @md_files + @sub_dirs.map(&:all_md_files).flatten
    end

    sig { returns(String) }
    def to_s
      @src_path.to_s
    end
    
    private

    sig { void }
    def build_next_level_nodes
      children = @src_path.children
      children.delete(@src_path.join(GIT_DIR))
      
      file_threads = []
      children.each do |child_path|
        if child_path.directory?
          @sub_dirs << Directory.new(child_path, @out_path, @git)
        elsif child_path.extname == MD_EXT
          file_threads << Thread.new { MdFile.new(child_path, @out_path, @git) }
        end
      end
      file_threads.each { |thread| @md_files << thread.value }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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