Module: Pray::Invocation

Defined in:
lib/pray/invocation.rb

Class Method Summary collapse

Class Method Details

.contextObject



15
16
17
# File 'lib/pray/invocation.rb', line 15

def context
  Thread.current[:pray_invocation_context]
end

.context=(value) ⇒ Object



19
20
21
# File 'lib/pray/invocation.rb', line 19

def context=(value)
  Thread.current[:pray_invocation_context] = value
end

.initialize(arguments) ⇒ Object



9
10
11
12
13
# File 'lib/pray/invocation.rb', line 9

def initialize(arguments)
  options, remaining = parse_global_options(arguments)
  self.context = ProjectContext.from_options(options)
  remaining
end

.invocation_contextObject



23
24
25
# File 'lib/pray/invocation.rb', line 23

def invocation_context
  context || ProjectContext.from_current_directory
end

.lockfile_pathObject



31
32
33
# File 'lib/pray/invocation.rb', line 31

def lockfile_path
  invocation_context.lockfile_path
end

.manifest_pathObject



27
28
29
# File 'lib/pray/invocation.rb', line 27

def manifest_path
  invocation_context.manifest_path
end

.parse_global_options(arguments) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pray/invocation.rb', line 61

def parse_global_options(arguments)
  options = ProjectInvocationOptions.new
  remaining = []
  index = 0
  while index < arguments.length
    argument = arguments[index]
    case argument
    when "--path"
      index += 1
      options.project_root = require_option_value("--path", arguments[index])
    when "--file-path"
      index += 1
      options.manifest_path = require_option_value("--file-path", arguments[index])
    when "--env", "--environment"
      index += 1
      options.environment = require_environment_value(argument, arguments[index])
    else
      if top_level_command?(argument) || argument.start_with?("-")
        remaining.concat(arguments[index..])
        break
      end
      remaining.concat(arguments[index..])
      break
    end
    index += 1
  end
  [options, remaining]
end

.project_rootObject



35
36
37
# File 'lib/pray/invocation.rb', line 35

def project_root
  invocation_context.project_root
end

.require_environment_value(flag, value) ⇒ Object



96
97
98
99
100
# File 'lib/pray/invocation.rb', line 96

def require_environment_value(flag, value)
  raise Error.usage("#{flag} requires a value") if value.nil?

  value
end

.require_option_value(flag, value) ⇒ Object



90
91
92
93
94
# File 'lib/pray/invocation.rb', line 90

def require_option_value(flag, value)
  raise Error.usage("#{flag} requires a value") if value.nil? || value.empty?

  value
end

.resolve_current_project(options = ResolveOptions.new) ⇒ Object



39
40
41
42
43
44
# File 'lib/pray/invocation.rb', line 39

def resolve_current_project(options = ResolveOptions.new)
  context = invocation_context
  resolved_options = options.dup
  resolved_options.environment ||= context.environment
  Resolve.resolve_project_in_context(context.manifest_path, context.project_root, resolved_options)
end

.resolve_current_project_with_git_refresh_fallback(options = ResolveOptions.new, allow_git_refresh_fallback: false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pray/invocation.rb', line 46

def resolve_current_project_with_git_refresh_fallback(options = ResolveOptions.new, allow_git_refresh_fallback: false)
  resolve_current_project(options)
rescue Error => error
  if allow_git_refresh_fallback &&
      !options.offline &&
      !options.refresh &&
      Resolve.resolution_may_benefit_from_git_source_refresh?(error)
    refreshed = options.dup
    refreshed.refresh = true
    resolve_current_project(refreshed)
  else
    raise
  end
end

.top_level_command?(token) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
# File 'lib/pray/invocation.rb', line 102

def top_level_command?(token)
  %w[
    manifest init prayer repo install add remove update unlock render plan apply verify drift
    format fmt package publish login serve confess list outdated explain vendor clean tree sync trust
    version -V --version -h --help help
  ].include?(token)
end