Programmatically Add Files To TFS with dependency files - tfs

How can I programmatically add files to a TFS project that have code behind files. I can say the following to add files. That will only add single files to a project and not the file plus the code behind file. I'm trying to add a resource file and it's code behind that were dynamically generated to a TFS project.
workspace.PendAdd(filesWithPathToEdit, true);

I had to put it in a T4 template to get access to the current Visual Studio DTE otherwise it would randomly work if I tried it outside of a t4. You can use the DTE to get a list of projects from a solution then add a ProjectItem and it contains ProjectItems so you can add your code behind there. ResxContainer is a custom class to just contain all information about my resx file i needed.
EnvDTE.DTE dte = (EnvDTE.DTE)HostServiceProvider.GetService(typeof(EnvDTE.DTE));
//dte = (EnvDTE.DTE) hostServiceProvider.GetService(typeof(EnvDTE.DTE));
//dte = (EnvDTE80.DTE2)Marshal.GetActiveObject("VisualStudio.DTE");
Projects projects = dte.Solution.Projects;
if (projects.Count > 0)
{
IEnumerator enumer = ((IEnumerable)projects).GetEnumerator();
while (enumer.MoveNext())
{
Project proj = (Project)enumer.Current;
if (proj.Name == projectName)
{
foreach (ResxContainer res in items)
{
ProjectItem item = proj.ProjectItems.AddFromFile(res.ResxPath);
item.ProjectItems.AddFromFile(res.CodeBehindPath);
}
}
}

There's no way for it to automatically know if a file depends on another. However, you can decide on your own which files will typically have a code behind file associated with them and add them yourself.
For example:
If you begin to add a file with an .aspx extension, then those files, as we know, typically have a code behind file. That code behind file, we can assume, has the same file name, with .cs appended. So, if we have "Default.aspx", then we can safely assume that there will be a "Default.aspx.cs" and that they are dependent on each other, so we should add both.
The same thing goes with .xaml and .xaml.cs files.

Related

Calling Another Project's Controller From A Project In The Same Solution (.NET Core )

There are 2 projects in the same solution. First project is a .NET Core project and it has all the codes(controllers, models etc.) related to packages. I need to get the information (id, name, description) of the packages and display it in the second project(.NET Core Web App with Razor). Is it possible to do it without changing the first project? I only want to show the package list on a single web page.
I tried calling the first project's controller but it didn't work. Maybe I missed a point. Any help is appreciated.
This requirement can be achieved, please see the gif image below.
Tips
If you want to call another project's controller from a project in the same solution, you need to make sure there is in HomeController in both project. I mean the name of any class should be unique in both projects.
Otherwise you will face the same issue like my homepage.
Test Code:
public List<PackageReference> GetPackageList5(string projectname)
{
List<PackageReference> list = new List<PackageReference>();
PackageReference p = null;
XDocument doc = XDocument.Load(_webHostEnvironment.ContentRootPath+ "/"+ projectname + ".csproj");
var packageReferences = doc.XPathSelectElements("//PackageReference")
.Select(pr => new PackageReference
{
Include = pr.Attribute("Include").Value,
Version = pr.Attribute("Version").Value
});
Console.WriteLine($"Project file contains {packageReferences.Count()} package references:");
foreach (var packageReference in packageReferences)
{
p = new PackageReference();
p.Version= packageReference.Version;
p.Include= packageReference.Include;
list.Add(packageReference);
//Console.WriteLine($"{packageReference.Include}, version {packageReference.Version}");
}
return list;
}
My Test Steps:
create two project, Net5MVC,Net6MVC
add project reference.
My .net6 project references a .net5 project. So in my HomeController (.net), I add below:
using Net5MVC.ForCore6;
using Net5MVC.Models;
Suggestion
When we reference the .net5 project in .net6 project, we can build success, but when we deploy it, it always failed. The reason is some file was multiple publish output files with the same relative path.
Found multiple publish output files with the same relative path:
D:\..\Net6\Net6\Net5MVC\appsettings.Development.json,
D:\..\Net6\Net6\Net6MVC\appsettings.Development.json,
D:\..\Net6\Net6\Net5MVC\appsettings.json,
D:\..\Net6\Net6\Net6MVC\appsettings.json.
And usually will add class library to current project, not add a web project.
As we know we can find packages info in .csproj file, so we need copy and paste .csproj file to publish folder.
I still recommend using the GetPackageList5 method above as an interface for your project, using HttpClient for requests.

How to check SPM repo link in xcode?

I am trying to check repo link for my existing project already added SPM.
I want to know the repo link for github/bitbuckect etc etc.
Why i wanted to know this - I have one existing project in which lots of SPMs are there. and I want to add all that SPM in my new project but I don’t know git links for those SPM.
Is there any way to do that?
Thank you for help.
If you have a Package.swift file in the root of your repo, that should also contain the links, if not, try the below one:
On the left pane, each of the dependencies will have a .podspec file with a s.source specification. You can find the URL there.
The root source of this information can be found inside the project file (./<project_name>.xcodeproj/project.pbxproj). Just open it with text editor and search for occurrences of XCRemoteSwiftPackageReference. You should be able to find sections like this one:
/* Begin XCRemoteSwiftPackageReference section */
A1BFE4BB2779F1410015B840 /* XCRemoteSwiftPackageReference "swift-atomics" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-atomics.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.0;
};
};
/* End XCRemoteSwiftPackageReference section */

ILASM does not set FileVersion

I have an .il file which I can compile without any problems. I can strong name it and so without any issues. But I am not able to set the file version via the attribute as I would expect it. How can I set the FileVersion for an assembly when using ilasm?
If I do a round trip I get always a .res file which does contain only binary data which is not readable. What is inside this res file and can I edit it?
The code does not work
.assembly myAssembly
{
.custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = { string('1.2.3.4') }
The issue can be solved by using the .res file. It is not sufficient to do a round trip with ildasm and ilasm. The IL file does not reference the .res file. I had to add it to the ilasm call manually. The data in the res file seemed to contain the infos which are written into the PE header which is ok for me.
The final command line needed was
ilasm test.il /dll /res:test.res
I still do not know what exactly is inside the res file but I can exhange it with the meta data information of any other assemlby that I create manually and then decompile it to replace the metadata of the original assembly as I need.
It seems not many people are doing such stuff.

How can I create a directory in my source control using TFS 2010 SDK?

I want to customize the creation of a TFS project using TFS 2010 SDK.
I have already create a process template and use this sample, but I want to create a specific directory tree for the new team project base on a XML file which describe the tree. My problem is this message; The array must contain at least one element.
Parameter name: checkinParameters.PendingChanges
I initialize the TFS, map the server folder with a local folder and create directories in both.
fooString = Array.Find<WorkingFolder>(workspace.Folders, m => m.ServerItem.Contains("$/FR_DEV"));
Directory.CreateDirectory(ElementPath);
Directory.CreateDirectory(fooString.ServerItem + ElementTfsPath);
After that:
PendingChange[] PendingChanges = workspace.GetPendingChanges();
// Checkin the items we added
int changesetForAdd = workspace.CheckIn(PendingChanges, "Project creation.");
However, I get an error for the pending change! How can I fix this?
my problem was that i need to add the directory in the workspace not with a simple path
workspace.PendAdd(currentSubDirectory, true);

how to embed an xml file to a resource file

i want to embed a xml file to a resource file in my project,whenever i need the file i must get it from resource and use it,how to do this and i want to modify the contents of the xml file depending upon my requirements.how to do this
If you add the XML file to a Visual Studio project and, in the Property window for it, select Build Action: Embedded resource, the file will be embedded into the build output artifact for that project.
To access it from code, use something like:
string resourceName = "Namespace.Prefix.FileName.xml";
Assembly someAssembly = LoadYourAssemblyContainingTheResource();
XmlDocument xml = new XmlDocument();
using (Stream resourceStream = someAssembly.GetManifestResourceStream(resourceName))
{
xml.Load(resourceStream);
}
// The embedded XML resource is now available in: xml
If the resource you're loading is embedded in your own assembly, you can do something like Assembly.GetExecutingAssembly() to achieve what I listed as LoadYourAssemblyContainingTheResource() above, or possibly typeof(SomeTypeInYourResourceAssembly).Assembly
It's unclear what you mean by "want to modify the contents" - you cannot modify the resource inside the assembly at run-time, but whenever you change the XML file and recompile, the new version will be embedded.

Resources