Module: Geb::Site::Build

Included in:
Geb::Site
Defined in:
lib/geb/site/build.rb

Defined Under Namespace

Classes: FailedToOutputSite, SiteNotLoadedError

Instance Method Summary collapse

Instance Method Details

#buildObject

build the site

Raises:

  • SiteNotLoaded if the site is not loaded



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/geb/site/build.rb', line 28

def build

  # make sure the site is laoded, if not, raise an error
  raise SiteNotLoadedError.new("Could not build the site.") unless @loaded

  # build the assets and pages
  # it is important to build pages first as there may be pages in the assets directory
  build_pages()
  build_assets()

end

#build_assetsObject

build the assets for the site

Raises:

  • SiteNotLoaded if the site is not loaded



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/geb/site/build.rb', line 98

def build_assets

  # make sure the site is laoded, if not, raise an error
  raise SiteNotLoadedError.new("Could not build assets.") unless @loaded

  # get the destination directory for the site output, depending on the release flag
  destination_directory = @releasing ? get_site_release_output_directory() : get_site_local_output_directory()

  # get the site asset and output assets directory
  site_assets_dir = get_site_assets_directory()
  output_assets_dir = File.join(destination_directory, site_assets_dir.gsub(@site_path, ""))

  Geb.log "Building #{site_name} assets #{@releasing ? 'for release' : 'locally'}\n\n"

  # step through all the asset files and copy them to the output directory
  Dir.glob("#{site_assets_dir}/**/*").each do |asset_file|

    # skip directories
    next if File.directory?(asset_file)

    # get the relative path of the asset file and the destination path
    asset_relative_path = asset_file.gsub(site_assets_dir, "")
    asset_full_destination_path = File.join(output_assets_dir, asset_relative_path)

    # check if the destination asset file exists
    if File.exist?(asset_full_destination_path)

      Geb.log " - skipping asset: #{asset_relative_path}"

    else

      Geb.log " - processing asset: #{asset_relative_path}"

      # create the output directory for the asset file
      output_dir = File.join(output_assets_dir, File.dirname(asset_relative_path))
      FileUtils.mkdir_p(output_dir)

      # copy the asset file to the output directory
      FileUtils.cp(asset_file, output_dir)

    end # if else

  end # Dir.glob
  Geb.log "\nDone building assets for #{site_name}"

end

#build_pagesObject

build the pages for the site

Raises:

  • SiteNotLoaded if the site is not loaded



42
43
44
45
46
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
91
92
93
94
# File 'lib/geb/site/build.rb', line 42

def build_pages

  # make sure the site is laoded, if not, raise an error
  raise SiteNotLoadedError.new("Could not build pages.") unless @loaded

  # expire page template and partial caches
  Geb::Template.expire_cache
  Geb::Partial.expire_cache

  # find all pages to build
  page_files = get_page_files(@site_path,
                              @site_config.page_extensions(),
                              @site_config.template_and_partial_identifier(),
                              [get_site_local_output_directory(), get_site_release_output_directory()])

  Geb.log "Building #{page_files.length} #{site_name} pages #{@releasing ? 'for release' : 'locally'}"

  # create a temporary directory
  Dir.mktmpdir do |tmp_dir|

    # iterate over the HTML files and build the pages
    page_files.each do |page_file|

      # create a new page object and buid the page into the temporary directory
      page = Geb::Page.new(self, page_file)
      page.build(tmp_dir)

    end # html_files.each
    Geb.log "\nDone building #{page_files.length} pages for #{site_name}"

    # attempt to write the site to the output directory
    begin

      # get the destination directory for the site output, depending on the release flag
      destination_directory = @releasing ? get_site_release_output_directory() : get_site_local_output_directory()

      # clear the output directory
      Geb.log_start "Clearing site output folder #{destination_directory} ... "
      clear_site_output_directory(destination_directory)
      Geb.log "done."

      # copy the files to the output directory
      Geb.log_start "Outputting site to #{destination_directory} ... "
      output_site(tmp_dir, destination_directory)
      Geb.log "done."

    rescue => e
      raise FailedToOutputSite.new(e.message)
    end # begin rescue

  end # Dir.mktmpdir

end

#clear_site_output_directory(output_directory) ⇒ Nil

clear the site output directory

Parameters:

  • output_directory (String)

    the output directory to clear

Returns:

  • (Nil)


180
181
182
# File 'lib/geb/site/build.rb', line 180

def clear_site_output_directory(output_directory)
  FileUtils.rm_rf(Dir.glob("#{output_directory}/*"))
end

#get_page_files(path, exts = Geb::Defaults::PAGE_EXTENSIONS, ignore_files_exp = Geb::Defaults::TEMPLATE_AND_PARTIAL_IDENTIFIER, ignore_directories = []) ⇒ Array

Note:

the ignore_files_exp and ignore_directories are used to ignore files that are not pages

get the page files in the specified path, with specified extentions and ignoring files that match the pattern

Parameters:

  • path (String)

    the path to the files

  • exts (Array) (defaults to: Geb::Defaults::PAGE_EXTENSIONS)

    the extentions to look for, default is Geb::Defaults.PAGE_EXTENSIONS

  • ignore_files_exp (Regexp) (defaults to: Geb::Defaults::TEMPLATE_AND_PARTIAL_IDENTIFIER)

    the pattern to ignore files, default is Geb::Defaults.TEMPLATE_AND_PARTIAL_IDENTIFIER

  • ignore_directories (Array) (defaults to: [])

    the directories to ignore, default is []

Returns:

  • (Array)

    the array of matched file paths



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/geb/site/build.rb', line 158

def get_page_files(path, exts = Geb::Defaults::PAGE_EXTENSIONS, ignore_files_exp = Geb::Defaults::TEMPLATE_AND_PARTIAL_IDENTIFIER, ignore_directories = [])

  # make sure every page extention specified starts with a dot
  exts.map! { |ext| ext.start_with?('.') ? ext : ".#{ext}" }

  # get all files in the path with the specified extentions
  files = Dir.glob("#{path}/**/*{#{exts.join(',')}}")

  # reject files that match the ignore pattern and that are within the output or release directories
  files.reject! do |file|
    File.basename(file) =~ ignore_files_exp ||
    ignore_directories.any? { |dir| file.start_with?(dir) }
  end # files.reject!

  # return the array of matched file paths
  return files

end

#get_site_assets_directoryString

get the site assets directory

Returns:

  • (String)

    the site assets directory



195
196
197
# File 'lib/geb/site/build.rb', line 195

def get_site_assets_directory
  return File.join(@site_path, @site_config.assets_dir)
end

#get_site_local_output_directoryString

get the site local output directory

Returns:

  • (String)

    the site output directory



147
148
149
# File 'lib/geb/site/build.rb', line 147

def get_site_local_output_directory
  return File.join(@site_path, @site_config.output_dir, Geb::Defaults::LOCAL_OUTPUT_DIR)
end

#output_site(source_dir, output_dir) ⇒ Nil

output the site from specified directory to the output directory. The specified directory is typically a temporary directory where the site has been built.

Parameters:

  • source_dir (String)

    the source directory

  • output_dir (String)

    the output directory

Returns:

  • (Nil)


189
190
191
# File 'lib/geb/site/build.rb', line 189

def output_site(source_dir, output_dir)
  FileUtils.cp_r("#{source_dir}/.", output_dir)
end