Script that creates CSharp files from XSD




Example of O2 Script used to Compile all projects

using System;
using System.IO;
using System.Collections.Generic;
using o2.core;

class CompileAllProjects
{
static String sPathToSourceCode = @"E:\O2\CodePlex_Dev";
static String sPathToMsBuildExe = @"c:\WINDOWS\Microsoft.NET\Framework\v3.5\msBuild.exe";

public static void Run()
{
DebugMsg._Info("Compiling All Projects");
List lsVSProjects = getListOfVisualStudioProjectsInFolder(sPathToSourceCode);
foreach(String sProject in lsVSProjects)
buildVisualStudioProject(sProject);
DebugMsg._Info("Done");
}

public static List getListOfVisualStudioProjectsInFolder(String sTargetFolder)
{
List lsVSProjects = new List();
lsVSProjects = Files.getListOfAllFilesFromDirectory(sTargetFolder, true, "*.sln");
DebugMsg._Info("Found {0} VS projects",lsVSProjects.Count);
return lsVSProjects;
}

public static void buildVisualStudioProject(String sPathToVSProject)
{
//Processes.startProcessAsConsoleApplication(sPathToMsBuildExe,sPathToVSProject);
bool CreateNoWindow = true;
String sExitCode = Processes.waitForProcessExitAndGetProcessExitCode(Processes.startProcess(sPathToMsBuildExe,sPathToVSProject,CreateNoWindow));
if (sExitCode != "0")
DebugMsg._Error("MsBuild Error Code:{0}", sExitCode,sPathToMsBuildExe,sPathToVSProject);
}

// to add to Core lib
public static void showInDebugContentsOfStringList(List lsTargetList)
{
DebugMsg._Debug("Showing contents of String List with {0} items", lsTargetList.Count);
foreach(String sItem in lsTargetList)
DebugMsg._Info("\t{0}", sItem);
}
}



Example of O2 Script used to Publish Binaries

using System;
using System.IO;
using System.Collections.Generic;
using o2.core;

