class Jekyll::Commands::NewSite

Public Class Methods

init_with_program(prog) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 6
def init_with_program(prog)
  prog.command(:"new-site") do |c|
    c.syntax      "new-site PATH"
    c.description "Creates a custom Jekyll site scaffold in PATH"

    c.option "classic", "--classic", "Classic Jekyll scaffolding"
    c.option "theme", "--theme GEM-NAME", "Scaffold with a custom gem-based theme"
    c.option "force", "--force", "Force creation even if PATH already exists"
    c.option "verbose", "--verbose", "Output messages while creating"

    c.action do |args, options|
      raise ArgumentError, "You must specify a path." if args.empty?
      process(args, options)
    end
  end
end
process(args, options = {}) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 23
def process(args, options = {})
  site_path = File.expand_path(args.join(" "), Dir.pwd)
  FileUtils.mkdir_p site_path
  if existing_source_location?(site_path, options)
    Jekyll.logger.abort_with "Conflict:", "#{site_path} exists and is not empty."
  end

  initialize_git site_path if git_installed?
  create_variables_from(args, options)

  create_site site_path, options
end

Private Class Methods

add_foundation_files(path) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 82
def add_foundation_files(path)
  print_header "Creating:", "Foundation files"
  process_template_for "Gemfile", site_template, path
  process_template_for "_config.yml", site_template, path
  verbose_print ""
end
bundle_extract(package) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 130
def bundle_extract(package)
  system("bundle", "exec", "jekyll+ extract #{package.join(" ")}")
end
bundle_install(path) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 162
def bundle_install(path)
  print_info "Jekyll+:", "Running bundle install in #{path.cyan}..."
  Dir.chdir(path) do
    process, output = Utils::Exec.run("bundle", "install")
    report = output.to_s.each_line.map(&:strip)
    print_info "Bundler:", report.first
    report[1..-1].each { |line| print_info "", line }
    raise SystemExit unless process.success?
  end
end
bundle_unless_theme_installed(path) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 151
def bundle_unless_theme_installed(path)
  print_info "Checking:", "Local theme installation..."
  Gem::Specification.find_by_name(@theme)
  theme_installed_msg
  print_info ""
rescue Gem::LoadError
  Jekyll.logger.error "Jekyll+:", "Theme #{@theme.inspect} could not be found."
  bundle_install path
  print_info ""
end
create_scaffold_at(path) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 89
def create_scaffold_at(path)
  print_header "Creating:", "Scaffold files"
  FileUtils.mkdir_p(File.expand_path("_posts", path))

  pages = %w(index.html about.md)
  pages << ".gitignore"
  pages.each do |page|
    write_file(page, erb_render("#{page}.erb", site_template), path)
  end
  write_file(welcome_post, erb_render(scaffold_path, site_template), path)
  verbose_print ""
end
create_site(path, options) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 67
def create_site(path, options)
  add_foundation_files path
  create_scaffold_at path

  if options["classic"]
    bundle_unless_theme_installed path
    extract_templates_and_config path
  elsif options["theme"]
    bundle_unless_theme_installed path
    extract_theme_config path
  end

  success_message path, options
end
create_variables_from(args, options) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 53
def create_variables_from(args, options)
  @verbose = options["verbose"]

  @theme = options["theme"] ? options["theme"] : "minima"
  @name  = user_name
  @email = user_email

  # extract capitalized blog title from the argument(s) when a 'path'
  # to the new site has been provided.
  #   e.g.  jekyll new work/blogs/exploring ruby would install a blog
  #   titled 'Exploring Ruby' at path ~/work/blogs/exploring ruby
  @title   = extract_title_from(args)
end
erb_render(filename, source) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 195
def erb_render(filename, source)
  ERB.new(
    File.read(File.expand_path(filename, source)), 0, "<>"
  ).result(binding)
end
existing_source_location?(path, options) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 215
def existing_source_location?(path, options)
  !options["force"] && !Dir["#{path}/**/*"].empty?
