@@ -60,6 +60,21 @@ def omnibus_install?
6060 File . exist? ( expected_omnibus_root ) && File . exist? ( File . join ( expected_omnibus_root , "version-manifest.json" ) )
6161 end
6262
63+ # The habitat version of the chef-cli can be installed with standalone or chef-development-kit-enterprise
64+ # This method checks if the habitat version of chef-cli is installed as standalone
65+ def habitat_standalone?
66+ @hab_standalone ||= ( hab_pkg_installed? ( ChefCLI ::Dist ::HAB_PKG_NAME ) && !habitat_chef_dke? )
67+ end
68+
69+ # This method checks if the habitat version of chef-cli is installed with chef-development-kit-enterprise
70+ def habitat_chef_dke?
71+ @hab_dke ||= hab_pkg_installed? ( ChefCLI ::Dist ::CHEF_DKE_PKG_NAME )
72+ end
73+
74+ def habitat_install?
75+ habitat_chef_dke? || habitat_standalone?
76+ end
77+
6378 def omnibus_root
6479 @omnibus_root ||= omnibus_expand_path ( expected_omnibus_root )
6580 end
@@ -110,29 +125,30 @@ def git_windows_bin_dir
110125 @git_windows_bin_dir ||= File . expand_path ( File . join ( omnibus_root , "embedded" , "git" , "usr" , "bin" ) )
111126 end
112127
113- # #
114- # # environment vars for habitat
115- # #
116- # def habitat_env
117- # @habitat_env ||=
118- # begin
119- # # Define the necessary paths for the Habitat environment
120- # # Custom GEM_HOME within Habitat
121- # pkg_prefix = get_pkg_prefix
122- # vendor_dir = File.join(pkg_prefix, "vendor")
123- # path = [
124- # File.join(pkg_prefix, "bin"),
125- # ENV["PATH"].split(File::PATH_SEPARATOR), # Preserve existing PATH
126- # ].flatten.uniq
127-
128- # {
129- # "PATH" => path.join(File::PATH_SEPARATOR),
130- # "GEM_ROOT" => Gem.default_dir, # Default directory for gems
131- # "GEM_HOME" => vendor_dir, # GEM_HOME pointing to the vendor directory
132- # "GEM_PATH" => vendor_dir, # GEM_PATH also pointing to the vendor directory
133- # }
134- # end
135- # end
128+ #
129+ # environment vars for habitat
130+ #
131+ def habitat_env
132+ @habitat_env ||=
133+ begin
134+ # Define the necessary paths for the Habitat environment
135+ # If it is a chef-dke installation, we will use the chef-dke bin path.
136+ # Otherwise, we will use the chef-cli bin path.
137+ bin_pkg_prefix = get_pkg_prefix ( habitat_chef_dke? ? ChefCLI ::Dist ::CHEF_DKE_PKG_NAME : ChefCLI ::Dist ::HAB_PKG_NAME )
138+ vendor_dir = File . join ( get_pkg_prefix ( ChefCLI ::Dist ::HAB_PKG_NAME ) , "vendor" )
139+ path = [
140+ File . join ( bin_pkg_prefix , "bin" ) ,
141+ ENV [ "PATH" ] . split ( File ::PATH_SEPARATOR ) , # Preserve existing PATH
142+ ] . flatten . uniq
143+
144+ {
145+ "PATH" => path . join ( File ::PATH_SEPARATOR ) ,
146+ "GEM_ROOT" => Gem . default_dir , # Default directory for gems
147+ "GEM_HOME" => vendor_dir , # GEM_HOME pointing to the vendor directory
148+ "GEM_PATH" => vendor_dir , # GEM_PATH also pointing to the vendor directory
149+ }
150+ end
151+ end
136152
137153 #
138154 # environment vars for omnibus
@@ -153,17 +169,15 @@ def omnibus_env
153169 end
154170 end
155171
156- # def get_pkg_prefix
157- # pkg_origin = "chef"
158- # pkg_name = "#{pkg_origin}/chef-cli" # Your origin and package name
159- # path = `hab pkg path #{pkg_name}`.strip
172+ def get_pkg_prefix ( pkg_name )
173+ path = `hab pkg path #{ pkg_name } ` . strip
160174
161- # if $?.success? && !path.empty?
162- # path
163- # else
164- # raise "Failed to get pkg_prefix for #{pkg_name}: #{path}"
165- # end
166- # end
175+ if $?. success? && !path . empty?
176+ path
177+ else
178+ raise "Failed to get pkg_prefix for #{ pkg_name } : #{ path } "
179+ end
180+ end
167181
168182 def omnibus_expand_path ( *paths )
169183 dir = File . expand_path ( File . join ( paths ) )
@@ -209,5 +223,14 @@ def reset!
209223 def macos?
210224 !!( RUBY_PLATFORM =~ /darwin/ )
211225 end
226+
227+ # @return [Boolean] Checks if a habitat package is installed.
228+ # If habitat itself is not installed, this method will return false.
229+ #
230+ # @api private
231+ #
232+ def hab_pkg_installed? ( pkg_name )
233+ `hab pkg list #{ pkg_name } 2>/dev/null` . include? ( pkg_name ) rescue false
234+ end
212235 end
213236end
0 commit comments