require 'cloud/projects' module Projects module Methods HVE_CONTENT_TYPE = 'application/vnd.perforce.project.hve.v1+json' HVE_ID = 'hveProject' # List HVE Projects as configured in the system. # # See the Appendix in the documentation for details on values. def list(details: false, extension: nil) return if extension and (extension != HVE_ID or extension != HVE_CONTENT_TYPE) if Cloud::Settings.cloud_enabled? projects_service = Cloud::Projects.new(env:@env) return projects_service.list(details: details) end project_dirs = list_project_names project_names = project_dirs.map { |d| File.basename(d) } if details project_names.map { |n| fetch_by_depot_dir_name(n) } else project_names.map { |n| encode_name(n) } end end def list_project_names if !Cloud::Settings.cloud_enabled? return list_project_names_onsite_impl else projects_service = Cloud::Projects.new(env:@env) return projects_service.list(false) end end def list_project_names_onsite_impl pattern = "#{hve_projects_path}/*" results = p4.run_dirs(pattern) results.map { |r| r['dir'] } end # The ID is a URL encoded version of the directory name under # HVE_PROJECTS_PATH. # # This will unencode the ID and fetch by name. def fetch(id) name = unencode_name(id) if !Cloud::Settings.cloud_enabled? return fetch_by_depot_dir_name(name) else projects_service = Cloud::Projects.new(env:@env) projects_service.fetch(id) end end # Returns the project's "details" based on the project name. # # No validation is done to ensure this directory actually exists in the # system. def fetch_by_depot_dir_name(name) return nil if env['hws_settings'].HVE_PROJECTS_PATH.nil? id = encode_name(name) { 'id': id, 'name': name, 'server': server_uri_for_id(id), HVE_ID => { 'depotPath': depot_path_for_name(name) } } end def encode_name(name) HWSStrings.component_encode(name) end def unencode_name(name) HWSStrings.component_decode(name) end def server_uri_for_id(id) "p4://#{userinfo}#{server}#{safe_hve_projects_path}/#{id}" end def depot_path_for_name(name) "#{hve_projects_path}/#{name}" end def hve_projects_path env['hws_settings'].HVE_PROJECTS_PATH || fail('HVE_PROJECTS_PATH not set') end def safe_hve_projects_path hve_projects_path.gsub('//', '/') end # For HVE Projects, it may be interesting to people to see various # connection settings for each server URL. def userinfo data = {} if env['hws_settings'].P4CHARSET data['P4CHARSET'] = env['hws_settings'].P4CHARSET end if data.keys.empty? '' else encoded_data = data.map {|k,v| "#{k}=#{v}"}.join(';') "#{encoded_data}@" end end def server return p4port if p4port.include?(':') host = p4host ? p4host : 'localhost' port = p4port "#{host}:#{port}" end def p4port env['hws_settings'].P4PORT || fail('P4PORT setting not available') end def p4host env['hws_settings'].P4HOST end end end