From 1e6d0521f7f333df7eff3148465f7df6191e0201 Mon Sep 17 00:00:00 2001 From: Enrico Sada Date: Sun, 4 Nov 2018 23:58:16 +0100 Subject: [PATCH] use CustomAfterMicrosoftCommonTargets property as hook (#29) fix #10 use `CustomAfterMicrosoftCommonTargets` property as hook instead of a custom target file in `obj` dir, who has issues with incremental rebuild remove the deprecated target, if found because was in `obj` dir from a previous run --- src/dotnet-proj/Inspect.fs | 60 ++++++++++++-------------------- src/dotnet-proj/Program.fs | 26 +++++--------- test/dotnet-proj.Tests/Sample.fs | 49 ++++++++++++++++++++++---- 3 files changed, 75 insertions(+), 60 deletions(-) diff --git a/src/dotnet-proj/Inspect.fs b/src/dotnet-proj/Inspect.fs index 92d08e3b..a033227c 100644 --- a/src/dotnet-proj/Inspect.fs +++ b/src/dotnet-proj/Inspect.fs @@ -131,13 +131,6 @@ let writeTargetFile log templates targetFileDestPath = Ok targetFileDestPath -let install_target_file log templates projPath = - let projDir, projName = Path.GetDirectoryName(projPath), Path.GetFileName(projPath) - let objDir = Path.Combine(projDir, "obj") - let targetFileDestPath = Path.Combine(objDir, (sprintf "%s.proj-info.targets" projName)) - - writeTargetFile log templates targetFileDestPath - type GetResult = | FscArgs of string list | CscArgs of string list @@ -412,6 +405,15 @@ let getResolvedP2PRefs () = Property ("_Inspect_GetResolvedProjectReferences_OutFile", outFile) ] template, args, (fun () -> bindSkipped parseResolvedP2PRefOut outFile) +let uninstall_old_target_file log projPath = + let projDir, projName = Path.GetDirectoryName(projPath), Path.GetFileName(projPath) + let objDir = Path.Combine(projDir, "obj") + let targetFileDestPath = Path.Combine(objDir, (sprintf "%s.proj-info.targets" projName)) + + log (sprintf "searching deprecated target file in '%s'." targetFileDestPath) + if File.Exists targetFileDestPath then + log (sprintf "found deprecated target file in '%s', deleting." targetFileDestPath) + File.Delete targetFileDestPath let getProjectInfos log msbuildExec getters additionalArgs projPath = @@ -422,18 +424,26 @@ let getProjectInfos log msbuildExec getters additionalArgs projPath = let args = argsList |> List.concat + // remove deprecated target file, if exists projPath - |> install_target_file log templates - |> Result.bind (fun _ -> msbuildExec projPath (args @ additionalArgs)) + |> uninstall_old_target_file log + + getNewTempFilePath "proj-info.hook.targets" + |> writeTargetFile log templates + |> Result.bind (fun targetPath -> msbuildExec projPath (args @ additionalArgs @ [ Property("CustomAfterMicrosoftCommonTargets", targetPath) ])) |> Result.map (fun _ -> parsers |> List.map (fun parse -> parse ())) -let getProjectInfo log msbuildExec getArgs additionalArgs projPath = - //TODO refactor to use getProjectInfos +let getProjectInfo log msbuildExec getArgs additionalArgs (projPath: string) = + //TODO refactor to use getProjectInfosOldSdk let template, args, parse = getArgs () + // remove deprecated target file, if exists projPath - |> install_target_file log [template] - |> Result.bind (fun _ -> msbuildExec projPath (args @ additionalArgs)) + |> uninstall_old_target_file log + + getNewTempFilePath "proj-info.hook.targets" + |> writeTargetFile log [template] + |> Result.bind (fun targetPath -> msbuildExec projPath (args @ additionalArgs @ [ Property("CustomAfterMicrosoftCommonTargets", targetPath) ])) |> Result.bind (fun _ -> parse ()) #if !NETSTANDARD1_6 @@ -483,30 +493,6 @@ let getFscArgsOldSdk propsToFscArgs () = |> Result.bind propsToFscArgs |> Result.map FscArgs) - -let getProjectInfosOldSdk log msbuildExec getters additionalArgs (projPath: string) = - - let templates, argsList, parsers = - getters - |> List.map (fun getArgs -> getArgs ()) - |> List.unzip3 - - let args = argsList |> List.concat - - getNewTempFilePath "proj-info.oldsdk-hook.targets" - |> writeTargetFile log templates - |> Result.bind (fun targetPath -> msbuildExec projPath (args @ additionalArgs @ [ Property("CustomAfterMicrosoftCommonTargets", targetPath) ])) - |> Result.map (fun _ -> parsers |> List.map (fun parse -> parse ())) - -let getProjectInfoOldSdk log msbuildExec getArgs additionalArgs projPath = - //TODO refactor to use getProjectInfosOldSdk - let template, args, parse = getArgs () - - getNewTempFilePath "proj-info.oldsdk-hook.targets" - |> writeTargetFile log [template] - |> Result.bind (fun targetPath -> msbuildExec projPath (args @ additionalArgs @ [ Property("CustomAfterMicrosoftCommonTargets", targetPath) ])) - |> Result.bind (fun _ -> parse ()) - #endif module ProjectRecognizer = diff --git a/src/dotnet-proj/Program.fs b/src/dotnet-proj/Program.fs index 84a86bb9..d625241b 100644 --- a/src/dotnet-proj/Program.fs +++ b/src/dotnet-proj/Program.fs @@ -252,13 +252,7 @@ let analizeProj projPath = attempt { Errors.GenericError "unsupported project format" |> Result.Error - let getProjectInfoBySdk = - if isDotnetSdk then - getProjectInfo - else - getProjectInfoOldSdk - - return isDotnetSdk, pi, getProjectInfoBySdk + return isDotnetSdk, pi, getProjectInfo } let propMain log (results: ParseResults) = attempt { @@ -465,11 +459,10 @@ let p2pMain log (results: ParseResults) = attempt { let netFwMain log (results: ParseResults) = attempt { - let! proj = + let projPath = //create the proj file - Ok (Dotnet.ProjInfo.NETFrameworkInfoFromMSBuild.createEnvInfoProj ()) - - let projPath = Path.GetFullPath(proj) + Dotnet.ProjInfo.NETFrameworkInfoFromMSBuild.createEnvInfoProj () + |> Path.GetFullPath let msbuildPath = results.GetResult(<@ NetFwCLIArguments.MSBuild @>, defaultValue = "msbuild") @@ -477,7 +470,7 @@ let netFwMain log (results: ParseResults) = attempt { let msbuildHost = MSBuildExePath.Path msbuildPath - return projPath, getProjectInfoOldSdk, cmd, msbuildHost, [] + return projPath, getProjectInfo, cmd, msbuildHost, [] } let netFwRefMain log (results: ParseResults) = attempt { @@ -487,11 +480,10 @@ let netFwRefMain log (results: ParseResults) = attempt { | [] -> Error (InvalidArgsState "multiple .*proj found in current directory, use --project argument to specify path") | props -> Ok props - let! proj = + let projPath = //create the proj file - Ok (Dotnet.ProjInfo.NETFrameworkInfoFromMSBuild.createEnvInfoProj ()) - - let projPath = Path.GetFullPath(proj) + Dotnet.ProjInfo.NETFrameworkInfoFromMSBuild.createEnvInfoProj () + |> Path.GetFullPath let msbuildPath = results.GetResult(<@ NetFwRefCLIArguments.MSBuild @>, defaultValue = "msbuild") @@ -499,7 +491,7 @@ let netFwRefMain log (results: ParseResults) = attempt { let msbuildHost = MSBuildExePath.Path msbuildPath - return projPath, getProjectInfoOldSdk, cmd, msbuildHost, [] + return projPath, getProjectInfo, cmd, msbuildHost, [] } let realMain argv = attempt { diff --git a/test/dotnet-proj.Tests/Sample.fs b/test/dotnet-proj.Tests/Sample.fs index 5d5cc5c9..f273426e 100644 --- a/test/dotnet-proj.Tests/Sample.fs +++ b/test/dotnet-proj.Tests/Sample.fs @@ -25,18 +25,25 @@ let SamplePkgDir = TestRunDir/"pkgs"/"SamplePkgDir" let checkExitCodeZero (cmd: Command) = Expect.equal 0 cmd.Result.ExitCode "command finished with exit code non-zero." +let renderNugetConfig clear feeds = + [ yield "" + yield " " + if clear then + yield " " + for (name, url) in feeds do + yield sprintf """ """ name url + yield " " + yield "" ] + let prepareTool (fs: FileUtils) pkgUnderTestVersion = for dir in [TestRunToolCfgDir; TestRunToolDir; TestRunInvariantDir] do fs.rm_rf dir fs.mkdir_p dir - fs.createFile (TestRunToolCfgDir/"nuget.config") (writeLines - [ "" - " " - sprintf """ """ NupkgsDir - " " - "" ]) + renderNugetConfig true ["local", NupkgsDir] + |> writeLines + |> fs.createFile (TestRunToolCfgDir/"nuget.config") fs.cd TestRunInvariantDir fs.shellExecRun "dotnet" ["tool"; "install"; "dotnet-proj"; "--version"; pkgUnderTestVersion; "--tool-path"; TestRunToolDir; "--configfile"; (TestRunToolCfgDir/"nuget.config")] @@ -372,6 +379,36 @@ let tests pkgUnderTestVersion = Expect.equal out (sprintf "MyProperty=%s" prop) "wrong output" ) + yield testCase |> withLog "delete deprecated target file" (fun _ fs -> + let testDir = inDir fs "netsdk_delete_depr_target" + copyDirFromAssets fs ``samples2 NetSdk library``.ProjDir testDir + + let projPath = testDir/ (``samples2 NetSdk library``.ProjectFile) + + dotnet fs ["restore"; projPath] + |> checkExitCodeZero + + let projDir, projName = Path.GetDirectoryName(projPath), Path.GetFileName(projPath) + let deprecatedTargetFilePath = projDir/"obj"/sprintf "%s.proj-info.targets" projName + + // create the deprecated target file + fs.createFile deprecatedTargetFilePath (writeLines + [ """""" + " " + " " + "" ]) + + "before exists" + |> Expect.isTrue (File.Exists deprecatedTargetFilePath) + + projInfo fs ["--verbose"; "prop"; projPath; "-get"; "AssemblyName"] + |> checkExitCodeZero + + "after doesnt exists" + |> Expect.isFalse (File.Exists deprecatedTargetFilePath) + ) + + ] [ generalTests