#!/usr/bin/env ruby require 'getoptlong' require File.expand_path('../lib/nesta/commands', File.dirname(__FILE__)) module Nesta class Cli def self.usage(exitcode=1) puts < [COMMAND OPTIONS] GLOBAL OPTIONS --help, -h Display this message. --version, -v Display version number. --bash-completion Output command completion setup for Bash. --zsh-completion Output command completion setup for Zsh. COMMANDS new Create a new Nesta project. edit Edit a document (path relative to content/pages). demo:content Install example pages in ./content-demo. plugin:create Create a gem called nesta-plugin-name. theme:install Install a theme from a git repository. theme:enable Make the theme active, updating config.yml. theme:create Makes a template for a new theme in ./themes. build [path] Build static copy of site (path defaults to ./dist) OPTIONS FOR new --git Create a new git repository for the project. --vlad Include config/deploy.rb. EOF exit exitcode end def self.version puts "Nesta #{Nesta::VERSION}" exit 0 end def self.parse_command_line opts = GetoptLong.new( ['--bash-completion', GetoptLong::NO_ARGUMENT], ['--domain', GetoptLong::REQUIRED_ARGUMENT], ['--git', GetoptLong::NO_ARGUMENT], ['--help', '-h', GetoptLong::NO_ARGUMENT], ['--version', '-v', GetoptLong::NO_ARGUMENT], ['--vlad', GetoptLong::NO_ARGUMENT], ['--zsh-completion', GetoptLong::NO_ARGUMENT] ) options = {} opts.each do |opt, arg| case opt when '--bash-completion' bash_completion when '--help' usage(exitcode=0) when '--version' version when '--zsh-completion' zsh_completion else options[opt.sub(/^--/, '')] = arg end end options rescue GetoptLong::InvalidOption => e $stderr.puts usage end def self.require_bundled_gems if File.exist?('Gemfile') require 'bundler' Bundler.setup(:default) Bundler.require(:default) end end def self.class_for_command(command) const_names = command.split(':').map { |word| word.capitalize } const_names.inject(Nesta::Commands) do |namespace, const| namespace.const_get(const) end end def self.main(options) command = ARGV.shift command.nil? && usage begin command_cls = class_for_command(command) rescue NameError usage else command_cls.new(ARGV[0], options).execute(Nesta::SystemCommand.new) end rescue RuntimeError => e $stderr.puts "ERROR: #{e}" exit 1 rescue Nesta::Commands::UsageError => e $stderr.puts "ERROR: #{e}" usage end def self.bash_completion puts <<-EOF # Bash completion configuration for the nesta command. # # Install it with these commands: # # $ nesta --bash-completion > ~/.nesta-completion.sh # $ echo '[ -f ~/.nesta-completion.sh ] && source ~/.nesta-completion.sh' \ # >> ~/.bash_profile # $ source ~/.nesta-completion.sh _nesta() { local cur prev opts pages COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" opts="new 'demo:content' edit 'plugin:create' 'theme:install' 'theme:enable' 'theme:create' build" case "${cur}" in theme:*) COMPREPLY=( $(compgen -W "enable install" -- ${cur##*:}) ) return 0 ;; esac case "${prev}" in edit) local pages_path="content/pages" local pages="$(find $pages_path -type f | sed s,$pages_path/,,)" COMPREPLY=( $(compgen -W "$pages" -- $cur) ) return 0 ;; *) ;; esac COMPREPLY=( $(compgen -W "$opts" -- $cur) ) return 0 } complete -F _nesta nesta EOF exit 0 end def self.zsh_completion puts <<-EOF # Zsh completion configuration for the nesta command. # # Install it with these commands: # # $ nesta --zsh-completion > /nesta-completion.sh # # Source form somewhere in your $fpath #compdef nesta _nesta_content() { content=(`ls content/pages/**/*`) } local -a _1st_arguments _1st_arguments=( 'new:Create a new Nesta project' 'edit:Edit a document' 'demo\:content:Install example pages in ./content-demo' 'theme\:install:Install a theme from a git repository' 'theme\:enable:Make the theme active, updating config.yml' 'theme\:create:Makes a template for a new theme in ./themes' 'build\:Build static copy of site' ) local expl local -a content _arguments \ '(-h)'{-h,--help}'[Display usage]' \ '(-v)'{-v,--version}'[Display Nesta version]' \ '(--bash-completion)--bash-completion[Output command for Bash completion]' \ '(--zsh-completion)--zsh-completion[Output command for Zsh completion]' \ '*:: :->subcmds' && return 0 if (( CURRENT == 1 )); then _describe -t commands "nesta subcommand" _1st_arguments return fi case "$words[1]" in edit) _nesta_content _wanted content expl 'all content' compadd -a content ;; esac EOF exit 0 end end end Nesta::Cli.require_bundled_gems Nesta::Plugin.initialize_plugins Nesta::Cli.main(Nesta::Cli.parse_command_line)