Class: MultiRepo::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_repo/repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config: nil, dry_run: false) ⇒ Repo

Returns a new instance of Repo.



8
9
10
11
12
13
# File 'lib/multi_repo/repo.rb', line 8

def initialize(name, config: nil, dry_run: false)
  @name    = name
  @dry_run = dry_run
  @config  = OpenStruct.new(config || {})
  @path    = MultiRepo.repos_dir.join(name)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/multi_repo/repo.rb', line 5

def config
  @config
end

#dry_runObject

Returns the value of attribute dry_run.



6
7
8
# File 'lib/multi_repo/repo.rb', line 6

def dry_run
  @dry_run
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/multi_repo/repo.rb', line 5

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/multi_repo/repo.rb', line 5

def path
  @path
end

Instance Method Details

#chdirObject



21
22
23
24
# File 'lib/multi_repo/repo.rb', line 21

def chdir
  git # Ensures the clone exists
  Dir.chdir(path) { yield }
end

#detect_readme_fileObject



48
49
50
51
52
53
54
# File 'lib/multi_repo/repo.rb', line 48

def detect_readme_file
  chdir do
    %w[README.md README README.txt].detect do |f|
      File.exist?(f)
    end
  end
end

#gitObject



17
18
19
# File 'lib/multi_repo/repo.rb', line 17

def git
  @git ||= MultiRepo::Service::Git.new(path: path, clone_source: config.clone_source || "[email protected]:#{name}.git", dry_run: dry_run)
end

#rm_file(file) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/multi_repo/repo.rb', line 38

def rm_file(file)
  return unless path.join(file).exist?

  if dry_run
    puts "** dry-run: Removing #{path.join(file).expand_path}".light_black
  else
    chdir { FileUtils.rm_f(file) }
  end
end

#short_nameObject



26
27
28
# File 'lib/multi_repo/repo.rb', line 26

def short_name
  name.split("/").last
end

#write_file(file, content, **kwargs) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/multi_repo/repo.rb', line 30

def write_file(file, content, **kwargs)
  if dry_run
    puts "** dry-run: Writing #{path.join(file).expand_path}".light_black
  else
    chdir { File.write(file, content) }
  end
end