Module: SourceTools

Defined in:
lib/source-tools.rb,
lib/source-tools/version.rb

Constant Summary collapse

VERSION =
'0.6.1'

Class Method Summary collapse

Class Method Details

.each_source_pathObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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

.extensionsObject



82
83
84
85
86
87
88
89
# 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

.filesObject



75
76
77
78
79
80
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# 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 = ' ') ⇒ Object



5
6
7
8
9
# 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) ⇒ Object



11
12
13
# 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) ⇒ Object



52
53
54
55
56
# 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 = {}) ⇒ Object



104
105
106
107
108
109
110
# 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