Share a file for C# project at home and at office - c#

I have formed a number of source code files as my library. For example, I wrote LinqExtension.cs providing Median() function.
Now I'm working on a project which needs LinqExtension.cs. As usually, I link the file to the project. As introduced here. The reason that I link files rather than copying them is to keep the files at a single location. If I modify a file, all dependent projects get affected.
I also add the project to Subversion and upload to and download from Google Code. The linked file is not under version control.
I work on the project at home as well as at office. I hate copying the linked file to my office, which makes the file not single.
I figure out a solution that add <Compile Include="http://www.example.com/LinqExtension.cs"/> to csproj file so that the file only exists on the Internet. Once I upload a new verison of the file, all dependent projects get affected. Unfortunately the solution doesn't work.
Any other suggesions or better practice?

A better way would be to share your core library at the binary level, rather than at source code. You could set up a private Nuget repository to make this easier.
If it is absolutely necessary to share files, you can use pre-build actions in your project to copy the file from a common location, or even download them from google code. It's not clean, but if you don't want to use source control for it then I don't think you will find a clean way.

I like to keep a library folder of binaries in my Dropbox. That way Common libraries that I use can be accessed from my home and work project workspaces and the service keeps the version up to date.

Related

Access files from inside class library

Background Info:
I needed to convert PDF files to images and have full control over how this is done, and noticed that this functionality does exist in NuGet packages, but only in paid packages. Because I am a cheap bastard I refused to pay for this as I can easily do this in python for free.
I created a way to call a python script from c# that converts the PDF I want to a jpg in the exact way I want, and used this for a while. Now I want to build on this project and thought about creating this into a class library that I can use in multiple projects.
The problem I am now facing is that for this python script to work I need a specific resource, a couple of files in a folder. When I add the existing project (the pdf to img converter) to another project it just adds the DLL, and ignores everything else. The files I need are still in a folder inside the class library.
When I try to access the necessary files the current path is inside the new project, and there those files don't exist.
Directory.GetCurrentDirectory(); tells me that the current path is inside the new project.
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); tells me where the DLL is located, but the files I need aren't there.
I have tried searching for a way to access files inside the class library but I came up with nothing useful.
The question:
Is there a way to access files inside of a project that you are referencing? or is there a way to ensure the files I need are brought over too, instead of just the DLL?
Just for completion, I intend to make this into my own private Nuget package, and Im sure I can get the files I need in the .nupkg, but the same problem will persist, How do I access those files.
If anyone has any experience with this kind of problem, I would be happy to hear from you. Thank you in advance.
I would try to use a c# solution in this case to convert pdf to jpeg.
May be something like this:
Convert pdf to jpeg using a free c# solution
or optimal solution: a nuget package, which you can define as dependency in your nuget package
but there are many other solutions instead a phyton script.
Reason:
If you like to provide a nuget package (even if its company intern) the library
should not have dependencies that are not transparent.
In your case ist the dependency to an available phyton installation in the envirement, where the package will be used.
The software that uses the package must have the rights to execute a phyton script
In additional, you will resolve the issue, that you must be able to store and execute a script, if you use a c# solution

Mono XBuild publish MVC site

I've got Monoserve and Nginx running perfectly in Ubuntu however I still have to publish the website locally on a Windows box using MSBuild and then copy the files over.
Preferably I'd like to have a Linux CI server that does this instead using XBuild however I can only get it to build the project into .dlls, how do I publish and deploy it with js, css, views, etc?
Typically the "build dlls" part is the hardest part. If you've got that solved, you're 80% there. The other half is publishing content. In it's most elementary aspect, you're copying a portion of the files from the source dir to the website folder. MSDeploy is Microsoft's answer to it, and it's waaaaaay too complex. I built an NAnt task that does this, though that also doesn't apply to your specific scenario. However, the general methodology can:
Crawl the sln file looking for web projects. What makes a web project? Technically guids in the csproj file or project type ids in the sln file. I cheated and defined it as "the target folder includes a web.config file". If you've only got one website project in your solution, you can skip this step, and just hard-code the .csproj file.
Crawl the csproj file looking for <Content Include="some\file.ext" /> nodes. XPath could do this, Linq to XML could do it too. This gives you all the .aspx, .cshtml, .js, .css, .png, .config, etc, etc, while carefully leaving behind all the .cs files. You'll need to prefix the path to the .csproj file to get the true origin file location, and you want to ensure you preserve the folder structure in the destination location. But this is trivial in comparison to harvesting the file list.
Now that you've got the file list, loop through it copying from the source folder to the destination folder. (You probably want to either empty the destination folder first or afterwards prune extra files from previous deployments. I find the former easier.) The only thing the csproj file crawl didn't give you was the bin folder content, but that's cake: copy all the contents of the bin folder. :D (There's a healthy debate about whether to copy .pdb files, but I say yes.)
Form a script to do the above 3 steps, then call it either from an XBuild task or call both XBuild and this script from the CI process. Poof. You've got a deploy target. Happy coding!
Preferably I'd like to have a Linux CI server that does this instead using XBuild
The good news is that you CAN do this with a workaround I found in this article. Here is an excerpt and the workaround link from the above article:
The build server does not have Microsoft MVC (any version) installed.
However, this is very easy to work around-Microsoft MVC is available
on NuGet at http://nuget.org/packages/Microsoft.AspNet.Mvc if you need
to install an older version, click on the older version at the bottom
of the page and you’ll reach instructions on how to install that
version of the framework.
Hope this make it easy for you!
#DHarun 's idea works great!
I just wrote a small script based on #Dharun 's idea, hope it may help others.
https://github.com/z-ji/MonoWebPublisher

