I'm trying to iteratively (part of automation):
Create backup of the projects in solution (physical files on the filesystem)
Using Microsoft.Build programmatically load and change projects inside of the solution (refernces, includes, some other properties)
Build it with console call of msbuild
Restore projects (physically overriding patched versions from backups)
This approach works well for first iteration, but for second it appears that it does not load restored projects and trying to work with values that I patched on the first iteration. It looks like projects are cached: inside of the csproj files I see correct values, but on the code I see previously patched values.
My best guess is that Microsoft.Build is caching solution/projects in the context of the current process.
Here is code that is responsible to load project and call method to update project information:
private static void ForEachProject(string slnPath, Func<ProjectRootElement> patchProject)
{
SolutionFile slnFile = SolutionFile.Parse(slnPath);
var filtredProjects = slnFile
.ProjectsInOrder
.Where(prj => prj.ProjectType == SolutionProjectType.KnownToBeMSBuildFormat);
foreach (ProjectInSolution projectInfo in filtredProjects)
{
try
{
ProjectRootElement project = ProjectRootElement.Open(projectInfo.AbsolutePath);
patchProject(project);
project.Save();
}
catch (InvalidProjectFileException ex)
{
Console.WriteLine("Failed to patch project '{0}' with error: {1}", projectInfo.AbsolutePath, ex);
}
}
}
There is Reload method for the ProjectRootElement that migh be called before iteraction with content of the project.
It will enforce Microsoft.Build to read latest information from the file.
Code that is working for me:
private static void ForEachProject(string slnPath, Func<ProjectRootElement> patchProject)
{
SolutionFile slnFile = SolutionFile.Parse(slnPath);
var filtredProjects = slnFile
.ProjectsInOrder
.Where(prj => prj.ProjectType == SolutionProjectType.KnownToBeMSBuildFormat);
foreach (ProjectInSolution projectInfo in filtredProjects)
{
try
{
ProjectRootElement project = ProjectRootElement.Open(projectInfo.AbsolutePath);
project.Reload(false); // Ignore cached state, read actual from the file
patchProject(project);
project.Save();
}
catch (InvalidProjectFileException ex)
{
Console.WriteLine("Failed to patch project '{0}' with error: {1}", projectInfo.AbsolutePath, ex);
}
}
}
Note: It better to use custom properties inside of the project and provide it for each msbuild call instead of physical project patching. Please consider it as better solution and use it if possible.
Related
Im using the TexturePacker implemented by LibGDX to load my sprites.
For some reason however, the files are not found and it gives me this exception:
Exception in thread "main" java.lang.RuntimeException: Error packing images.
at com.badlogic.gdx.tools.texturepacker.TexturePacker.process(TexturePacker.java:620)
at com.zebleck.OneRoom.desktop.DesktopLauncher.processSprites(DesktopLauncher.java:35)
at com.zebleck.OneRoom.desktop.DesktopLauncher.main(DesktopLauncher.java:17)
Caused by: java.lang.IllegalArgumentException: Input file does not exist: C:\Users\Kontor\Desktop\Codeporn\LibGDX-workspace\OneRoom\desktop\sprites\input
at com.badlogic.gdx.tools.FileProcessor.process(FileProcessor.java:117)
at com.badlogic.gdx.tools.texturepacker.TexturePackerFileProcessor.process(TexturePackerFileProcessor.java:70)
at com.badlogic.gdx.tools.texturepacker.TexturePacker.process(TexturePacker.java:618)
... 2 more
This code is causing the error:
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.width = 800;
config.height = 800;
deleteFiles();
processSprites();
new LwjglApplication(new OneRoom(), config);
}
public static void deleteFiles() {
File outputDir = new File("../android/assets/sprites/output");
File[] listFiles = outputDir.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
file.delete();
}
}
}
public static void processSprites() {
TexturePacker.Settings settings = new TexturePacker.Settings();
//System.out.println(Gdx.files.internal("sprites/input/player.png").toString());
TexturePacker.process(settings, "sprites/input", "sprites/output", "pack"); // THIS LINE CAUSES THE ERROR
}
I also got the EXACT same code in another project and it works just fine. I haven't found any differences in the project properties yet.
Make sure the sprites actually exist in that directory.
Sounds patronising but I was having the same issue and for me I was being misled by my assets directory in my desktop project being a "Linked Folder" that was actually just a reference to the assets folder of my core project. So in eclipse the folder is there and looks like there should be no problem but looking through windows file explorer it was clear the files didn't actually exist at that location.
My fix was to change the input and output to step back and check the core directory instead of the desktop.
So instead of:
TexturePacker.process(settings, "sprites/input", "sprites/output", "pack");
The following would work:
TexturePacker.process(settings, "../core/sprites/input", "../core/sprites/output", "pack");
Now I don't know your exact setup but considering your code works in a different project I would wager that the other project has the assets actually stored in the desktop directory where as this one stores the images in the core directory.
In SSIS
In a folder there are many flat files and by using for each loop container we are processing it one by one. If any new file is placed in the folder and it is still in copying mode. Then, We should not take it for continue process. We should process Only fully copied file alone to our next process.
How can we achieve this? Please give your suggestions.
Best way I have done this in the past is to use a C# Script Task and try to open the file - If the file is still being copied you will get an error (which you Catch). Then you can set a boolean variable to conditionally process the file if the Open worked.
EG:
Boolean b = true;
FileStream f;
try
{
f = new FileStream("C:\\Test\\Test.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException e)
{
if (e.Message == "hello")
{
b = false;
}
}
Hi I'm trying to save a file from a Java Webstart Application.
public class Main {
public static void main(String[] args) {
try {
FileSaveService fos = (FileSaveService) ServiceManager.lookup("javax.jnlp.FileSaveService");
//open Dialog
FileContents fc = fos.saveFileDialog("c:/data", null, new ByteArrayInputStream("Hallo Welt".getBytes()), "name.txt");
System.out.println("FileContents: " + fc);
} catch (UnavailableServiceException e) {
System.err.println("***" + e + "***");
} catch (IOException e) {
System.err.println("***" + e + "***");
}
//wait a minute
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
System.exit(0);
}
}
Everything works except that the dialog that comes up looks like a "open" file dialog, not like a "save" file dialog:
Any help would be appreciated.
The File-Open-dialog is necessary. You first need to let the user choose where to save the data. Thus a previous call to openFileDialog is absolute necessary for a jnlp-application. You are not allowed to directly save to a specific location like c:
If you follow the mentioned link (http://docs.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/examples.html#FileSaveService) you should be successful.
EDIT:
for clarification.
Saving via javax.jnlp.FileSaveService does exactly need one call. For instance calling saveFileDialog() like this should be sufficient:
fss.saveFileDialog(null, null, new ByteArrayInputStream("Hallo Welt".getBytes() ), "newFileName.txt");
The necessity of one User-Dialogue is due to the anonymizing nature of jnlp, where your application should not get any hint about the user-filesystem.
However, I have to admit, that this was not your question.
Your main trouble comes from the java app everytime presenting the "open-dialogue" instead of the "save-dialogue".
This should not happen! If I may humbly assume from your snippet where you call fos.saveFileDialog: did you just initialize fos by the FileOpenService instead of the FileSaveService?
More details on the FileSaveService can be found here: http://docs.oracle.com/javase/7/docs/jre/api/javaws/jnlp/javax/jnlp/FileSaveService.html
This seems to be fixed in JRE bersion 1.7.0_21-b11 Java HotSpot(TM) 64-Bit Server VM
And there it is: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=2227257
I am trying to render emails in a windows service host.
I use RazorEngine 3 forked by coxp which has support for Razor 2.
https://github.com/coxp/RazorEngine/tree/release-3.0/src
This works fine for a couple of emailtemplates but there is one causing me problems.
#model string
Click here to enter a new password for your account.
This throws a CompilationException: The name 'WriteAttribute' does not exist in the current context. So passing in a string as model and putting it in the href-attribute causes problems.
I can make it work by changing this line by:
#Raw(string.Format("Klik hier.", #Model))
but this makes the template very unreadable and harder to pass along to a marketing department for further styling.
I like to add that referencing the RazorEngine by using a Nuget package is not a solution since it is based on Razor 1 and somewhere along the process the DLL for system.web.razor gets replaced by version 2 which breaks any code using RazorEngine. It seems more interesting to use Razor 2 to benefit from the new features and to be up to date.
Any suggestions on how to fix this would be great. Sharing your experiences is also very welcome.
UPDATE 1
It seems like calling SetTemplateBaseType might help, but this method does not exist anymore, so I wonder how to be able to bind the templatebasetype?
//Missing method in the new RazorEngine build from coxp.
Razor.SetTemplateBaseType(typeof(HtmlTemplateBase<>));
I use Windsor to inject the template service rather than using the Razor object. Here is a simplified part of the code that shows how to set the base template type.
private static ITemplateService CreateTemplateService()
{
var config = new TemplateServiceConfiguration
{
BaseTemplateType = typeof (HtmlTemplateBase<>),
};
return new TemplateService(config);
}
RazorEngine 3.1.0
Little bit modified example based on coxp answer without the injection:
private static bool _razorInitialized;
private static void InitializeRazor()
{
if (_razorInitialized) return;
_razorInitialized = true;
Razor.SetTemplateService(CreateTemplateService());
}
private static ITemplateService CreateTemplateService()
{
var config = new TemplateServiceConfiguration
{
BaseTemplateType = typeof (HtmlTemplateBase<>),
};
return new TemplateService(config);
}
public static string ParseTemplate(string name, object model)
{
InitializeRazor();
var appFileName = "~/EmailTemplates/" + name + ".cshtml";
var template = File.ReadAllText(HttpContext.Current.Server.MapPath(appFileName));
return RazorEngine.Razor.Parse(template, model);
}
In my program I have a string which contains URLs separated by /n (One per line)
Let's say the string is called "links". I want to take this string and generate a HTML file that will automatically open in my default browser which will make each URL a hyperlink (one per line). How would I make such a report not using any third party components using WPF C# 4.0? I want the report to be generated by clicking a button called "Export".
There are plenty of ways to do this, but here is a quick and dirty example (debugging may be necessary since I wrote this on the fly). [Edit: Now uses Uri objects to formulate the actual address.]
private void export_Click(object sender, RoutedEventArgs e)
{
string tempFileName = "list.html";
string links = "http://www.google.com/#sclient=psy&hl=en&site=&source=hp&q=test+me&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=ddfbf15c2e2f4021\nhttp://www.testme.com/Test-Prep.html?afdt=Q3RzePF0jU8KEwja-5WM7PqkAhUUiZ0KHaoG_wcYASAAMJbwoAM4MEC4w6uX7dS53gdQlvCgA1CEra8PUJzr_xNQg73wFVCKttweUJStzNoBUNv67ZsD";
List<Uri> uriCollection = new List<Uri>();
foreach (string url in links.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries))
{
uriCollection.Add(new Uri(url));
}
// Create temporary file.
using (TextWriter writer = new StreamWriter(tempFileName))
{
try
{
writer.WriteLine("<html>");
writer.WriteLine("<head><title>Links</title></head>");
writer.WriteLine("<body>");
writer.WriteLine("<p>");
foreach (Uri uri in uriCollection)
{
writer.WriteLine("{1}<br />", uri.OriginalString, uri.Host);
}
writer.WriteLine("</p>");
writer.WriteLine("</body>");
writer.WriteLine("</html>");
}
catch (Exception ex)
{
System.Diagnostics.Trace.TraceError(ex.Message);
}
finally
{
writer.Close();
}
}
// Open browser with temporary file.
if (File.Exists(tempFileName))
{
System.Diagnostics.Process.Start(tempFileName);
}
}
The 'Export' button is wired to the event 'export_Click'. I hard-coded the the string with '\n''s for the example. Simply break these apart using split and write a temporary file creating the HTML you need. Then, once the file is completed, you can open it using the Process.Start() method.
Ideally this can be done using DataBinding and other elements available in WPF if the need to open a browser window was not required. This would also remove any external dependencies the program may have.