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

Example for CustomBeforeSlnGenTargets #448

Open
michael-hawker opened this issue Feb 1, 2023 · 4 comments
Open

Example for CustomBeforeSlnGenTargets #448

michael-hawker opened this issue Feb 1, 2023 · 4 comments

Comments

@michael-hawker
Copy link
Contributor

michael-hawker commented Feb 1, 2023

Think this allows us to run msbuild logic before a solution is generated? This should be useful for us as we want to generate some projects from common code for our project. (e.g. we have a runner that is basically the same to execute the code within the repo, we don't want to duplicate it in each folder for each project)

However, the docs just call this property/target out without a clear example of how this system should work. Is it a target to write a custom target section or is this supposed to just be a path to a props file or something???

Like I'd like to have actual logic to run like a file copy, which means I'd need it to be a target or something to do something before the rest of the slngen command runs, right?

@jeffkl
Copy link
Collaborator

jeffkl commented Feb 6, 2023

@michael-hawker the targets only run when you use SlnGen as an MSBuild target which is sort of considered legacy at this point. You could install the package and run msbuild /t:slngen. There are some drawbacks of that since SlnGen can't enable a few performance optimizations since MSBuild.exe is already running by the time it gets to do its thing. The preferred method of execution is just running slngen the global tool which does not run these targets. Running the targets would sort of slow things down.

We could come up with a new design of something like "run this before and after solution generation", I'm open to suggestions and designs...

@michael-hawker
Copy link
Contributor Author

michael-hawker commented Feb 17, 2023

I guess I don't still quite understand how the old property version worked. Was it just a path to an props type file?

Could we have a project owner just specify a custom target in their own project files that are getting analyzed by slngen itself?

Like:

<Target Name="PreSolutionGeneration">
    <Message Text="Target would be executed before slngen runs the rest of its code." />
</Target>
<Target Name="PostSolutionGeneration">
    <Message Text="Target would be executed after slngen runs its code before it opens Visual Studio" />
</Target>

I'd imagine someone could also do something similar like this instead too:

<Target Name="MyCustomStep" BeforeTargets="PreSolutionGeneration">
    <Message Text="Target would still be executed before slngen runs the rest of its code." />
</Target>

Which would be safer as instead of replacing the PreSolutionGeneration target, it'd just run before it. That way if multiple projects in the solution need to do different things they wouldn't clash.

Would something like that be possible? It'd basically just make it feel like it fits in with the rest of the msbuild ecosystem?

@jeffkl
Copy link
Collaborator

jeffkl commented Feb 21, 2023

The way the target version of SlnGen worked is you'd add a <PackageReference Include="SlnGen" /> and it would import a .targets file that would inject an slngen target so you could run:

msbuild /restore /Target:slngen

The .targets file imported is here: https://github.com/microsoft/slngen/blob/main/src/Microsoft.VisualStudio.SlnGen/Microsoft.VisualStudio.SlnGen.targets

And that file imports a file based on the value of the CustomBeforeSlnGenTargets property so you could inject/override/customize/extend how SlnGen worked for that scenario.

But the global tool just loads the projects and gets the info, it does not run any targets. This is because its much faster that way.

@michael-hawker
Copy link
Contributor Author

Thanks @jeffkl, where are the projects loaded in the code base? There are some properties specific to slngen that get looked at, right? Does that happen in the same spot.

Would it also not be possible to look at targets defined in the project files loaded and see if they have a specific name then?

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

2 participants