Skip to content

Commit

Permalink
use CustomAfterMicrosoftCommonTargets property as hook (#29)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
enricosada authored Nov 4, 2018
1 parent c10146f commit 1e6d052
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 60 deletions.
60 changes: 23 additions & 37 deletions src/dotnet-proj/Inspect.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =

Expand All @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
26 changes: 9 additions & 17 deletions src/dotnet-proj/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PropCLIArguments>) = attempt {
Expand Down Expand Up @@ -465,19 +459,18 @@ let p2pMain log (results: ParseResults<P2pCLIArguments>) = attempt {

let netFwMain log (results: ParseResults<NetFwCLIArguments>) = 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")

let cmd = Dotnet.ProjInfo.NETFrameworkInfoFromMSBuild.installedNETFrameworks

let msbuildHost = MSBuildExePath.Path msbuildPath

return projPath, getProjectInfoOldSdk, cmd, msbuildHost, []
return projPath, getProjectInfo, cmd, msbuildHost, []
}

let netFwRefMain log (results: ParseResults<NetFwRefCLIArguments>) = attempt {
Expand All @@ -487,19 +480,18 @@ let netFwRefMain log (results: ParseResults<NetFwRefCLIArguments>) = 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")

let cmd () = Dotnet.ProjInfo.NETFrameworkInfoFromMSBuild.getReferencePaths props

let msbuildHost = MSBuildExePath.Path msbuildPath

return projPath, getProjectInfoOldSdk, cmd, msbuildHost, []
return projPath, getProjectInfo, cmd, msbuildHost, []
}

let realMain argv = attempt {
Expand Down
49 changes: 43 additions & 6 deletions test/dotnet-proj.Tests/Sample.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<configuration>"
yield " <packageSources>"
if clear then
yield " <clear />"
for (name, url) in feeds do
yield sprintf """ <add key="%s" value="%s" />""" name url
yield " </packageSources>"
yield "</configuration>" ]

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
[ "<configuration>"
" <packageSources>"
sprintf """ <add key="local" value="%s" />""" NupkgsDir
" </packageSources>"
"</configuration>" ])
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")]
Expand Down Expand Up @@ -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
[ """<Project ToolsVersion="15.0">"""
" <PropertyGroup>"
" </PropertyGroup>"
"</Project>" ])

"before exists"
|> Expect.isTrue (File.Exists deprecatedTargetFilePath)

projInfo fs ["--verbose"; "prop"; projPath; "-get"; "AssemblyName"]
|> checkExitCodeZero

"after doesnt exists"
|> Expect.isFalse (File.Exists deprecatedTargetFilePath)
)


]

[ generalTests
Expand Down

0 comments on commit 1e6d052

Please sign in to comment.