module OnlyofficeGithubHelper::FileList

Module for working with file list

Public Instance Methods

file_list(repo, refs: 'master') click to toggle source

Get file list in repo @param [String] repo to check @param [String] refs to check @return [Array<String>] list of files

# File lib/onlyoffice_github_helper/github_client/file_list.rb, line 10
def file_list(repo, refs: 'master')
  Octokit.tree(repo, refs, recursive: true).tree
         .reject { |elem| elem.type == 'tree' }
         .map(&:path)
end
file_tree(repo, refs: 'master') click to toggle source

Get file tree in repo @param [String] repo to check @param [String] refs to check @return [Hash] file tree

# File lib/onlyoffice_github_helper/github_client/file_list.rb, line 20
def file_tree(repo, refs: 'master')
  list = file_list(repo, refs: refs)
  parse_tree(list)
end
parse_tree(list, path: '') click to toggle source

Parse file tree @param [Array<String>] list of file with full path @param [String] path to root @return [Hash] result of parse

# File lib/onlyoffice_github_helper/github_client/file_list.rb, line 29
def parse_tree(list, path: '')
  root_tree = { name: path }
  root_tree[:children] = []
  childs = tree_childs(list)
  childs[:dirs].each do |child_item|
    sub_files = subdir_content(list, child_item)
    root_tree[:children] << parse_tree(sub_files, path: child_item)
  end
  root_tree[:children] << childs[:files]
  root_tree[:children].flatten!
  root_tree
end

Private Instance Methods

subdir_content(list, dir) click to toggle source
# File lib/onlyoffice_github_helper/github_client/file_list.rb, line 64
def subdir_content(list, dir)
  subdir_list = []
  list.each do |item|
    subdir_list << item.sub("#{dir}/", '') if item.start_with?("#{dir}/")
  end
  subdir_list
end
subdir_name(path) click to toggle source
# File lib/onlyoffice_github_helper/github_client/file_list.rb, line 72
def subdir_name(path)
  Pathname(path).each_filename.to_a[0]
end
tree_childs(list) click to toggle source
# File lib/onlyoffice_github_helper/github_client/file_list.rb, line 44
def tree_childs(list)
  childs = []
  child_dirs = []
  list.each do |entry|
    if with_subdir?(entry)
      child_dirs << subdir_name(entry)
    else
      childs << { name: entry }
    end
  end
  { files: childs, dirs: child_dirs.uniq }
end
with_subdir?(file_path) click to toggle source

Check if path is a string @param file_path [String] path @return [True, False]

# File lib/onlyoffice_github_helper/github_client/file_list.rb, line 60
def with_subdir?(file_path)
  Pathname.new(file_path).dirname.to_s != '.'
end