public static DockerDetails InspectDockerFile(IToolLogger logger, string projectLocation, string dockerfile) { var details = new DockerDetails(); var projectFilename = DetermineProjectFile(projectLocation); var dockerFilePath = Path.Combine(projectLocation, dockerfile); if (File.Exists(dockerFilePath)) { logger?.WriteLine("Inspecting Dockerfile to figure how to build project and docker image"); using (var stream = File.OpenRead(dockerFilePath)) using (var reader = new StreamReader(stream)) { string line; while ((line = reader.ReadLine()) != null) { var noSpaceLine = line.Replace(" ", ""); if (noSpaceLine.StartsWith("COPY") && (noSpaceLine.EndsWith(".sln./") || (projectFilename != null && noSpaceLine.Contains("/" + projectFilename)))) { details.BuildFromSolutionDirectory = true; logger?.WriteLine("... Determined that docker build needs to be run from solution folder."); } } } } return(details); }
public static DockerDetails InspectDockerFile(IToolLogger logger, string projectLocation) { var details = new DockerDetails(); var projectFilename = DetermineProjectFile(projectLocation); var dockerFilePath = Path.Combine(projectLocation, "Dockerfile"); if (File.Exists(dockerFilePath)) { logger?.WriteLine("Inspecting Dockerfile to figure how to build project and docker image"); using (var stream = File.OpenRead(dockerFilePath)) using (var reader = new StreamReader(stream)) { string line; while ((line = reader.ReadLine()) != null) { var noSpaceLine = line.Replace(" ", ""); if (line.StartsWith("COPY ") && line.Contains(":-")) { int start = line.IndexOf(":-") + 2; int end = line.IndexOf('}', start); if (end == -1) { continue; } details.ExpectedPublishLocation = line.Substring(start, end - start).Trim(); logger?.WriteLine("... Determined dotnet publish location configured to: " + details.ExpectedPublishLocation); break; } else if (noSpaceLine.StartsWith("RUNdotnetpublish")) { details.SkipDotnetBuild = true; logger?.WriteLine("... Skip building project since it is done as part of Dockerfile"); } else if (noSpaceLine.StartsWith("COPY") && (noSpaceLine.EndsWith(".sln./") || (projectFilename != null && noSpaceLine.Contains("/" + projectFilename)))) { details.BuildFromSolutionDirectory = true; logger?.WriteLine("... Determined that docker build needs to be run from solution folder."); } } } if (!details.SkipDotnetBuild && string.IsNullOrEmpty(details.ExpectedPublishLocation)) { details.ExpectedPublishLocation = "obj/Docker/publish"; logger?.WriteLine("Warning: unable to determine dotnet publish folder location that Dockerfile expects. Assuming Visual Studio's default of " + details.ExpectedPublishLocation); } } return(details); }