How can I build all Solution (.SLN) files under a directory - build

We needed to automate testing that all of the C#, C++, & VB.NET samples we ship compile properly. We need it to build all files without our listing each one. Listing each one means if someone forgets to add a new one (which will happen someday), explicit calls will miss it. By walking all .sln files, we always get everything.
Doing this is pretty easy:
Install the samples on a clean VM (that we revert back to the snapshot for each test run).
Create a BuildAll.proj (MSBuild) file that calls all the .sln files installed.
Use MSBuild to run the generated BuildAll.proj file
Step 2 requires a means to generate the BuildAll.proj file. Is there any way to tell MSBuild to run all .sln files under a sub-directory or to create a BuildAll.proj that calls all the underlying .slnl files?

This PowerShell script will restore all NuGet packages and build all solutions recursively in the current directory. Make sure nuget.exe and msbuild are in your PATH.
$baseDir = (Get-Item -Path ".\" -Verbose).FullName
$items = Get-ChildItem -Path $baseDir -Include *.sln -Recurse
foreach ($item in $items){
nuget restore $item
msbuild $item
}

You can use the following PowerShell for .NET Core projects
$baseDir = (Get-Item -Path ".\" -Verbose).FullName
Write-Host ("Scanning *.sln files in " + $baseDir)
$solutionPaths = Get-ChildItem -Path $baseDir -Include *.sln -Recurse
Write-Host ("Total found: " + $solutionPaths.Count)
foreach ($solutionPath in $solutionPaths) {
Write-Host ("Building => " + $solutionPath)
dotnet build $solutionPath
}

We couldn't find anything so we wrote a program that creates a BuildAll.proj that calls all .sln files under a directory. Full solution is at Windward Wrocks (my blog).
The code is:
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace BuildDotNetTestScript
{
/// <summary>
/// Builds a test script to compile all .sln files under the directory in.
/// </summary>
public class Program
{
private static readonly XNamespace xmlns = "http://schemas.microsoft.com/developer/msbuild/2003";
private enum VS_VER
{
VS_2005,
VS_2008,
VS_2010,
NONE
}
private static VS_VER vsVersion = VS_VER.NONE;
/// <summary>
/// Build TestAll.proj for all .sln files in this directory and sub-directories.
/// </summary>
/// <param name="args">Optional: [-VS2005 | -VS2008 | -VS2010] TestAll.proj root_folder</param>
public static void Main(string[] args)
{
int indexArgs = 0;
if (args.Length >= 1 && args[0][0] == '-')
{
indexArgs = 1;
switch (args[0].ToUpper().Trim())
{
case "-VS2005":
vsVersion = VS_VER.VS_2005;
break;
case "-VS2008":
vsVersion = VS_VER.VS_2008;
break;
case "-VS2010":
vsVersion = VS_VER.VS_2010;
break;
default:
Console.Error.WriteLine("Only options are -VS2005, -VS2008, or -VS2010");
Environment.Exit(1);
return;
}
}
string projFile = Path.GetFullPath(args.Length > indexArgs ? args[indexArgs] : "TestAll.proj");
string rootDirectory =
Path.GetFullPath(args.Length > indexArgs + 1 ? args[indexArgs + 1] : Directory.GetCurrentDirectory());
Console.Out.WriteLine(string.Format("Creating project file {0}", projFile));
Console.Out.WriteLine(string.Format("Root directory {0}", rootDirectory));
XDocument xdoc = new XDocument();
XElement elementProject = new XElement(xmlns + "Project");
xdoc.Add(elementProject);
elementProject.Add(new XAttribute("DefaultTargets", "compile"));
elementProject.Add(new XAttribute("ToolsVersion", "3.5"));
XElement elementPropertyGroup = new XElement(xmlns + "PropertyGroup");
elementProject.Add(elementPropertyGroup);
XElement elementDevEnv = new XElement(xmlns + "devenv");
elementPropertyGroup.Add(elementDevEnv);
elementDevEnv.Value = "devenv.exe";
XElement elementTarget = new XElement(xmlns + "Target");
elementProject.Add(elementTarget);
elementTarget.Add(new XAttribute("Name", "compile"));
// add .sln files - recursively
AddSlnFiles(elementTarget, rootDirectory, rootDirectory);
Console.Out.WriteLine("writing project file to disk");
// no BOM
using (var writer = new XmlTextWriter(projFile, new UTF8Encoding(false)))
{
writer.Formatting = Formatting.Indented;
xdoc.Save(writer);
}
Console.Out.WriteLine("all done");
}
private static void AddSlnFiles(XElement elementTarget, string rootDirectory, string folder)
{
// add .sln files
foreach (string fileOn in Directory.GetFiles(folder, "*.sln"))
{
// .../JS/... is VS2005
bool isJSharp = fileOn.ToUpper().Replace('\\', '/').Contains("/JS/");
bool versionMatch = true;
switch (vsVersion)
{
case VS_VER.VS_2005:
if ((!fileOn.ToUpper().Contains("VS2005")) && (! isJSharp))
versionMatch = false;
break;
case VS_VER.VS_2008:
if (isJSharp || !fileOn.ToUpper().Contains("VS2008"))
versionMatch = false;
break;
case VS_VER.VS_2010:
if (isJSharp || !fileOn.ToUpper().Contains("VS2010"))
versionMatch = false;
break;
default:
if (isJSharp || fileOn.ToUpper().Contains("VS2005") || fileOn.ToUpper().Contains("VS2008") || fileOn.ToUpper().Contains("VS2010"))
versionMatch = false;
break;
}
if (!versionMatch)
continue;
string command = string.Format("\"$(devenv)\" \"{0}\" /Rebuild", Path.GetFileName(fileOn));
XElement elementExec = new XElement(xmlns + "Exec");
elementExec.Add(new XAttribute("Command", command));
string workingFolder;
if (folder.StartsWith(rootDirectory))
{
workingFolder = folder.Substring(rootDirectory.Length).Trim();
if ((workingFolder.Length > 0) && (workingFolder[0] == Path.DirectorySeparatorChar || workingFolder[0] == Path.AltDirectorySeparatorChar))
workingFolder = workingFolder.Substring(1);
}
else
workingFolder = folder;
if (workingFolder.Length > 0)
elementExec.Add(new XAttribute("WorkingDirectory", workingFolder));
elementTarget.Add(elementExec);
}
// look in sub-directories
foreach (string subDirectory in Directory.GetDirectories(folder))
AddSlnFiles(elementTarget, rootDirectory, subDirectory);
}
}
}

Related

How set console argument in symfony 3 console command testing

I am unable to set Argument while creating Unit test case in Symfony 3.4 console command application
My Console command
php bin\console identification-requests:process input.csv
My Console code
protected function execute(InputInterface $input, OutputInterface $output)
{
// Retrieve the argument value using getArgument()
$csv_name = $input->getArgument('file');
// Check file name
if ($csv_name == 'input.csv') {
// Get input file from filesystem
$csvData = array_map('str_getcsv', file($this->base_path.$csv_name));
$formatData = Helpers::formatInputData($csvData);
// Start session
$session = new Session();
$session->start();
foreach ($csvData as $key => $data) {
if (!empty($data[0])) {
$validation = Validator::getInformationData($data, $formatData[$data[1]]);
if (!empty($validation)) {
$output->writeln($validation);
} else {
$output->writeln('valid');
}
}
}
} else {
$output->writeln('Invalid file!');
}
}
I tried the following test code
$kernel = static::createKernel();
$kernel->boot();
$application = new Application($kernel);
$application->add(new DocumentCommand());
$command = $application->find('identification-requests:process')
->addArgument('file', InputArgument::REQUIRED, "input.csv");
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName()
));
$output = $commandTester->getOutput();
$this->assertContains('valid',$output);
When I run unit test it showing the following error message
There was 1 error:
1) Tests\AppBundle\Command\DocumentCommandTest::testExecute
Symfony\Component\Console\Exception\LogicException: An argument with name "file" already exists.
I think you should put your input in the command tester and not in the command finder, in this case you are trying to create another parameter for that command, and that's why it's telling you that it is existing already.
try this
$command = $application->find('identification-requests:process');
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName(),
'file' => 'input.csv'
));

