Class: Installer

Inherits:
Object show all
Includes:
FileOperations, HookScriptAPI
Defined in:
lib/mime-types-1.16/setup.rb

Defined Under Namespace

Classes: Shebang

Constant Summary collapse

FILETYPES =
%w( bin lib ext data conf man )
JUNK_FILES =

picked up many entries from cvs-1.11.1/src/ignore.c

%w( 
  core RCSLOG tags TAGS .make.state
  .nse_depinfo #* .#* cvslog.* ,* .del-* *.olb
  *~ *.old *.bak *.BAK *.orig *.rej _$* *$

  *.org *.in .*
)
GLOB2REGEX =
{
  '.' => '\.',
  '$' => '\$',
  '#' => '\#',
  '*' => '.*'
}
TESTDIR =

TASK test

'test'

Constants included from FileOperations

FileOperations::DIR_REJECT

Instance Method Summary collapse

Methods included from HookScriptAPI

#curr_objdir, #curr_srcdir, #get_config, #set_config, #srcdirectories, #srcdirectory?, #srcentries, #srcexist?, #srcfile, #srcfile?, #srcfiles

Methods included from FileOperations

#command, #diff?, #directories_of, #extdir?, #files_of, #force_remove_file, #install, #make, #mkdir_p, #move_file, #remove_file, #remove_tree, #remove_tree0, #rm_f, #rm_rf, #ruby

Constructor Details

#initialize(config, srcroot, objroot) ⇒ Installer

Returns a new instance of Installer.



1165
1166
1167
1168
1169
1170
# File 'lib/mime-types-1.16/setup.rb', line 1165

def initialize(config, srcroot, objroot)
  @config = config
  @srcdir = File.expand_path(srcroot)
  @objdir = File.expand_path(objroot)
  @currdir = '.'
end

Instance Method Details

#clean_dir_ext(rel) ⇒ Object



1486
1487
1488
1489
# File 'lib/mime-types-1.16/setup.rb', line 1486

def clean_dir_ext(rel)
  return unless extdir?(curr_srcdir())
  make 'clean' if File.file?('Makefile')
end

#config_dir_ext(rel) ⇒ Object



1229
1230
1231
# File 'lib/mime-types-1.16/setup.rb', line 1229

def config_dir_ext(rel)
  extconf if extdir?(curr_srcdir())
end

#distclean_dir_ext(rel) ⇒ Object



1504
1505
1506
1507
# File 'lib/mime-types-1.16/setup.rb', line 1504

def distclean_dir_ext(rel)
  return unless extdir?(curr_srcdir())
  make 'distclean' if File.file?('Makefile')
end

#dive_into(rel) ⇒ Object



1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
# File 'lib/mime-types-1.16/setup.rb', line 1540

def dive_into(rel)
  return unless File.dir?("#{@srcdir}/#{rel}")

  dir = File.basename(rel)
  Dir.mkdir dir unless File.dir?(dir)
  prevdir = Dir.pwd
  Dir.chdir dir
  $stderr.puts '---> ' + rel if verbose?
  @currdir = rel
  yield
  Dir.chdir prevdir
  $stderr.puts '<--- ' + rel if verbose?
  @currdir = File.dirname(rel)
end

#exec_cleanObject

TASK clean



1474
1475
1476
1477
1478
# File 'lib/mime-types-1.16/setup.rb', line 1474

def exec_clean
  exec_task_traverse 'clean'
  rm_f @config.savefile
  rm_f 'InstalledFiles'
end

#exec_configObject

TASK config



1222
1223
1224
# File 'lib/mime-types-1.16/setup.rb', line 1222

def exec_config
  exec_task_traverse 'config'
end

#exec_distcleanObject

TASK distclean



1495
1496
1497
1498
1499
# File 'lib/mime-types-1.16/setup.rb', line 1495

def exec_distclean
  exec_task_traverse 'distclean'
  rm_f @config.savefile
  rm_f 'InstalledFiles'
