Skip to content

Commit 1e6d052

Browse files
authored
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
1 parent c10146f commit 1e6d052

File tree

3 files changed

+75
-60
lines changed

3 files changed

+75
-60
lines changed

src/dotnet-proj/Inspect.fs

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,6 @@ let writeTargetFile log templates targetFileDestPath =
131131

132132
Ok targetFileDestPath
133133

134-
let install_target_file log templates projPath =
135-
let projDir, projName = Path.GetDirectoryName(projPath), Path.GetFileName(projPath)
136-
let objDir = Path.Combine(projDir, "obj")
137-
let targetFileDestPath = Path.Combine(objDir, (sprintf "%s.proj-info.targets" projName))
138-
139-
writeTargetFile log templates targetFileDestPath
140-
141134
type GetResult =
142135
| FscArgs of string list
143136
| CscArgs of string list
@@ -412,6 +405,15 @@ let getResolvedP2PRefs () =
412405
Property ("_Inspect_GetResolvedProjectReferences_OutFile", outFile) ]
413406
template, args, (fun () -> bindSkipped parseResolvedP2PRefOut outFile)
414407

408+
let uninstall_old_target_file log projPath =
409+
let projDir, projName = Path.GetDirectoryName(projPath), Path.GetFileName(projPath)
410+
let objDir = Path.Combine(projDir, "obj")
411+
let targetFileDestPath = Path.Combine(objDir, (sprintf "%s.proj-info.targets" projName))
412+
413+
log (sprintf "searching deprecated target file in '%s'." targetFileDestPath)
414+
if File.Exists targetFileDestPath then
415+
log (sprintf "found deprecated target file in '%s', deleting." targetFileDestPath)
416+
File.Delete targetFileDestPath
415417

416418
let getProjectInfos log msbuildExec getters additionalArgs projPath =
417419

@@ -422,18 +424,26 @@ let getProjectInfos log msbuildExec getters additionalArgs projPath =
422424

423425
let args = argsList |> List.concat
424426

427+
// remove deprecated target file, if exists
425428
projPath
426-
|> install_target_file log templates
427-
|> Result.bind (fun _ -> msbuildExec projPath (args @ additionalArgs))
429+
|> uninstall_old_target_file log
430+
431+
getNewTempFilePath "proj-info.hook.targets"
432+
|> writeTargetFile log templates
433+
|> Result.bind (fun targetPath -> msbuildExec projPath (args @ additionalArgs @ [ Property("CustomAfterMicrosoftCommonTargets", targetPath) ]))
428434
|> Result.map (fun _ -> parsers |> List.map (fun parse -> parse ()))
429435

430-
let getProjectInfo log msbuildExec getArgs additionalArgs projPath =
431-
//TODO refactor to use getProjectInfos
436+
let getProjectInfo log msbuildExec getArgs additionalArgs (projPath: string) =
437+
//TODO refactor to use getProjectInfosOldSdk
432438
let template, args, parse = getArgs ()
433439

440+
// remove deprecated target file, if exists
434441
projPath
435-
|> install_target_file log [template]
436-
|> Result.bind (fun _ -> msbuildExec projPath (args @ additionalArgs))
442+
|> uninstall_old_target_file log
443+
444+
getNewTempFilePath "proj-info.hook.targets"
445+
|> writeTargetFile log [template]
446+
|> Result.bind (fun targetPath -> msbuildExec projPath (args @ additionalArgs @ [ Property("CustomAfterMicrosoftCommonTargets", targetPath) ]))
437447
|> Result.bind (fun _ -> parse ())
438448

439449
#if !NETSTANDARD1_6
@@ -483,30 +493,6 @@ let getFscArgsOldSdk propsToFscArgs () =
483493
|> Result.bind propsToFscArgs
484494
|> Result.map FscArgs)
485495

486-
487-
let getProjectInfosOldSdk log msbuildExec getters additionalArgs (projPath: string) =
488-
489-
let templates, argsList, parsers =
490-
getters
491-
|> List.map (fun getArgs -> getArgs ())
492-
|> List.unzip3
493-
494-
let args = argsList |> List.concat
495-
496-
getNewTempFilePath "proj-info.oldsdk-hook.targets"
497-
|> writeTargetFile log templates
498-
|> Result.bind (fun targetPath -> msbuildExec projPath (args @ additionalArgs @ [ Property("CustomAfterMicrosoftCommonTargets", targetPath) ]))
499-
|> Result.map (fun _ -> parsers |> List.map (fun parse -> parse ()))
500-
501-
let getProjectInfoOldSdk log msbuildExec getArgs additionalArgs projPath =
502-
//TODO refactor to use getProjectInfosOldSdk
503-
let template, args, parse = getArgs ()
504-
505-
getNewTempFilePath "proj-info.oldsdk-hook.targets"
506-
|> writeTargetFile log [template]
507-
|> Result.bind (fun targetPath -> msbuildExec projPath (args @ additionalArgs @ [ Property("CustomAfterMicrosoftCommonTargets", targetPath) ]))
508-
|> Result.bind (fun _ -> parse ())
509-
510496
#endif
511497

512498
module ProjectRecognizer =

src/dotnet-proj/Program.fs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,7 @@ let analizeProj projPath = attempt {
252252
Errors.GenericError "unsupported project format"
253253
|> Result.Error
254254

255-
let getProjectInfoBySdk =
256-
if isDotnetSdk then
257-
getProjectInfo
258-
else
259-
getProjectInfoOldSdk
260-
261-
return isDotnetSdk, pi, getProjectInfoBySdk
255+
return isDotnetSdk, pi, getProjectInfo
262256
}
263257

