module SourceTools

Constants

VERSION

Public Instance Methods

each_source_path() { |path| ... } click to toggle source
# File lib/source-tools.rb, line 58
def each_source_path
  require 'pathname'

  Pathname.getwd.find{ |path|
    # skip unreadable, unwritable, .git and .svn directories
    skip_dir = path.directory? && (!path.readable? || !path.writable?)
    Find.prune if skip_dir || %w[.git .svn].include?(path.basename.to_s)

    # skip non-files, zero-sized files, files not matching specific names, or files without the matching extensions
    match = files.include?(path.basename.to_s) || extensions.include?(path.extname[1..-1])
    next unless path.file? && path.size? && match && path.readable? && path.writable?

    yield(path)
  }

end
extensions() click to toggle source
# File lib/source-tools.rb, line 82
def extensions
  source_codes = %w[ rb ru rake sake php pl py js sh c cpp cxx h hpp hs ml java cs d y as ]
  templates = %w[ haml builder erb eruby rxml rhtml rjs ratom rcsv ]
  markup = %w[ css htm html xml rdf yml yaml ]
  others = %w[ cgi fcgi conf deploy example htc key opts rpdf sql txt vcf log ]

  (source_codes + templates + markup + others)
end
files() click to toggle source
# File lib/source-tools.rb, line 75
def files
  # files and extensions to process
  %w[ CHANGELOG HISTORY MIT-LICENSE LICENSE README README_FOR_APP
      RUNNING_UNIT_TESTS TODO USAGE INSTALL NOTICE .autotest .gitignore
      Makefile Rakefile capfile ]
end
generate(path, args = [], task_args = nil, &block) click to toggle source
# File lib/source-tools.rb, line 15
def generate path, args = [], task_args = nil, &block
  require 'fileutils'

  args.each{ |arg|
    if task_args[arg].nil?
      puts 'please fill your arguments like:'
      puts "  > source-tools st:t:#{File.basename(path)}[#{args.join(',').upcase}]"
      exit(1)
    end
  }

  if File.exist?(path)
    puts "#{path} exists."
    exit(1)
  end

  FileUtils.mkdir_p(File.dirname(path))
  content = ''
  File.open(path, 'w'){ |file|
    require 'erb'
    args = task_args.to_hash
    block.call(args) if block_given?

    directory = "#{File.dirname(__FILE__)}/source-tools/templates/"
    template  = "t#{path}.erb"

    content = File.read(directory + template)
    file << ERB.new(content).result(binding)
  }
  if content =~ /\A#!/
    puts "chmod #{path} to 755"
    File.chmod(0755, path)
  end

  puts "#{path} generated."
end
glob(glob_str) { |path| ... } click to toggle source
# File lib/source-tools.rb, line 91
def glob glob_str
  if glob_str.nil?
    puts 'Please provide a glob to indicate which files you want to append the comment'
    exit(1)
  end

  require 'pathname'
  Pathname.glob(glob_str).each{ |path|
    next unless path.writable? && !path.directory?
    yield(path)
  }
end
strip(file, spaces = ' ') click to toggle source
# File lib/source-tools.rb, line 5
def strip file, spaces = '  '
  strip_utf8_bom(
    file.map{ |line| line.gsub("\t", spaces).rstrip }.join("\n") + "\n"
  )
end
strip_utf8_bom(str) click to toggle source
# File lib/source-tools.rb, line 11
def strip_utf8_bom str
  str[0..2] == "\xEF\xBB\xBF" ? str[3..-1] : str
end
task_template(path, *args, &block) click to toggle source
# File lib/source-tools.rb, line 52
def task_template path, *args, &block
  task(*[File.basename(path)] + args) do |t, task_args|
    SourceTools.generate(path, args, task_args, &block)
  end
end
wrap_source(path, content, args = {}) click to toggle source
# File lib/source-tools.rb, line 104
def wrap_source path, content, args = {}
  path.open('w'){ |f|
    f.puts(args[:header]) if args[:header]
    f.print(content)
    f.puts(args[:footer]) if args[:footer]
  }
end