newfile.rb
Path: newfile.rb
Modified: Mon Nov 03 14:59:37 MSK 2003
Required files
etc    optparse    singleton    shellwords    socket    fileproc    sytab   
Methods
create_file    create_output    create_proj    init_sytab    main    makepath    mkident    start   
Classes and Modules
Class FileFinder
Class Installed
Class NewfileOpts
Class OptionError
Public Instance methods
mkident(name)

Make an identifier from a (file)name.

name
a filename from which to stip punctuation etc to make an identifier, often used in ifdefs in C/C++ header files
# File newfile.rb, line 177
def mkident(name)
  name = File::basename(name)
  if /0-9/ =~ name[0,1]
    name = "_" + name
  else
    name = name + "_"
  end
  name.gsub(/(\/|\.)+/, "_").gsub(/\++/, "plus").delete("^A-Za-z0-9_")
end
init_sytab(sytab, opts)

Initialize symbol table from values in options.

sytab
empty symbol table to fill in
opts
command line options object
# File newfile.rb, line 193
def init_sytab(sytab, opts)
  t = Time::now
  sytab["TEMPLATE"] = opts["template"]
  sytab["YEAR"] = t.strftime("%Y")
  sytab["DATE"] = t.strftime("%Y-%m-%d")
  if opts["project"]
    sytab["PROJECT"] = opts["project"]
    sytab["PROJECTID"] = mkident(opts["project"])
  end
  sytab["LICENSE"] = opts["license"]
  sytab["AUTHOR"] = opts["author"]
  sytab["EMAIL"] = opts["email"]
  sytab["OWNER"] = opts["owner"]
  sytab["ORGANIZATION"] = opts["org"]
  opts["defs"].each_pair {
    |k,v| sytab[k] = v
  }
end
makepath(path)

Create a directory path like mkdir -p. Ruby's File#mkpath barfs on paths like +foo/.+, which is stupid.

path
path to create
# File newfile.rb, line 217
def makepath(path)
  path = File::expand_path(path)
  arr = path.split(File::Separator)
  d = arr.shift
  while arr.size > 0
    d = File::join(d, arr.shift)
    Dir::mkdir(d) if !File::directory?(d)
  end
end
create_file(tmplfile, newfile, sytab, canExec, installed)

Create output file of a given type.

tmplfile
template file
newfile
filename to create
sytab
initial symbol table
canExec
set the execute bit?
installed
installed template inventory
# File newfile.rb, line 252
def create_file(tmplfile, newfile, sytab, canExec, installed)
  puts "Creating '#{newfile}' ..." if $verbose
  newdir = File::split(newfile).first
  if !File::directory?(newdir)
    puts "Creating dir '#{newdir}' ..." if $verbose
    makepath(newdir)
  end    
  fp = FileProcessor.new
  fp.sytab = sytab.clone
  fp.sytab["NAME"] = File::basename(newfile)
  fp.sytab["NAMEID"] = mkident(File::basename(newfile))
  fp.fileFinder = FileFinder.new(installed)
  puts "Using '#{tmplfile}' ..." if $verbose
  puts "Writing '#{newfile}' ..." if $verbose
  outtext = fp << tmplfile
  if outtext != nil
    File::open(newfile, "w") {
      |io|
      outtext.each { |l| io.puts(l) }
    }
    puts "Wrote '#{newfile}': done." if $verbose
    if (canExec)
      u = ~File::umask
      m = File::stat(newfile).mode
      File::chmod(m | (0111 & u), newfile)
      puts "Marked '#{newfile}' executable." if $verbose
    end
  else
    puts "===> Errors generating '#{newfile}':"
    fp.errlist.each { |err| puts(err.message) }
  end
end
create_proj(dest, tmpl, sytab, setexec, installed, opts)

Create all files for a project type.

name
name of project
dest
dir to create project files in
sytab
initial symbol table
searchpath
search path array for include files
# File newfile.rb, line 295
def create_proj(dest, tmpl, sytab, setexec, installed, opts)
  dest = File::expand_path(dest)
  installed.findAllPrefix(opts["license"]).each {
    |tfile|
    destfile = File.basename(tfile).sub(>^.*@>,"").tr("^","/")
    create_file(installed[tfile], File::join(dest, destfile), sytab,
		setexec, installed)
  }
  installed.findAllPrefix(opts["template"]).each {
    |tfile|
    destfile = File.basename(tfile).sub(>^.*@>,"").tr("^","/")
    create_file(installed[tfile], File::join(dest, destfile), sytab,
		setexec, installed)
  }
end
create_output(opts, args, sytab, installed)

Create files given command line tail, symbol table, and options.

opts
parsed command line options object
args
command line args after options removed
sytab
initial symbol table
installed
installed templates inventory
# File newfile.rb, line 321
def create_output(opts, args, sytab, installed)
  if opts["project"]
    case args.size
    when 0 then
      projdir = opts["project"]
    when 1 then
      projdir = args.first
    else
      raise OptionError, "Only 1 dir allowed for projects."
    end
    create_proj(projdir, opts["template"], sytab, opts["exec"], installed, opts)
  else
    template = opts["template"]
    tmplfile = installed[ "tmpl@#{template}"]
    raise OptionError, "no such template: #{template}" if !tmplfile
    args.each {
      |file|
      create_file(tmplfile, file, sytab, opts["exec"], installed)
    }
  end # if opts.proj
end
main(parser, rest)

Entry point called by start().

# File newfile.rb, line 346
def main(parser, rest)
  opts = parser.opts
  path = (opts["path"].reverse + 
	  [File.expand_path("~/.#{APPNAME}"), PKGDATA]).find_all {
    |dir|
    File.directory?(dir)
  }
  installed = Installed.new(path)
  begin; installed.show(opts["v"]); exit(0); end if opts["show"]
  begin; installed.showpdoc(opts["pdoc"]); exit(0); end if opts["pdoc"]
  begin; installed.showdoc(opts["doc"]); exit(0); end if opts["doc"]
  rest.push(opts["project"]) if opts["project"] && rest.size < 1
  begin; parser.show_help; exit(0); end if rest.size < 1
  sytab = SyTab.new(FileProcessor::VarDelims)
  init_sytab(sytab, opts)
  create_output(opts, rest, sytab, installed)
  return 0
end
start(av)

Entry point if called as executable.

# File newfile.rb, line 541
def start(av)
  NewfileOpts.set_text(USAGE, BANNER)
  opts = NewfileOpts.instance
  return main(opts, opts.parse(av))
end