lib/jekyll_push/site.rb in jekyll_push-0.1.0 vs lib/jekyll_push/site.rb in jekyll_push-0.1.1

- old
+ new

@@ -2,40 +2,48 @@ module JekyllPush # # class Site - attr_reader :baseurl, :dir + attr_reader :baseurl, :repo, :dir # - # + # @param config [Hash] a site configuration hash def initialize(config = nil) - @config = config || config_from_file - @baseurl = @config.fetch 'baseurl', '' - @dir = compiled_site_dir + @config = config || JekyllPush.config_from_file + @baseurl = @config.fetch :baseurl, '' + @repo = @config.fetch :repo_name, '' + @dir = site_dir + rescue TypeError => e + raise JekyllPush::Error::InvalidConfig, "An invalid (non-Hash) config was provided.\n#{e}" end - # - # - def config_from_file - YAML.load_file "#{`pwd`.strip}/_config.yml" + # @return [String] + def site_dir + File.join `pwd`.strip, '_site' end + # Use the repostory name as the baseurl when + # publishing to GitHub pages if available # - # - def compiled_site_dir - File.join `pwd`.strip, '_site' + # @return [String] + def gh_baseurl + if @repo.empty? + warn Rainbow("Warning: Building the site without the 'repo_name' baseurl is not recommended when using GitHub pages.").yellow if @baseurl.empty? + @baseurl + else + puts Rainbow("Rebuilding with baseurl '/#{@repo}'").cyan + "/#{@repo}" + end end - # Rebuild the Jekyll site with @baseurl + # Rebuild the Jekyll site # @return [Nil] - def rebuild + def rebuild(target) + @baseurl = gh_baseurl if target == 'gh-pages' + FileUtils.rm_r @dir if File.directory? @dir - opts = { - destination: @dir, - baseurl: @baseurl - } - opts['source'] = @config.dig 'source_dir' if @config.key? 'source' - Jekyll::Site.new(Jekyll.configuration(opts)).process + opts = Jekyll.configuration destination: @dir, baseurl: @baseurl + Jekyll::Site.new(opts).process end end end