class PublishBinaries
{
static String sPathToSourceCode = @"E:\O2\CodePlex_Dev";
static String sPathToBinariesCode = @"E:\O2\Releases";
static String sPathTargetFolder = String.Format("{0}\\{1}_{2}_{3}_O2_Binaries",sPathToBinariesCode,DateTime.Now.Day,DateTime.Now.Month,DateTime.Now.Year);
static bool bDeleteOunceAndMySqlDlls = false;

public static void Run()
{
DebugMsg._Info("Publishing Binaries");
publishFolder("base",sPathToSourceCode,sPathTargetFolder);
publishFolder("OunceDependent",sPathToSourceCode,sPathTargetFolder);
publishFolder("ToolsForFrameworks",sPathToSourceCode,sPathTargetFolder);
DebugMsg._Info("Done");
}

public static void publishFolder(String sFolderToPublish ,String sToSourceCode,String sTargetFolder)
{
String sPathToSourceCode = Path.Combine(sToSourceCode,sFolderToPublish);
if (false == Directory.Exists(sPathToSourceCode))
DebugMsg._Error("Folder to publish doesn't exist: {0}", sPathToSourceCode);
else
{
String sPathTargetFolder = Path.Combine(sTargetFolder,sFolderToPublish);
String sPathTargetFolder_OneProjectPerFolder = Path.Combine(sTargetFolder,"_OneProjectPerFolder");
Files.deleteFolder(sPathTargetFolder);
//Files.deleteFolder(sPathTargetFolder_OneProjectPerFolder);
Files.checkIfDirectoryExistsAndCreateIfNot(sPathTargetFolder);
Files.checkIfDirectoryExistsAndCreateIfNot(sPathTargetFolder_OneProjectPerFolder);
String sPathTargetFolder_Binaries = Files.checkIfDirectoryExistsAndCreateIfNot(Path.Combine(sPathTargetFolder,"Binaries"));
List lsVSProjects = Files.getListOfAllFilesFromDirectory(sPathToSourceCode, true, "*.sln");
DebugMsg._Info("Found {0} VS projects",lsVSProjects.Count);
foreach(String sVsProject in lsVSProjects)
{
String sProjectName = Path.GetFileNameWithoutExtension(sVsProject);
if (true == File.Exists(Path.Combine(Path.GetDirectoryName(sVsProject),"web.config")))
DebugMsg._Info("Skipping project {0} because it is a webproject", sProjectName);
else
{
DebugMsg._Debug("Publishing project:{0}", sProjectName);
String sPathToCompiledFiles = Path.Combine(Path.GetDirectoryName(sVsProject),"bin\\debug");
if (false == Directory.Exists(sPathToCompiledFiles))
DebugMsg._Error("Couldn't find folder with binaries:{0}", sPathToCompiledFiles);
else
{
// handle copy into sFolderToPublish
Files.copyFilesFromDirectoryToDirectory(sPathToCompiledFiles, sPathTargetFolder_Binaries);
createBatchFileForExe(sProjectName, sPathTargetFolder_Binaries,sPathTargetFolder);
createBatchFileForExe(sProjectName, sPathTargetFolder_Binaries,sTargetFolder);

// handle copy into _OneProjectPerFolder
//String sOneProjectPerFolder = Path.Combine(sPathTargetFolder_OneProjectPerFolder,Path.GetFileName(Path.GetDirectoryName(sVsProject)));
String sOneProjectPerFolder = Path.Combine(sPathTargetFolder_OneProjectPerFolder,sProjectName);
String sOneProjectPerFolder_Binary = Path.Combine(sOneProjectPerFolder,"Binaries");

DebugMsg._Info(" sOneProjectPerFolder = {0}", sOneProjectPerFolder);

Files.deleteFolder(sOneProjectPerFolder);
Files.checkIfDirectoryExistsAndCreateIfNot(sOneProjectPerFolder);
Files.checkIfDirectoryExistsAndCreateIfNot(sOneProjectPerFolder_Binary);
Files.copyFilesFromDirectoryToDirectory(sPathToCompiledFiles, sOneProjectPerFolder_Binary);
createBatchFileForExe(sProjectName, sOneProjectPerFolder_Binary,sOneProjectPerFolder);
}
}
}
removeVsHostFiles(sPathTargetFolder_Binaries);
if (bDeleteOunceAndMySqlDlls)
deleteOunceAndMySqlDlls(sPathTargetFolder_Binaries);
}
}

public static void removeVsHostFiles(String sPathToRemoveFiles)
{
List lsVsHostFiles = Files.getListOfAllFilesFromDirectory(sPathToRemoveFiles,false,"*.vshost.exe");
foreach(String sFile in lsVsHostFiles)
Files.deleteFile(sFile);
}

public static void createBatchFileForExe(String sProjectName, String sPathToFiles, String sPathForBatchFiles)
{
String sExeFile = Path.Combine(sPathToFiles,String.Format("O2_{0}.exe",sProjectName));
if (false == File.Exists(sExeFile) && false == File.Exists(sExeFile.Replace(".exe",".dll")))
DebugMsg._Error("Could not find exe: {0}", sExeFile);
else
{
String sBatchFile = Path.Combine(sPathForBatchFiles, Path.GetFileName(sExeFile)+ ".bat");
String sVirtualPathToExe = sExeFile.Replace(sPathForBatchFiles, "");
sVirtualPathToExe = (sVirtualPathToExe[0] == '\\' ? sVirtualPathToExe.Substring(1) : sVirtualPathToExe);
String sExeDirectory = Path.GetDirectoryName(sVirtualPathToExe);
String sExeName = Path.GetFileName(sVirtualPathToExe);
String sFileContents = String.Format("cd {0} \nstart {1} ", sExeDirectory , sExeName);
Files.WriteFileContent(sBatchFile,sFileContents);
}
}

public static void deleteOunceAndMySqlDlls(String sPathToSearch)
{
List lsFilesToDelete = Files.getListOfAllFilesFromDirectory(sPathToSearch,true,"jnbOunce.dll");
Files.getListOfAllFilesFromDirectory(lsFilesToDelete,sPathToSearch,true,"JNBShare.dll",false);
Files.getListOfAllFilesFromDirectory(lsFilesToDelete,sPathToSearch,true,"JNBSharedMem.dll",false);
Files.getListOfAllFilesFromDirectory(lsFilesToDelete,sPathToSearch,true,"MySql.Data.dll",false);
foreach(String sFileToDelete in lsFilesToDelete)
{
Files.deleteFile(sFileToDelete);
DebugMsg._Info("Deleted File:{0}", sFileToDelete);
}
}

}