Sha256: d3672106bd4a0d26234f2fffa1ee850d3bb0cba73c9fb02f643fb36bb62379f2

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require_relative 'template'

module Docs
  class Generator
    module Methods
      def clear_dir(path, except: [])
        dir_path = docs_path(path)
        yellow "Clearing: #{dir_path}"
        Dir.foreach(dir_path) do |f|
          next if except.include?(f)

          fn = File.join(dir_path, f)
          next if f == '.'
          next if f == '..'

          rm(fn)
        end
        green "Cleared: #{dir_path}"
        nl
      end

      def cp(src, dest, *args)
        yellow "Copying #{src} to #{dest}"
        FileUtils.cp_r(src, dest, *args)
        green "Copied #{src} to #{dest}"
        nl
      end

      def destination_path(*args, **keywords)
        docs_path(*args, **{ format: :md }.merge(keywords))
      end

      def docs_path(*path, format: nil)
        path = path.map(&:to_s)

        File.join(
          docs_root_path,
          *path[0..-2],
          [path.last, format].flatten.compact.join('.')
        )
      end

      def docs_root_path
        @docs_root_path ||= ENV.fetch('DOCS_ROOT_PATH', File.join(__dir__, '../site'))
      end

      def ledgers
        LedgerSync.ledgers
                  .to_a
                  .map(&:last)
                  .uniq
                  .to_a
                  .sort_by(&:root_key)
      end

      def rm(fn)
        yellow "Removing: #{fn}"
        FileUtils.rm_rf(fn, secure: true)
        green "Removed: #{fn}"
        nl
      end

      def template_path(*path, format: :md)
        path = path.map(&:to_s)

        File.join(
          docs_root_path,
          '_templates',
          *path[0..-2],
          "#{path.last}.#{format}.erb"
        )
      end
    end

    include Methods
    extend Methods
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ledger_sync-1.4.2 documentation/generators/generator.rb
ledger_sync-1.4.1 documentation/generators/generator.rb
ledger_sync-1.4.0 documentation/generators/generator.rb