//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.presents.tools;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
/**
* Merges updates to a generated source file into a previously generated version of that source
* while leaving changes outside marked sections alone.
*/
public class GeneratedSourceMerger
{
/**
* Returns previouslyGenerated with marked sections updated from the same marked
* sections in newlyGenerated. Everything outside these sections in
* previouslyGenerated is returned as is. A marked section starts with //
* GENERATED {name} START and ends with // GENERATED {name} END
*
* If previouslyGenerated has a generated section replaced with //
* GENERATED {name} DISABLED, that section will no longer be updated.
*/
public String merge (String newlyGenerated, String previouslyGenerated)
throws Exception
{
// Extract the generated section names from the output and make sure they're all matched
Mapm is at the end of the section when this returns.
*/
protected Section extractGeneratedSection (Matcher m, String input)
{
int startIdx = m.start();
String name = m.group(1);
if (m.group(2).equals("DISABLED")) {
return new Section(name, input.substring(startIdx, m.end()), true);
}
Preconditions.checkArgument(m.group(2).equals("START"), "'%s' END without START",
name);
Preconditions.checkArgument(m.find(), "'%s' START without END", name);
String endName = m.group(1);
Preconditions.checkArgument(m.group(2).equals("END"),
"'%s' START after '%s' START", endName, name);
Preconditions.checkArgument(endName.equals(name),
"'%s' END after '%s' START", endName, name);
return new Section(name, input.substring(startIdx, m.end()), false);
}
protected static class Section {
public final String name, contents;
public final boolean disabled;
public Section (String name, String contents, boolean disabled) {
this.name = name;
this.contents = contents;
this.disabled = disabled;
}
}
protected final Pattern _sectionDelimiter =
Pattern.compile(" *// GENERATED (\\w+) (START|END|DISABLED)\n");
}