Class: HelixVersioningEngine::FileService

Inherits:
Object
  • Object
show all
Defined in:
lib/helix_versioning_engine/file_service.rb

Overview

Used to obtain consistent output to “files” commands and return output in our model format for consumption by other services.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (FileService) initialize(p4: nil)

Returns a new instance of FileService



11
12
13
# File 'lib/helix_versioning_engine/file_service.rb', line 11

def initialize(p4: nil)
  @p4 = p4
end

Instance Attribute Details

- (Object) p4 (readonly)

Returns the value of attribute p4



9
10
11
# File 'lib/helix_versioning_engine/file_service.rb', line 9

def p4
  @p4
end

Instance Method Details

- (Object) fetch_offset



111
112
113
114
# File 'lib/helix_versioning_engine/file_service.rb', line 111

def fetch_offset
  results = p4.run_info
  Util.p4_date_offset(results[0]['serverDate'])
end

- (Object) list(dirs)



15
16
17
18
19
20
21
# File 'lib/helix_versioning_engine/file_service.rb', line 15

def list(dirs)
  if dirs.nil? || dirs.empty?
    list_depots
  else
    list_dir(dirs)
  end
end

- (Object) list_depots



23
24
25
26
27
# File 'lib/helix_versioning_engine/file_service.rb', line 23

def list_depots
  results = p4.run_depots
  normalize_depots(results)
  results.map{ |r| HelixWebServicesClient::Models::Depot.new(r) }
end

- (Object) list_dir(dirs)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
89
90
# File 'lib/helix_versioning_engine/file_service.rb', line 47

def list_dir(dirs)
  file_selector = '//' + dirs.join('/')
  files_results = nil
  p4.at_exception_level(P4::RAISE_NONE) do
    files_results = p4.run_files(file_selector)
  end
  files_results = [] unless files_results

  return_val = nil

  if files_results.length == 1 &&
      files_results.first.key?('depotFile') &&
      files_results.first['depotFile'] == file_selector
    # Treat request like a single file GET
    normalize_files(files_results)
    results = files_results[0]

    print_results = p4.run_print(file_selector)

    results['Content'] = Base64.encode64(print_results[1])

    return_val = HelixWebServicesClient::Models::File.new(results)
  else

    # Treat request like a directory list
    selector = '//' + dirs.join('/') + '/*'

    files_results = p4.run_files('-e', selector)
    normalize_files(files_results)

    items = files_results.map { |f| HelixWebServicesClient::Models::File.new(f) }

    dirs_results = p4.run_dirs(selector)
    normalize_dirs(dirs_results)

    dirs = dirs_results.map { |d| HelixWebServicesClient::Models::Dir.new(d) }

    items.concat(dirs)

    return_val = items
  end

  return_val
end

- (Object) list_file(file_path)



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/helix_versioning_engine/file_service.rb', line 29

def list_file(file_path)
  files_results = nil
  p4.at_exception_level(P4::RAISE_NONE) do
    files_results = p4.run_files(file_path)
  end

  normalize_files(files_results)
  results = files_results[0]

  return nil unless results

  print_results = p4.run_print(file_path)

  results['Content'] = Base64.encode64(print_results[1])

  HelixWebServicesClient::Models::File.new(results)
end

- (Object) normalize_depots(results)



92
93
94
95
# File 'lib/helix_versioning_engine/file_service.rb', line 92

def normalize_depots(results)
  @normalize_depots ||= Util.normalizer('depots', offset)
  @normalize_depots.call(results)
end

- (Object) normalize_dirs(results)



102
103
104
105
# File 'lib/helix_versioning_engine/file_service.rb', line 102

def normalize_dirs(results)
  @normalize_dirs ||= Util.normalizer('dirs', offset)
  @normalize_dirs.call(results)
end

- (Object) normalize_files(results)



97
98
99
100
# File 'lib/helix_versioning_engine/file_service.rb', line 97

def normalize_files(results)
  @normalize_files ||= Util.normalizer('files', offset)
  @normalize_files.call(results)
end

- (Object) offset



107
108
109
# File 'lib/helix_versioning_engine/file_service.rb', line 107

def offset
  @offset ||= fetch_offset
end