Sha256: 7fcc1c2592b843d042d06a9594b0f02ddd8636e509a7f7bb79ef8acf786cbcef

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

#This class generates shell scripts from a configuration.
class Lightning
  class Generator
    class<<self
    def generate_completions(generated_file=nil)
      generated_file ||= Lightning.config[:generated_file]
      output = generate(Lightning.config[:shell], Lightning.config[:commands])
      File.open(generated_file, 'w'){|f| f.write(output) }
      output
    end
          
    def generate(*args)
      shell = args.shift
      send("#{shell}_generator", *args)
    end
    
    def bash_generator(commands)
      body = <<-INIT
      #### This file was generated by Lightning. ####
      #LBIN_PATH="$PWD/bin/" #only use for development
      LBIN_PATH="" 
      
      INIT
      commands.each do |e|
        body += <<-EOS
          
          #{'#' + e['description'] if e['description']}
          #{e['name']} () {
            if [ -z "$1" ]; then
              echo "No arguments given"
              return
            fi
            FULL_PATH="`${LBIN_PATH}lightning-full_path #{e['name']} $@`#{e['post_path'] if e['post_path']}"
            if [ $1 == '#{Lightning::TEST_FLAG}' ]; then
              CMD="#{e['map_to']} '$FULL_PATH'#{' '+ e['add_to_command'] if e['add_to_command']}"
              echo $CMD
            else
              #{e['map_to']} "$FULL_PATH"#{' '+ e['add_to_command'] if e['add_to_command']}
            fi
          }
          complete -o default -C "${LBIN_PATH}lightning-complete #{e['name']}" #{e['name']}
        EOS
      end
      body.gsub(/^\s{6,10}/, '')
    end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
cldwalker-lightning-0.1.2 lib/lightning/generator.rb
cldwalker-lightning-0.2.0 lib/lightning/generator.rb
lightning-0.2.1 lib/lightning/generator.rb