Skip to content

Instantly share code, notes, and snippets.

@DigiTec
Created November 6, 2012 06:59
Show Gist options
  • Save DigiTec/4023166 to your computer and use it in GitHub Desktop.
Save DigiTec/4023166 to your computer and use it in GitHub Desktop.
Imports files from a web site project into a WWA application to enable deploying to both without copying files.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
public class ImportWebSiteToWWA
{
private static void Main(string[] args)
{
string source = args[0];
string sourceFile = string.Format("{0}\\{0}.csproj", source);
string target = args[1];
string targetFile = string.Format("{0}\\{0}.jsproj", target);
if (!File.Exists(sourceFile) || !File.Exists(targetFile))
{
Console.WriteLine("Required files do not exist");
return;
}
XmlDocument xDocSource = new XmlDocument();
xDocSource.Load(sourceFile);
XmlDocument xDocTarget = new XmlDocument();
xDocTarget.Load(targetFile);
XmlNode targetNode = FindContentItemGroup(xDocTarget.DocumentElement);
List<XmlNode> removalNodes = new List<XmlNode>();
foreach(XmlNode contentNode in targetNode.ChildNodes)
{
if ( contentNode.LocalName == "Content" )
{
foreach(XmlNode linkNode in contentNode.ChildNodes)
{
if ( linkNode.LocalName == "Link" )
{
removalNodes.Add(contentNode);
break;
}
}
}
}
foreach(XmlNode removalNode in removalNodes)
{
targetNode.RemoveChild(removalNode);
}
foreach(XmlNode t1Node in xDocSource.DocumentElement.ChildNodes)
{
if ( t1Node.LocalName == "ItemGroup" )
{
foreach(XmlNode contentNode in t1Node.ChildNodes)
{
if ( contentNode.LocalName == "Content" )
{
XmlElement newContent = xDocTarget.CreateElement("Content", xDocTarget.DocumentElement.NamespaceURI);
newContent.SetAttribute("Include", string.Format("..\\{0}\\{1}", source, contentNode.Attributes["Include"].Value));
XmlElement newLink = xDocTarget.CreateElement("Link", xDocTarget.DocumentElement.NamespaceURI);
newLink.InnerText = contentNode.Attributes["Include"].Value;
newContent.AppendChild(newLink);
targetNode.AppendChild(newContent);
}
}
}
}
xDocTarget.Save(targetFile);
}
private static XmlNode FindContentItemGroup(XmlNode rootNode)
{
foreach(XmlNode t1Node in rootNode)
{
if ( t1Node.LocalName == "ItemGroup" )
{
foreach(XmlNode contentNode in t1Node.ChildNodes)
{
if ( contentNode.LocalName == "Content" )
{
return t1Node;
}
}
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment