Sharpmake icon indicating copy to clipboard operation
Sharpmake copied to clipboard

Dynamic project creation support

Open sammyfreg opened this issue 5 years ago • 1 comments

This is more of a feature request than an issue.

I have various version of a project, each in a diffrent folder:

  • Root
    • MyProject_v1
    • MyProject_v2
    • MyProject_v3

I wanted to create a generic 'class myProject' and in 'Solution.ConfigureAll()' find all directories to dynamically add them as different projects :

public class myProject : project
{
	myProject(string dirName)
	{
		Name = dirName;
		SourceRootPath = @"[project.SharpmakeCsPath]\" + dirName;
	}
}

public class mySolution
{
	public void ConfigureAll(...)
	{
		list dirList = FindDirectories();
		for each dirName in dirList
			conf.AddProject<myProject>(target);
	}
}

I don't think this is possible, since there's no way to pass constructor parameters and would probably cause issue with system unable to differentiate between various 'myProject' class instances. Templating 'myProject' doesn't seems like it would help either, with 'generic' limitations.

For the moment, I have to manually declare a child class of 'myProject' per version and specify the path with a string constant. Is there a way to achieve this that I'm missing? If not, it might be nice to have an extra parameter that help uniquely identify a 'Project' and which we could also read in the constructor to change configuration. Something like :

public class mySolution
{
	public void ConfigureAll(...)
	{
		for each dirName in dirList
			conf.AddProject<myProject>(target, dirName); // dirName added as constructor parameter
	}
}

public class otherProject : project
{
	public void ConfigureAll(...)
	{
		conf.AddPublicDependency<myProject>(target, "MyProject_v1");
	}
}

sammyfreg avatar May 31 '20 06:05 sammyfreg

I realise this is from a few years ago but I was looking to do similar. After looking at how Sharpmake works I have managed to do what you need I think. Although perhaps you managed to do what you were looking for by now :).

    [Generate]
   public class UOEngine : Solution
   {
       private List<Type> _projects = [];

       public UOEngine()
       {
           Name = "UOEngine";
           AddTargets(TargetTypes.GetDefaultTargets());

           var files = Directory.EnumerateFiles("E:\\CUO\\UOEngine\\Source\\Runtime", "*.Sharpmake.cs", SearchOption.AllDirectories);

           var assembler = new Assembler();

            var projectAssembly = assembler.BuildAssembly(files.ToArray());

            foreach (var exportedType in projectAssembly.ExportedTypes)
            {
                if (exportedType.BaseType == typeof(CSharpProject))
                {
                    _projects.Add(exportedType);
                }
            }
       }

       [Configure]
       public void Configure(Configuration conf, Target target)
       {
           conf.SolutionFileName = @"[solution.Name]";
           conf.SolutionPath = @"[solution.SharpmakeCsPath]";

           foreach(var project in _projects)
           {
               conf.AddProject(project, target);
           }
       }
   }

Scotty1234 avatar Jul 25 '24 22:07 Scotty1234