end

#exec_installObject

TASK install



1340
1341
1342
1343
# File 'lib/mime-types-1.16/setup.rb', line 1340

def exec_install
  rm_f 'InstalledFiles'
  exec_task_traverse 'install'
end

#exec_setupObject

TASK setup



1245
1246
1247
# File 'lib/mime-types-1.16/setup.rb', line 1245

def exec_setup
  exec_task_traverse 'setup'
end

#exec_task_traverse(task) ⇒ Object

Traversing



1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
# File 'lib/mime-types-1.16/setup.rb', line 1517

def exec_task_traverse(task)
  run_hook "pre-#{task}"
  FILETYPES.each do |type|
    if type == 'ext' and config('without-ext') == 'yes'
      $stderr.puts 'skipping ext/* by user option' if verbose?
      next
    end
    traverse task, type, "#{task}_dir_#{type}"
  end
  run_hook "post-#{task}"
end

#exec_testObject



1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
# File 'lib/mime-types-1.16/setup.rb', line 1454

def exec_test
  unless File.directory?('test')
    $stderr.puts 'no test in this package' if verbose?
    return
  end
  $stderr.puts 'Running tests...' if verbose?
  begin
    require 'test/unit'
  rescue LoadError
    setup_rb_error 'test/unit cannot loaded.  You need Ruby 1.8 or later to invoke this task.'
  end
  runner = Test::Unit::AutoRunner.new(true)
  runner.to_run << TESTDIR
  runner.run
end

#existfilesObject



1415
1416
1417
# File 'lib/mime-types-1.16/setup.rb', line 1415

def existfiles
  glob_reject(JUNK_FILES, (files_of(curr_srcdir()) | files_of('.')))
end

#extconfObject



1237
1238
1239
# File 'lib/mime-types-1.16/setup.rb', line 1237

def extconf
  ruby "#{curr_srcdir()}/extconf.rb", *@config.config_opt
end

#glob_reject(pats, ents) ⇒ Object



1430
1431
1432
1433
# File 'lib/mime-types-1.16/setup.rb', line 1430

def glob_reject(pats, ents)
  re = globs2re(pats)
  ents.reject {|ent| re =~ ent }
end

#glob_select(pat, ents) ⇒ Object



1425
1426
1427
1428
# File 'lib/mime-types-1.16/setup.rb', line 1425

def glob_select(pat, ents)
  re = globs2re([pat])
  ents.select {|ent| re =~ ent }
end

#globs2re(pats) ⇒ Object



1442
1443
1444
1445
1446
# File 'lib/mime-types-1.16/setup.rb', line 1442

def globs2re(pats)
  /\A(?:#{
    pats.map {|pat| pat.gsub(/[\.\$\#\*]/) {|ch| GLOB2REGEX[ch] } }.join('|')
  })\z/
end

#hookfilesObject



1419
1420
1421
1422
1423
# File 'lib/mime-types-1.16/setup.rb', line 1419

def hookfiles
  %w( pre-%s post-%s pre-%s.rb post-%s.rb ).map {|fmt|
    %w( config setup install clean ).map {|t| sprintf(fmt, t) }
  }.flatten
end

#inspectObject



1172
1173
1174
# File 'lib/mime-types-1.16/setup.rb', line 1172

def inspect
  "#<#{self.class} #{File.basename(@srcdir)}>"
end

#install_dir_bin(rel) ⇒ Object



1345
1346
1347
# File 'lib/mime-types-1.16/setup.rb', line 1345

def install_dir_bin(rel)
  install_files targetfiles(), "#{config('bindir')}/#{rel}", 0755
end

#install_dir_conf(rel) ⇒ Object



1364
1365
1366
1367
1368
# File 'lib/mime-types-1.16/setup.rb', line 1364

def install_dir_conf(rel)
  # FIXME: should not remove current config files
  # (rename previous file to .old/.org)
  install_files targetfiles(), "#{config('sysconfdir')}/#{rel}", 0644
end

#install_dir_data(rel) ⇒ Object



1360
1361
1362
# File 'lib/mime-types-1.16/setup.rb', line 1360

def install_dir_data(rel)
  install_files targetfiles(), "#{config('datadir')}/#{rel}", 0644
end

#install_dir_ext(rel) ⇒ Object



1353
1354
1355
1356
1357
1358
# File 'lib/mime-types-1.16/setup.rb', line 1353

def install_dir_ext(rel)
  return unless extdir?(curr_srcdir())
  install_files rubyextentions('.'),
                "#{config('sodir')}/#{File.dirname(rel)}",
                0555
end

#install_dir_lib(rel) ⇒ Object



1349
1350
1351
# File 'lib/mime-types-1.16/setup.rb', line 1349

def install_dir_lib(rel)
  install_files libfiles(), "#{config('rbdir')}/#{rel}", 0644
end

#install_dir_man(rel) ⇒ Object



1370
1371
1372
# File 'lib/mime-types-1.16/setup.rb', line 1370

def install_dir_man(rel)
  install_files targetfiles(), "#{config('mandir')}/#{rel}", 0644
end

#install_files(list, dest, mode) ⇒ Object



1374
1375
1376
1377
1378
1379
# File 'lib/mime-types-1.16/setup.rb', line 1374

def install_files(list, dest, mode)
  mkdir_p dest, @config.install_prefix
  list.each do |fname|
    install fname, dest, mode, @config.install_prefix
  end
end

#libfilesObject



1381
1382
1383
# File 'lib/mime-types-1.16/setup.rb', line 1381

def libfiles
  glob_reject(%w(*.y *.output), targetfiles())
end

#mapdir(ents) ⇒ Object



1397
1398
1399
1400
1401
1402
1403
1404
# File 'lib/mime-types-1.16/setup.rb', line 1397

def mapdir(ents)
  ents.map {|ent|
    if File.exist?(ent)
    then ent                         # objdir
    else "#{curr_srcdir()}/#{ent}"   # srcdir
    end
  }
end

#new_shebang(old) ⇒ Object



1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
# File 'lib/mime-types-1.16/setup.rb', line 1287

def new_shebang(old)
  if /\Aruby/ =~ File.basename(old.cmd)
    Shebang.new(config('rubypath'), old.args)
  elsif File.basename(old.cmd) == 'env' and old.args.first == 'ruby'
    Shebang.new(config('rubypath'), old.args[1..-1])
  else
    return old unless config('shebang') == 'all'
    Shebang.new(config('rubypath'))
  end
end

#no_harm?Boolean

module FileOperations requires this

Returns:

  • (Boolean)


1205
1206
1207
# File 'lib/mime-types-1.16/setup.rb', line 1205

def no_harm?
  @config.no_harm?
end

#noop(rel) ⇒ Object Also known as: config_dir_bin, config_dir_lib, config_dir_data, config_dir_conf, config_dir_man, setup_dir_lib, setup_dir_data, setup_dir_conf, setup_dir_man, clean_dir_bin, clean_dir_lib, clean_dir_data, clean_dir_conf, clean_dir_man, distclean_dir_bin, distclean_dir_lib, distclean_dir_data, distclean_dir_conf, distclean_dir_man



1176
1177
# File 'lib/mime-types-1.16/setup.rb', line 1176

def noop(rel)
end

#objdir_rootObject



1187
1188
1189
# File 'lib/mime-types-1.16/setup.rb', line 1187

def objdir_root
  @objdir
end

#open_atomic_writer(path, &block) ⇒ Object



1298
1299
1300
1301
1302
1303
1304
1305
1306
# File 'lib/mime-types-1.16/setup.rb', line 1298

def open_atomic_writer(path, &block)
  tmpfile = File.basename(path) + '.tmp'
  begin
    File.open(tmpfile, 'wb', &block)
    File.rename tmpfile, File.basename(path)
  ensure
    File.unlink tmpfile if File.exist?(tmpfile)
  end
end

#relpathObject



1191
1192
1193
# File 'lib/mime-types-1.16/setup.rb', line 1191

def relpath
  @currdir
end

#rubyextentions(dir) ⇒ Object



1385
1386
1387
1388
1389
1390
1391
# File 'lib/mime-types-1.16/setup.rb', line 1385

def rubyextentions(dir)
  ents = glob_select("*.#{@config.dllext}", targetfiles())
  if ents.empty?
    setup_rb_error "no ruby extention exists: 'ruby #{$0} setup' first"
  end
  ents
end

#run_hook(id) ⇒ Object



1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
# File 'lib/mime-types-1.16/setup.rb', line 1555

def run_hook(id)
  path = [ "#{curr_srcdir()}/#{id}",
           "#{curr_srcdir()}/#{id}.rb" ].detect {|cand| File.file?(cand) }
  return unless path
  begin
    instance_eval File.read(path), path, 1
  rescue
    raise if $DEBUG
    setup_rb_error "hook #{path} failed:\n" + $!.message
  end
end

#setup_dir_bin(rel) ⇒ Object



1249
1250
1251
1252
1253
# File 'lib/mime-types-1.16/setup.rb', line 1249

def setup_dir_bin(rel)
  files_of(curr_srcdir()).each do |fname|
    update_shebang_line "#{curr_srcdir()}/#{fname}"
  end
end

#setup_dir_ext(rel) ⇒ Object



1257
1258
1259
# File 'lib/mime-types-1.16/setup.rb', line 1257

def setup_dir_ext(rel)
  make if extdir?(curr_srcdir())
end

#srcdir_rootObject

Hook Script API base methods



1183
1184
1185
# File 'lib/mime-types-1.16/setup.rb', line 1183

def srcdir_root
  @srcdir
end

#targetfilesObject



1393
1394
1395
# File 'lib/mime-types-1.16/setup.rb', line 1393

def targetfiles
  mapdir(existfiles() - hookfiles())
end

#traverse(task, rel, mid) ⇒ Object



1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
# File 'lib/mime-types-1.16/setup.rb', line 1529

def traverse(task, rel, mid)
  dive_into(rel) {
    run_hook "pre-#{task}"
    __send__ mid, rel.sub(%r[\A.*?(?:/|\z)], '')
    directories_of(curr_srcdir()).each do |d|
      traverse task, "#{rel}/#{d}", mid
    end
    run_hook "post-#{task}"
  }
end

#update_shebang_line(path) ⇒ Object



1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
# File 'lib/mime-types-1.16/setup.rb', line 1265

def update_shebang_line(path)
  return if no_harm?
  return if config('shebang') == 'never'
  old = Shebang.load(path)
  if old
    $stderr.puts "warning: #{path}: Shebang line includes too many args.  It is not portable and your program may not work." if old.args.size > 1
    new = new_shebang(old)
    return if new.to_s == old.to_s
  else
    return unless config('shebang') == 'all'
    new = Shebang.new(config('rubypath'))
  end
  $stderr.puts "updating shebang: #{File.basename(path)}" if verbose?
  open_atomic_writer(path) {|output|
    File.open(path, 'rb') {|f|
      f.gets if old   # discard
      output.puts new.to_s
      output.print f.read
    }
  }
end

#verbose?Boolean

module FileOperations requires this

Returns:

  • (Boolean)


1200
1201
1202
# File 'lib/mime-types-1.16/setup.rb', line 1200

def verbose?
  @config.verbose?
end

#verbose_offObject



1209
1210
1211
1212
1213
1214
1215
1216
# File 'lib/mime-types-1.16/setup.rb', line 1209

def verbose_off
  begin
    save, @config.verbose = @config.verbose?, false
    yield
  ensure
    @config.verbose = save
  end
end