Class: Aidp::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/util.rb

Overview

Utility functions shared between execute and analyze modes

Class Method Summary collapse

Class Method Details

.ensure_dirs(output_files, project_dir) ⇒ Object



19
20
21
22
23
24
# File 'lib/aidp/util.rb', line 19

def self.ensure_dirs(output_files, project_dir)
  output_files.each do |file|
    dir = File.dirname(File.join(project_dir, file))
    FileUtils.mkdir_p(dir) unless dir == "."
  end
end

.find_project_root(start_dir = Dir.pwd) ⇒ Object

Walk upward to find the nearest project root (git/package manager markers)



32
33
34
35
36
37
38
39
40
# File 'lib/aidp/util.rb', line 32

def self.find_project_root(start_dir = Dir.pwd)
  dir = File.expand_path(start_dir)
  until dir == File.dirname(dir)
    return dir if project_root?(dir)
    dir = File.dirname(dir)
  end
  # Fall back to the original directory when no markers were found
  File.expand_path(start_dir)
end

.project_root?(dir = Dir.pwd) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/aidp/util.rb', line 42

def self.project_root?(dir = Dir.pwd)
  File.exist?(File.join(dir, ".git")) ||
    File.exist?(File.join(dir, "package.json")) ||
    File.exist?(File.join(dir, "Gemfile")) ||
    File.exist?(File.join(dir, "pom.xml")) ||
    File.exist?(File.join(dir, "build.gradle"))
end

.safe_file_write(path, content) ⇒ Object



26
27
28
29
# File 'lib/aidp/util.rb', line 26

def self.safe_file_write(path, content)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, content)
end

.which(cmd) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/aidp/util.rb', line 8

def self.which(cmd)
  exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]
  ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end