Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T4 Templates builds with the 2017 version but fails with your 2022 version #2

Open
valeryan opened this issue Jun 28, 2022 · 0 comments

Comments

@valeryan
Copy link

Hey, thanks for taking the time to rebuild this. I wanted to give it a try for a legacy project that still requires 2017. The extension installs but when I try to run a custom tool for template errors. It seems like some system information is not getting loaded. Here is a sample of the code that is failing to run:

public static void CalculateDatabaseConnectionString(ITextTemplatingEngineHost host)
	{
		DTE dte = (DTE)((IServiceProvider)host).GetService(typeof(SDTE));
		
		List<Project> projects = new List<Project>();
		
		try
		{
			foreach (var project in dte.Solution.Projects)
			{
				Project p = project as Project;
				// Skip test and misc projects
				if (p.Kind == "{67294A52-A4F0-11D2-AA88-00C04F688DDE}" || p.Kind == "66A26720-8FB5-11D2-AA7E-00C04F688DDE")
					continue;
				
				if (!p.Name.Contains("Curse.Cobalt"))
				{
					projects.Add(p);
				}
			}
		}
		catch (System.Runtime.Serialization.SerializationException)
		{
			// This is to skip projects that are a Database project
		}
		
		var solutionName = string.Empty;
		var regex = new Regex(@"(.*?\\)*(?<solutionName>.*?)\.sln", RegexOptions.Compiled);
		var match = regex.Match(dte.Solution.FullName);
		if(match != null && match.Groups != null && match.Groups.Count > 0)
		{
			solutionName = match.Groups["solutionName"].ToString();
		}

		var implementationProjects = !string.IsNullOrEmpty(solutionName) ? projects.Where(p => p.Name.StartsWith(solutionName)) : projects;
////////// Fails Here .//////
		var projectItem = implementationProjects
			.Where(project => project.ProjectItems != null)
			.SelectMany(project => project.ProjectItems.OfType<ProjectItem>())
			.Where(pi => pi.Name == "Cobalt.config")
			.First();
///////////////////////////////		
		string filename;
		try
		{
			filename = projectItem.get_FileNames(0);
		}
		catch
		{
			try
			{
				filename = projectItem.Document.FullName;
			}
			catch
			{
				throw new InvalidOperationException("Unable to calculate file path for Cobalt.config");
			}
		}
		
		var xml = XDocument.Load(filename);
		
		var database = xml
			.Elements("cobalt")
			.Single()
			.Elements("database")
			.Single();
		
		var activeKeyAtt = database.Attribute("activeKey");
		string activeKey;
		if (activeKeyAtt == null)
		{
			activeKey = "development";
		}
		else
		{
			activeKey = activeKeyAtt.Value;
		}
		var provider = database
			.Elements("databaseProvider")
			.Where(p => p.Attribute("key").Value == activeKey)
			.Single();
		
		string conn = provider.Attribute("connectionString").Value;
		if (string.IsNullOrEmpty(conn))
		{
			throw new InvalidOperationException("Cannot calculate connection string");
		}
		_connectionString = $"{conn}Connection Timeout=90;";

		string localConfigConnectionString = CalculateLocalConfigConnectionString(projects);

		if (localConfigConnectionString != null)
		{
			_connectionString = $"{localConfigConnectionString}Connection Timeout=90;";
		}
	}

When running this block in 2017 using the original T4toolbox it runs without issues but this code produces this in 2022 using this T4Toolbox

Error		Running transformation: System.InvalidOperationException: Sequence contains no elements
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)

Do you have any thoughts if you are still around?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant