Newfile - A Filestarter
What is it?
Newfile is a tool for creating starter files in various languages. It uses a
full-featured preprocessor (like the C preprocessor) to transform templates into
customized starter files. It can also generate trees of files for a specific purpose,
such as a project using automake and autoconf, or a FreeBSD port.
Where can I get it?
From
Program Documentation
You can browse the program itself
HERE
using the documentation generated by rdoc.
The man page for newfile
NAME
newfile - create new files from templates
SYNOPSIS
This man page documents newfile v. 0.8.5.b.3.
newfile -h
newfile -V
newfile [options] -s|--show
newfile [options] -t file ...
newfile [options] -p. [dir]
DESCRIPTION
newfile is a program for creating starter files, or trees
of files, by processsing templates with a C-preprocessor-
like syntax.
Finding templates
Templates and projects (file tree templates) are found by
looking in the search path for a directory with the name
of the template or project. E.g., the default location
for the "rb" (ruby) file template is $PREFIX/share/alane-
newfile/templates/rb. Project templates are stored under
the subdirectory "projects", and file templates under
"templates".
The user can create a directory hierarchy $HOME/.alane-
newfile in which to place new or modified templates and
projects. Its structure should mirror that of the system
directory.
Other search directories can be added with the
"-I/--include" command line option.
Variables
Builtins
Some variables are built into the system, and should
always be defined. Some will get default values if not
defined.
NAME
The name of the input file, minus any directory pre-
fix.
NAMEID
An identifier based on NAME.
PROJ
If a project template is being expanded, this is the
name of the template.
PROJID
An identifier based on PROJ.
LICENSE
Defined by the command line option "-l/--license".
Licenses are a special case of project templates, and
their files are mixed in with a project template's
file. In the case of a single file, the value of
LICENSE determines which directory to search for a
file called "license" that is included in the gener-
ated file.
AUTHOR
Defined by the command line option "-a/--author". The
name of the author of the code or document. If not
given, taken from the user's full name in the
"/etc/passwd" file.
EMAIL
Defined by the command line option "-e/--email". If
not given, constructed from the user's login name and
the hostname.
ORGANIZATION
The organization to which the author belongs, in the
context of the code or document being created. If not
given, defaults to the value of AUTHOR.
OWNER
Defined by the command line option "-o/--owner". The
owner of the copyright. May be an arbitrary string, or
one of the special strings "org", "organization", or
"author". If not given, defaults to "author".
BODY
Used by many templates to specify the name of a file
to include as the body of the new file. Common values
are "body" and "no". This is handled entirely within
the templates themselves.
NO_BODY
Where used, equivalent to BODY=no.
c(omment)
The value of a leading string to be prefixed to text
in a comment block. This is handled entirely within
the templates themselves.
Getting a variable's value
The syntax <:VARIABLE> will be substituted with the value
of the variable. If the variable does not exist, no sub-
stitution will occur.
Checking if a variable is defined
The special function defined("VARIABLE") can be used in
preprocessor conditionals (see below) to test if a vari-
able is defined or not. Note: the quotes around the vari-
able name are required.
Preprocessor directives
The pre-processor supports a set of directives very simi-
lar to the C preprocessor. The leading character for all
directives in "%", and must occur in the first column of
the line.
%define VAR [VALUE]
Define or redefined a variable. One layer of quotes is
stripped from the optional value unless the open quote
is preceded by a literal backslash ("\").
%undef VAR
Undefine a variable, if it is defined.
%include
Insert the contents of "file" at this point. The file
is found by searching the current search path. Vari-
able substitution is performed on "file", so "%include
<<:BODY>>" will include the file whose name is in the
variable "BODY".
%if COND-EXPRESSION
Test a conditional expression. Expressions are evalu-
ated by the Ruby interpreter after variable substitu-
tion. The text up to the next matching "%elif",
"%else", or "%endif" is used if the expression is
true.
%elif COND-EXPRESSION
Shorthand for "%else", followed by "%if", but saves an
extra "%endif".
%else
The text up to the matching "%endif" if used if the
previously tested condition(s) was/were false.
%endif
Ends a "%if/%elif/%else" sequence.
%end
Causes the preprocessor to ignore the rest of the
file. You can, e.g., put documentation about the tem-
plate after a "%end".
All other lines are either copied with variable substi-
tion, or ignored, depending on the current conditional
state.
OPTIONS
-h, --help Show this help.
-V, --version Show version and copyright.
-v, --verbose Turn on verbose messages.
-l, --license=LICENSE Set license type.
-t, --filetype=TEMPLATE Set file TEMPLATE to use.
Do *not* use with -p/--project.
-p=TEMPLATE.PROJECTNAME Set project TEMPLATE to use, and
PROJECTNAME of project being built.
Do *not* use with -t/--filetype.
--project
-s, --show Show installed file templates, project
templates, and license types.
-x, --exec-bit Make the resulting file executable.
This option has no effect if a project
template is selected.
-a, --author=AUTHOR Set name of author.
-e, --email=EMAIL Set author's email address.
-o, --organization=ORGANIZATION Set author's organization name.
-c, --owner=COPYRIGHT_OWNER Set copyright owner's name.
-I, --include=DIR Add DIR to search path.
-D, --define=VAR[=value] Define a variable. Value defaults
to 1.
DIAGNOSTICS
The preprocessor attempts to issue error messages that are
as accurate as possible. Each error message is formatted
like gcc's error messages, and shows the included files
leading up to the point of the the error.
ENVIRONMENT
NEWFILE
String containing default command line options. Normal
shell quoting conventions apply.
FILES
o $HOME/.alane-newfile/*
o $PREFIX/share/alane-newfile/*
CAVEATS
o Does not check before overwriting existing files.
BUGS
o There is no syntax for line continuation.
o Doesn't always issue good error messages and exit
gracefully if bad input is given. Too much of the Ruby
exception mechanism may be exposed to the user.
RESTRICTIONS
See the BSD License in the file "LICENSE" in the source
distribution.
NOTES
There's still room for improvement, but the basic engine
is solid. What is needed now are more templates, and get-
ting all the templates documented.
A command line option to fetch template documention after
a %end would be really nice, too.
AUTHOR
AlanE@geeksrus.net
HISTORY
This is the third rendition of this program, written first
in shell, then in Python, and finally in ruby. In this
case, I think ruby allows easier transition from intent to
code than Python.