user1618236
user1618236

Reputation:

How can I copy the content of ASP.NET project when building with MSBuild?

I am working on writing some new build scripts for a few of our applications, the current scripts build all projects in place with MSBuild and then manually copy all of the content (views, scripts, etc...) and the bin folder manually to an artifacts folder to be zipped; this requires that all files and folders that aren't part of the bin directory for an ASP.NET project to be explicitly listed in a configuration file which is of course immensely error prone.

Is there a way using MSBuild when specifying the output directory to also include the content files? Ideally this would be done without creating a publish profile as the current deployment method is not open to change so I need to make sure my build artifact matches the current one.

If it helps I am planning to use Cake for the build mechanism.

Upvotes: 5

Views: 1414

Answers (1)

user1618236
user1618236

Reputation:

Utilizing cake I was able to come up with a somewhat messy solution :

var excludedFiles = new List<string>{"Settings.settings", "packages.config"};
var artifactDir = "./Artifact";
var solutionFile = "./MySolution.sln";

Task("Package")
    .IsDependentOn("Build")
    .Does(() =>
    {
        var solution = ParseSolution(solutionFile);
        foreach (var project in solution.Projects)
        {
            var parsedProject = ParseProject(project.Path);
            var binDir = project.Path.GetDirectory() + Directory("/bin");

            if(project.Name.Contains("Host"))
            {             
                var soaDir = Directory(artifactDir + "/SOA");
                CreateDirectory(soaDir);
                CopyDirectory(binDir, soaDir + Directory("bin"));
                foreach(var file in parsedProject.Files)
                {
                    if(!file.Compile && excludedFiles.All(x=> !file.FilePath.ToString().Contains(x)))
                    {                        
                        var newFilePath = soaDir + File(file.RelativePath);
                        CreateDirectory(((FilePath)newFilePath).GetDirectory());
                        CopyFile(file.FilePath, newFilePath);
                    }
                }
            }
        }        
    });

The package task here will parse in the solution a loop through the projects to find any that contain the word Host is their names; this is how all of our web apps are named so for us it is safe but could definitely be improved.

Once we've identified the ASP.NET project we copy its bin contents and then loop through its files to find any that are not marked for compilation and excluded some specific files like the settings file and the nuget packages config file.

Finally we are able to use that files relative path to copy the file into the artifact directory.

Upvotes: 3

Related Questions