Is there any way to get the Active Solution Configuration name in C# Code?

I have three solution configurations in my UWP solution in Visual Studio:
Development
Staging
Production
Each is a associated with a different web service and auth provider in the configuration files. In my code, how do I tell which one is which? In the past I've explicitly provided DEFINE constants, but there must be a better way by now.
The active solution configuration is stored in the .suo file beneath the .vs directory at the root solution folder. The .suo file has a compound file binary format, which means you can't just parse it with text manipulation tools.
However, using OpenMcdf -- a tool that can be used to manipulate these types of files -- you can easily get the active solution configuration.
Here's a console app I wrote that works. Feel free to adapt the code to your situation:
using OpenMcdf;
using System;
using System.IO;
using System.Linq;
using System.Text;
namespace GetActiveBuildConfigFromSuo
{
internal enum ProgramReturnCode
{
Success = 0,
NoArg = -1,
InvalidFileFormat = -2
}
internal class Program
{
private const string SolutionConfigStreamName = "SolutionConfiguration";
private const string ActiveConfigTokenName = "ActiveCfg";
internal static int Main(string[] args)
{
try
{
ValidateCommandLineArgs(args);
string activeSolutionConfig = ExtractActiveSolutionConfig(
new FileInfo(args.First()));
throw new ProgramResultException(
activeSolutionConfig, ProgramReturnCode.Success);
}
catch (ProgramResultException e)
{
Console.Write(e.Message);
return (int)e.ReturnCode;
}
}
private static void ValidateCommandLineArgs(string[] args)
{
if (args.Count() != 1) throw new ProgramResultException(
"There must be exactly one command-line argument, which " +
"is the path to an input Visual Studio Solution User " +
"Options (SUO) file. The path should be enclosed in " +
"quotes if it contains spaces.", ProgramReturnCode.NoArg);
}
private static string ExtractActiveSolutionConfig(FileInfo fromSuoFile)
{
CompoundFile compoundFile;
try { compoundFile = new CompoundFile(fromSuoFile.FullName); }
catch (CFFileFormatException)
{ throw CreateInvalidFileFormatProgramResultException(fromSuoFile); }
if (compoundFile.RootStorage.TryGetStream(
SolutionConfigStreamName, out CFStream compoundFileStream))
{
var data = compoundFileStream.GetData();
string dataAsString = Encoding.GetEncoding("UTF-16").GetString(data);
int activeConfigTokenIndex = dataAsString.LastIndexOf(ActiveConfigTokenName);
if (activeConfigTokenIndex < 0)
CreateInvalidFileFormatProgramResultException(fromSuoFile);
string afterActiveConfigToken =
dataAsString.Substring(activeConfigTokenIndex);
int lastNullCharIdx = afterActiveConfigToken.LastIndexOf('\0');
string ret = afterActiveConfigToken.Substring(lastNullCharIdx + 1);
return ret.Replace(";", "");
}
else throw CreateInvalidFileFormatProgramResultException(fromSuoFile);
}
private static ProgramResultException CreateInvalidFileFormatProgramResultException(
FileInfo invalidFile) => new ProgramResultException(
$#"The provided file ""{invalidFile.FullName}"" is not a valid " +
$#"SUO file with a ""{SolutionConfigStreamName}"" stream and an " +
$#"""{ActiveConfigTokenName}"" token.", ProgramReturnCode.InvalidFileFormat);
}
internal class ProgramResultException : Exception
{
internal ProgramResultException(string message, ProgramReturnCode returnCode)
: base(message) => ReturnCode = returnCode;
internal ProgramReturnCode ReturnCode { get; }
}
}
Download DTE nuget packages : EnvDTE.8.0.2
Add below code
EnvDTE.DTE DTE = Marshal.GetActiveObject("VisualStudio.DTE.15.0") as EnvDTE.DTE;
var activeConfig = (string)DTE.Solution.Properties.Item("ActiveConfig").Value;

Sending TWIG output to a zip in Symfony for Templating

I need to create a system that will be able to output a template (a zip with an index.html with all js, css, img etc files as required) based on a set of options (include menu 1, use small footer etc).
So I can put code blocks in so it would not be hard to actually output to the browser the required look, but what if I want to save the rendered html (and this means we would need to change the locations of files like js etc) to a zip instead?
Any ideas? At the moment I'm thinking it would all need to be processed in PHP anyhow (the controller) and TWIG may not be too helpful, but that would make it more complicated.
So I worked this one out, here is the controller to output the required twig zipped:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
/**
* #Route("/{variableName}", name="homepage"})
*/
public function indexAction($variableName = "Default")
{
//do whatever you need to get the template correct
//send rendered TWIG to a variable
$renderedhtml = $this->render('default/index.html.twig', [
'variableName' => $variableName
])->getContent();
$zipName = "template.zip";
$main = $this->get('kernel')->getRootDir() . '/../web/workingdir/';
$vername = time(); //we use the exact time to get a unique dir name
//remove any projects older than a few minutes (5 in this case)
$dirHandle = opendir($main);
while($item = readdir($dirHandle)){
if(is_dir($main . $item) && $item != '.' && $item != '..') {
if ( ( ( $item + ( 60 * 5 ) ) < $vername ) ) {
system('/bin/rm -rf ' . escapeshellarg($main . $item));
}
}
}
//make the project dir and populate it with files (in this case just the template as index.html, you would want to copy all other files here though)
mkdir($main.$vername);
file_put_contents($main.$vername."/index.html", $renderedhtml);
//create the zip
$zip = new \ZipArchive();
$zip->open($zipName, \ZipArchive::CREATE);
$zip = $this->createZip($zip, $main.$vername);
$zip->close();
//get the zip ready to return
header('Content-Type', 'application/zip');
header('Content-disposition: attachment; filename="' . $zipName . '"');
header('Content-Length: ' . filesize($zipName));
readfile($zipName);
}
// Create a zip with zip object and root dir params (find and include sub-dirs with content)
private function createZip($zip, $source) {
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true) {
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = str_replace('\\', '/', $file);
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;
$file = realpath($file);
if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
return $zip;
}
}
Once you get it to this stage, you will just have to add any other required files into the working dir, but I think that could be done under a different issue, this one is about getting the website into a html in a zip.
Hope this helps someone.