264258
let propMain log (results: ParseResults<PropCLIArguments>) = attempt {
@@ -465,19 +459,18 @@ let p2pMain log (results: ParseResults<P2pCLIArguments>) = attempt {
465459

466460
let netFwMain log (results: ParseResults<NetFwCLIArguments>) = attempt {
467461

468-
let! proj =
462+
let projPath =
469463
//create the proj file
470-
Ok (Dotnet.ProjInfo.NETFrameworkInfoFromMSBuild.createEnvInfoProj ())
471-
472-
let projPath = Path.GetFullPath(proj)
464+
Dotnet.ProjInfo.NETFrameworkInfoFromMSBuild.createEnvInfoProj ()
465+
|> Path.GetFullPath
473466

474467
let msbuildPath = results.GetResult(<@ NetFwCLIArguments.MSBuild @>, defaultValue = "msbuild")
475468

476469
let cmd = Dotnet.ProjInfo.NETFrameworkInfoFromMSBuild.installedNETFrameworks
477470

478471
let msbuildHost = MSBuildExePath.Path msbuildPath
479472

480-
return projPath, getProjectInfoOldSdk, cmd, msbuildHost, []
473+
return projPath, getProjectInfo, cmd, msbuildHost, []
481474
}
482475

483476
let netFwRefMain log (results: ParseResults<NetFwRefCLIArguments>) = attempt {
@@ -487,19 +480,18 @@ let netFwRefMain log (results: ParseResults<NetFwRefCLIArguments>) = attempt {
487480
| [] -> Error (InvalidArgsState "multiple .*proj found in current directory, use --project argument to specify path")
488481
| props -> Ok props
489482

490-
let! proj =
483+
let projPath =
491484
//create the proj file
492-
Ok (Dotnet.ProjInfo.NETFrameworkInfoFromMSBuild.createEnvInfoProj ())
493-
494-
let projPath = Path.GetFullPath(proj)
485+
Dotnet.ProjInfo.NETFrameworkInfoFromMSBuild.createEnvInfoProj ()
486+
|> Path.GetFullPath
495487

496488
let msbuildPath = results.GetResult(<@ NetFwRefCLIArguments.MSBuild @>, defaultValue = "msbuild")
497489

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

500492
let msbuildHost = MSBuildExePath.Path msbuildPath
501493

502-
return projPath, getProjectInfoOldSdk, cmd, msbuildHost, []
494+
return projPath, getProjectInfo, cmd, msbuildHost, []
503495
}
504496

505497
let realMain argv = attempt {

test/dotnet-proj.Tests/Sample.fs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,25 @@ let SamplePkgDir = TestRunDir/"pkgs"/"SamplePkgDir"
2525
let checkExitCodeZero (cmd: Command) =
2626
Expect.equal 0 cmd.Result.ExitCode "command finished with exit code non-zero."
2727

28+
let renderNugetConfig clear feeds =
29+
[ yield "<configuration>"
30+
yield " <packageSources>"
31+
if clear then
32+
yield " <clear />"
33+
for (name, url) in feeds do
34+
yield sprintf """ <add key="%s" value="%s" />""" name url
35+
yield " </packageSources>"
36+
yield "</configuration>" ]
37+
2838
let prepareTool (fs: FileUtils) pkgUnderTestVersion =
2939

3040
for dir in [TestRunToolCfgDir; TestRunToolDir; TestRunInvariantDir] do
3141
fs.rm_rf dir
3242
fs.mkdir_p dir
3343

34-
fs.createFile (TestRunToolCfgDir/"nuget.config") (writeLines
35-
[ "<configuration>"
36-
" <packageSources>"
37-
sprintf """ <add key="local" value="%s" />""" NupkgsDir
38-
" </packageSources>"
39-
"</configuration>" ])
44+
renderNugetConfig true ["local", NupkgsDir]
45+
|> writeLines
46+
|> fs.createFile (TestRunToolCfgDir/"nuget.config")
4047

4148
fs.cd TestRunInvariantDir
4249
fs.shellExecRun "dotnet" ["tool"; "install"; "dotnet-proj"; "--version"; pkgUnderTestVersion; "--tool-path"; TestRunToolDir; "--configfile"; (TestRunToolCfgDir/"nuget.config")]
@@ -372,6 +379,36 @@ let tests pkgUnderTestVersion =
372379
Expect.equal out (sprintf "MyProperty=%s" prop) "wrong output"
373380
)
374381

382+
yield testCase |> withLog "delete deprecated target file" (fun _ fs ->
383+
let testDir = inDir fs "netsdk_delete_depr_target"
384+
copyDirFromAssets fs ``samples2 NetSdk library``.ProjDir testDir
385+
386+
let projPath = testDir/ (``samples2 NetSdk library``.ProjectFile)
387+
388+
dotnet fs ["restore"; projPath]
389+
|> checkExitCodeZero
390+
391+
let projDir, projName = Path.GetDirectoryName(projPath), Path.GetFileName(projPath)
392+
let deprecatedTargetFilePath = projDir/"obj"/sprintf "%s.proj-info.targets" projName
393+
394+
// create the deprecated target file
395+
fs.createFile deprecatedTargetFilePath (writeLines
396+
[ """<Project ToolsVersion="15.0">"""
397+
" <PropertyGroup>"
398+
" </PropertyGroup>"
399+
"</Project>" ])
400+
401+
"before exists"
402+
|> Expect.isTrue (File.Exists deprecatedTargetFilePath)
403+
404+
projInfo fs ["--verbose"; "prop"; projPath; "-get"; "AssemblyName"]
405+
|> checkExitCodeZero
406+
407+
"after doesnt exists"
408+
|> Expect.isFalse (File.Exists deprecatedTargetFilePath)
409+
)
410+
411+
375412
]
376413

377414
[ generalTests

0 commit comments

Comments
 (0)