Class: Amp::Core::Support::RootedOpener

Inherits:
Object
  • Object
show all
Defined in:
lib/amp-core/support/rooted_opener.rb

Overview

This creates an opener that prepends a directory to all File opening, allowing repository formats to worry only about file paths relative to the repository root.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ RootedOpener

Creates a new opener with a root of base, and also set to open files in the .hg subdirectory. If you set .default = :open_file, it will no longer open files in the .hg subdir.

Parameters:

  • base (String)

    the root directory of the repository this opener will be used on



35
36
37
38
39
# File 'lib/amp-core/support/rooted_opener.rb', line 35

def initialize(base)
  @root        = File.expand_path base
  @create_mode = nil
  @default     = :open_hg
end

Instance Attribute Details

#create_modeObject

Returns the value of attribute create_mode.



23
24
25
# File 'lib/amp-core/support/rooted_opener.rb', line 23

def create_mode
  @create_mode
end

#defaultObject

Returns the value of attribute default.



24
25
26
# File 'lib/amp-core/support/rooted_opener.rb', line 24

def default
  @default
end

#rootObject (readonly) Also known as: base

Returns the value of attribute root.



22
23
24
# File 'lib/amp-core/support/rooted_opener.rb', line 22

def root
  @root
end

Instance Method Details

#join(file) ⇒ Object



93
94
95
# File 'lib/amp-core/support/rooted_opener.rb', line 93

def join(file)
  File.join(root, file)
end

#open(file, mode = 'r') { ... } ⇒ Object

Opens up the given file, exactly like you would do with File.open. The parameters are the same. Defaults to opening a file in the .hg/ folder, but if @default == :open_file, will open it from the working directory.

If the mode includes write privileges, then the write will use an atomic temporary file.

Parameters:

  • file (String)

    the path to the file to open

  • mode (String) (defaults to: 'r')

    the read/write mode to open with (standard C choices here)

Yields:

  • Can yield the opened file if the block form is used



85
86
87
88
89
90
91
# File 'lib/amp-core/support/rooted_opener.rb', line 85

def open(file, mode='r', &block)
  if @default == :open_file
    open_file file, mode, &block
  else
    open_hg file, mode, &block
  end
end

#open_file(file, mode = 'w') {|file| ... } ⇒ Object

Opens a file in the repository (not in .hg). Writes are done atomically, and reads are efficiently done with Kernel::open. THIS IS NOT open_up_file!!!

Parameters:

  • file (String)

    the file to open

  • mode (String) (defaults to: 'w')

    the mode with which to open the file (“w”, “r”, “a”, …)

Yields:

  • (file)

    code to run on the file

Yield Parameters:

  • file (File)

    the opened file



122
123
124
# File 'lib/amp-core/support/rooted_opener.rb', line 122

def open_file(file, mode='w', &block)
  open_up_file root, file, mode, &block
end

#open_hg(file, mode = 'w') {|file| ... } ⇒ Object

Opens a file in the .hg repository using @root. This method operates atomically, and ensures that the file is always closed after use. The temporary files (while being atomically written) are stored in “#@root/.hg”, and are deleted after use. If only a read is being done, it instead uses Kernel::open instead of File::amp_atomic_write.

Parameters:

  • file (String)

    the file to open

  • mode (String) (defaults to: 'w')

    the mode with which to open the file (“w”, “r”, “a”, …)

Yields:

  • (file)

    code to run on the file

Yield Parameters:

  • file (File)

    the opened file



109
110
111
# File 'lib/amp-core/support/rooted_opener.rb', line 109

def open_hg(file, mode='w', &block)
  open_up_file File.join(root, ".hg"), file, mode, &block
end

#open_up_file(dir, file, mode, &block) ⇒ Object

This does the actual opening of a file.

Parameters:

  • dir (String)

    This dir is where the temp file is made, but ALSO the parent dir of file

  • file (String)

    Just the file name. It must exist at “#dir/#file”



132
133
134
135
136
137
138
139
# File 'lib/amp-core/support/rooted_opener.rb', line 132

def open_up_file(dir, file, mode, &block)
  path = File.join dir, file
  if mode == 'r' # if we're doing a read, make this super snappy
    Kernel::open path, mode, &block
  else # we're doing a write
    File::amp_atomic_write path, mode, @create_mode, dir, &block
  end
end

#pathObject

Returns the path to the opener’s root.

Returns:

  • path to the opener’s root, as an absolute path.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/amp-core/support/rooted_opener.rb', line 45

def path
  case @default
  when :open_file
    "#{root}/"
  when :open_hg
    "#{root}/.hg/"
  when :open_git
    "#{root}/.git"
  else
    raise abort "Unknown opener format #{@default.inspect}"
  end
end

#read(file) ⇒ String Also known as: contents

Read the file passed in with mode ‘r’. Synonymous with File.open(file, ‘r’) {|f| f.read } and File.read(file)

Parameters:

  • file (String)

    the relative path to the file we’re opening

Returns:

  • (String)

    the contents of the file



65
66
67
68
69
# File 'lib/amp-core/support/rooted_opener.rb', line 65

def read(file)
  res = nil
  open(file, 'r') {|f| res = f.read }
  res
end