* `nix-instantiate --{eval|parse}-only --xml': print an XML
representation instead of an ATerm. * Indent XML output.
This commit is contained in:
parent
fe101fa785
commit
18e4ac0fc6
5 changed files with 82 additions and 13 deletions
|
@ -91,7 +91,7 @@ sub addPkg {
|
||||||
close PROP;
|
close PROP;
|
||||||
my @propagated = split ' ', $propagated;
|
my @propagated = split ' ', $propagated;
|
||||||
foreach my $p (@propagated) {
|
foreach my $p (@propagated) {
|
||||||
addPkg $p;
|
# addPkg $p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
#include "xml-writer.hh"
|
#include "xml-writer.hh"
|
||||||
|
|
||||||
|
|
||||||
XMLWriter::XMLWriter(ostream & output)
|
XMLWriter::XMLWriter(bool indent, ostream & output)
|
||||||
: output(output)
|
: output(output), indent(indent)
|
||||||
{
|
{
|
||||||
output << "<?xml version='1.0' encoding='utf-8'?>\n";
|
output << "<?xml version='1.0' encoding='utf-8'?>\n";
|
||||||
closed = false;
|
closed = false;
|
||||||
|
@ -25,13 +25,22 @@ void XMLWriter::close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void XMLWriter::indent_(unsigned int depth)
|
||||||
|
{
|
||||||
|
if (!indent) return;
|
||||||
|
output << string(depth * 2, ' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void XMLWriter::openElement(const string & name,
|
void XMLWriter::openElement(const string & name,
|
||||||
const XMLAttrs & attrs)
|
const XMLAttrs & attrs)
|
||||||
{
|
{
|
||||||
assert(!closed);
|
assert(!closed);
|
||||||
|
indent_(pendingElems.size());
|
||||||
output << "<" << name;
|
output << "<" << name;
|
||||||
writeAttrs(attrs);
|
writeAttrs(attrs);
|
||||||
output << ">";
|
output << ">";
|
||||||
|
if (indent) output << "\n";
|
||||||
pendingElems.push_back(name);
|
pendingElems.push_back(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +48,9 @@ void XMLWriter::openElement(const string & name,
|
||||||
void XMLWriter::closeElement()
|
void XMLWriter::closeElement()
|
||||||
{
|
{
|
||||||
assert(!pendingElems.empty());
|
assert(!pendingElems.empty());
|
||||||
|
indent_(pendingElems.size() - 1);
|
||||||
output << "</" << pendingElems.back() << ">";
|
output << "</" << pendingElems.back() << ">";
|
||||||
|
if (indent) output << "\n";
|
||||||
pendingElems.pop_back();
|
pendingElems.pop_back();
|
||||||
if (pendingElems.empty()) closed = true;
|
if (pendingElems.empty()) closed = true;
|
||||||
}
|
}
|
||||||
|
@ -49,9 +60,11 @@ void XMLWriter::writeEmptyElement(const string & name,
|
||||||
const XMLAttrs & attrs)
|
const XMLAttrs & attrs)
|
||||||
{
|
{
|
||||||
assert(!closed);
|
assert(!closed);
|
||||||
|
indent_(pendingElems.size());
|
||||||
output << "<" << name;
|
output << "<" << name;
|
||||||
writeAttrs(attrs);
|
writeAttrs(attrs);
|
||||||
output << " />";
|
output << " />";
|
||||||
|
if (indent) output << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,13 +18,14 @@ private:
|
||||||
|
|
||||||
ostream & output;
|
ostream & output;
|
||||||
|
|
||||||
|
bool indent;
|
||||||
bool closed;
|
bool closed;
|
||||||
|
|
||||||
list<string> pendingElems;
|
list<string> pendingElems;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
XMLWriter(ostream & output);
|
XMLWriter(bool indent, ostream & output);
|
||||||
~XMLWriter();
|
~XMLWriter();
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
|
@ -40,6 +41,8 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void writeAttrs(const XMLAttrs & attrs);
|
void writeAttrs(const XMLAttrs & attrs);
|
||||||
|
|
||||||
|
void indent_(unsigned int depth);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -804,7 +804,7 @@ static void opQuery(Globals & globals,
|
||||||
/* Print the desired columns, or XML output. */
|
/* Print the desired columns, or XML output. */
|
||||||
Table table;
|
Table table;
|
||||||
ostringstream dummy;
|
ostringstream dummy;
|
||||||
XMLWriter xml(*(xmlOutput ? &cout : &dummy));
|
XMLWriter xml(true, *(xmlOutput ? &cout : &dummy));
|
||||||
XMLOpenElement xmlRoot(xml, "items");
|
XMLOpenElement xmlRoot(xml, "items");
|
||||||
|
|
||||||
for (vector<DrvInfo>::iterator i = elems2.begin();
|
for (vector<DrvInfo>::iterator i = elems2.begin();
|
||||||
|
@ -903,10 +903,9 @@ static void opQuery(Globals & globals,
|
||||||
columns.push_back(descr);
|
columns.push_back(descr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xmlOutput) {
|
if (xmlOutput)
|
||||||
xml.writeEmptyElement("item", attrs);
|
xml.writeEmptyElement("item", attrs);
|
||||||
xml.writeCharData("\n");
|
else
|
||||||
} else
|
|
||||||
table.push_back(columns);
|
table.push_back(columns);
|
||||||
|
|
||||||
} catch (AssertionError & e) {
|
} catch (AssertionError & e) {
|
||||||
|
|
|
@ -34,14 +34,65 @@ static int rootNr = 0;
|
||||||
static bool indirectRoot = false;
|
static bool indirectRoot = false;
|
||||||
|
|
||||||
|
|
||||||
|
static XMLAttrs singletonAttrs(const string & name, const string & value)
|
||||||
|
{
|
||||||
|
XMLAttrs attrs;
|
||||||
|
attrs[name] = value;
|
||||||
|
return attrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void printTermAsXML(EvalState & state, Expr e, XMLWriter & doc)
|
||||||
|
{
|
||||||
|
XMLAttrs attrs;
|
||||||
|
ATerm s;
|
||||||
|
int i;
|
||||||
|
ATermList as;
|
||||||
|
|
||||||
|
if (matchStr(e, s))
|
||||||
|
doc.writeEmptyElement("string", singletonAttrs("value", aterm2String(s)));
|
||||||
|
|
||||||
|
else if (matchPath(e, s))
|
||||||
|
doc.writeEmptyElement("path", singletonAttrs("value", aterm2String(s)));
|
||||||
|
|
||||||
|
else if (matchUri(e, s))
|
||||||
|
doc.writeEmptyElement("uri", singletonAttrs("value", aterm2String(s)));
|
||||||
|
|
||||||
|
else if (matchNull(e))
|
||||||
|
doc.writeEmptyElement("null");
|
||||||
|
|
||||||
|
else if (matchInt(e, i))
|
||||||
|
doc.writeEmptyElement("int",singletonAttrs("value", (format("%1%") % i).str()));
|
||||||
|
|
||||||
|
else if (matchAttrs(e, as)) {
|
||||||
|
XMLOpenElement _(doc, "attrs");
|
||||||
|
ATermMap attrs(128);
|
||||||
|
queryAllAttrs(e, attrs);
|
||||||
|
for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
|
||||||
|
XMLOpenElement _(doc, "attr", singletonAttrs("name", aterm2String(i->key)));
|
||||||
|
printTermAsXML(state, i->value, doc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
doc.writeEmptyElement("unknown");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void printResult(EvalState & state, Expr e,
|
static void printResult(EvalState & state, Expr e,
|
||||||
bool evalOnly, bool printArgs, const ATermMap & autoArgs)
|
bool evalOnly, bool printArgs, bool xmlOutput,
|
||||||
|
const ATermMap & autoArgs)
|
||||||
{
|
{
|
||||||
if (evalOnly)
|
if (evalOnly)
|
||||||
|
if (xmlOutput) {
|
||||||
|
XMLWriter doc(true, cout);
|
||||||
|
XMLOpenElement root(doc, "expr");
|
||||||
|
printTermAsXML(state, e, doc);
|
||||||
|
} else
|
||||||
cout << format("%1%\n") % e;
|
cout << format("%1%\n") % e;
|
||||||
|
|
||||||
else if (printArgs) {
|
else if (printArgs) {
|
||||||
XMLWriter doc(cout);
|
XMLWriter doc(true, cout);
|
||||||
XMLOpenElement root(doc, "args");
|
XMLOpenElement root(doc, "args");
|
||||||
|
|
||||||
ATermList formals;
|
ATermList formals;
|
||||||
|
@ -95,6 +146,7 @@ void run(Strings args)
|
||||||
bool evalOnly = false;
|
bool evalOnly = false;
|
||||||
bool parseOnly = false;
|
bool parseOnly = false;
|
||||||
bool printArgs = false;
|
bool printArgs = false;
|
||||||
|
bool xmlOutput = false;
|
||||||
string attrPath;
|
string attrPath;
|
||||||
ATermMap autoArgs(128);
|
ATermMap autoArgs(128);
|
||||||
|
|
||||||
|
@ -138,6 +190,8 @@ void run(Strings args)
|
||||||
}
|
}
|
||||||
else if (arg == "--indirect")
|
else if (arg == "--indirect")
|
||||||
indirectRoot = true;
|
indirectRoot = true;
|
||||||
|
else if (arg == "--xml")
|
||||||
|
xmlOutput = true;
|
||||||
else if (arg[0] == '-')
|
else if (arg[0] == '-')
|
||||||
throw UsageError(format("unknown flag `%1%'") % arg);
|
throw UsageError(format("unknown flag `%1%'") % arg);
|
||||||
else
|
else
|
||||||
|
@ -149,7 +203,7 @@ void run(Strings args)
|
||||||
if (readStdin) {
|
if (readStdin) {
|
||||||
Expr e = findAlongAttrPath(state, attrPath, parseStdin(state));
|
Expr e = findAlongAttrPath(state, attrPath, parseStdin(state));
|
||||||
if (!parseOnly) e = evalExpr(state, e);
|
if (!parseOnly) e = evalExpr(state, e);
|
||||||
printResult(state, e, evalOnly, printArgs, autoArgs);
|
printResult(state, e, evalOnly, printArgs, xmlOutput, autoArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Strings::iterator i = files.begin();
|
for (Strings::iterator i = files.begin();
|
||||||
|
@ -159,7 +213,7 @@ void run(Strings args)
|
||||||
Expr e = findAlongAttrPath(state, attrPath,
|
Expr e = findAlongAttrPath(state, attrPath,
|
||||||
parseExprFromFile(state, path));
|
parseExprFromFile(state, path));
|
||||||
if (!parseOnly) e = evalExpr(state, e);
|
if (!parseOnly) e = evalExpr(state, e);
|
||||||
printResult(state, e, evalOnly, printArgs, autoArgs);
|
printResult(state, e, evalOnly, printArgs, xmlOutput, autoArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
printEvalStats(state);
|
printEvalStats(state);
|
||||||
|
|
Loading…
Reference in a new issue