wtnabe (owner)

Revisions

gist: 213791 Download_button fork
public
Public Clone URL: git://gist.github.com/213791.git
Embed All Files: show embed
simple_webtasks_rakefile.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# -*- mode: ruby; coding: utf-8 -*-
 
#
# Require:
# * xoxo gem
# * lftp binary
# * linkchecker binary
# * html tidy binary
# * svn or hg or git
#
 
require 'yaml'
require 'open3'
require 'erb'
require 'pp'
require 'fileutils'
include FileUtils::Verbose
 
module SCM
  %w( svn hg git ).each { |scm|
    define_method( "scm_#{scm}?".to_sym ) {
      File.exist?( ".#{scm}" ) and which( scm )
    }
  }
end
include SCM
 
HTACCESS = '.htaccess'
HTACCESS_SEED = '_htaccess.txt'
SITEMAP = '_sitemap.html'
 
def win?
  return ( RUBY_PLATFORM =~ /mingw/ or
           (RUBY_PLATFORM =~ /win/ and RUBY_PLATFORM !~ /darwin/) )
end
 
def linebreak
  if ( !@linebreak )
    @linebreak = ( win? ) ? "\r\n" : "\n"
  end
 
  return @linebreak
end
 
def which( cmd )
  path = ENV['PATH']
  path += File::PATH_SEPARATOR + '/Program Files' if win?
  cmd += '.exe' if win?
 
  paths = path.split( File::PATH_SEPARATOR ).select { |path|
    File.exist?( path ) and File.exist?( File.join( path, cmd ) )
  }
  if ( paths.size > 0 )
    paths.first + File::SEPARATOR + cmd
  else
    nil
  end
end
 
def cut( cmd, chr = '?' )
  `#{cmd}`.map { |line|
    c = line.split( /\s+/ )
    if ( c[0] =~ Regexp.new( "[#{chr}]" ) )
      c[c.size-1]
    else
      nil
    end
  }.compact
end
 
def htmlfiles
  Dir.glob( "**/*.html\0**/*.htm" )
end
 
def ftp_conf
  conf = nil
 
  %w( yaml yml ).each { |suffix|
    %w( .ftp _ftp ftp ).each { |basename|
      file = "#{basename}.#{suffix}"
      if ( File.exist?( file ) )
        conf = file
        break
      end
    }
  }
 
  return conf
end
 
def load_conf
  if ( !@conf and ftp_conf )
    @conf = YAML.load_file( ftp_conf )
  end
 
  return @conf
end
 
def sitemap_yaml
  yaml = nil
  
  %w( yaml yml ).each { |suffix|
    %w( .sitemap _sitemap sitemap ).each { |basename|
      file = "#{basename}.#{suffix}"
      if ( File.exist?( file ) )
        yaml = file
        break
      end
    }
  }
  
  return yaml
end
 
def load_sitemap
  ( sitemap_yaml ) ? YAML.load_file( sitemap_yaml ) : nil
end
 
def ignores
  %w(Rakefile rakefile Rakefile.rb rakefile.rb *~ *.bak .*~ .*.bak .*ignore CVS/ .svn/ .hg/ .git/ .DS_Store) + [ftp_conf, HTACCESS_SEED, SITEMAP, sitemap_yaml]
end
 
def prepare_htaccess
  if ( File.exist?( HTACCESS_SEED ) )
    cp( HTACCESS_SEED, HTACCESS )
  end
end
 
def remove_htaccess
  if ( File.exist?( HTACCESS_SEED ) )
    rm( HTACCESS )
  end
end
 
LINEBREAK = linebreak
HTMLFILES = htmlfiles
TIDY = 'tidy -qe'
LINKCHECKER = 'linkchecker --stdin --no-status -a --ignore-url=^mailto:'
FTP_BIN = 'lftp'
c = load_conf
if ( c.class == Hash )
  FTP_OPTS = "-u #{c['user']},#{c['pass']} -e 'mirror -v <%= reverse %>-X #{ignores.join( ' -X ' )} <%= from %> <%= to %>; bye' #{c['host']}"
end
 
#
 
namespace :transfer do
  if ( which( FTP_BIN ) and defined?( FTP_OPTS ) )
    desc "upload contents from cwd"
    task :up => :clean do
      prepare_htaccess
      reverse = '-R '
      from = '.'
      to = load_conf['path']
      opts = ERB.new( FTP_OPTS ).result( binding )
      system( "#{FTP_BIN} #{opts}" )
      remove_htaccess
    end
  end
 
  if ( which( FTP_BIN ) and defined?( FTP_OPTS ) )
    desc "download contents into cwd"
    task :down do
      reverse = ''
      from = load_conf['path']
      to = '.'
      opts = ERB.new( FTP_OPTS ).result( binding )
      system( "#{FTP_BIN} #{opts}" )
    end
  end
 
  if ( c )
    desc "show ftp config"
    task :conf do
      pp c
    end
  end
end
 
if ( which( 'tidy' ) and HTMLFILES.size > 0 )
  desc 'check syntax of htmls'
  task :tidy do
    check = HTMLFILES.map { |html|
      Open3.popen3( TIDY + " #{html}" ) { |stdin, stdout, stder|
        e = stder.read
        html + LINEBREAK + e if e.size > 0
      }
    }.compact.join
 
    if ( check.size > 0 )
      puts check
    else
      puts "All html file's syntax OK."
    end
  end
end
 
if ( which( 'linkchecker' ) and HTMLFILES.size > 0 )
  desc "check links in htmls"
  task :linkcheck do
    Open3.popen3( LINKCHECKER ) { |stdin, stdout, stderr|
      stdin.puts HTMLFILES.join( ' ' )
      stdin.close
 
      started = false
      result = {}
      check = stdout.read.split( LINEBREAK ).map { |line|
        if ( !started )
          if ( line =~ /\AStart checking/ )
            started = true
          end
          nil
        else
          if ( line =~ /\AThat's it/ )
            result['warning'] = $1.to_i if ( line =~ /([0-9]+) warnings/ )
            result['error'] = $1.to_i if ( line =~ /([0-9]+) error/ )
          end
          line
        end
      }.compact.join( LINEBREAK )
 
      if ( (result.has_key?( 'warning' ) and result['warning'] > 0) or
           (result.has_key?( 'error' ) and result['error'] > 0) )
        puts check
      else
        puts "All html file's link OK."
      end
    }
  end
end
 
if ( sitemap_yaml )
  desc "create sitemap html from #{sitemap_yaml}"
  task :sitemap_html do
    require 'xoxo'
    open( SITEMAP, 'w' ) { |f|
      f.puts load_sitemap.to_xoxo
    }
  end
end
 
if ( methods.grep( /\Ascm_/ ).size > 0 )
  desc "list up untracked files"
  task :untracked do
    if ( scm_hg? )
      puts cut( 'hg stat -A', 'I?' )
    elsif ( scm_git? )
      puts `git ls-files -o`
    elsif ( scm_svn? )
      puts cut( 'svn stat -v', '?' )
    end
  end
end
 
desc "test html files"
task :test => [:tidy, :linkcheck] do; end
 
desc "remove backup and auto creation file"
task :clean do
  Dir.glob( "**/*~\0**/.*~\0**/.DS_Store\0**/Thumbs.db\0**/._*" ).map { |e|
    rm( e )
  }
end
 
task :default do
  if ( HTMLFILES.size == 0 and !ftp_conf and !sitemap_yaml )
    puts 'No HTML file, No Task.'
  else
    app = Rake.application
    app.options.show_task_pattern = Regexp.new('')
    app.display_tasks_and_comments
  end
end