end
extract_templates_and_config(path) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 102
def extract_templates_and_config(path)
  print_header(
    "Extracting:",
    "Templates and _config.yml from #{@theme.cyan} if available..",
    "="
  )
  package = %w(_layouts _includes _sass _data assets _config.yml)
  package << extraction_opts

  Dir.chdir(path) do
    bundle_extract package
  end
end
extract_theme_config(path) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 116
def extract_theme_config(path)
  print_header(
    "Extracting:",
    "_config.yml from theme-gem if available..",
    "="
  )
  package = %w(_config.yml)
  package << extraction_opts

  Dir.chdir(path) do
    bundle_extract package
  end
end
extract_title_from(args) click to toggle source

join the arguments given, with a whitespace; replace backslashes, if any with a forward slash; split the string into an array again and select the last entry. Further split the entry along a single whitespace, and map to a new array after capitalizing the split-entries. Join them again with a whitespace to form the final title string.

# File lib/jekyll/commands/new_site.rb, line 48
def extract_title_from(args)
  a = args.join(" ").tr("\\", "/").split("/").last
  a.split.map(&:capitalize).join(" ")
end
extraction_opts() click to toggle source
# File lib/jekyll/commands/new_site.rb, line 134
def extraction_opts
  @verbose ? "--force --lax --verbose" : "--force --lax --quiet"
end
git_installed?() click to toggle source
# File lib/jekyll/commands/new_site.rb, line 221
def git_installed?
  process, _output = Utils::Exec.run("git", "--version")
  process.success?
end
initialize_git(path) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 226
def initialize_git(path)
  verbose_print "Initialising:", File.join(path, ".git")
  Dir.chdir(path) { `git init` }
end
print_header(topic, message, style = "-") click to toggle source
print_info(topic, message = "") click to toggle source
process_template_for(file, source, destination) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 180
def process_template_for(file, source, destination)
  verbose_print "", File.join(destination, file)
  File.open(File.join(destination, file), "w") do |f|
    f.write(
      erb_render("#{file}.erb", source)
    )
  end
end
scaffold_path() click to toggle source
# File lib/jekyll/commands/new_site.rb, line 209
def scaffold_path
  "_posts/0000-00-00-welcome-to-jekyll.md.erb"
end
site_template() click to toggle source
# File lib/jekyll/commands/new_site.rb, line 205
def site_template
  File.expand_path("../site_template", File.dirname(__FILE__))
end
success_message(path, options) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 138
def success_message(path, options)
  print_info ""
  if options["classic"]
    print_info "A classic-style jekyll site installed in #{path.cyan}."

  elsif options["theme"]
    print_info "New #{@theme.cyan} themed jekyll site installed in #{path.cyan}."

  else
    print_info "New jekyll site #{@title.cyan} installed in #{path.cyan}."
  end
end
theme_installed_msg() click to toggle source
# File lib/jekyll/commands/new_site.rb, line 173
def theme_installed_msg
  print_info "", "#{@theme.inspect} local installation found." \
                 " Bundle install skipped".green
end
user_email() click to toggle source
# File lib/jekyll/commands/new_site.rb, line 236
def user_email
  email ||= `git config user.email`.chomp
  email.empty? ? "your-email@domain.com" : email
end
user_name() click to toggle source
# File lib/jekyll/commands/new_site.rb, line 231
def user_name
  name ||= `git config user.name`.chomp
  name.empty? ? "Github User" : name
end
verbose_print(topic, message = "") click to toggle source

only with –verbose switch

# File lib/jekyll/commands/new_site.rb, line 248
def verbose_print(topic, message = "")
  if @verbose
    Jekyll.logger.info topic, message
  end
end
welcome_post() click to toggle source
# File lib/jekyll/commands/new_site.rb, line 201
def welcome_post
  "_posts/#{Time.now.strftime("%Y-%m-%d")}-welcome-to-jekyll.md"
end
write_file(filename, contents, path) click to toggle source
# File lib/jekyll/commands/new_site.rb, line 189
def write_file(filename, contents, path)
  full_path = File.expand_path(filename, path)
  verbose_print "", full_path
  File.write(full_path, contents)
end