Class: RosettAi::ProjectContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rosett_ai/project_context.rb

Overview

Detects whether Dir.pwd is inside an rosett-ai-managed project by walking up from the start directory looking for a .rosett-ai/ marker directory.

When found, content commands (design, behaviour, compile) resolve their source paths relative to the project's .rosett-ai/ directory instead of the Rosett-AI installation root.

Constant Summary collapse

PROJECT_MARKER =

Directory name that marks the root of an rai project.

'.rosett-ai'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_dir: ENV.fetch('RAI_ORIGINAL_PWD', Dir.pwd)) ⇒ ProjectContext

Returns a new instance of ProjectContext.

Parameters:

  • start_dir (String) (defaults to: ENV.fetch('RAI_ORIGINAL_PWD', Dir.pwd))

    directory to begin searching from. Defaults to RAI_ORIGINAL_PWD env var (set by the .deb wrapper to preserve the user's directory), falling back to Dir.pwd.



23
24
25
# File 'lib/rosett_ai/project_context.rb', line 23

def initialize(start_dir: ENV.fetch('RAI_ORIGINAL_PWD', Dir.pwd))
  @project_root = detect_project_root(start_dir)
end

Instance Attribute Details

#project_rootPathname? (readonly)

Returns absolute path to the project root, or nil if no project detected.

Returns:

  • (Pathname, nil)

    absolute path to the project root, or nil if no project detected



18
19
20
# File 'lib/rosett_ai/project_context.rb', line 18

def project_root
  @project_root
end

Instance Method Details

#conf_rootPathname

When inside the rosett-ai source tree, returns the source tree root (not .rosett-ai/) so that conf/ resolves to the real configuration directory containing behaviour and design YAML files, rather than the empty .rosett-ai/conf/ placeholder created by rai init --project.

Returns:

  • (Pathname)

    the .rosett-ai/ directory if inside a project, otherwise RosettAi.root



44
45
46
47
48
# File 'lib/rosett_ai/project_context.rb', line 44

def conf_root
  return @project_root if rai_internal?

  project? ? @project_root.join(PROJECT_MARKER) : RosettAi.root
end

#project?Boolean

Returns true if a .rosett-ai/ marker was found.

Returns:

  • (Boolean)

    true if a .rosett-ai/ marker was found



28
29
30
# File 'lib/rosett_ai/project_context.rb', line 28

def project?
  !@project_root.nil?
end

#project_conf_dirPathname

Returns path to the conf/ subdirectory for behaviour/design files.

Returns:

  • (Pathname)

    path to the conf/ subdirectory for behaviour/design files



51
52
53
# File 'lib/rosett_ai/project_context.rb', line 51

def project_conf_dir
  conf_root.join('conf')
end

#rai_internal?Boolean

Returns true if the detected project is the Rosett-AI installation itself or the Rosett-AI source tree (when running the installed .deb from source).

Returns:

  • (Boolean)

    true if the detected project is the Rosett-AI installation itself or the Rosett-AI source tree (when running the installed .deb from source)



34
35
36
# File 'lib/rosett_ai/project_context.rb', line 34

def rai_internal?
  @project_root == RosettAi.root || nncc_source_tree?
end