Parsing Microsoft Office 2013 MRU Lists in Registry using Perl

I am currently trying to parse the keys in a Windows 7 registry containing the MRU lists for Microsoft Office 2013. However when I attempt to run the Perl script in RegRipper it says the plugin was not successfully run. Im not sure if there is a syntax error in my code or if it is unable to parse the registry as I have it written. The biggest problem is that one of the keys is named after the user's LiveId (it appear as LiveId_XXXXXXX) and this changes from user to user so i would like this plugin to work no matter what the user's LiveId is. Thanks!
my $reg = Parse::Win32Registry->new($ntuser);
my $root_key = $reg->get_root_key;
# ::rptMsg("officedocs2013_File_MRU v.".$VERSION); # 20110830 [fpi] - redundant
my $tag = 0;
my $key_path = "Software\\Microsoft\\Office\\15.0";
if (defined($root_key->get_subkey($key_path))) {
$tag = 1;
}
if ($tag) {
::rptMsg("MSOffice version 2013 located.");
my $key_path = "Software\\Microsoft\\Office\\15.0";
my $of_key = $root_key->get_subkey($key_path);
if ($of_key) {
# Attempt to retrieve Word docs
my $word_mru_key_path = 'Software\\Microsoft\\Office\\15.0\\Word\\User MRU';
my $word_mru_key = $of_key->get_subkey($word_mru_key_path);
foreach ($word_mru_key->get_list_of_subkeys())
{
if ($key->as_string() =~ /LiveId_\w+/)
{
$word = join($key->as_string(),'\\File MRU');
::rptMsg($key_path."\\".$word);
::rptMsg("LastWrite Time ".gmtime($word_key->get_timestamp())." (UTC)");
my #vals = $word_key->get_list_of_values();
if (scalar(#vals) > 0) {
my %files
# Retrieve values and load into a hash for sorting
foreach my $v (#vals) {
my $val = $v->get_name();
if ($val eq "Max Display") { next; }
my $data = getWinTS($v->get_data());
my $tag = (split(/Item/,$val))[1];
$files{$tag} = $val.":".$data;
}
# Print sorted content to report file
foreach my $u (sort {$a <=> $b} keys %files) {
my ($val,$data) = split(/:/,$files{$u},2);
::rptMsg(" ".$val." -> ".$data);
}
}
else {
::rptMsg($key_path.$word." has no values.");
}
else {
::rptMsg($key_path.$word." not found.");
}
::rptMsg("");
}
}
The regex
LiveId_(\w+)
will grab the string after LiveId_ and you can reference it with a \1 like this

get the list of source files from existing eclipse project using CDT

i am working on CDT eclipse plug in development, i am trying to get the list of sources files which exist in eclipse project explorer using CDT code using following code ,which results null.
Case1:
IFile[] files2 = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(new URI("file:/"+workingDirectory));
for (IFile file : files2) {
System.out.println("fullpath " +file.getFullPath());
}
Case2:
IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(getProject().getRawLocationURI());
for (IFile file : files) {
System.out.println("fullpath " +file.getFullPath());
}
Case3:
IFile[] files3 = ResourceLookup.findFilesByName(getProject().getFullPath(),ResourcesPlugin.getWorkspace().getRoot().getProjects(),false);
for (IFile file : files3) {
System.out.println("fullpath " +file.getFullPath());
}
Case4:
IFolder srcFolder = project.getFolder("src");
Case 1 ,2,3 giving me output null, where i am expecting list of files;
in Case 4: i am getting the list of "helloworld/src" files, but i am expecting to get the files from the existing project mean main root ,ex:"helloworld"
please suggest me on this.
You can either walk through the worspace resources tree using IResourceVisitor - or you can walk through CDT model:
private void findSourceFiles(final IProject project) {
final ICProject cproject = CoreModel.getDefault().create(project);
if (cproject != null) {
try {
cproject.accept(new ICElementVisitor() {
#Override
public boolean visit(final ICElement element) throws CoreException {
if (element.getElementType() == ICElement.C_UNIT) {
ITranslationUnit unit = (ITranslationUnit) element;
if (unit.isSourceUnit()) {
System.out.printf("%s, %s, %s\n", element.getElementName(), element.getClass(), element
.getUnderlyingResource().getFullPath());
}
return false;
} else {
return true;
}
}
});
} catch (final CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Note there may be more source files then you actually want (e.g. you may not care system about headers) - you can filter them by checking what the underlying resource is.