Class: Jekyll::ThemeGemExtractor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, options) ⇒ ThemeGemExtractor

Returns a new instance of ThemeGemExtractor.



7
8
9
10
11
# File 'lib/theme_gem_extractor.rb', line 7

def initialize(site, options)
  @site = site
  @options = options
  @theme_root = site.in_theme_dir("/")
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#siteObject (readonly)

Returns the value of attribute site.



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

def site
  @site
end

#theme_rootObject (readonly)

Returns the value of attribute theme_root.



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

def theme_root
  @theme_root
end

Instance Method Details

#already_exists_msg(file) ⇒ Object



96
97
98
99
# File 'lib/theme_gem_extractor.rb', line 96

def already_exists_msg(file)
  Jekyll.logger.warn "Error:", "'#{relative_path(file)}' already " \
    "exists at destination. Use --force to overwrite."
end

#directory_listing(path) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/theme_gem_extractor.rb', line 75

def directory_listing(path)
  Jekyll.logger.info "Listing:",
    "Contents of '#{relative_path(path)}' in theme gem..."

  files_in(path).each do |file|
    Jekyll.logger.info "", "  * #{relative_path(file)}"
  end
end

#extract(path) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/theme_gem_extractor.rb', line 13

def extract(path)
  file_path = site.in_theme_dir path
  unless File.exist?(file_path)
    puts ""
    raise ArgumentError,
      "Specified path #{path.yellow} doesn't exist in the theme-gem."
  end
  extract_to_source file_path
end

#extract_contents(source, destination) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/theme_gem_extractor.rb', line 42

def extract_contents(source, destination)
  FileUtils.cp_r "#{source}/.", destination
  files_in(source).each do |file|
    extraction_msg file
  end
rescue Errno::ENOENT
  FileUtils.mkdir_p destination
  retry
end

#extract_directory_contents(path) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/theme_gem_extractor.rb', line 33

def extract_directory_contents(path)
  destination = site.in_source_dir relative_path(path)
  if File.exist?(destination) && !options["force"]
    already_exists_msg path
  else
    extract_contents path, destination
  end
end

#extract_file_with_directory(file_path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/theme_gem_extractor.rb', line 52

def extract_file_with_directory(file_path)
  file = file_path.split("/").last
  dir_path = File.dirname(
    site.in_source_dir(relative_path(file_path))
  )
  FileUtils.mkdir_p dir_path

  if File.exist?(File.join(dir_path, file)) && !options["force"]
    already_exists_msg file_path
  else
    FileUtils.cp_r file_path, dir_path
    extraction_msg file_path
  end
end

#extract_to_source(path) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/theme_gem_extractor.rb', line 23

def extract_to_source(path)
  return list_contents(path) if options["show"]

  if File.directory? path
    extract_directory_contents path
  else
    extract_file_with_directory path
  end
end

#extraction_msg(file) ⇒ Object



92
93
94
# File 'lib/theme_gem_extractor.rb', line 92

def extraction_msg(file)
  Jekyll.logger.info "Extract:", relative_path(file)
end

#files_in(dir_path) ⇒ Object



84
85
86
# File 'lib/theme_gem_extractor.rb', line 84

def files_in(dir_path)
  Dir["#{dir_path}/**/*"].reject { |d| File.directory? d }
end

#list_contents(path) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/theme_gem_extractor.rb', line 67

def list_contents(path)
  if File.directory? path
    directory_listing path
  else
    Jekyll.logger.warn "", "The --show switch only works for directories"
  end
end

#relative_path(path) ⇒ Object



88
89
90
# File 'lib/theme_gem_extractor.rb', line 88

def relative_path(path)
  path.sub theme_root, ""
end