How to add source file (.cs) to resources (.resx) in a pre-build event?

Is it simple or even possible?
Why: Following my other question and using this approach, now I would like to simply embed the source file to the resources so I can copy it to the temp folder while the application is running - and keep the application as a single file.
I assume using a pre-build event is the best way to do this. If so, how would it be done?
EDIT: I'm asking about adding it before building because I also assume it won't be automatically updated if I add it manually once and then change the code after.
I recently went through this same issue when developing an examples suite for a .NET control which had to display its own example source code in the application.
The approach I ended up with was as follows.
I created a batch script (run in pre-build) to copy all the code files under /Examples/* to another folder in the solution Resources/ExamplesSrc/*
I then included all these files under Resources/ExamplesSrc/* in the csproj and saved it.
The next step was I set all the files under Resources/ExamplesSrc/* as embedded resource and committed the change to SVN. I did not commit the examples (generated) to SVN, just the modified csproj and batch file.
The effect when developers checked out was that they had (!) icons on all the resources (they were not on disk, but the csproj was looking for them), however as soon as they built the generated files appeared and all was well. This approach also worked with a build server (Team city) and the examples suite can now load and browse its own code via embedded resources.
One caveat you should be aware of. There is a quirk (by design apparently) in VS2010. If an embedded resource has ".cs" in it (i.e. any code file!) it cannot be loaded from the assembly using Assembly.GetManifestResourceStream(). To work around this all source files were renamed in the batch copy step from *.Xaml.cs to *.Xaml.c.txt, from *.cs to *.c.txt
Hope this helps!
Turns out I don't need to (re)add the source file each time I build the solution!
When I add it manually once it becomes a "Text" file on resources - and I can easily access it as a string using Properties.Resources.SourceCode (having the file named "SourceCode.cs"). And yes, it is updated automatically since the resource property "Persistence": the file is linked at compile time.

Why does VS2010 allow for the concept of "include in project"?

I'm still learning the basics of how VS2010 sees the world. Apparently, you can optionally "include" a file in a project. I'm a bit confused by this: If a file is version-controlled, AND the file is within the project directory, shouldn't it implicitly be "included" in the project? If not, what's the use case where a version-controlled file in the project directory should NOT be included in the project?
=== Addition ===
Based on the answers I've gotten so far, maybe I should rephrased my question: What does it mean for a file to be "included" in a project?
A project needs to know about files in order for compilation and distribution to occur. Just because you have a file that's under source-control, doesn't mean that it will be compiled if the project is unaware of it.
Also, you may want to include files as part of a distribution package. We do this quite often for our web projects that we distribute using web app gallery.
Conversely, you could have documentation or sql scripts that you version control, but do not want them to be part of the project.
EDIT: In answer to your update, what it means for a file to be included in a project is that the file is actually added to the .csproj or .vbproj file and will be used during compilation and/or distribution. VS does differentiate if the file is Content or if it needs to Compile it. This can be seen by clicking on the file in Solution Explorer and looking at the Build Action property.
No, you don't want random files that happen to be in the project directory included in source control.
We do sometimes put documentation (pdfs) or drawings/schematics in the project folder and under version control but you don't need them inside the visual studio project (especially when they are not being distributed because they are for internal use only).
Excluding the file from your project can be useful if the file is related to the project but not necessarily needed in the solution.
Example
If I need some test XML for an application that i'm writing; that is designed to normally be pulling this from a WCF service, it can be useful to keep that file in the directory for a development environment where I use IO to get the XML for testing, but I don't necessarily want it in my solution which is source controlled.
When you exclude a file from a project is no longer compiled or embedded, then when you want to include it again you can do so without having lost your settings.
If you e.g. copy a file (containing a helpful class which want to have in your project) into a folder of your project, then you will see ... nothing. You have to check the option "Show all files" of the solution explorer and the copied file can be seen, but it is still "greyed out". No you can choose the menuitem Include in project and that file will be integrated in your project and a pending change (add) for your source control is added too. Visual Studio doesn't include all files it can find in the project folder automatically to the project - and that is a good feature.
One of my colleagues explained to me a scenario in which a version-controlled file should NOT be part of the project. Here's the idea:
A developer writes some new code.
The code is experimental, and not intended to be part of the normal build.
The easiest way to exclude the file from the build is to NOT include it in the project, but still version-control it.
This way, the file can be shared with other developers, but not break the build.

determine SVN checked in state of a file in c#

I currently have a requirement where I need to validate that all the files in a folder structure which are in SVN to see if they are up to date. Is there a way to do this using the svn artefacts that are held in the same directory as the file without interacting with SVN?
Apologies, this appears to need some further explanation...
I'm iterating through the projects loaded within a solution and can access the corresponding files on the file system. I need to check
a) if corresponding files on the file system match the latest revision if the file is under source control and
b) if any or the files or projects have not been added to the SVN.
This is essentially a validation check prior to calling a build server to start a build in case a user has forgotten to check in some changes, new files or new projects to a solution. The build server retrieves the code directly from the source control.
Do you ask how to detect what files stored in SVN folder(s) are changed?
You ask to do that without interacting with SVN, but I would recommend avoid this because file or folder structure can be changed by Subversion team in future.
Try to use SharpSVN I use it in one of my projects. It handy, stable, has nice API and actively developed.
Also check comments in your question. Anyway you can check source code of SharpSVN to undestand how to make it by yourself

Categories

Resources