class Licensee::Projects::FSProject

Public Class Methods

new(path, **args) click to toggle source
Calls superclass method Licensee::Projects::Project::new
# File lib/licensee/projects/fs_project.rb, line 13
def initialize(path, **args)
  if ::File.file?(path)
    @pattern = File.basename(path)
    @dir = File.expand_path File.dirname(path)
  else
    @pattern = '*'
    @dir = File.expand_path(path)
  end

  @root = File.expand_path(args.delete(:search_root) || @dir)
  unless valid_search_root?
    raise 'Search root must be the project path directory or its ancestor'
  end

  super(**args)
end

Private Instance Methods

dir_path() click to toggle source
# File lib/licensee/projects/fs_project.rb, line 84
def dir_path
  @dir_path ||= Pathname.new(@dir)
end
files() click to toggle source

Returns an array of hashes representing the project's files. Hashes will have the the following keys:

:name - the relative file name
:dir  - the directory path containing the file
# File lib/licensee/projects/fs_project.rb, line 36
def files
  @files ||= search_directories.flat_map do |dir|
    relative_dir = Pathname.new(dir).relative_path_from(dir_path).to_s
    Dir.glob(::File.join(dir, @pattern).tr('\\', '/')).map do |file|
      next unless ::File.file?(file)

      { name: ::File.basename(file), dir: relative_dir }
    end.compact
  end
end
load_file(file) click to toggle source

Retrieve a file's content from disk, enforcing encoding

file - the file hash, with the :name key as the file's relative path

Returns the file contents as a string

# File lib/licensee/projects/fs_project.rb, line 52
def load_file(file)
  content = File.read dir_path.join(file[:dir], file[:name])
  content.force_encoding(ProjectFiles::ProjectFile::ENCODING)

  return content if content.valid_encoding?

  content.encode(ProjectFiles::ProjectFile::ENCODING, **ProjectFiles::ProjectFile::ENCODING_OPTIONS)
end
search_directories() click to toggle source

Returns the set of unique paths to search for project files in order from @dir -> @root

# File lib/licensee/projects/fs_project.rb, line 68
def search_directories
  search_enumerator.map(&:to_path)
                   .push(@root) # ensure root is included in the search
                   .uniq # don't include the root twice if @dir == @root
end
search_enumerator() click to toggle source

Enumerates all directories to search, from @dir to @root

# File lib/licensee/projects/fs_project.rb, line 75
def search_enumerator
  root = Pathname.new(@root)
  Enumerator.new do |yielder|
    dir_path.relative_path_from(root).ascend do |relative|
      yielder.yield root.join(relative)
    end
  end
end
valid_search_root?() click to toggle source

Returns true if @dir is @root or it's descendant

# File lib/licensee/projects/fs_project.rb, line 62
def valid_search_root?
  dir_path.fnmatch?(@root) || dir_path.fnmatch?(::File.join(@root, '**'))
end